2020-11-03 13:09:08 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.portlets.user.geoportaldataviewer.server.util;
|
|
|
|
|
2021-11-18 15:55:05 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-11-03 13:09:08 +01:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2020-11-19 15:19:27 +01:00
|
|
|
import javax.servlet.http.HttpSession;
|
2020-11-03 13:09:08 +01:00
|
|
|
|
2021-12-07 17:13:20 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.legacy.Concessione;
|
2021-12-21 16:52:35 +01:00
|
|
|
import org.gcube.application.geoportalcommon.shared.GNADataViewerConfigProfile;
|
2021-07-30 14:57:08 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2020-11-03 13:09:08 +01:00
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2021-11-18 15:55:05 +01:00
|
|
|
import org.gcube.portlets.user.geoportaldataviewer.server.GNABaseMapsResourceReader;
|
|
|
|
import org.gcube.portlets.user.geoportaldataviewer.shared.gis.BaseMapLayer;
|
2020-11-19 15:19:27 +01:00
|
|
|
import org.gcube.portlets.user.urlshortener.UrlShortener;
|
2020-11-03 13:09:08 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.GroupManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
|
|
|
|
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
|
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
|
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class SessionUtil.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
|
|
*
|
2021-07-30 14:57:08 +02:00
|
|
|
* Oct 20, 2020
|
2020-11-03 13:09:08 +01:00
|
|
|
*/
|
|
|
|
public class SessionUtil {
|
2022-10-11 15:00:39 +02:00
|
|
|
|
|
|
|
private static final String MAP_UCD_ID_TO_GCUBE_PROFILES = "MAP_UCD_ID_TO_GCUBE_PROFILES";
|
2020-11-03 13:09:08 +01:00
|
|
|
|
|
|
|
/** The log. */
|
|
|
|
private static Logger LOG = LoggerFactory.getLogger(SessionUtil.class);
|
2021-07-30 14:57:08 +02:00
|
|
|
|
2021-09-23 18:18:02 +02:00
|
|
|
private static final String URL_SHORTENER_SERVICE = "URL_SHORTENER_SERVICE";
|
2021-11-18 15:55:05 +01:00
|
|
|
private static final String LIST_BASE_MAPS_LAYERS = "LIST_BASE_MAPS_LAYERS";
|
2021-12-07 17:13:20 +01:00
|
|
|
private static final String LIST_OF_CONCESSIONI = "LIST_OF_CONCESSIONI_DATA_VIEWER";
|
2021-12-21 16:52:35 +01:00
|
|
|
private static final String GNA_DATAVIEWER_CONFIG_PROFILE = "GNA_DATAVIEWER_CONFIG_PROFILE";
|
2022-10-12 17:12:09 +02:00
|
|
|
|
|
|
|
private static final String COUNT_DOCS_FOR_PROFILE_ID = "GNA_DATAVIEWER_COUNT_DOCS_FOR_PROFILE_ID";
|
2021-12-07 17:13:20 +01:00
|
|
|
|
2020-11-03 13:09:08 +01:00
|
|
|
/**
|
|
|
|
* Checks if is into portal.
|
|
|
|
*
|
|
|
|
* @return true, if is into portal
|
|
|
|
*/
|
|
|
|
public static boolean isIntoPortal() {
|
|
|
|
try {
|
|
|
|
UserLocalServiceUtil.getService();
|
|
|
|
return true;
|
2021-07-30 14:57:08 +02:00
|
|
|
} catch (Exception ex) {
|
2020-11-03 18:04:47 +01:00
|
|
|
LOG.warn("Development Mode ON");
|
2020-11-03 13:09:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 14:57:08 +02:00
|
|
|
|
2020-11-19 15:19:27 +01:00
|
|
|
/**
|
|
|
|
* Checks if is session expired.
|
|
|
|
*
|
2021-07-30 14:57:08 +02:00
|
|
|
* @param httpServletRequest the http servlet request
|
2020-11-19 15:19:27 +01:00
|
|
|
* @return true, if is session expired
|
2021-07-30 14:57:08 +02:00
|
|
|
* @throws Exception the exception
|
2020-11-19 15:19:27 +01:00
|
|
|
*/
|
|
|
|
public static boolean isSessionExpired(HttpServletRequest httpServletRequest) throws Exception {
|
|
|
|
LOG.trace("workspace session validating...");
|
|
|
|
return PortalContext.getConfiguration().getCurrentUser(httpServletRequest) == null;
|
|
|
|
}
|
2020-11-03 13:09:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the current user by using the portal manager.
|
|
|
|
*
|
|
|
|
* @param request the request
|
|
|
|
* @return a GcubeUser object
|
|
|
|
*/
|
2021-07-30 14:57:08 +02:00
|
|
|
public static GCubeUser getCurrentUser(HttpServletRequest request) {
|
2020-11-03 13:09:08 +01:00
|
|
|
|
2021-07-30 14:57:08 +02:00
|
|
|
if (request == null)
|
2020-11-03 13:09:08 +01:00
|
|
|
throw new IllegalArgumentException("HttpServletRequest is null!");
|
|
|
|
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
GCubeUser user = pContext.getCurrentUser(request);
|
|
|
|
LOG.debug("Returning user " + user);
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the current scope by using the portal manager.
|
|
|
|
*
|
2021-07-30 14:57:08 +02:00
|
|
|
* @param request the request
|
2020-11-03 13:09:08 +01:00
|
|
|
* @param setInThread the set in thread
|
|
|
|
* @return a GcubeUser object
|
|
|
|
*/
|
2021-07-30 14:57:08 +02:00
|
|
|
public static String getCurrentContext(HttpServletRequest request, boolean setInThread) {
|
2020-11-03 13:09:08 +01:00
|
|
|
|
2021-07-30 14:57:08 +02:00
|
|
|
if (request == null)
|
2020-11-03 13:09:08 +01:00
|
|
|
throw new IllegalArgumentException("HttpServletRequest is null!");
|
|
|
|
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
String context = pContext.getCurrentScope(request);
|
|
|
|
|
2021-07-30 14:57:08 +02:00
|
|
|
if (context != null && setInThread)
|
2020-11-03 13:09:08 +01:00
|
|
|
ScopeProvider.instance.set(context);
|
|
|
|
|
2021-07-30 14:57:08 +02:00
|
|
|
LOG.debug("Returning context " + context);
|
2020-11-03 13:09:08 +01:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:57:08 +02:00
|
|
|
/**
|
|
|
|
* Retrieve the current scope by using the portal manager.
|
|
|
|
*
|
|
|
|
* @param request the request
|
|
|
|
* @param setInThread the set in thread
|
|
|
|
* @return a GcubeUser object
|
|
|
|
*/
|
|
|
|
public static String getCurrentToken(HttpServletRequest request, boolean setInThread) {
|
|
|
|
|
|
|
|
if (request == null)
|
|
|
|
throw new IllegalArgumentException("HttpServletRequest is null!");
|
|
|
|
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
String scope = pContext.getCurrentScope(request);
|
|
|
|
GCubeUser user = pContext.getCurrentUser(request);
|
|
|
|
String token = PortalContext.getConfiguration().getCurrentUserToken(scope, user.getUsername());
|
|
|
|
|
|
|
|
if (token != null && setInThread)
|
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
|
2021-09-24 13:14:02 +02:00
|
|
|
LOG.debug("Returning token " + token.substring(1, 10) + "_MASKED_TOKEN_");
|
2021-07-30 14:57:08 +02:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2020-11-03 13:09:08 +01:00
|
|
|
/**
|
|
|
|
* Retrieve the group given the scope.
|
|
|
|
*
|
|
|
|
* @param scope the scope
|
|
|
|
* @return the group from scope
|
|
|
|
* @throws UserManagementSystemException the user management system exception
|
2021-07-30 14:57:08 +02:00
|
|
|
* @throws GroupRetrievalFault the group retrieval fault
|
2020-11-03 13:09:08 +01:00
|
|
|
*/
|
2021-07-30 14:57:08 +02:00
|
|
|
public static GCubeGroup getGroupFromScope(String scope) throws UserManagementSystemException, GroupRetrievalFault {
|
2020-11-03 13:09:08 +01:00
|
|
|
|
2021-07-30 14:57:08 +02:00
|
|
|
if (scope == null || scope.isEmpty())
|
2020-11-03 13:09:08 +01:00
|
|
|
throw new IllegalArgumentException("Scope is missing here!!");
|
|
|
|
|
|
|
|
GroupManager gm = new LiferayGroupManager();
|
|
|
|
long groupId = gm.getGroupIdFromInfrastructureScope(scope);
|
|
|
|
return gm.getGroup(groupId);
|
|
|
|
|
|
|
|
}
|
2020-11-19 15:19:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the url shortener.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @return the url shortener
|
|
|
|
*/
|
|
|
|
public static UrlShortener getUrlShortener(HttpServletRequest httpServletRequest) {
|
|
|
|
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
UrlShortener shortener = null;
|
|
|
|
try {
|
|
|
|
shortener = (UrlShortener) session.getAttribute(URL_SHORTENER_SERVICE);
|
|
|
|
|
|
|
|
if (shortener == null) {
|
|
|
|
shortener = new UrlShortener();
|
|
|
|
session.setAttribute(URL_SHORTENER_SERVICE, shortener);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
2021-07-30 14:57:08 +02:00
|
|
|
LOG.warn("Error occurred when instancing the " + UrlShortener.class.getSimpleName(), e);
|
2020-11-19 15:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return shortener;
|
|
|
|
}
|
2022-10-11 15:00:39 +02:00
|
|
|
|
2021-11-18 15:55:05 +01:00
|
|
|
/**
|
|
|
|
* Gets the GNA base maps.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @return the GNA base maps
|
|
|
|
*/
|
|
|
|
public static List<BaseMapLayer> getGNABaseMaps(HttpServletRequest httpServletRequest) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
List<BaseMapLayer> lstBML = null;
|
|
|
|
try {
|
|
|
|
lstBML = (List<BaseMapLayer>) session.getAttribute(LIST_BASE_MAPS_LAYERS);
|
|
|
|
|
|
|
|
if (lstBML == null) {
|
|
|
|
GNABaseMapsResourceReader gnaBMRR = new GNABaseMapsResourceReader();
|
|
|
|
lstBML = gnaBMRR.getListBaseMaps();
|
|
|
|
session.setAttribute(LIST_BASE_MAPS_LAYERS, lstBML);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.warn("Error occurred reading the base maps by " + GNABaseMapsResourceReader.class.getSimpleName(), e);
|
|
|
|
}
|
|
|
|
return lstBML;
|
|
|
|
}
|
2021-12-21 16:52:35 +01:00
|
|
|
|
2021-12-07 17:13:20 +01:00
|
|
|
/**
|
|
|
|
* Gets the list of concessioni.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @param reloadFromService the reload from service
|
|
|
|
* @return the list of concessioni
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
public static List<Concessione> getListOfConcessioni(HttpServletRequest httpServletRequest,
|
|
|
|
boolean reloadFromService) throws Exception {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
List<Concessione> listOfConcessioni = (List<Concessione>) session.getAttribute(LIST_OF_CONCESSIONI);
|
2022-10-11 15:00:39 +02:00
|
|
|
throw new Exception("getListConcessioni must be revisited!!!!");
|
|
|
|
/*
|
2021-12-07 17:13:20 +01:00
|
|
|
// setting null to force reloading from service
|
|
|
|
if (reloadFromService)
|
|
|
|
listOfConcessioni = null;
|
|
|
|
|
|
|
|
if (listOfConcessioni == null) {
|
|
|
|
listOfConcessioni = new ArrayList<Concessione>();
|
|
|
|
LOG.info("Loading list of concessione from client mongo");
|
|
|
|
SessionUtil.getCurrentContext(httpServletRequest, true);
|
|
|
|
MongoServiceCommon serviceUtil = new MongoServiceCommon();
|
|
|
|
MongoConcessioni clientMongo = serviceUtil.getInstanceMongoConcessioni();
|
2021-12-21 16:52:35 +01:00
|
|
|
|
2021-12-07 17:13:20 +01:00
|
|
|
Iterator<Concessione> concessioni = clientMongo.getList();
|
|
|
|
if (concessioni != null) {
|
|
|
|
while (concessioni.hasNext()) {
|
|
|
|
Concessione concessione = (Concessione) concessioni.next();
|
|
|
|
listOfConcessioni.add(concessione);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// LOG.debug("Got list of concessioni from client mongo: " + listOfConcessioni);
|
|
|
|
session.setAttribute(LIST_OF_CONCESSIONI, listOfConcessioni);
|
|
|
|
LOG.info("Saved in session list of concessioni from client mongo with size: " + listOfConcessioni.size());
|
2022-10-11 15:00:39 +02:00
|
|
|
|
2021-12-07 17:13:20 +01:00
|
|
|
} else
|
|
|
|
LOG.info("list of concessioni presents in session, using it");
|
|
|
|
|
|
|
|
LOG.info("read list of concessioni with size: " + listOfConcessioni.size());
|
|
|
|
return listOfConcessioni;
|
2022-10-11 15:00:39 +02:00
|
|
|
|
|
|
|
*/
|
2021-12-07 17:13:20 +01:00
|
|
|
}
|
2021-12-21 16:52:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the GNA data viewer config profile.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @return the GNA data viewer config profile
|
|
|
|
*/
|
|
|
|
public static GNADataViewerConfigProfile getGNADataViewerConfigProfile(HttpServletRequest httpServletRequest) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
return (GNADataViewerConfigProfile) session.getAttribute(GNA_DATAVIEWER_CONFIG_PROFILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the list item fields config.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @param gNADVConfigProfile the g NADV config profile
|
|
|
|
*/
|
|
|
|
public static void setListItemFieldsConfig(HttpServletRequest httpServletRequest,
|
|
|
|
GNADataViewerConfigProfile gNADVConfigProfile) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
session.setAttribute(GNA_DATAVIEWER_CONFIG_PROFILE, gNADVConfigProfile);
|
|
|
|
}
|
2022-10-12 17:12:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the total document for profile ID.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @param theProfileID the the profile ID
|
|
|
|
* @return the total document for profile ID
|
|
|
|
*/
|
|
|
|
public static Integer getTotalDocumentForProfileID(HttpServletRequest httpServletRequest, String theProfileID) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
return (Integer) session.getAttribute(COUNT_DOCS_FOR_PROFILE_ID + theProfileID);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the total document for profile ID.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @param theProfileID the the profile ID
|
|
|
|
* @param countForProfileId the count for profile id
|
|
|
|
*/
|
|
|
|
public static void setTotalDocumentForProfileID(HttpServletRequest httpServletRequest, String theProfileID,
|
|
|
|
Integer countForProfileId) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
session.setAttribute(COUNT_DOCS_FOR_PROFILE_ID + theProfileID, countForProfileId);
|
|
|
|
|
|
|
|
}
|
2020-11-03 13:09:08 +01:00
|
|
|
}
|