catalogue-sharing-widget/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ShareServicesImpl.java

122 lines
4.3 KiB
Java

package org.gcube.portlets_widgets.catalogue_sharing_widget.server;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.ckanutillibrary.server.ApplicationProfileScopePerUrlReader;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory;
import org.gcube.portlets.user.urlshortener.UrlShortener;
import org.gcube.portlets_widgets.catalogue_sharing_widget.client.ShareServices;
import org.gcube.portlets_widgets.catalogue_sharing_widget.shared.ItemUrls;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
public class ShareServicesImpl extends RemoteServiceServlet implements ShareServices{
private static final long serialVersionUID = -2060855544534802987L;
private static final Log logger = LogFactoryUtil.getLog(ShareServicesImpl.class);
public static final String GCUBE_REQUEST_URL = "gcube-request-url";
/**
* Retrieve an instance of the library for the scope
* @param scope if it is null it is evaluated from the session
* @return
* @throws Exception
*/
public DataCatalogue getCatalogue(String scope) throws Exception{
String scopeInWhichDiscover = (scope != null && !scope.isEmpty()) ? scope : getCurrentContext(getThreadLocalRequest(), false);
logger.debug("Discovering ckan instance into scope " + scopeInWhichDiscover);
return DataCatalogueFactory.getFactory().getUtilsPerScope(scopeInWhichDiscover);
}
@Override
public ItemUrls getPackageUrl(String uuid) throws Exception{
String scopePerCurrentUrl = getScopeFromClientUrl(getThreadLocalRequest());
DataCatalogue catalogue = getCatalogue(scopePerCurrentUrl);
String longUrl = catalogue.getUnencryptedUrlFromDatasetIdOrName(uuid);
String shortUrl = null;
UrlShortener shortener = new UrlShortener();
if(shortener!=null && shortener.isAvailable())
shortUrl = shortener.shorten(longUrl);
return new ItemUrls(shortUrl, longUrl, uuid);
}
/**
* Get the scope in which ckan information needs to be discovered from the url
* @param httpServletRequest
* @return
*/
public static String getScopeFromClientUrl(HttpServletRequest httpServletRequest){
if(httpServletRequest == null)
throw new IllegalArgumentException("HttpServletRequest is null!");
String scopeToReturn = null;
try{
String clientUrl = getCurrentClientUrl(httpServletRequest).split("\\?")[0];
logger.debug("Client url is " + clientUrl);
// check if this information is in session, otherwise set it and return
HttpSession session = httpServletRequest.getSession();
if((scopeToReturn = (String) session.getAttribute(clientUrl)) != null){
logger.debug("Scope to return is " + scopeToReturn);
}else{
// ask to the ckan library and set it
scopeToReturn = ApplicationProfileScopePerUrlReader.getScopePerUrl(clientUrl);
logger.debug("Scope to return is " + scopeToReturn);
session.setAttribute(clientUrl, scopeToReturn);
}
}catch(Exception e){
scopeToReturn = getCurrentContext(httpServletRequest, false);
logger.warn("Failed to determine the scope from the client url, returning the current one: " + scopeToReturn);
}
return scopeToReturn;
}
/**
* Retrieve the current scope by using the portal manager
* @param b
* @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);
logger.debug("Returning context " + context);
if(context != null && setInThread)
ScopeProvider.instance.set(context);
return context;
}
/**
* Needed to get the url of the client
* @param httpServletRequest the httpServletRequest object
* @return the instance of the user
* @see the url at client side
*/
public static String getCurrentClientUrl(HttpServletRequest httpServletRequest) {
if(httpServletRequest == null)
throw new IllegalArgumentException("HttpServletRequest is null!");
return httpServletRequest.getHeader(GCUBE_REQUEST_URL);
}
}