diff --git a/.classpath b/.classpath index f0e4315..e1a1961 100644 --- a/.classpath +++ b/.classpath @@ -44,5 +44,10 @@ + + + + + diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 8ef44a2..a5ee78d 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -3,6 +3,7 @@ + diff --git a/pom.xml b/pom.xml index 5c74425..8665042 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,12 @@ gwt-user provided + + org.gcube.core + common-scope-maps + provided + + com.google.gwt gwt-dev @@ -75,6 +81,12 @@ slf4j-api provided + + org.gcube.portlets.user + gcube-url-shortener + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + compile + org.gcube.common.portal portal-manager @@ -86,6 +98,21 @@ [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) compile + + org.gcube.common + authorization-client + provided + + + org.gcube.common.portal + portal-manager + provided + + + com.liferay.portal + portal-service + provided + junit junit diff --git a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareServices.java b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareServices.java index 4541b20..7849863 100644 --- a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareServices.java +++ b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareServices.java @@ -8,9 +8,9 @@ import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; /** * The client side stub for the RPC service. */ -@RemoteServiceRelativePath("share") +@RemoteServiceRelativePath("shareservices") public interface ShareServices extends RemoteService { - ItemUrls greetServer(String uuid) throws Exception; + ItemUrls getPackageUrl(String uuid) throws Exception; } diff --git a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareServicesAsync.java b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareServicesAsync.java index d0b0785..018479b 100644 --- a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareServicesAsync.java +++ b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/client/ShareServicesAsync.java @@ -6,6 +6,6 @@ import com.google.gwt.user.client.rpc.AsyncCallback; public interface ShareServicesAsync { - void greetServer(String uuid, AsyncCallback callback); + void getPackageUrl(String uuid, AsyncCallback callback); } diff --git a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ShareServicesImpl.java b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ShareServicesImpl.java index f5689b9..e8d9fb8 100644 --- a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ShareServicesImpl.java +++ b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/server/ShareServicesImpl.java @@ -1,5 +1,121 @@ package org.gcube.portlets_widgets.catalogue_sharing_widget.server; -public class ShareServicesImpl { +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); + } } diff --git a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/shared/ItemUrls.java b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/shared/ItemUrls.java index 64fb29c..35536f0 100644 --- a/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/shared/ItemUrls.java +++ b/src/main/java/org/gcube/portlets_widgets/catalogue_sharing_widget/shared/ItemUrls.java @@ -13,6 +13,16 @@ public class ItemUrls implements Serializable { private String shortUrl; private String url; private String catalogueUUID; + + + + /** + * + */ + public ItemUrls() { + super(); + // TODO Auto-generated constructor stub + } /** * @param shortUrl diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 54f2340..30c1592 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -7,13 +7,13 @@ - greetServlet - org.gcube.portlets_widgets.catalogue_sharing_widget.server.GreetingServiceImpl + shareservices + org.gcube.portlets_widgets.catalogue_sharing_widget.server.ShareServicesImpl - greetServlet - /ShareCatalogue/greet + shareservices + /ShareCatalogue/shareservices