2016-06-13 09:48:29 +02:00
|
|
|
package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server;
|
|
|
|
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
2016-06-23 14:57:20 +02:00
|
|
|
import java.io.IOException;
|
2016-06-27 12:06:28 +02:00
|
|
|
import java.io.InputStream;
|
2016-06-23 14:57:20 +02:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2016-06-13 09:48:29 +02:00
|
|
|
import java.util.ArrayList;
|
2016-06-29 16:50:18 +02:00
|
|
|
import java.util.HashMap;
|
2016-06-13 09:48:29 +02:00
|
|
|
import java.util.List;
|
2016-06-27 12:06:28 +02:00
|
|
|
import java.util.Map;
|
2016-06-13 09:48:29 +02:00
|
|
|
|
2016-06-27 16:58:01 +02:00
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
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-06-28 12:45:24 +02:00
|
|
|
import org.apache.http.HttpStatus;
|
2016-06-13 09:48:29 +02:00
|
|
|
import org.gcube.application.framework.core.session.ASLSession;
|
|
|
|
import org.gcube.application.framework.core.session.SessionManager;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2016-07-03 00:01:07 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.CKanUtils;
|
2016-08-02 09:49:24 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.CkanUtilsFactory;
|
2016-09-05 18:18:57 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.utils.ApplicationProfileScopePerUrlReader;
|
2016-06-13 09:48:29 +02:00
|
|
|
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
|
|
|
|
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService;
|
2016-06-23 12:49:45 +02:00
|
|
|
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.CkanConnectorAccessPoint;
|
2016-06-13 09:48:29 +02:00
|
|
|
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.CkanRole;
|
2016-07-20 22:42:39 +02: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 org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
2016-06-27 11:41:05 +02:00
|
|
|
|
2016-06-29 16:50:18 +02:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanOrganization;
|
2016-06-27 12:06:28 +02:00
|
|
|
import eu.trentorise.opendata.traceprov.internal.org.apache.commons.io.IOUtils;
|
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-06-13 09:48:29 +02:00
|
|
|
public static final String USERNAME_ATTRIBUTE = ScopeHelper.USERNAME_ATTRIBUTE;
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(GcubeCkanDataCatalogServiceImpl.class);
|
|
|
|
private final static String DEFAULT_ROLE = "OrganizationMember";
|
|
|
|
|
2016-06-29 18:35:01 +02:00
|
|
|
public final static String TEST_USER = "test.user";
|
2016-06-27 16:58:01 +02:00
|
|
|
public final static String TEST_SCOPE = "/gcube/devsec/devVRE";
|
2016-06-29 18:35:01 +02:00
|
|
|
public final static String TEST_MAIL = "test.user@test-com";
|
2016-06-27 16:58:01 +02:00
|
|
|
public final static String TEST_SEC_TOKEN = "4620e6d0-2313-4f48-9d54-eb3efd01a810";
|
2016-06-13 09:48:29 +02:00
|
|
|
|
2016-07-20 19:14:46 +02:00
|
|
|
// CKAN KEYS (PLEASE NOTE THAT THESE INFO ARE SAVED INTO SESSION PER SCOPE)
|
|
|
|
private static final String CKAN_ORGS_USER_KEY = "ckanOrgs"; // organizations to whom he belongs (retrieved by the instance in the current scope)
|
|
|
|
private static final String CKAN_HIGHEST_ROLE = "ckanHighestRole"; // editor, member, admin (this information is retrieved according the scope)
|
|
|
|
private static final String CKAN_ORGANIZATIONS_PUBLISH_KEY = "ckanOrganizationsPublish"; // here he can publish (admin role)
|
2016-06-18 14:01:58 +02:00
|
|
|
|
2016-09-05 18:18:57 +02:00
|
|
|
// THIS IS NEEDED TO HANDLE SPECIAL CASES (e.g. for a certain portlet url we want to discover in another scope)
|
|
|
|
private final static String CLIENT_PORTLET_URL = "currentClientUrlPortlet";
|
|
|
|
|
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-07-20 19:14:46 +02:00
|
|
|
public CKanUtils getCkanUtilsObj(String discoverScope){
|
2016-06-27 11:10:47 +02:00
|
|
|
|
2016-07-04 16:15:19 +02:00
|
|
|
HttpSession httpSession = getThreadLocalRequest().getSession();
|
2016-07-07 16:20:44 +02:00
|
|
|
ASLSession aslSession = getASLSession(httpSession);
|
|
|
|
String currentScope = aslSession.getScope();
|
|
|
|
String user = aslSession.getUsername();
|
2016-07-04 16:15:19 +02:00
|
|
|
|
2016-07-07 15:37:04 +02:00
|
|
|
CKanUtils instance = null;
|
2016-07-04 16:24:53 +02:00
|
|
|
try{
|
2016-09-05 18:18:57 +02:00
|
|
|
String scopeInWhichDiscover = (discoverScope != null && !discoverScope.isEmpty()) ? discoverScope : currentScope;
|
|
|
|
logger.debug("Discovering ckan utils library into scope " + scopeInWhichDiscover);
|
|
|
|
instance = CkanUtilsFactory.getFactory().getUtilsPerScope(scopeInWhichDiscover);
|
2016-07-04 16:24:53 +02:00
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Unable to retrieve ckan utils", e);
|
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
|
2016-09-05 18:18:57 +02:00
|
|
|
public CkanConnectorAccessPoint getCKanConnector(String pathInfoParameter, String queryStringParameters, String currentUrl) throws Exception {
|
|
|
|
logger.info("getCKanConnector [pathInfo: "+pathInfoParameter + ", query: "+queryStringParameters+"], current url is " + currentUrl);
|
2016-06-13 09:48:29 +02:00
|
|
|
try{
|
2016-06-20 16:05:58 +02:00
|
|
|
|
2016-09-05 18:18:57 +02:00
|
|
|
// call asl session otherwise ScopeProvider.instance.get(); returns null
|
|
|
|
ASLSession aslSession = getASLSession(this.getThreadLocalRequest().getSession());
|
|
|
|
|
|
|
|
// retrieve scope per current portlet url
|
|
|
|
String scopePerCurrentUrl = new ApplicationProfileScopePerUrlReader().getScopePerUrl(currentUrl);
|
|
|
|
|
|
|
|
// save it
|
|
|
|
this.getThreadLocalRequest().getSession().setAttribute(CLIENT_PORTLET_URL, currentUrl);
|
|
|
|
|
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-05 18:18:57 +02:00
|
|
|
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(getCkanUtilsObj(scopePerCurrentUrl).getCatalogueUrl(),"");
|
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
|
|
|
|
ASLSession session = getASLSession(this.getThreadLocalRequest().getSession());
|
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-06-20 16:05:58 +02:00
|
|
|
if(SessionUtil.isIntoPortal()){
|
2016-06-23 12:49:45 +02:00
|
|
|
gcubeTokenValue = getGcubeSecurityToken();
|
2016-06-20 16:05:58 +02:00
|
|
|
}else{
|
|
|
|
logger.warn("******** Using TEST_USER security token!!!");
|
2016-06-23 12:49:45 +02:00
|
|
|
gcubeTokenValue = TEST_SEC_TOKEN;
|
2016-06-20 16:05:58 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-21 16:12:27 +02:00
|
|
|
List<String> listVres = UserUtil.getListVreForUser(session.getUserEmailAddress());
|
2016-06-23 12:49:45 +02:00
|
|
|
ckan.addListOfVREs(listVres);
|
|
|
|
|
|
|
|
return ckan;
|
2016-06-20 15:22:21 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:48:29 +02:00
|
|
|
/**
|
|
|
|
* Gets the gcube security token.
|
|
|
|
*
|
|
|
|
* @return the gcube security token
|
|
|
|
*/
|
|
|
|
protected String getGcubeSecurityToken() {
|
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
|
|
|
ASLSession session = getASLSession(httpSession);
|
|
|
|
logger.debug("Get security token return: "+session.getSecurityToken());
|
|
|
|
|
|
|
|
if(session.getSecurityToken()==null || session.getSecurityToken().isEmpty()){
|
|
|
|
logger.warn("Security token retured from ASL is null or empty, I'm setting security token...");
|
|
|
|
setAuthorizationToken(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.getSecurityToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporary method to set the authorization token.
|
|
|
|
*
|
|
|
|
* @param session the new authorization token
|
|
|
|
*/
|
|
|
|
private static void setAuthorizationToken(ASLSession session) {
|
|
|
|
String username = session.getUsername();
|
|
|
|
String scope = session.getScope();
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
logger.debug("calling service token on scope " + scope);
|
|
|
|
List<String> userRoles = new ArrayList<String>();
|
|
|
|
userRoles.add(DEFAULT_ROLE);
|
|
|
|
session.setSecurityToken(null);
|
|
|
|
String token = authorizationService().build().generate(session.getUsername(), userRoles);
|
|
|
|
logger.debug("received token: "+token);
|
|
|
|
session.setSecurityToken(token);
|
|
|
|
logger.info("Security token set in session for: "+username + " on " + scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the ASL session.
|
|
|
|
*
|
|
|
|
* @param httpSession the http session
|
|
|
|
* @return the ASL session
|
|
|
|
*/
|
|
|
|
protected ASLSession getASLSession(HttpSession httpSession)
|
|
|
|
{
|
|
|
|
String sessionID = httpSession.getId();
|
|
|
|
String user = (String) httpSession.getAttribute(USERNAME_ATTRIBUTE);
|
|
|
|
|
|
|
|
if (user == null) {
|
|
|
|
logger.warn("****** STARTING IN TEST MODE - NO USER FOUND *******");
|
|
|
|
//for test only
|
|
|
|
user = TEST_USER;
|
|
|
|
httpSession.setAttribute(USERNAME_ATTRIBUTE, user);
|
|
|
|
ASLSession session = SessionManager.getInstance().getASLSession(sessionID, user);
|
2016-07-01 15:36:20 +02:00
|
|
|
session.setScope(TEST_SCOPE);
|
2016-06-13 09:48:29 +02:00
|
|
|
return session;
|
2016-09-05 18:18:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
logger.trace("user found in session "+user);
|
2016-06-13 09:48:29 +02:00
|
|
|
return SessionManager.getInstance().getASLSession(sessionID, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService#getMyRole()
|
|
|
|
*/
|
|
|
|
@Override
|
2016-06-30 17:22:37 +02:00
|
|
|
public CkanRole getMyRole(){
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
// base role as default value
|
|
|
|
CkanRole toReturn = CkanRole.MEMBER;
|
2016-06-18 10:25:17 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
if(!SessionUtil.isIntoPortal()){
|
|
|
|
logger.warn("OUT FROM PORTAL DETECTED RETURNING ROLE: "+CkanRole.ADMIN);
|
|
|
|
toReturn = CkanRole.ADMIN;
|
|
|
|
}else{
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
|
|
|
ASLSession session = getASLSession(httpSession);
|
|
|
|
String username = session.getUsername();
|
2016-07-04 12:44:58 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
// check if session expired
|
|
|
|
if(username.equals(TEST_USER)){
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
logger.warn("Session expired, returning " + toReturn);
|
2016-06-18 10:25:17 +02:00
|
|
|
|
2016-06-18 13:58:31 +02:00
|
|
|
}else{
|
2016-09-05 18:18:57 +02:00
|
|
|
|
|
|
|
// save it
|
|
|
|
String currentUrl = (String)this.getThreadLocalRequest().getSession().getAttribute(CLIENT_PORTLET_URL);
|
|
|
|
|
|
|
|
// retrieve scope per current portlet url
|
|
|
|
String scopePerCurrentUrl = new ApplicationProfileScopePerUrlReader().getScopePerUrl(currentUrl);
|
|
|
|
|
|
|
|
// get key per scope
|
|
|
|
String keyPerScope = UserUtil.concatenateSessionKeyScope(CKAN_HIGHEST_ROLE, scopePerCurrentUrl);
|
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
// check into session
|
2016-07-04 12:44:58 +02:00
|
|
|
if(httpSession.getAttribute(keyPerScope) != null){
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-07-04 12:44:58 +02:00
|
|
|
toReturn = (CkanRole)httpSession.getAttribute(keyPerScope);
|
2016-07-20 21:29:39 +02: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-06-30 17:22:37 +02:00
|
|
|
}else{
|
2016-06-18 13:58:31 +02:00
|
|
|
|
2016-09-05 18:18:57 +02:00
|
|
|
try{
|
|
|
|
GroupManager gm = new LiferayGroupManager();
|
|
|
|
String groupName = gm.getGroup(gm.getGroupIdFromInfrastructureScope(scopePerCurrentUrl)).getGroupName();
|
|
|
|
|
|
|
|
// we build up also a list that keeps track of the scopes (orgs) in which the user has role ADMIN
|
|
|
|
List<OrganizationBean> orgsInWhichAdminRole = new ArrayList<OrganizationBean>();
|
|
|
|
toReturn = UserUtil.getHighestRole(scopePerCurrentUrl, username, groupName, this, orgsInWhichAdminRole);
|
|
|
|
httpSession.setAttribute(keyPerScope, toReturn);
|
|
|
|
|
|
|
|
logger.info("Set role " + toReturn + " into session for user " + username);
|
|
|
|
|
|
|
|
// if he is an admin preload:
|
|
|
|
// 1) organizations in which he can publish (the widget will find these info in session)
|
|
|
|
if(toReturn.equals(CkanRole.ADMIN)){
|
|
|
|
httpSession.setAttribute(UserUtil.concatenateSessionKeyScope(CKAN_ORGANIZATIONS_PUBLISH_KEY, scopePerCurrentUrl), orgsInWhichAdminRole);
|
|
|
|
logger.info("Set organizations in which he can publish to " + orgsInWhichAdminRole + " into session for user " + username);
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Error while retreving roles... returning MEMBER", 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
|
|
|
|
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-06-20 15:22:21 +02:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService#getUser()
|
|
|
|
*/
|
2016-06-13 12:54:15 +02:00
|
|
|
@Override
|
|
|
|
public String getUser() {
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-06-13 12:54:15 +02:00
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-06-13 12:54:15 +02:00
|
|
|
logger.debug("User in session is " + getASLSession(httpSession).getUsername());
|
|
|
|
return getASLSession(httpSession).getUsername();
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-06-13 12:54:15 +02:00
|
|
|
}
|
2016-06-20 15:22:21 +02:00
|
|
|
|
2016-06-28 12:45:24 +02:00
|
|
|
@Override
|
|
|
|
public String logoutURIFromCkan() {
|
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
|
|
|
ASLSession session = getASLSession(httpSession);
|
|
|
|
String username = session.getUsername();
|
2016-07-19 12:31:53 +02:00
|
|
|
CkanConnectorAccessPoint ckanAP = SessionUtil.getCkanAccessPoint(this.getThreadLocalRequest().getSession(), session.getScope());
|
2016-06-28 12:45:24 +02:00
|
|
|
// String token = getGcubeSecurityToken();
|
|
|
|
logger.info("Logout from CKAN for: "+username +" by token: "+ckanAP.getGcubeTokenValue());
|
|
|
|
|
|
|
|
String ckanConnectorLogut = getServletContext().getInitParameter(CKANCONNECTORLOGOUT);
|
|
|
|
logger.debug(CKANCONNECTORLOGOUT + " is: "+ckanConnectorLogut);
|
|
|
|
|
2016-07-01 16:51:51 +02:00
|
|
|
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(ckanAP.getBaseUrl(), ckanConnectorLogut);
|
2016-06-28 12:45:24 +02:00
|
|
|
ckan.addGubeToken(ckanAP.getGcubeTokenValue());
|
|
|
|
return ckan.buildURI();
|
|
|
|
}
|
|
|
|
|
2016-06-23 12:49:45 +02:00
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService#logoutFromCkan()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void logoutFromCkan() {
|
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
|
|
|
ASLSession session = getASLSession(httpSession);
|
|
|
|
String username = session.getUsername();
|
2016-07-19 12:31:53 +02:00
|
|
|
CkanConnectorAccessPoint ckanAP = SessionUtil.getCkanAccessPoint(this.getThreadLocalRequest().getSession(), session.getScope());
|
2016-06-27 11:10:47 +02:00
|
|
|
// String token = getGcubeSecurityToken();
|
2016-06-23 14:57:20 +02:00
|
|
|
logger.info("Logout from CKAN for: "+username +" by token: "+ckanAP.getGcubeTokenValue());
|
2016-06-23 12:49:45 +02:00
|
|
|
|
|
|
|
String ckanConnectorLogut = getServletContext().getInitParameter(CKANCONNECTORLOGOUT);
|
|
|
|
logger.debug(CKANCONNECTORLOGOUT + " is: "+ckanConnectorLogut);
|
|
|
|
|
2016-07-01 16:51:51 +02:00
|
|
|
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(ckanAP.getBaseUrl(), ckanConnectorLogut);
|
2016-06-23 14:57:20 +02:00
|
|
|
ckan.addPathInfo(ckanConnectorLogut);
|
|
|
|
ckan.addGubeToken(ckanAP.getGcubeTokenValue());
|
|
|
|
|
|
|
|
URL url;
|
|
|
|
try {
|
|
|
|
String deleteURI = ckan.buildURI();
|
|
|
|
logger.debug("Perfoming HTTP delete to URI: "+deleteURI);
|
2016-06-27 11:31:13 +02:00
|
|
|
url = new URL(deleteURI);
|
2016-06-23 14:57:20 +02:00
|
|
|
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
|
|
|
|
httpCon.setDoOutput(true);
|
|
|
|
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded" );
|
|
|
|
httpCon.setRequestMethod("DELETE");
|
|
|
|
httpCon.connect();
|
2016-06-27 11:31:13 +02:00
|
|
|
|
2016-06-27 16:58:01 +02:00
|
|
|
HttpServletResponse response = this.getThreadLocalResponse();
|
|
|
|
|
2016-06-27 11:41:05 +02:00
|
|
|
if(httpCon.getResponseCode()==HttpStatus.SC_OK){
|
2016-06-27 16:58:01 +02:00
|
|
|
response.setContentLength(httpCon.getContentLength());
|
2016-06-27 12:06:28 +02:00
|
|
|
Map<String, List<String>> map = httpCon.getHeaderFields();
|
|
|
|
for (String key : map.keySet()) {
|
2016-06-27 16:58:01 +02:00
|
|
|
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 12:06:28 +02:00
|
|
|
}
|
2016-06-27 16:58:01 +02:00
|
|
|
|
|
|
|
response.setContentLength(httpCon.getContentLength());
|
2016-06-27 12:06:28 +02:00
|
|
|
String encoding = httpCon.getContentEncoding();
|
2016-06-27 16:58:01 +02:00
|
|
|
encoding = encoding == null ? UTF_8 : encoding;
|
|
|
|
response.setCharacterEncoding(encoding);
|
|
|
|
response.setStatus(HttpStatus.SC_OK);
|
|
|
|
|
|
|
|
InputStream in = httpCon.getInputStream();
|
|
|
|
ServletOutputStream out = response.getOutputStream();
|
|
|
|
IOUtils.copy(in, out);
|
2016-06-27 12:06:28 +02:00
|
|
|
logger.info("Logout Completed, response code: "+HttpStatus.SC_OK);
|
2016-06-27 16:58:01 +02:00
|
|
|
|
2016-06-27 11:31:13 +02:00
|
|
|
}else{
|
|
|
|
logger.warn("An error occurred during perfoming CKAN logout, Response status is: "+httpCon.getResponseCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
2016-06-27 11:41:05 +02:00
|
|
|
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
|
|
|
|
public Map<String, String> getCkanOrganizationsNamesAndUrlsForUser() {
|
2016-07-22 11:16:50 +02:00
|
|
|
|
2016-06-29 16:50:18 +02:00
|
|
|
Map<String, String> toReturn = new HashMap<>();
|
|
|
|
|
|
|
|
if(!SessionUtil.isIntoPortal()){
|
|
|
|
|
|
|
|
logger.warn("You are not into the portal");
|
2016-06-30 17:22:37 +02:00
|
|
|
toReturn.put("testVRE", "/organization/devvre");
|
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();
|
|
|
|
ASLSession session = getASLSession(httpSession);
|
|
|
|
String username = session.getUsername();
|
|
|
|
|
2016-09-05 18:18:57 +02:00
|
|
|
// get it
|
|
|
|
String currentUrl = (String)this.getThreadLocalRequest().getSession().getAttribute(CLIENT_PORTLET_URL);
|
|
|
|
|
|
|
|
// retrieve scope per current portlet url
|
|
|
|
String scopePerCurrentUrl = new ApplicationProfileScopePerUrlReader().getScopePerUrl(currentUrl);
|
|
|
|
|
|
|
|
String keyPerScope = UserUtil.concatenateSessionKeyScope(CKAN_ORGS_USER_KEY, scopePerCurrentUrl);
|
2016-07-04 12:44:58 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
if(!username.equals(TEST_USER)){
|
|
|
|
|
|
|
|
// check if the aslsession already has such information
|
2016-07-04 12:44:58 +02:00
|
|
|
if(httpSession.getAttribute(keyPerScope) != null){
|
|
|
|
toReturn = (Map<String, String>) httpSession.getAttribute(keyPerScope);
|
2016-06-30 17:22:37 +02:00
|
|
|
logger.debug("List of organizations was into the session " + toReturn);
|
|
|
|
}else{
|
|
|
|
logger.debug("Organizations list wasn't into session, retrieving them");
|
2016-09-05 18:18:57 +02:00
|
|
|
List<CkanOrganization> organizations = getCkanUtilsObj(scopePerCurrentUrl).getOrganizationsByUser(username);
|
2016-06-30 17:22:37 +02:00
|
|
|
for (CkanOrganization ckanOrganization : organizations) {
|
|
|
|
toReturn.put(ckanOrganization.getTitle(), "/organization/" + ckanOrganization.getName());
|
|
|
|
}
|
|
|
|
logger.debug("List of organizations to return for user " + username + " is " + toReturn);
|
2016-07-04 12:44:58 +02:00
|
|
|
httpSession.setAttribute(keyPerScope, toReturn);
|
2016-06-30 17:22:37 +02:00
|
|
|
}
|
2016-06-29 16:50:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2016-06-29 18:35:01 +02:00
|
|
|
@Override
|
|
|
|
public boolean outsidePortal() {
|
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
if(!SessionUtil.isIntoPortal()){
|
|
|
|
|
|
|
|
logger.warn("You are not into the portal");
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
|
|
|
ASLSession session = getASLSession(httpSession);
|
|
|
|
String username = session.getUsername();
|
|
|
|
|
|
|
|
if(username.equals(TEST_USER))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-29 18:35:01 +02:00
|
|
|
}
|
2016-06-13 09:48:29 +02:00
|
|
|
}
|