From 9a749d41ca4c33f7aeb189195eb44879147cf772 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 16 Apr 2020 13:11:26 +0200 Subject: [PATCH] renamed WsUtil to WsInstanceUtil --- .../DownloadWorkspaceExplorerServlet.java | 4 +- .../WorkspaceExplorerAppServiceImpl.java | 4 +- .../workspace/AllowedMimeTypeToInline.java | 2 +- .../server/workspace/WorkspaceInstance.java | 2 +- .../server/workspace/WsInstanceUtil.java | 118 ++++++++++++++++++ 5 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/WsInstanceUtil.java diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/DownloadWorkspaceExplorerServlet.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/DownloadWorkspaceExplorerServlet.java index 571087b..0fa25b1 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/DownloadWorkspaceExplorerServlet.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/DownloadWorkspaceExplorerServlet.java @@ -31,7 +31,7 @@ import org.gcube.common.storagehubwrapper.shared.tohl.items.URLItem; import org.gcube.portlets.user.workspaceexplorerapp.client.WorkspaceExplorerAppConstants; import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.AllowedMimeTypeToInline; import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.MimeTypeUtility; -import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.WsUtil; +import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.WsInstanceUtil; import org.gcube.portlets.user.workspaceexplorerapp.shared.HandlerResultMessage; /** @@ -87,7 +87,7 @@ public class DownloadWorkspaceExplorerServlet extends HttpServlet{ Workspace wa = null; try { - wa = WsUtil.getWorkspace(req.getSession()); + wa = WsInstanceUtil.getWorkspace(req.getSession()); } catch (Exception e) { /*if (e instanceof SessionExpiredException){ diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WorkspaceExplorerAppServiceImpl.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WorkspaceExplorerAppServiceImpl.java index 5959209..c7706f1 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WorkspaceExplorerAppServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/WorkspaceExplorerAppServiceImpl.java @@ -19,7 +19,7 @@ import org.gcube.portlets.user.workspaceexplorerapp.client.WorkspaceExplorerAppC import org.gcube.portlets.user.workspaceexplorerapp.client.rpc.WorkspaceExplorerAppService; import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.ItemBuilder; import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.WorkspaceInstance; -import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.WsUtil; +import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.WsInstanceUtil; import org.gcube.portlets.user.workspaceexplorerapp.shared.FilterCriteria; import org.gcube.portlets.user.workspaceexplorerapp.shared.Item; import org.gcube.portlets.user.workspaceexplorerapp.shared.ItemType; @@ -555,7 +555,7 @@ public class WorkspaceExplorerAppServiceImpl extends RemoteServiceServlet implem @Override public String getFolderIdFromEncrypted(String encodedFolderId) throws Exception{ try{ - String scope = WsUtil.getScope(this.getThreadLocalRequest().getSession()); + String scope = WsInstanceUtil.getScope(this.getThreadLocalRequest().getSession()); ScopeProvider.instance.set(scope); logger.info("Trying to decode encoded folder Id: "+encodedFolderId +" in the scope: "+scope); String base64DecodedId = StringUtil.base64DecodeString(encodedFolderId); diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/AllowedMimeTypeToInline.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/AllowedMimeTypeToInline.java index b4f55eb..373b1f0 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/AllowedMimeTypeToInline.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/AllowedMimeTypeToInline.java @@ -21,7 +21,7 @@ public class AllowedMimeTypeToInline { public static final String filename = "MimeTypePrefixToInline.properties"; - protected static Logger logger = LoggerFactory.getLogger(WsUtil.class); + protected static Logger logger = LoggerFactory.getLogger(AllowedMimeTypeToInline.class); /** * Load mime type prefix. diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/WorkspaceInstance.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/WorkspaceInstance.java index ad2291b..43b7431 100644 --- a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/WorkspaceInstance.java +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/WorkspaceInstance.java @@ -40,7 +40,7 @@ public class WorkspaceInstance implements Serializable{ */ public Workspace get() throws Exception{ if(workspace==null) - workspace = WsUtil.getWorkspace(httpSession); + workspace = WsInstanceUtil.getWorkspace(httpSession); return workspace; } diff --git a/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/WsInstanceUtil.java b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/WsInstanceUtil.java new file mode 100644 index 0000000..3e4dca9 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/workspaceexplorerapp/server/workspace/WsInstanceUtil.java @@ -0,0 +1,118 @@ +/** + * + */ +package org.gcube.portlets.user.workspaceexplorerapp.server.workspace; + +import javax.servlet.http.HttpSession; + +import org.gcube.common.storagehubwrapper.server.StorageHubWrapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The Class WsUtil. + * + * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) + * Apr 9, 2020 + */ +public class WsInstanceUtil { + + public static final Logger logger = LoggerFactory.getLogger(WsInstanceUtil.class); + public static final String ENVIRONMENT_VARIABLE_SCOPE_NAME = "EnvironmentVariableScope"; + public static final String ENVIRONMENT_VARIABLE_TOKEN_NAME = "EnvironmentVariableAppToken"; + public static final String SESSION_SCOPE = "session_scope"; + + /** + * Gets the storage hub wrapper. + * + * @param httpSession the http session + * @return the storage hub wrapper + * @throws Exception the exception + */ + private static StorageHubWrapper getStorageHubWrapper(HttpSession httpSession) throws Exception { + + try { + String scope = getScope(httpSession); + String applicationToken = getApplicationToken(httpSession); + + if(applicationToken==null || applicationToken.isEmpty()) { + throw new Exception("No Application Token found. Impossible to istance the SHUB"); + } + + return new StorageHubWrapper(scope, applicationToken, false, false, false); + } catch (Exception e) { + logger.error("Error when instancing the storageHub wrapper", e); + throw new Exception("Error on inizializing the StorageHub wrapper. Please contact the support!"); + } + } + + /** + * Gets the workpace. + * + * @param httpSession the http session + * @return the workpace + * @throws Exception the exception + */ + public static org.gcube.common.storagehubwrapper.server.tohl.Workspace getWorkspace(HttpSession httpSession) throws Exception { + + try { + StorageHubWrapper wrapper = getStorageHubWrapper(httpSession); + return wrapper.getWorkspace(); + } catch (Exception e) { + logger.error("Error on getting the Workspace", e); + throw new Exception("Error on getting the Workspace"); + } + } + + /** + * Gets the scope. + * + * @param httpSession the http session + * @return the scope + */ + public static String getScope(HttpSession httpSession) { + String scope = (String) httpSession.getAttribute(SESSION_SCOPE); + logger.info("Variable " + SESSION_SCOPE + " read from httpsession is: " + scope); + + if (scope == null) { + logger.info("Variable " + SESSION_SCOPE + " is null reading from web.xml context"); + String variableScopeName = httpSession.getServletContext() + .getInitParameter(ENVIRONMENT_VARIABLE_SCOPE_NAME); + logger.info("Found param-value '" + variableScopeName + + "' from web context, reading its value from ENVIRONMENT"); + scope = System.getenv(variableScopeName); + logger.info("Value of " + variableScopeName + " from ENVIRONMENT is: " + scope); + } + + httpSession.setAttribute(ENVIRONMENT_VARIABLE_SCOPE_NAME, scope); + return scope; + } + + + /** + * Gets the application token. + * + * @param httpSession the http session + * @return the application token + */ + public static String getApplicationToken(HttpSession httpSession) { + + String appToken = null; + String appTokenEnvVarName = null; + try { + logger.info("Reading " + ENVIRONMENT_VARIABLE_TOKEN_NAME + " from web.xml context"); + appTokenEnvVarName = httpSession.getServletContext() + .getInitParameter(ENVIRONMENT_VARIABLE_TOKEN_NAME); + logger.info("Found param-value '" + appTokenEnvVarName + + "' from web context, reading its value from ENVIRONMENT"); + appToken = System.getenv(appTokenEnvVarName); + logger.info("Value of " + appTokenEnvVarName + " from ENVIRONMENT is: " + appToken); + + }catch (Exception e) { + logger.error("Error on reading the ENV VARIABLE '"+appTokenEnvVarName+"' from ENVIRONMENT"); + } + + return appToken; + } +}