The token is now stored into the session as long as it is alive.

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@129148 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-06-16 16:09:54 +00:00
parent 9086cf43c1
commit 7613502aaa
1 changed files with 89 additions and 57 deletions

View File

@ -6,6 +6,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.common.homelibrary.home.workspace.Workspace;
@ -13,7 +15,6 @@ import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
import org.gcube.common.homelibrary.home.workspace.folder.items.GCubeItem;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.ckanutillibrary.CKanUtilsFactory;
import org.gcube.datacatalogue.ckanutillibrary.CKanUtilsImpl;
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader;
@ -22,6 +23,7 @@ import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataField;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataValidator;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataVocabulary;
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherService;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.DatasetMetadataBean;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean;
@ -46,26 +48,85 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CKANPublisherServicesImpl.class);
public static final String TEST_SCOPE = "/gcube/devsec/devVRE";
public static final String CKAN_TOKEN_KEY = "ckanToken";
// library util instance
private CKanUtilsImpl instance;
@Override
public void init(){
// discover the library
try {
logger.debug("Creating ckan utility lib object");
instance = new CKanUtilsImpl(ScopeProvider.instance.get());
logger.debug("Created");
} catch (Exception e) {
logger.error("Failed to find ckan information", e);
}
}
/**
* the current ASLSession
* @return the session
*/
private ASLSession getASLSession() {
String sessionID = this.getThreadLocalRequest().getSession().getId();
String user = (String) this.getThreadLocalRequest().getSession().getAttribute(ScopeHelper.USERNAME_ATTRIBUTE);
if (user == null) {
logger.warn("USER IS NULL setting testing user and Running OUTSIDE PORTAL");
user = getDevelopmentUser();
SessionManager.getInstance().getASLSession(sessionID, user).setScope(TEST_SCOPE);
}
return SessionManager.getInstance().getASLSession(sessionID, user);
}
/**
* when packaging test will fail if the user is not set to test.user
* @return .
*/
public String getDevelopmentUser() {
String user = "test.user";
// user = "costantino.perciante";
return user;
}
/**
* Get current user's token
* @return String the ckan user's token
*/
private String getUserCKanTokenFromSession(){
ASLSession session = getASLSession();
String username = session.getUsername();
logger.debug("User in session is " + username);
String token = null;
if(this.getThreadLocalRequest().getSession().getAttribute("") != null)
token = (String)this.getThreadLocalRequest().getSession().getAttribute(CKAN_TOKEN_KEY);
else{
token = instance.getApiKeyFromUsername(username);
this.getThreadLocalRequest().getSession().setAttribute(CKAN_TOKEN_KEY, token);
logger.debug("Ckan token has been set for user " + username);
}
logger.debug("Found ckan token " + token + " for user " + username);
return token;
}
/**
* Retrieve the list of organizations in which the user can publish
* @param owner
* @return
* @param username
* @return the list of organizations
*/
private List<String> getUserOrganizationsList(String owner) {
logger.debug("Request for user " + owner + " organizations list");
try{
String currentScope = ScopeProvider.instance.get();
return CKanUtilsFactory.getInstance().getCkanUtilsForScope(currentScope).getOrganizationsNamesByUser(owner);
}catch(Exception e){
logger.error("Failed to retrieve user's organizations", e);
}
return null;
private List<String> getUserOrganizationsList(String username) {
logger.debug("Request for user " + username + " organizations list");
return instance.getOrganizationsNamesByUser(username);
}
/**
@ -89,15 +150,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
* @return
*/
private String findLicenseIdByLicense(String chosenLicense) {
String currentScope = ScopeProvider.instance.get();
try {
return CKanUtilsFactory.getInstance().getCkanUtilsForScope(currentScope).findLicenseIdByLicense(chosenLicense);
} catch (Exception e) {
logger.error("Failed to retrieve license id", e);
}
return null;
return instance.findLicenseIdByLicense(chosenLicense);
}
/**
@ -164,9 +217,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
logger.info("Request for CKAN licenses");
try {
String currentScope = ScopeProvider.instance.get();
List<String> titlesLicenses;
titlesLicenses = CKanUtilsFactory.getInstance().getCkanUtilsForScope(currentScope).getLicenseTitles();
List<String> titlesLicenses = instance.getLicenseTitles();
// return the bean
return new LicensesBean(titlesLicenses);
} catch (Exception e) {
@ -202,7 +253,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
bean.setAuthorEmail(userOwner.getEmail());
bean.setMaintainer(userOwner.getFullname());
bean.setMaintainerEmail(userOwner.getEmail());
bean.setOrganizationList(getUserOrganizationsList(fromOwnerToCKanOwner(owner)));
bean.setOrganizationList(getUserOrganizationsList(owner));
// if the request comes from the workspace
if(folderId != null && !folderId.isEmpty()){
@ -254,7 +305,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(fromOwnerToCKanOwner(owner)));
bean.setOrganizationList(getUserOrganizationsList(owner));
bean.setOwnerIdentifier(owner);
if(folderId != null && !folderId.isEmpty()){
@ -325,12 +376,8 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
logger.debug("Request for creating a dataset with these information " + toCreate);
try{
String currentScope = ScopeProvider.instance.get();
// get the owner
String username = fromOwnerToCKanOwner(toCreate.getOwnerIdentifier());
CKanUtilsImpl utilityInstance = CKanUtilsFactory.getInstance().getCkanUtilsForScope(currentScope);
String username = toCreate.getOwnerIdentifier();
// set dataset info (same id as the folder, if the request comes from the workspace)
String withId = null;
@ -384,7 +431,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
}
String datasetId = utilityInstance.createCKanDataset(username, withId, title, organizationNameOrId, author,
String datasetId = instance.createCKanDataset(getUserCKanTokenFromSession(), withId, title, organizationNameOrId, author,
authorMail, maintainer, maintainerMail, version, description, licenseId,
listOfTags, customFields, resources, setPublic);
@ -394,7 +441,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
toCreate.setId(datasetId);
// retrieve the url
String datasetUrl = utilityInstance.getUrlFromDatasetIdOrName(username, datasetId);
String datasetUrl = instance.getUrlFromDatasetIdOrName(getUserCKanTokenFromSession(), datasetId);
toCreate.setSource(datasetUrl);
return toCreate;
@ -420,20 +467,16 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
try{
String currentScope = ScopeProvider.instance.get();
ResourceBean resourceBean = new ResourceBean(
resource.getUrl(),
resource.getName(),
resource.getDescription(),
null,
fromOwnerToCKanOwner(owner),
owner,
datasetId,
null);
String resourceId = CKanUtilsFactory.getInstance().
getCkanUtilsForScope(currentScope).
addResourceToDataset(resourceBean);
String resourceId = instance.addResourceToDataset(resourceBean, getUserCKanTokenFromSession());
if(resourceId != null){
@ -462,11 +505,9 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
boolean deleted = false;
try{
String currentScope = ScopeProvider.instance.get();
deleted = CKanUtilsFactory.getInstance().
getCkanUtilsForScope(currentScope).
deleteResourceFromDataset(fromOwnerToCKanOwner(owner), resource.getId());
deleted = instance.
deleteResourceFromDataset(resource.getId(), getUserCKanTokenFromSession());
if(deleted){
logger.debug("Resource described by " + resource + " deleted");
@ -480,13 +521,4 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
return deleted;
}
/**
* Ckan username has _ instead of . (that is, costantino.perciante -> costantino_perciante)
* @param owner
* @return
*/
private String fromOwnerToCKanOwner(String owner){
return owner.replaceAll("\\.", "_");
}
}