2020-10-20 11:49:23 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.portlets.user.geoportaldataentry.server;
|
|
|
|
|
2021-08-02 12:52:30 +02:00
|
|
|
import java.util.ArrayList;
|
2021-08-04 17:11:46 +02:00
|
|
|
import java.util.Iterator;
|
2021-08-02 12:52:30 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-10-20 11:49:23 +02:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2021-08-02 12:52:30 +02:00
|
|
|
import javax.servlet.http.HttpSession;
|
2020-10-20 11:49:23 +02:00
|
|
|
|
2021-08-02 12:52:30 +02:00
|
|
|
import org.gcube.application.geoportal.common.model.legacy.Concessione;
|
|
|
|
import org.gcube.application.geoportal.common.rest.MongoConcessioni;
|
2021-08-05 16:49:04 +02:00
|
|
|
import org.gcube.application.geoportalcommon.GeoportalCommon;
|
2021-12-21 16:51:56 +01:00
|
|
|
import org.gcube.application.geoportalcommon.shared.GNADataEntryConfigProfile;
|
|
|
|
import org.gcube.application.geoportalcommon.shared.GNADataViewerConfigProfile;
|
2022-08-11 12:07:07 +02:00
|
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.project.ProjectDV;
|
2021-07-23 17:40:50 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2020-10-20 11:49:23 +02:00
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2021-08-06 16:23:17 +02:00
|
|
|
import org.gcube.portlets.user.geoportaldataentry.shared.ResultSetSorted;
|
2020-10-20 11:49:23 +02: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-08-05 16:49:04 +02:00
|
|
|
* Oct 20, 2020
|
2020-10-20 11:49:23 +02:00
|
|
|
*/
|
|
|
|
public class SessionUtil {
|
|
|
|
|
2021-12-21 16:51:56 +01:00
|
|
|
private static final String GNA_DATAENTRY_CONFIG_PROFILE = "GNA_DATAENTRY_CONFIG_PROFILE";
|
|
|
|
private static final String LATEST_RESULT_SET_SORTED = "LATEST_RESULT_SET_SORTED";
|
2021-08-05 16:49:04 +02:00
|
|
|
private static final String GEONA_DATAVIEWER_PROFILE = "GEONA_DATAVIEWER_PROFILE";
|
2021-08-02 12:52:30 +02:00
|
|
|
private static final String LIST_OF_CONCESSIONI = "LIST_OF_CONCESSIONI";
|
2022-03-17 18:08:58 +01:00
|
|
|
|
|
|
|
private static final String LIST_OF_PROJECTS = "LIST_OF_PROJECTS";
|
2022-08-11 12:07:07 +02:00
|
|
|
private static final String COUNT_DOCS_FOR_PROFILE_ID = "COUNT_DOCS_FOR_PROFILE_ID";
|
2020-10-20 11:49:23 +02:00
|
|
|
/** The log. */
|
|
|
|
private static Logger LOG = LoggerFactory.getLogger(SessionUtil.class);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if is into portal.
|
|
|
|
*
|
|
|
|
* @return true, if is into portal
|
|
|
|
*/
|
|
|
|
public static boolean isIntoPortal() {
|
|
|
|
try {
|
|
|
|
UserLocalServiceUtil.getService();
|
|
|
|
return true;
|
2021-08-05 16:49:04 +02:00
|
|
|
} catch (Exception ex) {
|
2020-10-20 11:49:23 +02:00
|
|
|
LOG.debug("Development Mode ON");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the current user by using the portal manager.
|
|
|
|
*
|
|
|
|
* @param request the request
|
|
|
|
* @return a GcubeUser object
|
|
|
|
*/
|
2021-08-05 16:49:04 +02:00
|
|
|
public static GCubeUser getCurrentUser(HttpServletRequest request) {
|
2020-10-20 11:49:23 +02:00
|
|
|
|
2021-08-05 16:49:04 +02:00
|
|
|
if (request == null)
|
2020-10-20 11:49:23 +02: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-08-05 16:49:04 +02:00
|
|
|
* @param request the request
|
|
|
|
* @param setContextsInThread set the scope and the security token into current
|
|
|
|
* thread
|
2020-10-20 11:49:23 +02:00
|
|
|
* @return a GcubeUser object
|
|
|
|
*/
|
2021-08-05 16:49:04 +02:00
|
|
|
public static String getCurrentContext(HttpServletRequest request, boolean setContextsInThread) {
|
2020-10-20 11:49:23 +02:00
|
|
|
|
2021-08-05 16:49:04 +02:00
|
|
|
if (request == null)
|
2020-10-20 11:49:23 +02:00
|
|
|
throw new IllegalArgumentException("HttpServletRequest is null!");
|
|
|
|
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
|
|
String context = pContext.getCurrentScope(request);
|
2021-08-05 16:49:04 +02:00
|
|
|
|
|
|
|
if (setContextsInThread) {
|
2021-07-23 17:40:50 +02:00
|
|
|
GCubeUser user = getCurrentUser(request);
|
|
|
|
String token = pContext.getCurrentUserToken(context, user.getUsername());
|
2021-08-05 16:49:04 +02:00
|
|
|
|
|
|
|
if (context != null)
|
2021-07-23 17:40:50 +02:00
|
|
|
ScopeProvider.instance.set(context);
|
2021-08-05 16:49:04 +02:00
|
|
|
|
|
|
|
if (token != null)
|
2021-07-23 17:40:50 +02:00
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
}
|
2020-10-20 11:49:23 +02:00
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the group given the scope.
|
|
|
|
*
|
|
|
|
* @param scope the scope
|
|
|
|
* @return the group from scope
|
|
|
|
* @throws UserManagementSystemException the user management system exception
|
2021-08-05 16:49:04 +02:00
|
|
|
* @throws GroupRetrievalFault the group retrieval fault
|
2020-10-20 11:49:23 +02:00
|
|
|
*/
|
2021-08-05 16:49:04 +02:00
|
|
|
public static GCubeGroup getGroupFromScope(String scope) throws UserManagementSystemException, GroupRetrievalFault {
|
2020-10-20 11:49:23 +02:00
|
|
|
|
2021-08-05 16:49:04 +02:00
|
|
|
if (scope == null || scope.isEmpty())
|
2020-10-20 11:49:23 +02:00
|
|
|
throw new IllegalArgumentException("Scope is missing here!!");
|
|
|
|
|
|
|
|
GroupManager gm = new LiferayGroupManager();
|
|
|
|
long groupId = gm.getGroupIdFromInfrastructureScope(scope);
|
|
|
|
return gm.getGroup(groupId);
|
|
|
|
|
|
|
|
}
|
2021-08-02 12:52:30 +02:00
|
|
|
|
2022-03-17 18:08:58 +01:00
|
|
|
/**
|
|
|
|
* Gets the list of projects for profile ID.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
2022-08-11 12:07:07 +02:00
|
|
|
* @param profileID the profile ID
|
2022-03-17 18:08:58 +01:00
|
|
|
* @return the list of projects for profile ID
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
public static List<ProjectDV> getListOfProjectsForProfileID(HttpServletRequest httpServletRequest, String profileID)
|
|
|
|
throws Exception {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
return (List<ProjectDV>) session.getAttribute(LIST_OF_PROJECTS + profileID);
|
|
|
|
}
|
|
|
|
|
2022-08-11 12:07:07 +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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-17 18:08:58 +01:00
|
|
|
/**
|
|
|
|
* Sets the list of projects for profile ID.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
2022-08-11 12:07:07 +02:00
|
|
|
* @param profileID the profile ID
|
|
|
|
* @param listOfProjects the list of projects
|
2022-03-17 18:08:58 +01:00
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2022-08-11 12:07:07 +02:00
|
|
|
public static void setListOfProjectsForProfileID(HttpServletRequest httpServletRequest, String profileID,
|
|
|
|
List<ProjectDV> listOfProjects) throws Exception {
|
2022-03-17 18:08:58 +01:00
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
session.setAttribute(LIST_OF_PROJECTS + profileID, listOfProjects);
|
|
|
|
}
|
|
|
|
|
2021-08-02 12:52:30 +02:00
|
|
|
/**
|
|
|
|
* Gets the list of concessioni.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
2021-08-05 16:49:04 +02:00
|
|
|
* @param reloadFromService the reload from service
|
2021-08-02 12:52:30 +02:00
|
|
|
* @return the list of concessioni
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2021-08-05 16:49:04 +02:00
|
|
|
public static List<Concessione> getListOfConcessioni(HttpServletRequest httpServletRequest,
|
|
|
|
boolean reloadFromService) throws Exception {
|
2021-08-02 12:52:30 +02:00
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
List<Concessione> listOfConcessioni = (List<Concessione>) session.getAttribute(LIST_OF_CONCESSIONI);
|
2021-08-05 16:49:04 +02:00
|
|
|
|
|
|
|
// setting null to force reloading from service
|
|
|
|
if (reloadFromService)
|
|
|
|
listOfConcessioni = null;
|
|
|
|
|
2021-08-02 12:52:30 +02:00
|
|
|
if (listOfConcessioni == null) {
|
2021-08-04 17:11:46 +02:00
|
|
|
listOfConcessioni = new ArrayList<Concessione>();
|
2021-08-02 12:52:30 +02:00
|
|
|
LOG.info("Loading list of concessione from client mongo");
|
|
|
|
SessionUtil.getCurrentContext(httpServletRequest, true);
|
2021-09-16 12:15:07 +02:00
|
|
|
MongoServiceUtil serviceUtil = new MongoServiceUtil();
|
2021-08-02 12:52:30 +02:00
|
|
|
MongoConcessioni clientMongo = serviceUtil.getInstanceMongoConcessioni();
|
2021-12-21 16:51:56 +01:00
|
|
|
|
2021-08-04 17:11:46 +02:00
|
|
|
Iterator<Concessione> concessioni = clientMongo.getList();
|
2021-08-02 12:52:30 +02:00
|
|
|
if (concessioni != null) {
|
2021-08-04 17:11:46 +02:00
|
|
|
while (concessioni.hasNext()) {
|
|
|
|
Concessione concessione = (Concessione) concessioni.next();
|
|
|
|
listOfConcessioni.add(concessione);
|
2021-08-05 16:49:04 +02:00
|
|
|
|
2021-08-02 12:52:30 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-06 16:23:17 +02:00
|
|
|
// LOG.debug("Got list of concessioni from client mongo: " + listOfConcessioni);
|
2021-08-04 17:11:46 +02:00
|
|
|
session.setAttribute(LIST_OF_CONCESSIONI, listOfConcessioni);
|
2021-08-05 16:49:04 +02:00
|
|
|
LOG.info("Saved in session list of concessioni from client mongo with size: " + listOfConcessioni.size());
|
|
|
|
} else
|
2021-08-06 16:23:17 +02:00
|
|
|
LOG.info("list of concessioni presents in session, using it");
|
2021-08-05 16:49:04 +02:00
|
|
|
|
|
|
|
LOG.info("read list of concessioni with size: " + listOfConcessioni.size());
|
2021-08-04 17:11:46 +02:00
|
|
|
return listOfConcessioni;
|
2021-08-02 12:52:30 +02:00
|
|
|
}
|
2021-08-05 16:49:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the geportal viewer resource profile.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @return the geportal viewer resource profile
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2021-12-21 16:51:56 +01:00
|
|
|
public static GNADataViewerConfigProfile getGeportalViewerResourceProfile(HttpServletRequest httpServletRequest)
|
2021-08-05 16:49:04 +02:00
|
|
|
throws Exception {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
2021-12-21 16:51:56 +01:00
|
|
|
GNADataViewerConfigProfile geoNaDataViewerProfile = (GNADataViewerConfigProfile) session
|
2021-08-05 16:49:04 +02:00
|
|
|
.getAttribute(GEONA_DATAVIEWER_PROFILE);
|
|
|
|
|
|
|
|
if (geoNaDataViewerProfile == null) {
|
|
|
|
GeoportalCommon gc = new GeoportalCommon();
|
2021-12-21 16:51:56 +01:00
|
|
|
geoNaDataViewerProfile = gc.readGNADataViewerConfig(null);
|
2021-08-05 16:49:04 +02:00
|
|
|
session.setAttribute(GEONA_DATAVIEWER_PROFILE, geoNaDataViewerProfile);
|
|
|
|
}
|
2021-08-06 16:23:17 +02:00
|
|
|
|
2021-08-05 16:49:04 +02:00
|
|
|
return geoNaDataViewerProfile;
|
|
|
|
|
|
|
|
}
|
2021-08-06 16:23:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the latest result set sorted.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @return the latest result set sorted
|
|
|
|
*/
|
|
|
|
public static ResultSetSorted getLatestResultSetSorted(HttpServletRequest httpServletRequest) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
2021-12-21 16:51:56 +01:00
|
|
|
return (ResultSetSorted) session.getAttribute(LATEST_RESULT_SET_SORTED);
|
2021-08-06 16:23:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the latest sort filter.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
2021-12-21 16:51:56 +01:00
|
|
|
* @param rsSorted the rs sorted
|
2021-08-06 16:23:17 +02:00
|
|
|
* @return the latest sort filter
|
|
|
|
*/
|
|
|
|
public static void setLatestResultSetSorted(HttpServletRequest httpServletRequest, ResultSetSorted rsSorted) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
2021-12-21 16:51:56 +01:00
|
|
|
session.setAttribute(LATEST_RESULT_SET_SORTED, rsSorted);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the GNA data entry config profile.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @return the GNA data entry config profile
|
|
|
|
*/
|
|
|
|
public static GNADataEntryConfigProfile getGNADataEntryConfigProfile(HttpServletRequest httpServletRequest) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
return (GNADataEntryConfigProfile) session.getAttribute(GNA_DATAENTRY_CONFIG_PROFILE);
|
2021-08-06 16:23:17 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 16:51:56 +01:00
|
|
|
/**
|
|
|
|
* Sets the GNA data entry config profile.
|
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
|
|
|
* @param gNADEConfigProfile the g NADE config profile
|
|
|
|
*/
|
|
|
|
public static void setGNADataEntryConfigProfile(HttpServletRequest httpServletRequest,
|
|
|
|
GNADataEntryConfigProfile gNADEConfigProfile) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
session.setAttribute(GNA_DATAENTRY_CONFIG_PROFILE, gNADEConfigProfile);
|
|
|
|
}
|
2022-08-11 12:07:07 +02:00
|
|
|
|
2020-10-20 11:49:23 +02:00
|
|
|
}
|