2016-06-13 09:48:29 +02:00
|
|
|
package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server;
|
|
|
|
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-11-29 17:20:50 +01:00
|
|
|
import java.util.Map;
|
2017-01-27 11:27:40 +01:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Set;
|
2016-06-13 09:48:29 +02:00
|
|
|
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
2016-06-20 15:22:21 +02:00
|
|
|
import org.apache.commons.codec.binary.Base64;
|
2016-11-17 14:17:29 +01:00
|
|
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
2016-09-14 16:42:46 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
2017-01-25 18:30:53 +01:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.utils.SessionCatalogueAttributes;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.utils.UtilMethods;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg;
|
2016-06-13 09:48:29 +02:00
|
|
|
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService;
|
2016-11-21 10:12:24 +01:00
|
|
|
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.BeanUserInOrgGroupRole;
|
2016-06-23 12:49:45 +02:00
|
|
|
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.CkanConnectorAccessPoint;
|
2017-01-25 18:30:53 +01:00
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.OrganizationBean;
|
2016-09-05 18:18:57 +02:00
|
|
|
import org.gcube.vomanagement.usermanagement.GroupManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
|
2016-06-13 09:48:29 +02:00
|
|
|
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
2016-12-26 23:12:33 +01:00
|
|
|
import com.liferay.portal.kernel.log.Log;
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
2016-06-27 11:41:05 +02:00
|
|
|
|
2016-11-17 14:31:01 +01:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanGroup;
|
2016-06-29 16:50:18 +02:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanOrganization;
|
2016-06-13 09:48:29 +02:00
|
|
|
/**
|
|
|
|
* The server side implementation of the RPC service.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
2016-09-05 18:18:57 +02:00
|
|
|
* @author Costantino Perciante costantino.perciante@isti.cnr.it
|
2016-06-13 09:48:29 +02:00
|
|
|
* Jun 10, 2016
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implements GcubeCkanDataCatalogService {
|
|
|
|
|
2016-06-27 16:58:01 +02:00
|
|
|
public static final String UTF_8 = "UTF-8";
|
2016-06-13 09:48:29 +02:00
|
|
|
private static final String PORT_HTTP = ":80";
|
|
|
|
private static final String PORT_HTTPS = ":443";
|
|
|
|
private static final String HTTPS = "https";
|
|
|
|
private static final String HTTP = "http";
|
|
|
|
public static String CKANCONNECTORCONTEXT = "CkanConnectorContext";
|
2016-06-23 12:49:45 +02:00
|
|
|
public static String CKANCONNECTORLOGOUT = "CkanConnectorLogout";
|
2016-12-26 23:12:33 +01:00
|
|
|
private static final Log logger = LogFactoryUtil.getLog(GcubeCkanDataCatalogServiceImpl.class);
|
2016-06-13 09:48:29 +02:00
|
|
|
|
2016-06-27 11:10:47 +02:00
|
|
|
/**
|
2016-07-20 19:14:46 +02:00
|
|
|
* Instanciate the ckan util library.
|
2016-06-27 11:10:47 +02:00
|
|
|
* Since it needs the scope, we need to check if it is null or not
|
2016-07-20 19:14:46 +02:00
|
|
|
* @param discoverScope if you want to the discover the utils library in this specified scope
|
2016-06-27 11:10:47 +02:00
|
|
|
* @return
|
2016-06-20 15:22:21 +02:00
|
|
|
*/
|
2016-09-28 16:44:27 +02:00
|
|
|
public DataCatalogue getCatalogue(String discoverScope){
|
2017-01-25 18:30:53 +01:00
|
|
|
String currentScope = SessionUtil.getCurrentContext(getThreadLocalRequest(), false);
|
2016-09-28 16:44:27 +02:00
|
|
|
DataCatalogue instance = null;
|
2016-07-04 16:24:53 +02:00
|
|
|
try{
|
2016-11-04 13:01:35 +01:00
|
|
|
String scopeInWhichDiscover = discoverScope != null && !discoverScope.isEmpty() ? discoverScope : currentScope;
|
2016-09-05 18:18:57 +02:00
|
|
|
logger.debug("Discovering ckan utils library into scope " + scopeInWhichDiscover);
|
2016-09-28 16:44:27 +02:00
|
|
|
instance = DataCatalogueFactory.getFactory().getUtilsPerScope(scopeInWhichDiscover);
|
2016-07-04 16:24:53 +02:00
|
|
|
}catch(Exception e){
|
2016-11-11 19:18:02 +01:00
|
|
|
logger.error("Unable to retrieve ckan utils. Error was " + e.toString());
|
2016-06-18 10:25:17 +02:00
|
|
|
}
|
2016-06-27 11:10:47 +02:00
|
|
|
return instance;
|
|
|
|
}
|
2016-06-18 14:01:58 +02:00
|
|
|
|
2016-06-13 09:48:29 +02:00
|
|
|
/* (non-Javadoc)
|
2016-06-20 15:22:21 +02:00
|
|
|
* @see org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService#getCKanConnector(java.lang.String, java.lang.String)
|
2016-06-13 09:48:29 +02:00
|
|
|
*/
|
|
|
|
@Override
|
2017-01-25 18:30:53 +01:00
|
|
|
public CkanConnectorAccessPoint getCKanConnector(String pathInfoParameter, String queryStringParameters) throws Exception {
|
|
|
|
logger.info("getCKanConnector [pathInfo: "+pathInfoParameter + ", query: "+queryStringParameters+"]");
|
2016-06-13 09:48:29 +02:00
|
|
|
try{
|
2016-06-20 16:05:58 +02:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
// just get the current scope and set it into ScopeProvider...
|
|
|
|
SessionUtil.getCurrentContext(getThreadLocalRequest(), true);
|
2016-09-05 18:18:57 +02:00
|
|
|
|
|
|
|
// retrieve scope per current portlet url
|
2016-11-25 10:37:56 +01:00
|
|
|
String scopePerCurrentUrl = SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
|
2016-09-05 18:18:57 +02:00
|
|
|
|
2016-06-20 16:05:58 +02:00
|
|
|
if(queryStringParameters!=null && Base64.isBase64(queryStringParameters.getBytes())){
|
|
|
|
byte[] valueDecoded=Base64.decodeBase64(queryStringParameters.getBytes());
|
|
|
|
queryStringParameters = new String(valueDecoded);
|
|
|
|
logger.info("queryStringParameters detected like Base64 and decoded like: "+queryStringParameters);
|
|
|
|
}
|
|
|
|
|
2016-09-05 18:18:57 +02:00
|
|
|
CkanConnectorAccessPoint ckAP = getCkanConnectorAccessPoint(pathInfoParameter, queryStringParameters, scopePerCurrentUrl);
|
|
|
|
SessionUtil.saveCkanAccessPoint(this.getThreadLocalRequest().getSession(), scopePerCurrentUrl, ckAP);
|
2016-06-23 14:57:20 +02:00
|
|
|
logger.info("Builded URI to CKAN Connector: "+ckAP.buildURI());
|
|
|
|
logger.debug("returning ckanConnectorUri: "+ckAP);
|
2016-06-23 12:49:45 +02:00
|
|
|
return ckAP;
|
2016-06-13 09:48:29 +02:00
|
|
|
}catch(Exception e ){
|
|
|
|
String message = "Sorry an error occurred during contacting gCube Ckan Data Catalogue";
|
|
|
|
logger.error(message, e);
|
|
|
|
throw new Exception(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 15:22:21 +02:00
|
|
|
|
|
|
|
/**
|
2016-06-23 14:57:20 +02:00
|
|
|
* Gets the ckan connector access point.
|
2016-06-20 15:22:21 +02:00
|
|
|
*
|
2016-06-23 14:57:20 +02:00
|
|
|
* @param pathInfoParameter the path info parameter
|
2016-06-20 15:22:21 +02:00
|
|
|
* @param queryStringParameters the query string parameters
|
2016-06-23 14:57:20 +02:00
|
|
|
* @return the ckan connector access point
|
2016-07-18 12:39:45 +02:00
|
|
|
* @throws Exception
|
2016-06-20 15:22:21 +02:00
|
|
|
*/
|
2016-09-05 18:18:57 +02:00
|
|
|
private CkanConnectorAccessPoint getCkanConnectorAccessPoint(String pathInfoParameter, String queryStringParameters, String scopePerCurrentUrl) throws Exception {
|
2016-06-30 17:22:37 +02:00
|
|
|
|
2016-06-29 18:35:01 +02:00
|
|
|
if(outsidePortal()){
|
2016-09-28 16:44:27 +02:00
|
|
|
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(getCatalogue(scopePerCurrentUrl).getCatalogueUrl(),"");
|
2017-02-03 18:06:49 +01:00
|
|
|
ckan.setOutsideFromPortal(true);
|
2017-10-24 19:21:29 +02:00
|
|
|
ckan.addPathInfo(pathInfoParameter);
|
|
|
|
ckan.addQueryString(queryStringParameters);
|
2016-06-29 18:35:01 +02:00
|
|
|
return ckan;
|
|
|
|
}
|
2016-06-20 15:22:21 +02:00
|
|
|
|
2016-06-23 14:57:20 +02:00
|
|
|
//CKAN BASE URL
|
2016-07-18 12:39:45 +02:00
|
|
|
GcoreEndpointReader ckanEndPoint = null;
|
|
|
|
try{
|
2016-09-05 18:18:57 +02:00
|
|
|
ckanEndPoint = SessionUtil.getCkanEndPoint(this.getThreadLocalRequest().getSession(), scopePerCurrentUrl);
|
2016-07-18 12:39:45 +02:00
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("CkanConnectorAccessPoint error: "+e.getMessage());
|
|
|
|
throw new Exception("Sorry, an error occurred during contacting d4Science Data Catalogue, try again later");
|
|
|
|
}
|
2016-06-23 14:57:20 +02:00
|
|
|
String ckanConnectorBaseUrl = ckanEndPoint.getCkanResourceEntyName();
|
|
|
|
ckanConnectorBaseUrl = ckanConnectorBaseUrl.startsWith(HTTP) && !ckanConnectorBaseUrl.startsWith(HTTPS)?ckanConnectorBaseUrl.replaceFirst(HTTP, HTTPS):ckanConnectorBaseUrl;
|
|
|
|
ckanConnectorBaseUrl = ckanConnectorBaseUrl.contains(PORT_HTTP)?ckanConnectorBaseUrl.replace(PORT_HTTP, PORT_HTTPS):ckanConnectorBaseUrl;
|
|
|
|
logger.debug("Base URL is: "+ckanConnectorBaseUrl);
|
2016-06-29 11:25:46 +02:00
|
|
|
|
2016-06-23 14:57:20 +02:00
|
|
|
//GET CONTEXT
|
|
|
|
String ckanContext = getServletContext().getInitParameter(CKANCONNECTORCONTEXT);
|
|
|
|
logger.debug(CKANCONNECTORCONTEXT + " is: "+ckanContext);
|
|
|
|
ckanContext= ckanContext!=null?ckanContext:"";
|
2016-06-23 12:49:45 +02:00
|
|
|
|
2016-07-01 16:51:51 +02:00
|
|
|
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(ckanConnectorBaseUrl, ckanContext);
|
2016-06-29 11:25:46 +02:00
|
|
|
pathInfoParameter = CkanConnectorAccessPoint.checkURLPathSeparator(pathInfoParameter, true, false);
|
2016-06-23 14:57:20 +02:00
|
|
|
logger.debug("External Path Info parameter: "+pathInfoParameter);
|
2016-06-23 12:49:45 +02:00
|
|
|
|
2016-06-23 14:57:20 +02:00
|
|
|
//ADD PATH INFO
|
2016-06-29 11:25:46 +02:00
|
|
|
ckan.addPathInfo(pathInfoParameter);
|
2016-06-23 14:57:20 +02:00
|
|
|
logger.debug("CKanConnector pathInfo: "+ckan.getPathInfoParameter());
|
|
|
|
ckan.addQueryString(queryStringParameters);
|
2016-06-23 12:49:45 +02:00
|
|
|
|
2016-06-23 14:57:20 +02:00
|
|
|
//GET TOKEN
|
2016-06-23 12:49:45 +02:00
|
|
|
String gcubeTokenValue = null;
|
2016-11-25 10:37:56 +01:00
|
|
|
gcubeTokenValue = getGcubeSecurityToken(scopePerCurrentUrl);
|
2016-06-20 16:05:58 +02:00
|
|
|
|
2016-09-14 16:42:46 +02:00
|
|
|
// set the token into the CkanConnectorAccessPoint
|
2016-06-23 12:49:45 +02:00
|
|
|
ckan.addGubeToken(gcubeTokenValue);
|
2016-06-21 16:12:27 +02:00
|
|
|
|
|
|
|
//ADDING LIST OF VRE TO WHICH USER BELONGS
|
2016-06-23 12:49:45 +02:00
|
|
|
if(!SessionUtil.isIntoPortal()){
|
|
|
|
return ckan;
|
|
|
|
}
|
|
|
|
|
2017-01-27 11:27:40 +01:00
|
|
|
// retrieve the list of VREs to whom the user belongs (actually one vre at most is sent)
|
2017-01-26 18:47:46 +01:00
|
|
|
Map<String, String> roleForVre = UserUtil.getVreRoleForUser(
|
2017-02-03 18:06:49 +01:00
|
|
|
SessionUtil.getCurrentUser(getThreadLocalRequest()).getEmail(),
|
|
|
|
scopePerCurrentUrl,
|
2017-01-26 18:47:46 +01:00
|
|
|
getCatalogue(scopePerCurrentUrl));
|
2017-01-25 18:30:53 +01:00
|
|
|
ckan.addListOfVREs(roleForVre);
|
2016-06-23 12:49:45 +02:00
|
|
|
|
|
|
|
return ckan;
|
2016-06-20 15:22:21 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:48:29 +02:00
|
|
|
/**
|
2016-09-14 16:42:46 +02:00
|
|
|
* Gets the gcube security token for the user in current session and for a given scope
|
2016-06-13 09:48:29 +02:00
|
|
|
*
|
|
|
|
* @return the gcube security token
|
2016-11-04 13:01:35 +01:00
|
|
|
* @throws Exception
|
2016-06-13 09:48:29 +02:00
|
|
|
*/
|
2016-11-17 14:17:29 +01:00
|
|
|
protected String getGcubeSecurityToken(String context) throws Exception {
|
2016-06-13 09:48:29 +02:00
|
|
|
|
2016-09-14 16:42:46 +02:00
|
|
|
// ask it directly to the auth service
|
2016-11-25 10:37:56 +01:00
|
|
|
String username = SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername();
|
2016-11-17 14:17:29 +01:00
|
|
|
String token = null;
|
|
|
|
try{
|
|
|
|
logger.debug("Checking if token for user " + username + " in context " + context + " already exists...");
|
|
|
|
token = authorizationService().resolveTokenByUserAndContext(username, context);
|
|
|
|
logger.debug("It exists!");
|
|
|
|
}catch(ObjectNotFound e){
|
|
|
|
logger.info("Creating token for user " + username + " and context " + context);
|
|
|
|
token = authorizationService().generateUserToken(new UserInfo(username, new ArrayList<String>()), context);
|
|
|
|
logger.debug("received token: "+ token.substring(0, 5) + "***********************");
|
|
|
|
}
|
2016-09-14 16:42:46 +02:00
|
|
|
return token;
|
2016-06-13 09:48:29 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService#getMyRole()
|
|
|
|
*/
|
|
|
|
@Override
|
2017-01-25 18:30:53 +01:00
|
|
|
public RolesCkanGroupOrOrg getMyRole(){
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
// base role as default value
|
2017-01-25 18:30:53 +01:00
|
|
|
RolesCkanGroupOrOrg toReturn = RolesCkanGroupOrOrg.MEMBER;
|
2016-06-18 10:25:17 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
if(!SessionUtil.isIntoPortal()){
|
2017-01-25 18:30:53 +01:00
|
|
|
logger.warn("OUT FROM PORTAL DETECTED RETURNING ROLE: "+RolesCkanGroupOrOrg.ADMIN);
|
|
|
|
toReturn = RolesCkanGroupOrOrg.ADMIN;
|
2016-06-30 17:22:37 +02:00
|
|
|
}else{
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
2016-11-25 10:37:56 +01:00
|
|
|
String username = SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername();
|
2016-07-04 12:44:58 +02:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
// get the scope
|
|
|
|
String scopePerCurrentUrl = SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
|
2016-09-05 18:18:57 +02:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
// get key per scope
|
|
|
|
String keyPerScopeRole = UtilMethods.concatenateSessionKeyScope(SessionCatalogueAttributes.CKAN_HIGHEST_ROLE, scopePerCurrentUrl);
|
|
|
|
String keyPerScopeOrganizations = UtilMethods.concatenateSessionKeyScope(SessionCatalogueAttributes.CKAN_ORGANIZATIONS_PUBLISH_KEY, scopePerCurrentUrl);
|
|
|
|
String keyPerScopeGroups = UtilMethods.concatenateSessionKeyScope(SessionCatalogueAttributes.CKAN_GROUPS_MEMBER, scopePerCurrentUrl);
|
2016-11-04 13:01:35 +01:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
// check into session
|
|
|
|
if(httpSession.getAttribute(keyPerScopeRole) != null){
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2017-01-25 18:30:53 +01:00
|
|
|
toReturn = (RolesCkanGroupOrOrg)httpSession.getAttribute(keyPerScopeRole);
|
2016-11-25 10:37:56 +01:00
|
|
|
logger.info("Found user role into session " + toReturn + " and it is going to be returned for user " + username);
|
2016-06-18 13:58:31 +02:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
}else{
|
2016-06-18 13:58:31 +02:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
try{
|
|
|
|
GroupManager gm = new LiferayGroupManager();
|
|
|
|
String groupName = gm.getGroup(gm.getGroupIdFromInfrastructureScope(scopePerCurrentUrl)).getGroupName();
|
2016-09-05 18:18:57 +02:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
// we build up also a list that keeps track of the scopes (orgs) in which the user has at least role EDITOR
|
|
|
|
List<OrganizationBean> orgsInWhichAtLeastEditorRole = new ArrayList<OrganizationBean>();
|
|
|
|
toReturn = UserUtil.getHighestRole(scopePerCurrentUrl, username, groupName, this, orgsInWhichAtLeastEditorRole);
|
2016-11-04 13:01:35 +01:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
// put role in session
|
|
|
|
httpSession.setAttribute(keyPerScopeRole, toReturn);
|
|
|
|
logger.info("Set role " + toReturn + " into session for user " + username);
|
2016-09-05 18:18:57 +02:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
// if he is an admin/editor preload:
|
|
|
|
// 1) organizations in which he can publish (the widget will find these info in session)
|
|
|
|
// 2) the list of groups
|
2017-01-25 18:30:53 +01:00
|
|
|
if(!toReturn.equals(RolesCkanGroupOrOrg.MEMBER)){
|
2016-11-25 10:37:56 +01:00
|
|
|
httpSession.setAttribute(keyPerScopeOrganizations, orgsInWhichAtLeastEditorRole);
|
|
|
|
httpSession.setAttribute(keyPerScopeGroups, fetchUserGroups(scopePerCurrentUrl, username));
|
2016-06-30 17:22:37 +02:00
|
|
|
}
|
2016-11-25 10:37:56 +01:00
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Error while retreving roles... returning " + toReturn, e);
|
2016-06-30 17:22:37 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-14 17:42:59 +02:00
|
|
|
}
|
2016-06-30 17:22:37 +02:00
|
|
|
|
2017-02-01 18:11:54 +01:00
|
|
|
logger.debug("Returning highest role " + toReturn);
|
2016-11-25 10:37:56 +01:00
|
|
|
|
2016-07-20 19:14:46 +02:00
|
|
|
// return the role
|
2016-06-30 17:22:37 +02:00
|
|
|
return toReturn;
|
2016-06-13 09:48:29 +02:00
|
|
|
}
|
2016-06-13 12:54:15 +02:00
|
|
|
|
2016-11-17 14:31:01 +01:00
|
|
|
/**
|
|
|
|
* Fetch the list of ckan groups for which the user is member
|
2017-02-03 18:06:49 +01:00
|
|
|
* @param context
|
2016-11-17 14:31:01 +01:00
|
|
|
* @return
|
|
|
|
*/
|
2017-04-27 16:45:15 +02:00
|
|
|
private List<OrganizationBean> fetchUserGroups(String context, String username) {
|
2016-11-17 14:31:01 +01:00
|
|
|
|
2017-04-27 16:45:15 +02:00
|
|
|
List<OrganizationBean> toReturn = null;
|
2016-11-17 14:31:01 +01:00
|
|
|
logger.info("Preloading user's groups");
|
|
|
|
|
|
|
|
try{
|
|
|
|
DataCatalogue catalogue = getCatalogue(context);
|
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
2017-04-27 16:45:15 +02:00
|
|
|
toReturn = new ArrayList<OrganizationBean>();
|
2017-01-27 11:27:40 +01:00
|
|
|
Map<String, Map<CkanGroup, RolesCkanGroupOrOrg>> mapRoleGroup = catalogue.getUserRoleByGroup(username, apiKey);
|
|
|
|
Set<Entry<String, Map<CkanGroup, RolesCkanGroupOrOrg>>> set = mapRoleGroup.entrySet();
|
|
|
|
for (Entry<String, Map<CkanGroup, RolesCkanGroupOrOrg>> entry : set) {
|
|
|
|
Set<Entry<CkanGroup, RolesCkanGroupOrOrg>> subSet = entry.getValue().entrySet();
|
|
|
|
for (Entry<CkanGroup, RolesCkanGroupOrOrg> subEntry : subSet) {
|
2017-04-27 16:45:15 +02:00
|
|
|
toReturn.add(new OrganizationBean(subEntry.getKey().getTitle(), subEntry.getKey().getName(), false));
|
2017-01-27 11:27:40 +01:00
|
|
|
}
|
2016-11-17 14:31:01 +01:00
|
|
|
}
|
2017-01-27 11:27:40 +01:00
|
|
|
|
2016-11-17 14:31:01 +01:00
|
|
|
logger.debug("List of groups to return is " + toReturn);
|
|
|
|
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Failed to preload user's groups");
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2017-02-15 18:49:48 +01:00
|
|
|
// @Override
|
|
|
|
// public String logoutURIFromCkan() {
|
|
|
|
// String username = SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername();
|
|
|
|
//
|
|
|
|
// // get the scope from session
|
|
|
|
// String scopePerCurrentUrl = SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
|
|
|
|
//
|
|
|
|
// CkanConnectorAccessPoint ckanAP = SessionUtil.getCkanAccessPoint(this.getThreadLocalRequest().getSession(), scopePerCurrentUrl);
|
|
|
|
// logger.info("Logout from CKAN for: "+username +" by token: "+ckanAP.getGcubeTokenValue());
|
|
|
|
//
|
|
|
|
// String ckanConnectorLogut = getServletContext().getInitParameter(CKANCONNECTORLOGOUT);
|
|
|
|
// logger.debug(CKANCONNECTORLOGOUT + " is: "+ckanConnectorLogut);
|
|
|
|
//
|
|
|
|
// CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(ckanAP.getBaseUrl(), ckanConnectorLogut);
|
|
|
|
// ckan.addGubeToken(ckanAP.getGcubeTokenValue());
|
|
|
|
// return ckan.buildURI();
|
|
|
|
// }
|
2016-06-28 12:45:24 +02:00
|
|
|
|
2016-06-23 12:49:45 +02:00
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService#logoutFromCkan()
|
|
|
|
*/
|
|
|
|
@Override
|
2016-11-04 13:01:35 +01:00
|
|
|
public String logoutFromCkanURL() {
|
2017-02-15 18:49:48 +01:00
|
|
|
|
2017-02-03 18:24:11 +01:00
|
|
|
// check if the portlet is accessed from outside the portal
|
|
|
|
if(outsidePortal())
|
|
|
|
return null;
|
2016-11-04 13:01:35 +01:00
|
|
|
|
2016-06-23 12:49:45 +02:00
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
2016-11-25 10:37:56 +01:00
|
|
|
String username = SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername();
|
2016-11-29 17:20:50 +01:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
if(username == null){
|
|
|
|
logger.warn("User is null");
|
|
|
|
return null;
|
|
|
|
}
|
2016-11-29 17:20:50 +01:00
|
|
|
|
2016-11-04 13:01:35 +01:00
|
|
|
|
|
|
|
logger.info("Getting CKAN Logout URL...");
|
2016-11-25 10:37:56 +01:00
|
|
|
String scopePerCurrentUrl = SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
|
2016-11-04 13:01:35 +01:00
|
|
|
CkanConnectorAccessPoint ckanAP = SessionUtil.getCkanAccessPoint(httpSession, scopePerCurrentUrl);
|
|
|
|
// String token = getGcubeSecurityToken();
|
|
|
|
logger.info("Logout from CKAN for: "+username );
|
|
|
|
logger.info(" by token: "+ckanAP.getGcubeTokenValue() +", the scope is: "+scopePerCurrentUrl);
|
|
|
|
|
|
|
|
String ckanConnectorLogout = getServletContext().getInitParameter(CKANCONNECTORLOGOUT);
|
|
|
|
logger.debug(GcubeCkanDataCatalogServiceImpl.CKANCONNECTORLOGOUT + " is: "+ckanConnectorLogout);
|
|
|
|
|
|
|
|
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(ckanAP.getBaseUrl(), ckanConnectorLogout);
|
|
|
|
ckan.addGubeToken(ckanAP.getGcubeTokenValue());
|
|
|
|
|
|
|
|
String deleteURI = ckan.buildURI();
|
|
|
|
logger.debug(GcubeCkanDataCatalogServiceImpl.CKANCONNECTORLOGOUT + " returning: "+deleteURI);
|
|
|
|
return deleteURI;
|
|
|
|
|
|
|
|
|
|
|
|
/*HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
2017-02-15 18:49:48 +01:00
|
|
|
ASLSession session = getASLSession(httpSession);
|
|
|
|
String username = session.getUsername();
|
|
|
|
// get the scope from session
|
|
|
|
String scopePerCurrentUrl = (String)this.getThreadLocalRequest().getSession().getAttribute(SessionCatalogueAttributes.SCOPE_CLIENT_PORTLET_URL);
|
|
|
|
CkanConnectorAccessPoint ckanAP = SessionUtil.getCkanAccessPoint(this.getThreadLocalRequest().getSession(), scopePerCurrentUrl);
|
|
|
|
// String token = getGcubeSecurityToken();
|
|
|
|
logger.info("Logout from CKAN for: "+username +" by token: "+ckanAP.getGcubeTokenValue());
|
|
|
|
|
|
|
|
String ckanConnectorLogut = getServletContext().getInitParameter(CKANCONNECTORLOGOUT);
|
|
|
|
logger.debug(CKANCONNECTORLOGOUT + " is: "+ckanConnectorLogut);
|
|
|
|
|
|
|
|
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(ckanAP.getBaseUrl(), ckanConnectorLogut);
|
|
|
|
ckan.addPathInfo(ckanConnectorLogut);
|
|
|
|
ckan.addGubeToken(ckanAP.getGcubeTokenValue());
|
|
|
|
URL url;
|
|
|
|
try {
|
|
|
|
String deleteURI = ckan.buildURI();
|
|
|
|
logger.debug("Perfoming HTTP delete to URI: "+deleteURI);
|
|
|
|
url = new URL(deleteURI);
|
|
|
|
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
|
|
|
|
httpCon.setDoOutput(true);
|
|
|
|
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded" );
|
|
|
|
httpCon.setRequestMethod("DELETE");
|
|
|
|
httpCon.connect();
|
|
|
|
|
|
|
|
HttpServletResponse response = this.getThreadLocalResponse();
|
|
|
|
|
|
|
|
if(httpCon.getResponseCode()==HttpStatus.SC_OK){
|
|
|
|
response.setContentLength(httpCon.getContentLength());
|
|
|
|
Map<String, List<String>> map = httpCon.getHeaderFields();
|
|
|
|
for (String key : map.keySet()) {
|
|
|
|
String hf = httpCon.getHeaderField(key);
|
|
|
|
logger.trace("key: "+key +", value: "+hf);
|
|
|
|
if(key==null){
|
|
|
|
logger.trace("skyp key: "+key +", value: "+hf);
|
|
|
|
}else
|
|
|
|
response.setHeader(key,hf);
|
|
|
|
}
|
2016-06-27 16:58:01 +02:00
|
|
|
|
2017-02-15 18:49:48 +01:00
|
|
|
response.setContentLength(httpCon.getContentLength());
|
|
|
|
String encoding = httpCon.getContentEncoding();
|
|
|
|
encoding = encoding == null ? UTF_8 : encoding;
|
|
|
|
response.setCharacterEncoding(encoding);
|
|
|
|
response.setStatus(HttpStatus.SC_OK);
|
2016-06-27 16:58:01 +02:00
|
|
|
|
2017-02-15 18:49:48 +01:00
|
|
|
InputStream in = httpCon.getInputStream();
|
|
|
|
ServletOutputStream out = response.getOutputStream();
|
|
|
|
IOUtils.copy(in, out);
|
|
|
|
logger.info("Logout Completed, response code: "+HttpStatus.SC_OK);
|
2016-06-27 16:58:01 +02:00
|
|
|
|
2017-02-15 18:49:48 +01:00
|
|
|
}else{
|
|
|
|
logger.warn("An error occurred during perfoming CKAN logout, Response status is: "+httpCon.getResponseCode());
|
|
|
|
}
|
2016-06-27 11:31:13 +02:00
|
|
|
}
|
2017-02-15 18:49:48 +01:00
|
|
|
catch (IOException e) {
|
|
|
|
logger.error("An error occured during performing Logout from CKAN for: "+username +" by token: "+ckanAP.getGcubeTokenValue(), e);
|
|
|
|
}*/
|
2016-06-27 11:31:13 +02:00
|
|
|
}
|
2016-06-27 12:06:28 +02:00
|
|
|
|
2016-06-29 16:50:18 +02:00
|
|
|
@Override
|
2016-11-21 10:12:24 +01:00
|
|
|
public List<BeanUserInOrgGroupRole> getCkanOrganizationsNamesAndUrlsForUser() {
|
2016-07-22 11:16:50 +02:00
|
|
|
|
2016-11-21 10:12:24 +01:00
|
|
|
List<BeanUserInOrgGroupRole> toReturn = new ArrayList<BeanUserInOrgGroupRole>();
|
2016-06-29 16:50:18 +02:00
|
|
|
|
|
|
|
if(!SessionUtil.isIntoPortal()){
|
|
|
|
|
|
|
|
logger.warn("You are not into the portal");
|
2017-01-25 18:30:53 +01:00
|
|
|
BeanUserInOrgGroupRole org = new BeanUserInOrgGroupRole("testVRE", "/organization/devvre", RolesCkanGroupOrOrg.ADMIN);
|
2016-09-27 18:38:33 +02:00
|
|
|
toReturn.add(org);
|
2016-06-29 16:50:18 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
}else{
|
2016-06-29 16:50:18 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
2016-11-25 10:37:56 +01:00
|
|
|
String username = SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername();
|
2016-06-30 17:22:37 +02:00
|
|
|
|
2016-09-05 18:18:57 +02:00
|
|
|
// retrieve scope per current portlet url
|
2016-11-25 10:37:56 +01:00
|
|
|
String scopePerCurrentUrl = SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
|
2016-09-15 16:30:21 +02:00
|
|
|
String keyPerScope = UtilMethods.concatenateSessionKeyScope(SessionCatalogueAttributes.CKAN_ORGS_USER_KEY, scopePerCurrentUrl);
|
2016-07-04 12:44:58 +02:00
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
// check if the aslsession already has such information
|
|
|
|
if(httpSession.getAttribute(keyPerScope) != null){
|
|
|
|
toReturn = (List<BeanUserInOrgGroupRole>) httpSession.getAttribute(keyPerScope);
|
|
|
|
logger.debug("List of organizations was into the session " + toReturn);
|
|
|
|
}else{
|
|
|
|
logger.debug("Organizations list wasn't into session, retrieving them");
|
|
|
|
DataCatalogue catalogue = getCatalogue(scopePerCurrentUrl);
|
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
2017-01-27 11:27:40 +01:00
|
|
|
Map<String, Map<CkanOrganization, RolesCkanGroupOrOrg>> mapRoleGroup = catalogue.getUserRoleByOrganization(username, apiKey);
|
|
|
|
Set<Entry<String, Map<CkanOrganization, RolesCkanGroupOrOrg>>> set = mapRoleGroup.entrySet();
|
|
|
|
for (Entry<String, Map<CkanOrganization, RolesCkanGroupOrOrg>> entry : set) {
|
|
|
|
Set<Entry<CkanOrganization, RolesCkanGroupOrOrg>> subSet = entry.getValue().entrySet();
|
|
|
|
for (Entry<CkanOrganization, RolesCkanGroupOrOrg> subEntry : subSet) {
|
|
|
|
BeanUserInOrgGroupRole org = new BeanUserInOrgGroupRole(subEntry.getKey().getTitle(), "/organization/" + subEntry.getKey().getName(), subEntry.getValue());
|
|
|
|
toReturn.add(org);
|
|
|
|
}
|
2016-06-30 17:22:37 +02:00
|
|
|
}
|
2016-11-25 10:37:56 +01:00
|
|
|
logger.debug("List of organizations to return for user " + username + " is " + toReturn);
|
|
|
|
httpSession.setAttribute(keyPerScope, toReturn);
|
2016-06-29 16:50:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
2016-11-25 10:37:56 +01:00
|
|
|
|
2016-11-21 10:12:24 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<BeanUserInOrgGroupRole> getCkanGroupsNamesAndUrlsForUser() {
|
|
|
|
List<BeanUserInOrgGroupRole> toReturn = new ArrayList<BeanUserInOrgGroupRole>();
|
|
|
|
|
|
|
|
if(!SessionUtil.isIntoPortal()){
|
|
|
|
|
|
|
|
logger.warn("You are not into the portal");
|
2017-01-25 18:30:53 +01:00
|
|
|
BeanUserInOrgGroupRole org = new BeanUserInOrgGroupRole("testGroup", "/group/testgroup", RolesCkanGroupOrOrg.MEMBER);
|
2016-11-21 10:12:24 +01:00
|
|
|
toReturn.add(org);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
HttpSession httpSession = getThreadLocalRequest().getSession();
|
|
|
|
String username = SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername();
|
2016-11-21 10:12:24 +01:00
|
|
|
|
|
|
|
// retrieve scope per current portlet url
|
2016-11-25 10:37:56 +01:00
|
|
|
String scopePerCurrentUrl = SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
|
2016-11-21 10:12:24 +01:00
|
|
|
String keyPerScope = UtilMethods.concatenateSessionKeyScope(SessionCatalogueAttributes.CKAN_GROUPS_USER_KEY, scopePerCurrentUrl);
|
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
// check if the aslsession already has such information
|
|
|
|
if(httpSession.getAttribute(keyPerScope) != null){
|
|
|
|
toReturn = (List<BeanUserInOrgGroupRole>) httpSession.getAttribute(keyPerScope);
|
|
|
|
logger.debug("List of groups was into the session " + toReturn);
|
|
|
|
}else{
|
|
|
|
logger.debug("Groups list wasn't into session, retrieving them");
|
|
|
|
DataCatalogue catalogue = getCatalogue(scopePerCurrentUrl);
|
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
2017-01-27 11:27:40 +01:00
|
|
|
|
|
|
|
Map<String, Map<CkanGroup, RolesCkanGroupOrOrg>> mapRoleGroup = catalogue.getUserRoleByGroup(username, apiKey);
|
|
|
|
Set<Entry<String, Map<CkanGroup, RolesCkanGroupOrOrg>>> set = mapRoleGroup.entrySet();
|
|
|
|
for (Entry<String, Map<CkanGroup, RolesCkanGroupOrOrg>> entry : set) {
|
|
|
|
Set<Entry<CkanGroup, RolesCkanGroupOrOrg>> subSet = entry.getValue().entrySet();
|
|
|
|
for (Entry<CkanGroup, RolesCkanGroupOrOrg> subEntry : subSet) {
|
|
|
|
BeanUserInOrgGroupRole org = new BeanUserInOrgGroupRole(subEntry.getKey().getTitle(), "/group/" + subEntry.getKey().getName(), subEntry.getValue());
|
|
|
|
toReturn.add(org);
|
|
|
|
}
|
2016-11-21 10:12:24 +01:00
|
|
|
}
|
2016-12-13 17:46:40 +01:00
|
|
|
logger.debug("List of groups to return for user " + username + " is " + toReturn);
|
2016-11-25 10:37:56 +01:00
|
|
|
httpSession.setAttribute(keyPerScope, toReturn);
|
2016-11-21 10:12:24 +01:00
|
|
|
}
|
2016-11-25 10:37:56 +01:00
|
|
|
|
2016-11-21 10:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
|
|
|
|
}
|
2016-06-29 16:50:18 +02:00
|
|
|
|
2016-06-29 18:35:01 +02:00
|
|
|
@Override
|
|
|
|
public boolean outsidePortal() {
|
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
if(!SessionUtil.isIntoPortal()){
|
|
|
|
|
2016-09-14 16:42:46 +02:00
|
|
|
logger.warn("You are in DEV mode");
|
2016-06-30 17:22:37 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
2016-11-25 10:37:56 +01:00
|
|
|
String username = null;
|
|
|
|
try{
|
|
|
|
username = SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername();
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.warn("Maybe there is no user ");
|
|
|
|
}
|
|
|
|
return username == null || username.isEmpty();
|
2016-06-30 17:22:37 +02:00
|
|
|
}
|
2016-06-29 18:35:01 +02:00
|
|
|
}
|
2016-11-29 17:20:50 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isManageProductEnabled() {
|
2017-02-16 18:36:09 +01:00
|
|
|
logger.info("Checking if the manage product button needs to be shown or not for the current context");
|
2016-11-29 17:20:50 +01:00
|
|
|
String scopePerCurrentUrl = SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
|
|
|
|
DataCatalogue catalogue = getCatalogue(scopePerCurrentUrl);
|
|
|
|
if(catalogue == null){
|
|
|
|
logger.warn("There is no catalogue instance here..., returning false");
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-26 22:17:09 +01:00
|
|
|
else{
|
2016-12-26 23:12:33 +01:00
|
|
|
try{
|
2017-02-16 18:36:09 +01:00
|
|
|
boolean toReturn = catalogue.isManageProductEnabled();
|
2017-01-02 11:00:17 +01:00
|
|
|
logger.info("Will manage product be enabled for this user? " + Boolean.toString(toReturn));
|
|
|
|
return toReturn;
|
2016-12-26 23:12:33 +01:00
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Unable to determine if the manage product needs to be shown or not", e);
|
|
|
|
return false;
|
2016-12-26 22:17:09 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-29 17:20:50 +01:00
|
|
|
}
|
2016-06-13 09:48:29 +02:00
|
|
|
}
|