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-06-16 18:10:50 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.CKanUtilsImpl;
|
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;
|
|
|
|
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
|
|
|
|
* 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-07-07 16:20:44 +02:00
|
|
|
public final static String PRODUCTION_SCOPE = "/d4science.research-infrastructures.eu";
|
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-02 23:41:11 +02:00
|
|
|
// ckan keys for ASL
|
|
|
|
private static final String CKAN_TOKEN_KEY = "ckanToken";
|
2016-07-03 00:02:34 +02:00
|
|
|
private static final String CKAN_ORGS_USER_KEY = "ckanOrgs"; // organizations to whom he belongs
|
2016-07-02 23:41:11 +02:00
|
|
|
private static final String CKAN_HIGHEST_ROLE = "ckanHighestRole"; // editor, member, admin, sysadmin
|
|
|
|
private static final String CKAN_LICENSES_KEY = "ckanLicenses"; // licenses
|
|
|
|
private static final String CKAN_ORGANIZATIONS_PUBLISH_KEY = "ckanOrganizationsPublish"; // here he can publish
|
|
|
|
private static final String CKAN_PROFILES_KEY = "ckanProfiles"; // product profiles
|
2016-06-18 14:01:58 +02:00
|
|
|
|
2016-06-27 11:10:47 +02:00
|
|
|
/**
|
|
|
|
* Since it needs the scope, we need to check if it is null or not
|
|
|
|
* @return
|
2016-06-20 15:22:21 +02:00
|
|
|
*/
|
2016-07-03 00:01:07 +02:00
|
|
|
private CKanUtils getCkanUtilsObj(){
|
2016-06-27 11:10:47 +02:00
|
|
|
|
2016-07-04 16:15:19 +02:00
|
|
|
// check into session
|
|
|
|
HttpSession httpSession = getThreadLocalRequest().getSession();
|
2016-07-07 16:20:44 +02:00
|
|
|
ASLSession aslSession = getASLSession(httpSession);
|
|
|
|
String currentScope = aslSession.getScope();
|
2016-07-04 16:15:19 +02:00
|
|
|
|
2016-07-07 16:20:44 +02:00
|
|
|
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-07-07 15:37:04 +02:00
|
|
|
if(user.equals(TEST_USER)){
|
|
|
|
logger.warn("User is "+TEST_USER +" are we out from portal?");
|
|
|
|
logger.warn("I'm using root scope "+PRODUCTION_SCOPE);
|
2016-07-07 16:20:44 +02:00
|
|
|
instance = new CKanUtilsImpl(PRODUCTION_SCOPE);
|
|
|
|
}else
|
|
|
|
instance = new CKanUtilsImpl(currentScope);
|
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-06-23 12:49:45 +02: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
|
|
|
|
|
|
|
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-06-23 14:57:20 +02:00
|
|
|
CkanConnectorAccessPoint ckAP = getCkanConnectorAccessPoint(pathInfoParameter, queryStringParameters);
|
|
|
|
SessionUtil.saveCkanAccessPoint(this.getThreadLocalRequest().getSession(), ckAP);
|
|
|
|
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-22 17:31:47 +02:00
|
|
|
// return "http://ckan-d-d4s.d4science.org";
|
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-06-20 15:22:21 +02:00
|
|
|
*/
|
2016-06-23 14:57:20 +02:00
|
|
|
private CkanConnectorAccessPoint getCkanConnectorAccessPoint(String pathInfoParameter, String queryStringParameters) {
|
2016-06-30 17:22:37 +02:00
|
|
|
|
2016-06-29 18:35:01 +02:00
|
|
|
if(outsidePortal()){
|
2016-06-30 17:22:37 +02:00
|
|
|
|
2016-07-01 16:51:51 +02:00
|
|
|
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(getCkanUtilsObj().getCatalogueUrl(),"");
|
2016-06-29 18:35:01 +02:00
|
|
|
return ckan;
|
2016-06-30 17:22:37 +02:00
|
|
|
|
2016-06-29 18:35:01 +02:00
|
|
|
}
|
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-06-29 18:35:01 +02:00
|
|
|
GcoreEndpointReader ckanEndPoint = SessionUtil.getCkanEndPoint(this.getThreadLocalRequest().getSession(), session.getScope());
|
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-20 15:22:21 +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-29 18:35:01 +02:00
|
|
|
//session.setUserEmailAddress(TEST_MAIL);
|
2016-06-13 09:48:29 +02:00
|
|
|
|
|
|
|
return session;
|
|
|
|
} else logger.trace("user found in session "+user);
|
|
|
|
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
|
|
|
// we need to evaluate which roles the user has in this scope / or in his vre if the current scope is the root
|
|
|
|
HttpSession httpSession = this.getThreadLocalRequest().getSession();
|
|
|
|
ASLSession session = getASLSession(httpSession);
|
|
|
|
String currentScope = session.getScope();
|
|
|
|
String username = session.getUsername();
|
|
|
|
String groupName = session.getGroupName(); // e.g. devVRE
|
2016-06-18 13:58:31 +02:00
|
|
|
|
2016-07-04 12:44:58 +02:00
|
|
|
// get key per scope
|
|
|
|
String keyPerScope = concatenateSessionKeyScope(CKAN_HIGHEST_ROLE, session.getScope());
|
|
|
|
|
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-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-06-30 17:22:37 +02:00
|
|
|
logger.info("Found user role into session " + toReturn + " and it is going to be returned");
|
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-07-03 00:01:07 +02:00
|
|
|
CKanUtils ckanUtils = getCkanUtilsObj();
|
2016-06-18 10:25:17 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
// first of all, check if the user is a sysadmin in the catalog (in this case he can do everything)
|
|
|
|
boolean isSysAdmin = ckanUtils.isSysAdmin(username, getUserCKanTokenFromSession());
|
2016-06-18 13:58:31 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
if(isSysAdmin){
|
2016-06-14 17:42:59 +02:00
|
|
|
|
2016-07-02 23:41:11 +02:00
|
|
|
logger.info("The user is a sysadmin of the catalog -> he can edit/add");
|
2016-07-04 12:44:58 +02:00
|
|
|
httpSession.setAttribute(keyPerScope, CkanRole.SYSADMIN);
|
2016-06-30 17:22:37 +02:00
|
|
|
toReturn = CkanRole.SYSADMIN;
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
toReturn = UserUtil.getHighestRole(currentScope, username, groupName, ckanUtils);
|
2016-07-04 12:44:58 +02:00
|
|
|
httpSession.setAttribute(keyPerScope, toReturn);
|
2016-07-02 23:41:11 +02:00
|
|
|
logger.info("Set role " + toReturn + " into session for user " + username);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if he is not a member (so he can publish), preload:
|
|
|
|
// 1) organizations in which he can publish
|
|
|
|
// 2) the list of licenses
|
|
|
|
// 3) metadata profiles
|
|
|
|
// In this way the publish widget can simply retrieve those information from the ASL session
|
|
|
|
if(!toReturn.equals(CkanRole.MEMBER)){
|
2016-07-04 12:44:58 +02:00
|
|
|
UserUtil.getLicenses(httpSession, username, concatenateSessionKeyScope(CKAN_LICENSES_KEY, currentScope), ckanUtils);
|
2016-07-04 14:11:43 +02:00
|
|
|
UserUtil.getUserOrganizationsList(httpSession, username, isSysAdmin, concatenateSessionKeyScope(CKAN_ORGANIZATIONS_PUBLISH_KEY, currentScope), ckanUtils, getUserCKanTokenFromSession());
|
2016-07-04 12:44:58 +02:00
|
|
|
UserUtil.getMetadataProfilesList(httpSession, username, concatenateSessionKeyScope(CKAN_PROFILES_KEY, currentScope), ckanUtils);
|
2016-06-30 17:22:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 17:42:59 +02:00
|
|
|
}
|
2016-06-30 17:22:37 +02:00
|
|
|
|
|
|
|
// return the base role
|
|
|
|
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-07-04 12:44:58 +02:00
|
|
|
* Get current user's token
|
2016-06-20 15:22:21 +02:00
|
|
|
* @return String the ckan user's token
|
|
|
|
*/
|
|
|
|
private String getUserCKanTokenFromSession(){
|
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
String token = null;
|
2016-06-20 15:22:21 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
if(!SessionUtil.isIntoPortal()){
|
|
|
|
logger.warn("You are running outside the portal");
|
|
|
|
token = TEST_SEC_TOKEN;
|
|
|
|
}else{
|
2016-06-27 11:10:47 +02:00
|
|
|
|
2016-07-04 12:44:58 +02:00
|
|
|
// store info in the http session
|
|
|
|
HttpSession httpSession = getThreadLocalRequest().getSession();
|
|
|
|
|
|
|
|
ASLSession aslSession = getASLSession(httpSession);
|
|
|
|
String username = aslSession.getUsername();
|
|
|
|
|
|
|
|
// get the key per scope
|
|
|
|
String keyPerScope = concatenateSessionKeyScope(CKAN_TOKEN_KEY, aslSession.getScope());
|
2016-06-20 15:22:21 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
// check if session expired
|
|
|
|
if(username.equals(TEST_USER)){
|
2016-06-27 11:10:47 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
logger.warn("Session expired, returning null token");
|
|
|
|
token = null;
|
2016-06-20 15:22:21 +02:00
|
|
|
|
2016-06-30 17:22:37 +02:00
|
|
|
}else{
|
|
|
|
try{
|
|
|
|
logger.debug("User in session is " + username);
|
2016-07-04 12:44:58 +02:00
|
|
|
if(httpSession.getAttribute(keyPerScope) != null)
|
|
|
|
token = (String)httpSession.getAttribute(keyPerScope);
|
2016-06-30 17:22:37 +02:00
|
|
|
else{
|
|
|
|
token = getCkanUtilsObj().getApiKeyFromUsername(username);
|
2016-07-04 12:44:58 +02:00
|
|
|
httpSession.setAttribute(keyPerScope, token);
|
2016-06-30 17:22:37 +02:00
|
|
|
logger.debug("Ckan token has been set for user " + username);
|
|
|
|
}
|
|
|
|
logger.debug("Found ckan token " + token.substring(0, 3) + "************************" + " for user " + username);
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Error while retrieving the key" , e);
|
|
|
|
}
|
|
|
|
}
|
2016-06-27 11:10:47 +02:00
|
|
|
}
|
2016-06-30 17:22:37 +02:00
|
|
|
return token;
|
2016-06-20 15:22:21 +02:00
|
|
|
}
|
2016-06-23 12:49:45 +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();
|
|
|
|
CkanConnectorAccessPoint ckanAP = SessionUtil.getCkanAccessPoint(this.getThreadLocalRequest().getSession());
|
|
|
|
// 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-06-23 14:57:20 +02:00
|
|
|
CkanConnectorAccessPoint ckanAP = SessionUtil.getCkanAccessPoint(this.getThreadLocalRequest().getSession());
|
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() {
|
|
|
|
|
|
|
|
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-07-04 12:44:58 +02:00
|
|
|
String keyPerScope = concatenateSessionKeyScope(CKAN_ORGS_USER_KEY, session.getScope());
|
|
|
|
|
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-07-01 17:01:54 +02:00
|
|
|
List<CkanOrganization> organizations = getCkanUtilsObj().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-07-04 12:44:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a string made of key + scope
|
|
|
|
* @param key
|
|
|
|
* @param scope
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String concatenateSessionKeyScope(String key, String scope){
|
|
|
|
return key.concat(scope);
|
|
|
|
}
|
2016-06-13 09:48:29 +02:00
|
|
|
}
|