added fallback when retrieve gcore social endpoint -> search for service endpoint

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@144030 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-02-21 19:35:36 +00:00
parent a7003850ce
commit 3233eba11c
2 changed files with 118 additions and 7 deletions

View File

@ -60,8 +60,8 @@ public abstract class HelperMethods {
private static final String PATH_SEPARATOR = "/";
// caches
private static CacheInterface<String, String> userEmailCache = new CacheImpl<String, String>(1000 * 60 * 30);
private static CacheInterface<String, String> userFullnameCache = new CacheImpl<String, String>(1000 * 60 * 30);
private static CacheInterface<String, String> userEmailCache = new CacheImpl<String, String>(1000 * 60 * 120);
private static CacheInterface<String, String> userFullnameCache = new CacheImpl<String, String>(1000 * 60 * 120);
/**
* Convert a group name to its id on ckan
@ -141,7 +141,10 @@ public abstract class HelperMethods {
if((result = (String) userEmailCache.get(token)) != null){
return result;
}else{
String baseUrl = new GcoreEndPointReaderSocial(context).getBasePath();
String baseUrl = new ServiceEndPointReaderSocial(context).getBasePath();
// fallback: check for gcore endpoint
if(baseUrl == null || baseUrl.isEmpty())
baseUrl = new GcoreEndPointReaderSocial(context).getBasePath();
String url = baseUrl.endsWith("/") ? baseUrl + "users/getUserEmail?gcube-token=" + token :
baseUrl + "/users/getUserEmail?gcube-token=" + token;
logger.debug("Request url is " + url);
@ -165,7 +168,10 @@ public abstract class HelperMethods {
if((result = (String) userFullnameCache.get(token)) != null){
return result;
}else{
String baseUrl = new GcoreEndPointReaderSocial(context).getBasePath();
String baseUrl = new ServiceEndPointReaderSocial(context).getBasePath();
// fallback: check for gcore endpoint
if(baseUrl == null || baseUrl.isEmpty())
baseUrl = new GcoreEndPointReaderSocial(context).getBasePath();
String url = baseUrl.endsWith("/") ? baseUrl + "users/getUserFullname?gcube-token=" + token :
baseUrl + "/users/getUserFullname?gcube-token=" + token;
logger.debug("Request url is " + url);
@ -359,14 +365,17 @@ public abstract class HelperMethods {
String tokenUser = SecurityTokenProvider.instance.get();
logger.info("Current scope for writeProductPost is " + currentScope + " and token is " + tokenUser.substring(0, 10) + "***************");
String basePath = new GcoreEndPointReaderSocial(currentScope).getBasePath();
String basePath = new ServiceEndPointReaderSocial(currentScope).getBasePath();
// fallback: check for gcore endpoint
if(basePath == null || basePath.isEmpty())
basePath = new GcoreEndPointReaderSocial(currentScope).getBasePath();
if(basePath == null){
logger.error("Unable to write a post because there is no social networking service available");
}else{
basePath = basePath.endsWith("/") ? basePath : basePath + "/";
try(CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();){
@ -468,6 +477,6 @@ public abstract class HelperMethods {
DataCatalogue catalogue) throws ClassNotFoundException, SQLException {
return catalogue.getProductsIdsInGroupOrOrg(groupName, false, 0, Integer.MAX_VALUE);
}
}

View File

@ -0,0 +1,102 @@
package org.gcube.data_catalogue.grsf_publish_ws.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<ServiceEndpoint> 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<AccessPoint> 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<ServiceEndpoint> 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<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> toReturn = client.submit(query);
return toReturn;
}
/**
* Get the base path of the social networking service
* @return
*/
public String getBasePath() {
return basePath;
}
}