From ffe9c462e0ca8fa828ab2be1ea8e6000acd599ac Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Wed, 22 Feb 2017 08:39:06 +0000 Subject: [PATCH] added fallback for GcoreReaderEndPoint social git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@144038 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../utils/ServiceEndPointReaderSocial.java | 102 ++++++++++++++++++ .../server/utils/Utils.java | 7 ++ 2 files changed, 109 insertions(+) create mode 100644 src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/ServiceEndPointReaderSocial.java diff --git a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/ServiceEndPointReaderSocial.java b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/ServiceEndPointReaderSocial.java new file mode 100644 index 0000000..c55cdee --- /dev/null +++ b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/ServiceEndPointReaderSocial.java @@ -0,0 +1,102 @@ +package org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils; + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.Iterator; +import java.util.List; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Retrieves the base url of the social-networking service in the scope provided + * @author Costantino Perciante at ISTI-CNR + * (costantino.perciante@isti.cnr.it) + */ +public class ServiceEndPointReaderSocial { + + private String basePath = null; + + private static Logger logger = LoggerFactory.getLogger(ServiceEndPointReaderSocial.class); + private final static String RUNTIME_RESOURCE_NAME = "SocialNetworking"; + private final static String CATEGORY = "Portal"; + + public ServiceEndPointReaderSocial(String context){ + + if(context == null || context.isEmpty()) + throw new IllegalArgumentException("A valid context is needed to discover the service"); + + + String oldContext = ScopeProvider.instance.get(); + ScopeProvider.instance.set(context); + + try{ + + List resources = getConfigurationFromIS(); + if (resources.size() == 0){ + logger.error("There is no Runtime Resource having name " + RUNTIME_RESOURCE_NAME +" and Category " + CATEGORY + " in this scope."); + throw new Exception("There is no Runtime Resource having name " + RUNTIME_RESOURCE_NAME +" and Category " + CATEGORY + " in this scope."); + } + else { + + for (ServiceEndpoint res : resources) { + + Iterator accessPointIterator = res.profile().accessPoints().iterator(); + + while (accessPointIterator.hasNext()) { + ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator + .next(); + + // get base path + basePath = accessPoint.address(); + + // break + break; + } + } + + } + + }catch(Exception e){ + + logger.error("Unable to retrieve such service endpoint information!", e); + + }finally{ + + if(oldContext != null && !oldContext.equals(context)) + ScopeProvider.instance.set(oldContext); + + } + + logger.info("Found base path " + basePath + " for the service"); + + } + + /** + * Retrieve endpoints information from IS for the Service endpoint + * @return list of endpoints + * @throws Exception + */ + private List getConfigurationFromIS() throws Exception{ + + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_RESOURCE_NAME +"'"); + query.addCondition("$resource/Profile/Category/text() eq '"+ CATEGORY +"'"); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List toReturn = client.submit(query); + return toReturn; + + } + + /** + * Get the base path of the social networking service + * @return + */ + public String getBasePath() { + return basePath; + } +} diff --git a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/Utils.java b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/Utils.java index b3bab73..e366e2d 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/Utils.java +++ b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/utils/Utils.java @@ -399,12 +399,19 @@ public class Utils { logger.info("Current scope for writeProductPost is " + currentScope + " and token is " + tokenUser.substring(0, 10) + "***************"); String basePath = new GCoreEndPointReaderSocial(currentScope).getBasePath(); + + // add fallback + if(basePath == null || basePath.isEmpty()) + new ServiceEndPointReaderSocial(currentScope).getBasePath(); if(basePath == null){ logger.error("Unable to write a post because there is no social networking service available"); }else{ + + // check base path form + basePath = basePath.endsWith("/") ? basePath : basePath + "/"; try(CloseableHttpClient client = HttpClientBuilder.create().build();){