diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/SessionUtil.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/SessionUtil.java index 380d568..0474301 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/SessionUtil.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/SessionUtil.java @@ -37,11 +37,11 @@ public class SessionUtil { throws StatAlgoImporterServiceException { return getServiceCredentials(httpServletRequest, null); } - + public static ServiceCredentials getServiceCredentials( HttpServletRequest httpServletRequest, String scopeGroupId) throws StatAlgoImporterServiceException { - + ServiceCredentials sCredentials = null; String userName = null; String scope = null; @@ -58,16 +58,37 @@ public class SessionUtil { } else { logger.info("Retrieving credential in session!"); PortalContext pContext = PortalContext.getConfiguration(); - if(scopeGroupId!=null&&!scopeGroupId.isEmpty()){ + if (scopeGroupId != null && !scopeGroupId.isEmpty()) { scope = pContext.getCurrentScope(scopeGroupId); } else { scope = pContext.getCurrentScope(httpServletRequest); } - + + if (scope == null || scope.isEmpty()) { + String error = "Error retrieving scope: " + scope; + logger.error(error); + throw new StatAlgoImporterServiceException(error); + } + userName = pContext.getCurrentUser(httpServletRequest) .getUsername(); - + + if (userName == null || userName.isEmpty()) { + String error = "Error retrieving username in scope " + scope + + ": " + userName; + logger.error(error); + throw new StatAlgoImporterServiceException(error); + } + token = pContext.getCurrentUserToken(httpServletRequest); + + if (token == null || token.isEmpty()) { + String error = "Error retrieving token for " + userName + + " in " + scope + ": " + token; + logger.error(error); + throw new StatAlgoImporterServiceException(error); + } + String name = pContext.getCurrentUser(httpServletRequest) .getFirstName(); String lastName = pContext.getCurrentUser(httpServletRequest) @@ -85,8 +106,8 @@ public class SessionUtil { String groupName = pContext.getCurrentGroupName(httpServletRequest); sCredentials = new ServiceCredentials(userName, fullName, name, - lastName, email, scope, groupId, groupName, userAvatarURL, token); - + lastName, email, scope, groupId, groupName, userAvatarURL, + token); } logger.info("ServiceCredentials: " + sCredentials); @@ -94,10 +115,6 @@ public class SessionUtil { return sCredentials; } - - - - // public static ArrayList getRecipients( ServletContext servletContest) {