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
This commit is contained in:
Costantino Perciante 2016-07-04 10:44:20 +00:00
parent f0ab36c234
commit 7d768461e2
1 changed files with 58 additions and 22 deletions

View File

@ -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<String> getUserOrganizationsList(String username) {
private List<String> getUserOrganizationsList(String username, String scope) {
logger.debug("Request for user " + username + " organizations list");
List<String> orgsName = new ArrayList<String>();
ASLSession session = getASLSession();
CKanUtils ckanUtils = getCkanUtilsObj();
if(session.getAttribute(CKAN_ORGANIZATIONS_PUBLISH_KEY) != null){
orgsName = (List<String>)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<String>)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<MetaDataProfileBean>)session.getAttribute(CKAN_PROFILES_KEY);
if(httpSession.getAttribute(keyPerScope) != null){
beans = (List<MetaDataProfileBean>)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<String> 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);
}
}