461 lines
15 KiB
Java
461 lines
15 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.workspace.server.util;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
|
|
import org.gcube.applicationsupportlayer.social.NotificationsManager;
|
|
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite;
|
|
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser;
|
|
import org.gcube.common.portal.PortalContext;
|
|
import org.gcube.common.storagehubwrapper.server.StorageHubWrapper;
|
|
import org.gcube.portlets.user.urlshortener.UrlShortener;
|
|
import org.gcube.portlets.user.workspace.client.model.FileModel;
|
|
import org.gcube.portlets.user.workspace.server.notifications.tostoragehub.NotificationsProducerToStorageHub;
|
|
import org.gcube.portlets.user.workspace.server.resolver.UriResolverReaderParameterForResolverIndex;
|
|
import org.gcube.portlets.user.workspace.server.resolver.UriResolverReaderParameterForResolverIndex.RESOLVER_TYPE;
|
|
import org.gcube.portlets.user.workspace.server.tostoragehub.StorageHubToWorkpaceConverter;
|
|
import org.gcube.portlets.user.workspace.server.util.resource.PropertySpecialFolderReader;
|
|
import org.gcube.portlets.user.workspace.server.util.scope.ScopeUtilFilter;
|
|
import org.gcube.portlets.widgets.workspacesharingwidget.server.notifications.NotificationsProducer;
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
|
|
/**
|
|
* The Class WsUtil.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
*
|
|
* Aug 1, 2019
|
|
*/
|
|
public class WsUtil {
|
|
|
|
public static final String FOLDER_PUBLISHING_ON_THREDDS = "FolderPublishingOnThredds";
|
|
|
|
protected static Logger logger = LoggerFactory.getLogger(WsUtil.class);
|
|
|
|
public static final String FOLDERIMPORTER_ATTRIBUTE = "FOLDER_IMPORTER";
|
|
public static final String METADATACONVERTER_ATTRIBUTE = "METADATA_CONVERTER";
|
|
public static final String WORKSPACE_EVENT_COLLECTOR_ATTRIBUTE = "EVENT_COLLECTOR";
|
|
public static final String WORKSPACEBUILDER_ATTRIBUTE = "WORKSPACEBUILDER";
|
|
public static final String NOTIFICATION_MANAGER = "NOTIFICATION_MANAGER";
|
|
public static final String NOTIFICATION_PRODUCER = "NOTIFICATION_PRODUCER";
|
|
// public static final String NOTIFICATION_MANAGER_TO_STORAGEHUB =
|
|
// "NOTIFICATION_MANAGER_TO_STORAGEHUB";
|
|
public static final String NOTIFICATION_PRODUCER_TO_STORAGEHUB = "NOTIFICATION_PRODUCER_TO_STORAGEHUB";
|
|
public static final String WS_RUN_IN_TEST_MODE = "WS_RUN_IN_TEST_MODE";
|
|
public static final String WORKSPACE_SCOPE_UTIL = "WORKSPACE_SCOPE_UTIL";
|
|
public static final String WORKSPACE_STORAGE_HUB_CONVERTER = "WORKSPACE_STORAGE_HUB_CONVERTER";
|
|
public static final String URL_SHORTENER_SERVICE = "URL_SHORTENER_SERVICE";
|
|
public static final String URI_RESOLVER_SERVICE = "URI_RESOLVER_SERVICE";
|
|
public static final String PROPERTY_SPECIAL_FOLDER = "PROPERTY_SPECIAL_FOLDER";
|
|
public static final String NOTIFICATION_PORTLET_CLASS_ID = "org.gcube.portlets.user.workspace.server.GWTWorkspaceServiceImpl"; // IN
|
|
// DEV
|
|
|
|
/**
|
|
* Checks if is within portal.
|
|
*
|
|
* @return true if you're running into the portal, false if in development
|
|
*/
|
|
public static boolean isWithinPortal() {
|
|
try {
|
|
UserLocalServiceUtil.getService();
|
|
return true;
|
|
} catch (Exception ex) {
|
|
logger.trace("Development Mode ON");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the portal context.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @return the portal context
|
|
*/
|
|
public static PortalContextInfo getPortalContext(HttpServletRequest httpServletRequest) {
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
// USER
|
|
GCubeUser user = pContext.getCurrentUser(httpServletRequest);
|
|
String username = user.getUsername();
|
|
String fullName = user.getFullname();
|
|
String email = user.getEmail();
|
|
String avatarID = user.getUserAvatarId();
|
|
String avatarURL = user.getUserAvatarURL();
|
|
// SESSION
|
|
String currentScope = pContext.getCurrentScope(httpServletRequest);
|
|
String userToken = pContext.getCurrentUserToken(httpServletRequest);
|
|
long currGroupId = pContext.getCurrentGroupId(httpServletRequest);
|
|
|
|
return new PortalContextInfo(username, fullName, email, avatarID, avatarURL, currentScope, userToken,
|
|
currGroupId);
|
|
}
|
|
|
|
/**
|
|
* Gets the portal context.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @param overrideScope
|
|
* the override scope
|
|
* @return the portal context
|
|
*/
|
|
public static PortalContextInfo getPortalContext(HttpServletRequest httpServletRequest, String overrideScope) {
|
|
PortalContextInfo info = getPortalContext(httpServletRequest);
|
|
info.setCurrentScope(overrideScope);
|
|
return info;
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
logger.trace("workspace session validating...");
|
|
return PortalContext.getConfiguration().getCurrentUser(httpServletRequest) == null;
|
|
}
|
|
|
|
/**
|
|
* Gets the notification manager.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @return the notification manager
|
|
*/
|
|
public static NotificationsManager getNotificationManager(HttpServletRequest httpServletRequest) {
|
|
PortalContextInfo info = getPortalContext(httpServletRequest);
|
|
HttpSession session = httpServletRequest.getSession();
|
|
NotificationsManager notifMng = (NotificationsManager) session.getAttribute(NOTIFICATION_MANAGER);
|
|
|
|
if (notifMng == null) {
|
|
try {
|
|
logger.trace("Create new NotificationsManager for user: " + info.getUsername());
|
|
logger.trace("New ApplicationNotificationsManager with portlet class name: "
|
|
+ NOTIFICATION_PORTLET_CLASS_ID);
|
|
SocialNetworkingSite site = new SocialNetworkingSite(httpServletRequest);
|
|
SocialNetworkingUser curser = new SocialNetworkingUser(info.getUsername(), info.getUserEmail(),
|
|
info.getUserFullName(), info.getUserAvatarID());
|
|
notifMng = new ApplicationNotificationsManager(site, info.getCurrentScope(), curser,
|
|
NOTIFICATION_PORTLET_CLASS_ID);
|
|
session.setAttribute(NOTIFICATION_MANAGER, notifMng);
|
|
} catch (Exception e) {
|
|
logger.error(
|
|
"An error occurred instancing ApplicationNotificationsManager for user: " + info.getUsername(),
|
|
e);
|
|
}
|
|
}
|
|
|
|
return notifMng;
|
|
}
|
|
|
|
/**
|
|
* Gets the notification producer.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @return the notification producer
|
|
*/
|
|
public static NotificationsProducer getNotificationProducer(HttpServletRequest httpServletRequest) {
|
|
PortalContextInfo info = getPortalContext(httpServletRequest);
|
|
HttpSession session = httpServletRequest.getSession();
|
|
NotificationsProducer notifProducer = (NotificationsProducer) session.getAttribute(NOTIFICATION_PRODUCER);
|
|
|
|
if (notifProducer == null) {
|
|
logger.trace("Create new Notification Producer for user: " + info.getUsername());
|
|
notifProducer = new NotificationsProducer(httpServletRequest);
|
|
session.setAttribute(NOTIFICATION_PRODUCER, notifProducer);
|
|
}
|
|
return notifProducer;
|
|
}
|
|
|
|
/**
|
|
* Gets the user id.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @return the user id
|
|
*/
|
|
public static String getUserId(HttpServletRequest httpServletRequest) {
|
|
|
|
PortalContextInfo info = getPortalContext(httpServletRequest);
|
|
return info.getUsername();
|
|
}
|
|
|
|
/**
|
|
* Checks if is vre.
|
|
*
|
|
* @param scope
|
|
* the scope
|
|
* @return true, if is vre
|
|
*/
|
|
public static boolean isVRE(String scope) {
|
|
|
|
if(scope==null || scope.isEmpty())
|
|
return false;
|
|
|
|
String[] splitted = scope.split("/");
|
|
logger.trace("scope splitted: "+splitted.length);
|
|
|
|
if(splitted.length==4) {
|
|
logger.trace("currentScope is VRE");
|
|
return true;
|
|
}
|
|
|
|
logger.trace("currentScope is not VRE");
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets the scope util filter.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @return the scope util filter
|
|
*/
|
|
public static ScopeUtilFilter getScopeUtilFilter(HttpServletRequest httpServletRequest) {
|
|
|
|
PortalContextInfo info = getPortalContext(httpServletRequest);
|
|
HttpSession session = httpServletRequest.getSession();
|
|
ScopeUtilFilter scopeUtil = null;
|
|
try {
|
|
scopeUtil = (ScopeUtilFilter) session.getAttribute(WsUtil.WORKSPACE_SCOPE_UTIL);
|
|
|
|
if (scopeUtil == null) {
|
|
scopeUtil = new ScopeUtilFilter(info.getCurrentScope(), true);
|
|
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("an error occurred in getscope filter " + e);
|
|
}
|
|
|
|
return scopeUtil;
|
|
}
|
|
|
|
/**
|
|
* Gets the url shortener.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @return the url shortener
|
|
*/
|
|
public static UrlShortener getUrlShortener(HttpServletRequest httpServletRequest) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
PortalContextInfo info = getPortalContext(httpServletRequest);
|
|
UrlShortener shortener = null;
|
|
try {
|
|
shortener = (UrlShortener) session.getAttribute(WsUtil.URL_SHORTENER_SERVICE);
|
|
|
|
if (shortener == null) {
|
|
shortener = new UrlShortener();
|
|
session.setAttribute(URL_SHORTENER_SERVICE, shortener);
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
logger.error("an error occurred in instancing url shortener ", e);
|
|
}
|
|
|
|
return shortener;
|
|
}
|
|
|
|
/**
|
|
* Gets the uri resolver.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @return the uri resolver
|
|
*/
|
|
public static UriResolverReaderParameterForResolverIndex getUriResolver(HttpServletRequest httpServletRequest) {
|
|
|
|
HttpSession session = httpServletRequest.getSession();
|
|
PortalContextInfo info = getPortalContext(httpServletRequest);
|
|
UriResolverReaderParameterForResolverIndex uriResolver = null;
|
|
try {
|
|
uriResolver = (UriResolverReaderParameterForResolverIndex) session
|
|
.getAttribute(WsUtil.URI_RESOLVER_SERVICE);
|
|
|
|
if (uriResolver == null) {
|
|
uriResolver = new UriResolverReaderParameterForResolverIndex(info.getCurrentScope(),
|
|
RESOLVER_TYPE.SMP_ID);
|
|
session.setAttribute(URI_RESOLVER_SERVICE, uriResolver);
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
logger.error("an error occurred instancing URI Resolver ", e);
|
|
}
|
|
|
|
return uriResolver;
|
|
}
|
|
|
|
/**
|
|
* Gets the property special folder reader.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @param pathProperty
|
|
* the path property
|
|
* @return the property special folder reader
|
|
*/
|
|
public static PropertySpecialFolderReader getPropertySpecialFolderReader(HttpServletRequest httpServletRequest,
|
|
String pathProperty) {
|
|
HttpSession session = httpServletRequest.getSession();
|
|
|
|
PropertySpecialFolderReader psFolderReader = null;
|
|
try {
|
|
psFolderReader = (PropertySpecialFolderReader) session.getAttribute(WsUtil.PROPERTY_SPECIAL_FOLDER);
|
|
|
|
if (psFolderReader == null) {
|
|
psFolderReader = new PropertySpecialFolderReader(pathProperty);
|
|
session.setAttribute(PROPERTY_SPECIAL_FOLDER, psFolderReader);
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
logger.error("an error occurred instancing PropertySpecialFolderReader ", e);
|
|
}
|
|
|
|
return psFolderReader;
|
|
}
|
|
|
|
/**
|
|
* Gets the storage hub wrapper.
|
|
*
|
|
* @param request
|
|
* the request
|
|
* @param scopeGroupId
|
|
* the scope group id. If scopeGroupId is null the scope is read
|
|
* by using the request else by using the scopeGroupId
|
|
* @param user
|
|
* the user
|
|
* @return the storage hub wrapper
|
|
* @throws Exception
|
|
* the exception
|
|
*/
|
|
public static StorageHubWrapper getStorageHubWrapper(final HttpServletRequest request, String scopeGroupId,
|
|
GCubeUser user) throws Exception {
|
|
|
|
if (user == null || user.getUsername().isEmpty())
|
|
throw new Exception("Session expired");
|
|
|
|
try {
|
|
String scope;
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
if (WsUtil.isWithinPortal() && scopeGroupId != null) {
|
|
scope = pContext.getCurrentScope(scopeGroupId);
|
|
logger.debug(scope + " has retrieved by using the scopeGroupId=" + scopeGroupId);
|
|
} else
|
|
scope = pContext.getCurrentScope(request);
|
|
|
|
logger.debug("Getting " + StorageHubWrapper.class.getSimpleName() + " for user: " + user.getUsername()
|
|
+ " by using the scope: " + scope);
|
|
String token = pContext.getCurrentUserToken(scope, user.getUsername());
|
|
return new StorageHubWrapper(scope, token, false, false, true);
|
|
} catch (Exception e) {
|
|
logger.error("Error during getting storageHub wrapper", e);
|
|
throw new Exception("Error on gettig the StorageHub wrapper for userId: " + user);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the storage hub to workpace converter.
|
|
*
|
|
* @param request
|
|
* the request
|
|
* @param scopeGroupId
|
|
* the scope group id
|
|
* @param user
|
|
* the user
|
|
* @return the storage hub to workpace converter
|
|
* @throws Exception
|
|
* the exception
|
|
*/
|
|
public static StorageHubToWorkpaceConverter getStorageHubToWorkpaceConverter(final HttpServletRequest request,
|
|
String scopeGroupId, GCubeUser user) throws Exception {
|
|
|
|
if (user == null || user.getUsername().isEmpty())
|
|
throw new Exception("Session expired");
|
|
|
|
try {
|
|
HttpSession session = request.getSession();
|
|
StorageHubToWorkpaceConverter converter = (StorageHubToWorkpaceConverter) session
|
|
.getAttribute(WORKSPACE_STORAGE_HUB_CONVERTER);
|
|
|
|
String scope;
|
|
PortalContext pContext = PortalContext.getConfiguration();
|
|
if (WsUtil.isWithinPortal() && scopeGroupId != null) {
|
|
scope = pContext.getCurrentScope(scopeGroupId);
|
|
logger.debug(scope + " has retrieved by using the scopeGroupId=" + scopeGroupId);
|
|
} else
|
|
scope = pContext.getCurrentScope(request);
|
|
|
|
if (converter == null) {
|
|
converter = new StorageHubToWorkpaceConverter(scope, user);
|
|
session.setAttribute(WORKSPACE_STORAGE_HUB_CONVERTER, converter);
|
|
}
|
|
|
|
return converter;
|
|
} catch (Exception e) {
|
|
logger.error("Error during getting storageHub conveter", e);
|
|
throw new Exception("Error on gettig the StorageHub conveter for user: " + user);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks if is root folder.
|
|
*
|
|
* @param folder
|
|
* the folder
|
|
* @param converter
|
|
* the converter
|
|
* @return true, if is root folder
|
|
*/
|
|
public static boolean isRootFolder(FileModel folder, StorageHubToWorkpaceConverter converter) {
|
|
|
|
if (folder == null)
|
|
return false;
|
|
|
|
if (folder.getIdentifier().compareTo(converter.getWorkspaceRootId()) == 0)
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
* Gets the notification producer to storage hub.
|
|
*
|
|
* @param httpServletRequest
|
|
* the http servlet request
|
|
* @return the notification producer to storage hub
|
|
*/
|
|
public static NotificationsProducerToStorageHub getNotificationProducerToStorageHub(
|
|
HttpServletRequest httpServletRequest) {
|
|
PortalContextInfo info = getPortalContext(httpServletRequest);
|
|
HttpSession session = httpServletRequest.getSession();
|
|
NotificationsProducerToStorageHub notifProducer = (NotificationsProducerToStorageHub) session
|
|
.getAttribute(NOTIFICATION_PRODUCER_TO_STORAGEHUB);
|
|
|
|
if (notifProducer == null) {
|
|
logger.trace("Create new Notification Producer for user: " + info.getUsername());
|
|
notifProducer = new NotificationsProducerToStorageHub(httpServletRequest);
|
|
session.setAttribute(NOTIFICATION_PRODUCER_TO_STORAGEHUB, notifProducer);
|
|
}
|
|
return notifProducer;
|
|
}
|
|
|
|
}
|