Added Weblet support
parent
ca364579c1
commit
1222051250
@ -1,93 +0,0 @@
|
||||
package org.gcube.portlets.user;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
|
||||
import javax.portlet.PortletException;
|
||||
import javax.portlet.RenderRequest;
|
||||
import javax.portlet.RenderResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.gcube.common.portal.PortalContext;
|
||||
import org.gcube.oidc.rest.JWTToken;
|
||||
import org.gcube.portal.oidc.lr62.OIDCUmaUtil;
|
||||
|
||||
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
||||
import com.liferay.portal.util.PortalUtil;
|
||||
import com.liferay.util.bridges.mvc.MVCPortlet;
|
||||
|
||||
import okhttp3.*;
|
||||
|
||||
/**
|
||||
* Portlet implementation class ExtAppManager
|
||||
*/
|
||||
public class ExtAppManager extends MVCPortlet {
|
||||
private static com.liferay.portal.kernel.log.Log _log = LogFactoryUtil.getLog(ExtAppManager.class);
|
||||
final String D4S_BOOT_URL = "https://d4science-cdn.nubisware.cloud/d4s-cdn/visuals/d4s-boot";
|
||||
final String EXT_APP_MANAGER_URL = "https://d4science-cdn.nubisware.cloud/d4s-cdn/visuals/ext-app-manager";
|
||||
|
||||
|
||||
public static final String D4S_BOOT_ATTR = "d4s-boot-div";
|
||||
public static final String EXT_APP_MANAGERATTR = "ext-app-div";
|
||||
|
||||
|
||||
@Override
|
||||
public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException {
|
||||
JWTToken umaToken = null;
|
||||
try {
|
||||
String username = PortalUtil.getUser(renderRequest).getScreenName();
|
||||
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));
|
||||
String context = getCurrentContext(renderRequest);
|
||||
umaToken = OIDCUmaUtil.getUMAToken(httpReq, username, context);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String token = umaToken.getAccessTokenString();
|
||||
|
||||
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
Request request = new Request.Builder()
|
||||
.url(D4S_BOOT_URL)
|
||||
.addHeader("cache-control", "no-cache")
|
||||
.addHeader("Authorization", "Bearer " + token)
|
||||
.build();
|
||||
Call call = client.newCall(request);
|
||||
Response response = call.execute();
|
||||
String d4sBootDIV = response.body().string();
|
||||
String encodedSd4sBootDIV = Base64.getEncoder().encodeToString(d4sBootDIV.getBytes());
|
||||
renderRequest.setAttribute(D4S_BOOT_ATTR, encodedSd4sBootDIV);
|
||||
request = new Request.Builder()
|
||||
.url(EXT_APP_MANAGER_URL)
|
||||
.addHeader("cache-control", "no-cache")
|
||||
.addHeader("Authorization", "Bearer " + token)
|
||||
.build();
|
||||
call = client.newCall(request);
|
||||
response = call.execute();
|
||||
String extAppDIV = response.body().string();
|
||||
String encodedextAppDIV = Base64.getEncoder().encodeToString(extAppDIV.getBytes());
|
||||
renderRequest.setAttribute(EXT_APP_MANAGERATTR, encodedextAppDIV);
|
||||
|
||||
super.render(renderRequest, renderResponse);
|
||||
}
|
||||
|
||||
public static String getCurrentContext(RenderRequest request) {
|
||||
long groupId = -1;
|
||||
try {
|
||||
groupId = PortalUtil.getScopeGroupId(request);
|
||||
return getCurrentContext(groupId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getCurrentContext(long groupId) {
|
||||
try {
|
||||
PortalContext pContext = PortalContext.getConfiguration();
|
||||
return pContext.getCurrentScope(""+groupId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package org.gcube.portlets.user.weblet;
|
||||
|
||||
import javax.portlet.ActionRequest;
|
||||
import javax.portlet.ActionResponse;
|
||||
import javax.portlet.PortletConfig;
|
||||
import javax.portlet.PortletPreferences;
|
||||
import javax.portlet.RenderRequest;
|
||||
import javax.portlet.RenderResponse;
|
||||
|
||||
import com.liferay.portal.kernel.log.Log;
|
||||
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
||||
import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
|
||||
|
||||
public class WebletConfigurationAction extends DefaultConfigurationAction {
|
||||
private static Log _log = LogFactoryUtil.getLog(WebletConfigurationAction.class);
|
||||
@Override
|
||||
public void processAction(
|
||||
PortletConfig portletConfig, ActionRequest actionRequest,
|
||||
ActionResponse actionResponse) throws Exception {
|
||||
|
||||
super.processAction(portletConfig, actionRequest, actionResponse);
|
||||
|
||||
PortletPreferences prefs = actionRequest.getPreferences();
|
||||
String bootURL = prefs.getValue("bootURL", "true");
|
||||
String managerURL = prefs.getValue("managerURL", "true");
|
||||
String iamClientId = prefs.getValue("iamClientId", "true");
|
||||
|
||||
_log.debug("bootURL = " + bootURL + " in PublicWebappConfigurationAction.processAction().");
|
||||
_log.debug("managerURL = " + managerURL + " in PublicWebappConfigurationAction.processAction().");
|
||||
_log.debug("iamClientId = " + iamClientId + " in PublicWebappConfigurationAction.processAction().");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String render(PortletConfig portletConfig,
|
||||
RenderRequest renderRequest, RenderResponse renderResponse)
|
||||
throws Exception {
|
||||
|
||||
return "/html/extappmanager/config.jsp";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
package org.gcube.portlets.user.weblet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
|
||||
import javax.portlet.PortletException;
|
||||
import javax.portlet.PortletPreferences;
|
||||
import javax.portlet.RenderRequest;
|
||||
import javax.portlet.RenderResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.gcube.common.portal.PortalContext;
|
||||
import org.gcube.oidc.rest.JWTToken;
|
||||
import org.gcube.portal.oidc.lr62.OIDCUmaUtil;
|
||||
|
||||
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
||||
import com.liferay.portal.kernel.util.GetterUtil;
|
||||
import com.liferay.portal.kernel.util.StringPool;
|
||||
import com.liferay.portal.util.PortalUtil;
|
||||
import com.liferay.util.bridges.mvc.MVCPortlet;
|
||||
|
||||
import okhttp3.*;
|
||||
|
||||
/**
|
||||
* Portlet implementation class WebletManager
|
||||
*/
|
||||
public class WebletManager extends MVCPortlet {
|
||||
private static com.liferay.portal.kernel.log.Log _log = LogFactoryUtil.getLog(WebletManager.class);
|
||||
//final String D4S_BOOT_URL = "https://cdn.dev.d4science.org/visuals/d4s-cdn/d4s-boot";
|
||||
//final String EXT_APP_MANAGER_URL = "https://cdn.dev.d4science.org/visuals/d4s-vre-manager/ext-app-manager?app=_myextapp";
|
||||
|
||||
|
||||
public static final String D4S_BOOT_ATTR = "d4s-boot-div";
|
||||
public static final String EXT_APP_MANAGERATTR = "ext-app-div";
|
||||
|
||||
|
||||
@Override
|
||||
public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException {
|
||||
|
||||
PortletPreferences portletPreferences = renderRequest.getPreferences();
|
||||
String D4S_BOOT_URL = GetterUtil.getString(portletPreferences.getValue("bootURL", StringPool.BLANK));
|
||||
String WEBLET_MANAGER_URL = GetterUtil.getString(portletPreferences.getValue("managerURL", StringPool.BLANK));
|
||||
String IAM_CLIENTID = GetterUtil.getString(portletPreferences.getValue("iamClientId", StringPool.BLANK));
|
||||
|
||||
String webletForIamClientURL = new StringBuilder(WEBLET_MANAGER_URL).append("?app=").append(IAM_CLIENTID).toString();
|
||||
|
||||
if (D4S_BOOT_URL.equals(StringPool.BLANK) || WEBLET_MANAGER_URL.equals(StringPool.BLANK) || IAM_CLIENTID.equals(StringPool.BLANK)) {
|
||||
_log.warn("Missing parameters in config");
|
||||
}
|
||||
else {
|
||||
JWTToken umaToken = null;
|
||||
try {
|
||||
String username = PortalUtil.getUser(renderRequest).getScreenName();
|
||||
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));
|
||||
String context = getCurrentContext(renderRequest);
|
||||
umaToken = OIDCUmaUtil.getUMAToken(httpReq, username, context);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String token = umaToken.getAccessTokenString();
|
||||
|
||||
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
Request request = new Request.Builder()
|
||||
.url(D4S_BOOT_URL)
|
||||
.addHeader("cache-control", "no-cache")
|
||||
.addHeader("Authorization", "Bearer " + token)
|
||||
.build();
|
||||
Call call = client.newCall(request);
|
||||
Response response = call.execute();
|
||||
String d4sBootDIV = response.body().string();
|
||||
String encodedSd4sBootDIV = Base64.getEncoder().encodeToString(d4sBootDIV.getBytes());
|
||||
renderRequest.setAttribute(D4S_BOOT_ATTR, encodedSd4sBootDIV);
|
||||
request = new Request.Builder()
|
||||
.url(webletForIamClientURL)
|
||||
.addHeader("cache-control", "no-cache")
|
||||
.addHeader("Authorization", "Bearer " + token)
|
||||
.build();
|
||||
call = client.newCall(request);
|
||||
response = call.execute();
|
||||
String extAppDIV = response.body().string();
|
||||
String encodedextAppDIV = Base64.getEncoder().encodeToString(extAppDIV.getBytes());
|
||||
renderRequest.setAttribute(EXT_APP_MANAGERATTR, encodedextAppDIV);
|
||||
}
|
||||
super.render(renderRequest, renderResponse);
|
||||
}
|
||||
|
||||
public static String getCurrentContext(RenderRequest request) {
|
||||
long groupId = -1;
|
||||
try {
|
||||
groupId = PortalUtil.getScopeGroupId(request);
|
||||
return getCurrentContext(groupId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getCurrentContext(long groupId) {
|
||||
try {
|
||||
PortalContext pContext = PortalContext.getConfiguration();
|
||||
return pContext.getCurrentScope(""+groupId);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
<%@include file="/html/init.jsp"%>
|
||||
|
||||
<liferay-portlet:actionURL portletConfiguration="true"
|
||||
var="configurationURL" />
|
||||
|
||||
<%
|
||||
String bootURL_cfg = GetterUtil.getString(portletPreferences.getValue("bootURL", StringPool.BLANK));
|
||||
String managerURLcfg = GetterUtil.getString(portletPreferences.getValue("managerURL", StringPool.BLANK));
|
||||
String iamClientId_cfg = GetterUtil.getString(portletPreferences.getValue("iamClientId", StringPool.BLANK));
|
||||
%>
|
||||
|
||||
<aui:form action="<%=configurationURL%>" method="post" name="fm">
|
||||
<aui:input name="<%=Constants.CMD%>" type="hidden"
|
||||
value="<%=Constants.UPDATE%>" />
|
||||
|
||||
<!-- Boot URL -->
|
||||
<aui:field-wrapper cssClass="field-group">
|
||||
<aui:input style="width: 100%;" name="preferences--bootURL--"
|
||||
type="text" cssClass="text long-field" showRequiredLabel="true"
|
||||
label="D4Science Visuals CDN Boot URL" inlineField="true" inlineLabel="left"
|
||||
placeholder="e.g. https://cdn.d4science.org/visuals/d4s-cdn/d4s-boot"
|
||||
helpMessage="Actual endpoint of the cdn visuals on D4Science"
|
||||
value="<%=bootURL_cfg%>" required="true" />
|
||||
</aui:field-wrapper>
|
||||
<!-- manager URL -->
|
||||
<aui:field-wrapper cssClass="field-group">
|
||||
<aui:input style="width: 100%;" name="preferences--managerURL--"
|
||||
type="text" cssClass="text long-field" showRequiredLabel="true"
|
||||
label="D4Science Visuals CDN Manager URL" inlineField="true" inlineLabel="left"
|
||||
placeholder="e.g. https://cdn.d4science.org/visuals/d4s-vre-manager/ext-app-manager"
|
||||
helpMessage="Actual endpoint of the cdn visuals Manager on D4Science"
|
||||
value="<%=managerURLcfg%>" required="true" />
|
||||
</aui:field-wrapper>
|
||||
<!-- IAM Client ID (App Name) -->
|
||||
<aui:field-wrapper cssClass="field-group">
|
||||
<aui:input style="width: 100%;" name="preferences--iamClientId--"
|
||||
type="text" cssClass="text long-field" showRequiredLabel="true"
|
||||
label="IAM Client Id" inlineField="true" inlineLabel="left"
|
||||
placeholder="enter the IAM ClientId of the App here"
|
||||
helpMessage="Actual IAM Client Id for this app"
|
||||
value="<%=iamClientId_cfg%>" required="true" />
|
||||
</aui:field-wrapper>
|
||||
|
||||
<aui:button-row>
|
||||
<aui:button type="submit" />
|
||||
</aui:button-row>
|
||||
|
||||
</aui:form>
|
@ -1,25 +0,0 @@
|
||||
<%@include file="/html/init.jsp"%>
|
||||
|
||||
|
||||
<%@ page import="java.util.Base64" %>
|
||||
|
||||
<%
|
||||
|
||||
|
||||
String encodedD4sBootDIV = (String) request.getAttribute("d4s-boot-div");
|
||||
String encodedExtAppDIV = (String) request.getAttribute("ext-app-div");
|
||||
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(encodedD4sBootDIV);
|
||||
String d4sBootDIV = new String(decodedBytes);
|
||||
|
||||
decodedBytes = Base64.getDecoder().decode(encodedExtAppDIV);
|
||||
String extAppDIV = new String(decodedBytes);
|
||||
|
||||
pageContext.setAttribute("d4sBootDIV", d4sBootDIV);
|
||||
pageContext.setAttribute("extAppDIV", extAppDIV);
|
||||
|
||||
|
||||
%>
|
||||
${d4sBootDIV}
|
||||
|
||||
<div id="extAppDIV">${extAppDIV}</div>
|
@ -0,0 +1,26 @@
|
||||
<%@include file="/html/init.jsp"%>
|
||||
|
||||
|
||||
<%@ page import="java.util.Base64"%>
|
||||
|
||||
<%
|
||||
String encodedD4sBootDIV = (String) request.getAttribute("d4s-boot-div");
|
||||
String encodedExtAppDIV = (String) request.getAttribute("ext-app-div");
|
||||
|
||||
if (encodedD4sBootDIV == null || encodedExtAppDIV == null) {
|
||||
String d4sBootDIV = new String(
|
||||
"Configuration is missing, please enter boot, manager urls and clientId name in portlet configuration");
|
||||
} else {
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(encodedD4sBootDIV);
|
||||
String d4sBootDIV = new String(decodedBytes);
|
||||
|
||||
decodedBytes = Base64.getDecoder().decode(encodedExtAppDIV);
|
||||
String extAppDIV = new String(decodedBytes);
|
||||
|
||||
pageContext.setAttribute("d4sBootDIV", d4sBootDIV);
|
||||
pageContext.setAttribute("extAppDIV", extAppDIV);
|
||||
}
|
||||
%>
|
||||
${d4sBootDIV}
|
||||
|
||||
<div id="extAppDIV">${extAppDIV}</div>
|
Loading…
Reference in New Issue