renamed WsUtil to WsInstanceUtil

This commit is contained in:
Francesco Mangiacrapa 2020-04-16 13:11:26 +02:00
parent 9ec6960eb8
commit 9a749d41ca
5 changed files with 124 additions and 6 deletions

View File

@ -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){

View File

@ -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);

View File

@ -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.

View File

@ -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;
}

View File

@ -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;
}
}