154 lines
4.8 KiB
Java
154 lines
4.8 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.geoportaldataentry.server;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
import org.gcube.application.geoportal.common.model.legacy.Concessione;
|
|
import org.gcube.application.geoportal.common.rest.MongoConcessioni;
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
import org.gcube.common.portal.PortalContext;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
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 {
|
|
|
|
private static final String LIST_OF_CONCESSIONI = "LIST_OF_CONCESSIONI";
|
|
/** 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;
|
|
}catch (Exception ex) {
|
|
LOG.debug("Development Mode ON");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 setContextsInThread set the scope and the security token into current thread
|
|
* @return a GcubeUser object
|
|
*/
|
|
public static String getCurrentContext(HttpServletRequest request, boolean setContextsInThread){
|
|
|
|
if(request == null)
|
|
throw new IllegalArgumentException("HttpServletRequest is null!");
|
|
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
String context = pContext.getCurrentScope(request);
|
|
|
|
if(setContextsInThread) {
|
|
GCubeUser user = getCurrentUser(request);
|
|
String token = pContext.getCurrentUserToken(context, user.getUsername());
|
|
|
|
if(context!=null)
|
|
ScopeProvider.instance.set(context);
|
|
|
|
if(token!=null)
|
|
SecurityTokenProvider.instance.set(token);
|
|
}
|
|
|
|
return context;
|
|
}
|
|
|
|
/**
|
|
* 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 list of concessioni.
|
|
*
|
|
* @param httpServletRequest the http servlet request
|
|
* @return the list of concessioni
|
|
* @throws Exception the exception
|
|
*/
|
|
public static List<Concessione> getListOfConcessioni(HttpServletRequest httpServletRequest) throws Exception {
|
|
HttpSession session = httpServletRequest.getSession();
|
|
List<Concessione> listOfConcessioni = (List<Concessione>) session.getAttribute(LIST_OF_CONCESSIONI);
|
|
List<Concessione> result = new ArrayList<Concessione>();
|
|
|
|
if (listOfConcessioni == null) {
|
|
LOG.info("Loading list of concessione from client mongo");
|
|
SessionUtil.getCurrentContext(httpServletRequest, true);
|
|
ServiceUtil serviceUtil = new ServiceUtil();
|
|
MongoConcessioni clientMongo = serviceUtil.getInstanceMongoConcessioni();
|
|
Iterable<Concessione> concessioni = clientMongo.getList();
|
|
if (concessioni != null) {
|
|
for (Concessione conc : concessioni) {
|
|
result.add(conc);
|
|
}
|
|
}
|
|
LOG.debug("Got list of concessione from client mongo: "+result);
|
|
session.setAttribute(LIST_OF_CONCESSIONI, result);
|
|
LOG.info("Saved in session list of concessione from client mongo with size: "+result.size());
|
|
}
|
|
LOG.info("returning list of concessioni with size: "+result.size());
|
|
return result;
|
|
}
|
|
}
|