You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
geoportal-data-viewer-app/src/main/java/org/gcube/portlets/user/geoportaldataviewer/server/util/SessionUtil.java

226 lines
7.1 KiB
Java

/**
*
*/
package org.gcube.portlets.user.geoportaldataviewer.server.util;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.gcube.application.geoportalcommon.shared.products.model.UploadedImageDV;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.geoportaldataviewer.server.ConcessioneImageUtil;
import org.gcube.portlets.user.urlshortener.UrlShortener;
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)
*
* Oct 20, 2020
*/
public class SessionUtil {
/** The log. */
private static Logger LOG = LoggerFactory.getLogger(SessionUtil.class);
public static final String URL_SHORTENER_SERVICE = "URL_SHORTENER_SERVICE";
public static final String CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE = "MAP_IMAGE_PREVIEW_FOR_CONCESSIONE";
/**
* Checks if is into portal.
*
* @return true, if is into portal
*/
public static boolean isIntoPortal() {
try {
UserLocalServiceUtil.getService();
return true;
} catch (Exception ex) {
LOG.warn("Development Mode ON");
return false;
}
}
/**
* Checks if is session expired.
*
* @param httpServletRequest the http servlet request
* @return true, if is session expired
* @throws Exception the exception
*/
public static boolean isSessionExpired(HttpServletRequest httpServletRequest) throws Exception {
LOG.trace("workspace session validating...");
return PortalContext.getConfiguration().getCurrentUser(httpServletRequest) == null;
}
/**
* Retrieve the current user by using the portal manager.
*
* @param request the request
* @return a GcubeUser object
*/
public static GCubeUser getCurrentUser(HttpServletRequest request) {
if (request == null)
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.
*
* @param request the request
* @param setInThread the set in thread
* @return a GcubeUser object
*/
public static String getCurrentContext(HttpServletRequest request, boolean setInThread) {
if (request == null)
throw new IllegalArgumentException("HttpServletRequest is null!");
PortalContext pContext = PortalContext.getConfiguration();
String context = pContext.getCurrentScope(request);
if (context != null && setInThread)
ScopeProvider.instance.set(context);
LOG.debug("Returning context " + context);
return context;
}
/**
* 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);
LOG.debug("Returning token " + token.substring(1, 10) + "MASKED_TOKEN");
return token;
}
/**
* Retrieve the group given the scope.
*
* @param scope the scope
* @return the group from scope
* @throws UserManagementSystemException the user management system exception
* @throws GroupRetrievalFault the group retrieval fault
*/
public static GCubeGroup getGroupFromScope(String scope) throws UserManagementSystemException, GroupRetrievalFault {
if (scope == null || scope.isEmpty())
throw new IllegalArgumentException("Scope is missing here!!");
GroupManager gm = new LiferayGroupManager();
long groupId = gm.getGroupIdFromInfrastructureScope(scope);
return gm.getGroup(groupId);
}
/**
* 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) {
LOG.warn("Error occurred when instancing the " + UrlShortener.class.getSimpleName(), e);
}
return shortener;
}
/**
* Gets the preview image for concessione. It is the first image retrieved from
* mongoService for mongoConcessioneId. Caching it into session
*
* @param httpServletRequest the http servlet request
* @param itemType the item type
* @param mongoConcessioneId the mongo concessione id
* @return the preview image for concessione
*/
public static UploadedImageDV getPreviewImageForConcessione(HttpServletRequest httpServletRequest, String itemType,
String mongoConcessioneId) {
HttpSession session = httpServletRequest.getSession();
Map<String, List<UploadedImageDV>> mapImages = null;
List<UploadedImageDV> lUI = null;
try {
mapImages = (LinkedHashMap) session.getAttribute(CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE);
if (mapImages == null) {
mapImages = new LinkedHashMap<String, List<UploadedImageDV>>();
}
List<UploadedImageDV> imagePreviewForConcessione = mapImages.get(mongoConcessioneId);
if (imagePreviewForConcessione == null || imagePreviewForConcessione.size() == 0) {
LOG.info("Into " + CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE + " object session the mongoConcessioneId "
+ mongoConcessioneId + " is empty or null, loading from service and filling it");
lUI = new ConcessioneImageUtil().getUploadedImagesForId(httpServletRequest, itemType,
mongoConcessioneId, 1);
mapImages.put(mongoConcessioneId, lUI);
}
lUI = mapImages.get(mongoConcessioneId);
LOG.info("From " + CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE + " object session read image: " + lUI);
session.setAttribute(CACHE_IMAGE_PREVIEW_FOR_CONCESSIONE, mapImages);
} catch (Exception e) {
LOG.warn("Error occurred when instancing the " + UrlShortener.class.getSimpleName(), e);
}
if (lUI == null || lUI.isEmpty())
return null;
return lUI.get(0);
}
}