From 7d768461e23fa795237b07c91a24931a7398fac6 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Mon, 4 Jul 2016 10:44:20 +0000 Subject: [PATCH] Ckan information are now stored into http session and are associated to the scope git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@129834 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../server/CKANPublisherServicesImpl.java | 80 ++++++++++++++----- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java index 1ba169a..3d041c7 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java +++ b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java @@ -8,6 +8,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import javax.servlet.http.HttpSession; + import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.SessionManager; import org.gcube.common.homelibrary.home.HomeLibrary; @@ -130,8 +132,14 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C token = TEST_SEC_TOKEN; }else{ - ASLSession session = getASLSession(); - String username = session.getUsername(); + ASLSession aslSession = getASLSession(); + String username = aslSession.getUsername(); + + // store info in the http session + HttpSession httpSession = getThreadLocalRequest().getSession(); + + // get the key per scope + String keyPerScope = concatenateSessionKeyScope(CKAN_TOKEN_KEY, aslSession.getScope()); // check if session expired if(username.equals(TEST_USER)){ @@ -142,11 +150,13 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C }else{ try{ logger.debug("User in session is " + username); - if(session.getAttribute(CKAN_TOKEN_KEY) != null) - token = (String)session.getAttribute(CKAN_TOKEN_KEY); + if(httpSession.getAttribute(keyPerScope) != null){ + token = (String)httpSession.getAttribute(keyPerScope); + logger.debug("Found ckan token into session"); + } else{ token = getCkanUtilsObj().getApiKeyFromUsername(username); - session.setAttribute(CKAN_TOKEN_KEY, token); + httpSession.setAttribute(keyPerScope, token); logger.debug("Ckan token has been set for user " + username); } logger.debug("Found ckan token " + token.substring(0, 3) + "************************" + " for user " + username); @@ -164,16 +174,20 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C * @param username * @return the list of organizations */ - private List getUserOrganizationsList(String username) { + private List getUserOrganizationsList(String username, String scope) { logger.debug("Request for user " + username + " organizations list"); List orgsName = new ArrayList(); - ASLSession session = getASLSession(); - - CKanUtils ckanUtils = getCkanUtilsObj(); - - if(session.getAttribute(CKAN_ORGANIZATIONS_PUBLISH_KEY) != null){ - orgsName = (List)session.getAttribute(CKAN_ORGANIZATIONS_PUBLISH_KEY); + CKanUtils ckanUtils = getCkanUtilsObj(); + + // get http session + HttpSession httpSession = getThreadLocalRequest().getSession(); + + // get key + String keyPerScope = concatenateSessionKeyScope(CKAN_ORGANIZATIONS_PUBLISH_KEY, scope); + + if(httpSession.getAttribute(keyPerScope) != null){ + orgsName = (List)httpSession.getAttribute(keyPerScope); logger.info("List of organizations was into session"); } else{ @@ -201,7 +215,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C logger.debug("The user has a role ADMIN/EDITOR into org " + entry.getKey()); } } - session.setAttribute(CKAN_ORGANIZATIONS_PUBLISH_KEY, orgsName); + httpSession.setAttribute(keyPerScope, orgsName); logger.info("Organizations name for user " + username + " has been saved into session"); } @@ -243,9 +257,15 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C ASLSession session = getASLSession(); String username = session.getUsername(); logger.debug("User in session is " + username); + + // get http session + HttpSession httpSession = getThreadLocalRequest().getSession(); + + // get key per scope + String keyPerScope = concatenateSessionKeyScope(CKAN_PROFILES_KEY, session.getScope()); - if(session.getAttribute(CKAN_PROFILES_KEY) != null){ - beans = (List)session.getAttribute(CKAN_PROFILES_KEY); + if(httpSession.getAttribute(keyPerScope) != null){ + beans = (List)httpSession.getAttribute(keyPerScope); logger.info("List of profiles was into session"); } else{ @@ -291,8 +311,8 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C beans.add(bean); } - logger.info("List of beans is " + beans); - session.setAttribute(CKAN_PROFILES_KEY, beans); + logger.debug("List of beans is " + beans); + httpSession.setAttribute(keyPerScope, beans); logger.debug("List of profiles has been saved into session"); } catch (Exception e) { @@ -311,16 +331,22 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C ASLSession session = getASLSession(); String username = session.getUsername(); logger.debug("User in session is " + username); + + // get http session + HttpSession httpSession = getThreadLocalRequest().getSession(); + + // get key per scope + String keyPerScope = concatenateSessionKeyScope(CKAN_LICENSES_KEY, session.getScope()); LicensesBean licensesBean = null; - if(session.getAttribute(CKAN_LICENSES_KEY) != null){ - licensesBean = (LicensesBean)session.getAttribute(CKAN_LICENSES_KEY); + if(httpSession.getAttribute(keyPerScope) != null){ + licensesBean = (LicensesBean)httpSession.getAttribute(keyPerScope); logger.debug("List of licenses was into session"); } else{ List titlesLicenses = getCkanUtilsObj().getLicenseTitles(); licensesBean = new LicensesBean(titlesLicenses); - session.setAttribute(CKAN_LICENSES_KEY, licensesBean); + httpSession.setAttribute(keyPerScope, licensesBean); logger.debug("List of licenses has been saved into session"); } @@ -363,7 +389,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C bean.setAuthorEmail(userOwner.getEmail()); bean.setMaintainer(userOwner.getFullname()); bean.setMaintainerEmail(userOwner.getEmail()); - bean.setOrganizationList(getUserOrganizationsList(owner)); + bean.setOrganizationList(getUserOrganizationsList(owner, aslSession.getScope())); // if the request comes from the workspace if(folderId != null && !folderId.isEmpty()){ @@ -415,7 +441,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C bean.setAuthorEmail("costantino.perciante@isti.cnr.it"); bean.setMaintainer("Costantino Perciante"); bean.setMaintainerEmail("costantino.perciante@isti.cnr.it"); - bean.setOrganizationList(getUserOrganizationsList(owner)); + bean.setOrganizationList(getUserOrganizationsList(owner, TEST_SCOPE)); bean.setOwnerIdentifier(owner); if(folderId != null && !folderId.isEmpty()){ @@ -664,4 +690,14 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C } } } + + /** + * Builds a string made of key + scope + * @param key + * @param scope + * @return + */ + private String concatenateSessionKeyScope(String key, String scope){ + return key.concat(scope); + } } \ No newline at end of file