Added caches for the GisResolver

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@173938 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-11-02 14:58:38 +00:00
parent b5e73449fe
commit 0cbd1b0bd3
11 changed files with 1158 additions and 624 deletions

View File

@ -4,6 +4,9 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="ckan-util-library-2.7.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ckan-util-library-TRUNK/ckan-util-library-TRUNK">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="uri-resolver"/>
<property name="java-output-path" value="/uri-resolver/target/classes"/>
</wb-module>

View File

@ -0,0 +1,77 @@
/**
*
*/
package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.TimeUnit;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader;
import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/**
* The Class GeoExplorerApplicationHostnameGuavaCache.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 2, 2018
*/
public class GeoExplorerApplicationHostnameGuavaCache {
private static Logger logger = LoggerFactory.getLogger(GeoExplorerApplicationHostnameGuavaCache.class);
private static LoadingCache<String, String> geoExplorerApplicationURLCache;
static {
geoExplorerApplicationURLCache =
CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(
1, TimeUnit.HOURS).build(
new CacheLoader<String, String>() {
@Override
public String load(String scope)
throws Exception {
return loadGeoExplorerApplicationURL(scope);
}
});
logger.info(GeoExplorerApplicationHostnameGuavaCache.class.getSimpleName() +" instancied");
}
/**
* Gets the cache.
*
* @return the cache
*/
public static LoadingCache<String, String> getCache() {
return geoExplorerApplicationURLCache;
}
/**
* Load geo explorer application url.
*
* @param scope the scope
* @return the string
*/
public static String loadGeoExplorerApplicationURL(String scope){
if (scope == null || scope.isEmpty())
logger.warn("Scope is null or ermpty, skipping loadGisViewerApplicationURL");
ApplicationProfileReader reader = new ApplicationProfileReader(scope, UriResolverStartupListener.getGeoExplorerProfile().getGenericResource(), UriResolverStartupListener.getGeoExplorerProfile().getAppId(), false);
String url = reader.getApplicationProfile().getUrl();
logger.info("With scope "+scope+" loaded the GeoExplorer Application URL "+url);
return url;
}
}

View File

@ -0,0 +1,80 @@
/**
*
*/
package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.TimeUnit;
import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter;
import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter.GeonetworkLoginLevel;
import org.gcube.datatransfer.resolver.gis.GeonetworkInstance;
import org.gcube.datatransfer.resolver.gis.exception.GeonetworkInstanceException;
import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/**
* The Class GeoentworkInstanceGuavaCache.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 2, 2018
*/
public class GeoentworkInstanceGuavaCache {
private static Logger logger = LoggerFactory.getLogger(UriResolverStartupListener.class);
private static LoadingCache<String, GeonetworkInstance> geonetworkInstancesCache;
static {
geonetworkInstancesCache =
CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(
10, TimeUnit.MINUTES).build(
new CacheLoader<String, GeonetworkInstance>() {
@Override
public GeonetworkInstance load(String scope)
throws Exception {
return loadGeonetworkInstance(scope);
}
});
logger.info(GeoentworkInstanceGuavaCache.class.getSimpleName() +" instancied");
}
/**
* Gets the cache.
*
* @return the cache
*/
public static LoadingCache<String, GeonetworkInstance> getCache() {
return geonetworkInstancesCache;
}
/**
* Load geonetwork instance.
*
* @param scope the scope
* @return the geonetwork instance
* @throws GeonetworkInstanceException the geonetwork instance exception
*/
public static GeonetworkInstance loadGeonetworkInstance(String scope)
throws GeonetworkInstanceException {
if (scope == null || scope.isEmpty())
logger.warn("Scope is null or ermpty, skipping loadGeonetworkInstance");
GeonetworkAccessParameter gntwAccess = new GeonetworkAccessParameter(scope);
GeonetworkInstance gnInstance = gntwAccess.getGeonetworkInstance(true, GeonetworkLoginLevel.ADMIN);
logger.info("Loaded "+gnInstance+" for scope: " + scope);
return gnInstance;
}
}

View File

@ -0,0 +1,75 @@
/**
*
*/
package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.TimeUnit;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader;
import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/**
* The Class GisViewerApplicationHostnameGuavaCache.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 2, 2018
*/
public class GisViewerApplicationHostnameGuavaCache {
private static Logger logger = LoggerFactory.getLogger(GisViewerApplicationHostnameGuavaCache.class);
private static LoadingCache<String, String> gisViewerApplicationURLCache;
static {
gisViewerApplicationURLCache =
CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(
1, TimeUnit.HOURS).build(
new CacheLoader<String, String>() {
@Override
public String load(String scope)
throws Exception {
return loadGisViewerApplicationURL(scope);
}
});
logger.info(GisViewerApplicationHostnameGuavaCache.class.getSimpleName() +" instancied");
}
/**
* Gets the cache.
*
* @return the cache
*/
public static LoadingCache<String, String> getCache() {
return gisViewerApplicationURLCache;
}
/**
* Load gis viewer application url.
*
* @param scope the scope
* @return the string
*/
public static String loadGisViewerApplicationURL(String scope){
if (scope == null || scope.isEmpty())
logger.warn("Scope is null or ermpty, skipping loadGisViewerApplicationURL");
ApplicationProfileReader reader = new ApplicationProfileReader(scope, UriResolverStartupListener.getGisViewerProfile().getGenericResource(), UriResolverStartupListener.getGisViewerProfile().getAppId(), false);
String url = reader.getApplicationProfile().getUrl();
logger.info("With scope "+scope+" loaded the GisViewer Application URL "+url);
return url;
}
}

View File

@ -8,12 +8,12 @@ import org.apache.log4j.Logger;
/**
* The Class ApplicationProfileGenericResourcePropertyReader.
* The Class ApplicationProfileGenericResourceReader.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Mar 9, 2017
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 2, 2018
*/
public class ApplicationProfileGenericResourcePropertyReader {
public class ApplicationProfileGenericResourceReader {
//protected static final String GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES = "gisviewerappgenericresource.properties";
protected static final String SECONDARY_TYPE = "SECONDARY_TYPE";
@ -22,20 +22,19 @@ public class ApplicationProfileGenericResourcePropertyReader {
private String appId;
private String genericResource;
private Logger logger = Logger.getLogger(ApplicationProfileGenericResourcePropertyReader.class);
private Logger logger = Logger.getLogger(ApplicationProfileGenericResourceReader.class);
/**
* Instantiates a new gis viewer app generic resource property reader.
* Instantiates a new application profile generic resource reader.
*
* @param fileNameProperty the file name property
* @param in the in
* @throws PropertyFileNotFoundException the property file not found exception
*/
public ApplicationProfileGenericResourcePropertyReader(String fileNameProperty) throws PropertyFileNotFoundException {
public ApplicationProfileGenericResourceReader(InputStream in) throws PropertyFileNotFoundException {
Properties prop = new Properties();
try {
InputStream in = ApplicationProfileGenericResourcePropertyReader.class.getResourceAsStream(fileNameProperty);
// load a properties file
prop.load(in);
// get the property value - the application Id

View File

@ -0,0 +1,121 @@
/**
*
*/
package org.gcube.datatransfer.resolver.listeners;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.gcube.datatransfer.resolver.caches.GeoentworkInstanceGuavaCache;
import org.gcube.datatransfer.resolver.caches.GisViewerApplicationHostnameGuavaCache;
import org.gcube.datatransfer.resolver.gis.property.ApplicationProfileGenericResourceReader;
import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The listener interface for receiving startup events.
* The class that is interested in processing a startup
* event implements this interface, and the object created
* with that class is registered with a component using the
* component's <code>addStartupListener<code> method. When
* the startup event occurs, that object's appropriate
* method is invoked.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Nov 2, 2018
*/
@WebListener
public class UriResolverStartupListener implements ServletContextListener {
private static Logger logger = LoggerFactory.getLogger(UriResolverStartupListener.class);
public static final String GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES = "gisviewerappgenericresource.properties";
public static final String GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES = "geoexplorerappgenericresource.properties";
protected static final String SECONDARY_TYPE = "SECONDARY_TYPE";
protected static final String APP_ID = "APP_ID";
private static ApplicationProfileGenericResourceReader gisViewerProfile;
private static ApplicationProfileGenericResourceReader geoExplorerProfile;
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent event) {
logger.info("Context initialized!");
gisViewerProfile = loadApplicationProfile(event.getServletContext(), GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
geoExplorerProfile = loadApplicationProfile(event.getServletContext(), GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
new GeoentworkInstanceGuavaCache();
new GisViewerApplicationHostnameGuavaCache();
}
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
// Perform action during application's shutdown
}
/**
* Gets the currency.
*
* @param context the context
* @param propertyFileName the property file name
* @return the currency
*/
private static ApplicationProfileGenericResourceReader loadApplicationProfile(ServletContext context, String propertyFileName) {
String contextPath = "/WEB-INF/property/"+propertyFileName;
String realPath = context.getRealPath(contextPath);
try {
Properties props = new Properties();
props.load(new FileInputStream(new File(realPath)));
return new ApplicationProfileGenericResourceReader(new FileInputStream(new File(realPath)));
} catch (PropertyFileNotFoundException | FileNotFoundException ex) {
logger.error("PropertyFileNotFoundException: "+contextPath, ex);
}catch (IOException e) {
logger.error("Error on loading property from: "+contextPath, e);
}
return null;
}
/**
* Gets the gis viewer profile.
*
* @return the gis viewer profile
*/
public static ApplicationProfileGenericResourceReader getGisViewerProfile() {
return gisViewerProfile;
}
/**
* Gets the geo explorer profile.
*
* @return the geoExplorerProfile
*/
public static ApplicationProfileGenericResourceReader getGeoExplorerProfile() {
return geoExplorerProfile;
}
}

View File

@ -160,11 +160,6 @@ public class GeonetworkResolver {
ExceptionManager.throwWrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, GeonetworkResolver.class, help);
}
/*if(requestDelimiter==null || requestDelimiter.compareTo(VALUE_OF_REQUEST_DELIMITIER)!=0){
logger.error("Path Parameter to REQUEST_DELIMITIER '"+VALUE_OF_REQUEST_DELIMITIER+"' not found");
ExceptionManager.throwBadRequestException(req, "Path Parameter '"+VALUE_OF_REQUEST_DELIMITIER+"' not found as REQUEST DELIMITER", GeonetworkResolver.class, help);
}*/
if(resetCache!=null && Boolean.parseBoolean(resetCache)){
purgeCacheGeonetworkInstances();
}
@ -173,39 +168,19 @@ public class GeonetworkResolver {
resetGeonetoworkInstanceCacheForScope(scope);
}
// String fullURL = Util.getFullURL(req);
// int index = fullURL.indexOf(GeonetworkRequestFilterParameters.REQUEST_DELIMITIER);
// int delimiterIndex = index+GeonetworkRequestFilterParameters.REQUEST_DELIMITIER.length();
// //BUILDING REMAINING PATH WITHOUT GeonetworkRequestFilterParameters.REQUEST_DELIMITIER
// if(delimiterIndex<fullURL.length()){
// remainPath = fullURL.substring(delimiterIndex, fullURL.length());
// }
logger.info("Remaining path is: "+remainPath);
String gnGetlURL = null;
try {
GeonetworkInstance gnInstance = getGeonetworkInstanceForScope(scope);
// if(gnInstance==null){
// logger.info("GeonetworkInstance not istanciable via geonetwork library.. using ");
// ServerParameters serverParams = getGeonetworkCachedServerParameters(scopeValue);
// gnInstance = gntwAccess.getGeonetworkInstance();
// }
ScopeProvider.instance.set(scope);
HTTPCallsUtils httpUtils = new HTTPCallsUtils();
Configuration config = gnInstance.getGeonetworkPublisher().getConfiguration();
String geonetworkUrl = config.getGeoNetworkEndpoint();
// boolean authorized = GNAuthentication.login(httpUtils, geonetworkParams.getUrl(), geonetworkParams.getUser(), geonetworkParams.getPassword());
// logger.trace("Authorized on "+geonetworkParams +" ? "+authorized);
// String newQueryString = purgeScopeFromQueryString(scope, req.getQueryString());
// logger.trace("Purged query string from "+scope+" is: "+newQueryString);
String baseURL = remainPath==null ||remainPath.isEmpty()?geonetworkUrl+"/"+CSW_SERVER:geonetworkUrl+"/"+CSW_SERVER+remainPath;
logger.info("The base URL is: "+baseURL);
//newQueryString = purgeRemainFromQueryString(remainPath, newQueryString);
//logger.trace("Purged query string from "+remainPath+" is: "+newQueryString);
String queryString = req.getQueryString()==null || req.getQueryString().isEmpty()?"":"?"+req.getQueryString();
gnGetlURL = baseURL+queryString;
logger.info("Sending get request to URL: "+gnGetlURL);
@ -224,19 +199,6 @@ public class GeonetworkResolver {
.ok(so)
//.header(ConstantsResolver.CONTENT_DISPOSITION,"attachment; filename = \""+fileName+"\"")
.header(ConstantsResolver.CONTENT_TYPE, httpUtils.getLastContentType());
/*resp.setContentType(httpUtils.getLastContentType());
InputStream in = IOUtils.toInputStream(proxedGNResponse.getResponse());
OutputStream out = resp.getOutputStream();
try{
int bytes = IOUtils.copy(in, out);
if(bytes==0)
logger.warn("ResponseBody is empty, returning empty resp");
}catch(Exception e){
logger.error("Error on copy response:", e);
}finally{
IOUtils.closeQuietly(in);
}*/
return responseBuilder.build();
case HttpServletResponse.SC_FORBIDDEN:
@ -329,20 +291,6 @@ public class GeonetworkResolver {
ExceptionManager.throwWrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, GeonetworkResolver.class, help);
}
// if(requestDelimiter==null || requestDelimiter.compareTo(VALUE_OF_REQUEST_DELIMITIER)!=0){
// logger.error("Path Parameter to REQUEST_DELIMITIER '"+VALUE_OF_REQUEST_DELIMITIER+"' not found");
// ExceptionManager.throwBadRequestException(req, "Path Parameter '"+VALUE_OF_REQUEST_DELIMITIER+"' not found as REQUEST DELIMITER", GeonetworkResolver.class, help);
// }
// String fullURL = Util.getFullURL(req);
// int index = fullURL.indexOf(GeonetworkRequestFilterParameters.REQUEST_DELIMITIER);
// int delimiterIndex = index+GeonetworkRequestFilterParameters.REQUEST_DELIMITIER.length();
//BUILDING REMAINING PATH WITHOUT GeonetworkRequestFilterParameters.REQUEST_DELIMITIER
// String remainPath = null;
// if(delimiterIndex<fullURL.length()){
// remainPath = fullURL.substring(delimiterIndex, fullURL.length());
// }
//HOW TO PASS MORE THAN ONE?
Map<String,String> filters = new HashMap<String, String>();
if(filterKey!=null && filterValue!=null){
@ -368,7 +316,6 @@ public class GeonetworkResolver {
// logger.debug("param "+p + " value "+Arrays.toString(req.getParameterValues(p)));
// }
//DEBUG BODY
// String readBody = IOUtils.toString(req.getReader());
// logger.debug("doPost read body request: "+readBody);
@ -399,8 +346,6 @@ public class GeonetworkResolver {
IOUtils.copy(req.getInputStream(), byteArray);
}
//filterPublicMetadata = theVisibility.equals(VISIBILITY.PRV)?true:false;
HTTPCallsUtils httpUtils = new HTTPCallsUtils();
//PRIVATE LAYERS
@ -409,16 +354,8 @@ public class GeonetworkResolver {
//VRE LAYERS
if(mode.equals(MODE.VRE)){
logger.info("Getting "+MODE.VRE+" layers..");
//HARVESTED LAYERS
}else{
// logger.debug("Getting "+MODE.HARVEST+" layers, I'm using the owner: '"+owner +"' passed as parameter to filter layer/s returned..");
// if(owner==null || owner.isEmpty()){
// String error = "Harvest owner is missing. It is not possible to filter layers for the request "+MODE.HARVEST + " in the scope: "+scope+", without a valid owner as input";
// logger.error(error);
// ExceptionManager.throwBadRequestException(req, error, GeonetworkResolver.class, help);
// }
filters.put("isHarvested", "y");
logger.info("Getting "+MODE.HARVEST+" layers, I added 'isHarvested = y' to the filters ["+filters+"]");
}
@ -436,15 +373,8 @@ public class GeonetworkResolver {
if(mode.equals(MODE.VRE)){
logger.info("Getting "+MODE.VRE+" layers, the VRE account: "+account.getUser() +" will be used as owner user for filtering... Is it right?");
filters.put("ownername", account.getUser());
//HARVESTED LAYERS
}else{
// logger.debug("Getting "+MODE.HARVEST+" layers, I'm using the owner: '"+owner +"' passed as parameter to filter layer/s returned..");
// if(owner==null || owner.isEmpty()){
// String error = "Harvest owner is missing. It is not possible to filter layers for the request "+MODE.HARVEST + " in the scope: "+scope+", without a valid owner as input";
// logger.error(error);
// ExceptionManager.throwBadRequestException(req, error, GeonetworkResolver.class, help);
// }
//filters.put("isHarvested", "y");
logger.info("Getting "+MODE.HARVEST+" layers, I'm applying the filters ["+filters+"]");
}
@ -453,12 +383,9 @@ public class GeonetworkResolver {
logger.info("Sending CSW POST request to URL: "+gnCSWlURL);
logger.info("Content-Type: "+req.getContentType());
//DEBUG
//logger.debug("POST - BODY : "+byteArray.toString());
InputStream in = httpUtils.post(gnCSWlURL, new ByteArrayInputStream(byteArray.toByteArray()), req.getContentType(), req.getParameterMap());
// resp.setContentType(httpUtils.getLastContentType());
// OutputStream out = resp.getOutputStream();
if(in==null){
logger.warn("Input stream returned is null, sending "+HttpServletResponse.SC_NOT_FOUND);
@ -477,28 +404,6 @@ public class GeonetworkResolver {
in = GetResponseRecordFilter.overrideResponseIdsByListIds(reus, filterGetRecords.getFoundPublicIds(), REPLACED_A_PUBLIC_UUID_PLEASE_IGNORE);
}
}
// else {
//
// logger.info("Public VISIBILITY perfoming check on ownership...");
// Document doc = GetResponseRecordFilter.inputStreamToW3CDocument(reus);
// List<String> fileIdentifiers = GetResponseRecordFilter.getTextContentStringsForTagName(doc, "gmd:fileIdentifier");
// List<String> noMatchingOwner = new ArrayList<String>();
// for (String fileId : fileIdentifiers) {
// String own = GetResponseRecordFilter.getMetaCategoryByFileIdentifier(fileId, config.getGeoNetworkEndpoint(),config.getAdminAccount().getUser(), config.getAdminAccount().getPassword());
// //String own = GetResponseRecordFilter.getMetaOwnerNameByFileIdentifier(fileId, config.getGeoNetworkEndpoint(),config.getAdminAccount().getUser(), config.getAdminAccount().getPassword());
// if(own.compareTo(owner)!=0){
// logger.debug("Owner of file Identifier "+fileId+" not matching the owner passed: "+owner+", removing it..");
// noMatchingOwner.add(fileId);
// }
// }
// if(noMatchingOwner.size()>0){
// logger.info("Removing "+noMatchingOwner.size()+" layer/s not macthing the owner: "+owner);
// in = GetResponseRecordFilter.overrideResponseIdsByListIds(reus, noMatchingOwner, "Replaced UUID owned by another user, please ignore");
// }else{
// logger.info("No replace on UUIDs was applied for the owner: "+owner);
// in = reus;
// }
// }
if(filters.size()>0){
logger.info("Applying filtering on geonet:info... filter/s used: "+filters);

View File

@ -0,0 +1,252 @@
/**
*
*/
package org.gcube.datatransfer.resolver.services;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.caches.GeoExplorerApplicationHostnameGuavaCache;
import org.gcube.datatransfer.resolver.caches.GeoentworkInstanceGuavaCache;
import org.gcube.datatransfer.resolver.caches.GisViewerApplicationHostnameGuavaCache;
import org.gcube.datatransfer.resolver.gis.GeonetworkInstance;
import org.gcube.datatransfer.resolver.gis.MetadataConverter;
import org.gcube.datatransfer.resolver.gis.entity.GisLayerItem;
import org.gcube.datatransfer.resolver.gis.exception.GeonetworkInstanceException;
import org.gcube.datatransfer.resolver.gis.exception.IllegalArgumentException;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class GisResolver.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Nov 2, 2018
*/
@Path("gis")
public class GisResolver {
private static Logger logger = LoggerFactory.getLogger(GisResolver.class);
public static String help = "https://wiki.gcube-system.org/gcube/URI_Resolver#GIS_Resolver";
public static final String UTF_8 = "UTF-8";
public static final String GIS_UUID = "gis-UUID";
public static final String SCOPE = "scope";
public static final String GEO_EXPLORER_LAYER_UUID = "geo-exp";
/**
* Submit get.
*
* @param req the req
* @param scope the scope
* @param gisUUID the gis uuid
* @param geoExplorerUUID the geo explorer uuid
* @return the response
*/
@GET
@Path("")
public Response submitGet(@Context HttpServletRequest req, @QueryParam(SCOPE) String scope, @QueryParam(GIS_UUID) String gisUUID, @QueryParam(GEO_EXPLORER_LAYER_UUID) String geoExplorerUUID){
logger.info(this.getClass().getSimpleName()+" GET starts...");
boolean isGisLink = false;
boolean isGeoExplorerLink = false;
if(scope==null || scope.isEmpty()){
logger.error("Query Parameter 'scope' not found");
ExceptionManager.throwBadRequestException(req, "Missing mandatory query parameter 'scope'", this.getClass(), help);
}
if(gisUUID==null || gisUUID.isEmpty()){
logger.error("Path Parameter 'gis-UUID' not found");
ExceptionManager.throwBadRequestException(req, "Missing mandatory query parameter 'gis-UUID'", this.getClass(), help);
}else
isGisLink = true;
logger.info(SCOPE +" is: " + scope);
logger.info(GIS_UUID +" is: " + gisUUID);
if (geoExplorerUUID == null || geoExplorerUUID.isEmpty()) {
logger.debug(GEO_EXPLORER_LAYER_UUID+ " not found");
}else
isGeoExplorerLink = true;
logger.info(GEO_EXPLORER_LAYER_UUID +" is: " + geoExplorerUUID);
if(!isGisLink && !isGeoExplorerLink){
String err = GIS_UUID+" or "+GEO_EXPLORER_LAYER_UUID+" not found or empty in the query string";
logger.error(err);
ExceptionManager.throwBadRequestException(req, err, this.getClass(), help);
}
try {
if(isGisLink){
//ScopeProvider.instance.set(scope);
//ServerParameters geonetworkParams = getCachedServerParameters(scope);
GisLayerItem gisLayerItem = getGisLayerForLayerUUID(scope, gisUUID);
logger.info("wms url is: " + gisLayerItem.getFullWmsUrlRequest());
String wmsRequest = URLEncoder.encode(gisLayerItem.getFullWmsUrlRequest(), UTF_8);
logger.info("encoded WMS url is: " + wmsRequest);
String layerTitle = null;
if(gisLayerItem.getCitationTitle()!=null && !gisLayerItem.getCitationTitle().isEmpty())
layerTitle = URLEncoder.encode(gisLayerItem.getCitationTitle(), UTF_8);
logger.info("layer Title encoded is: " + layerTitle);
String gisViewerPortletUrl = getGisViewerApplicationURL(scope);
logger.info("Gis Viewer Application url is: " + gisViewerPortletUrl);
gisViewerPortletUrl+="?rid="+new Random().nextLong()
+"&wmsrequest="+wmsRequest
+"&uuid="+URLEncoder.encode(gisUUID, UTF_8);
if(layerTitle!=null)
gisViewerPortletUrl+="&layertitle="+layerTitle;
logger.info("Redirecting to: "+gisViewerPortletUrl);
return Response.seeOther(new URI(gisViewerPortletUrl)).build();
}
if(isGeoExplorerLink){
ScopeProvider.instance.set(scope);
String geoExplorerPortletUrl = getGeoExplorerApplicationURL(scope);
logger.info("GeoExplorer Application url is: " + geoExplorerPortletUrl);
geoExplorerPortletUrl+="?rid="+new Random().nextLong()
+"&luuid="+URLEncoder.encode(geoExplorerUUID, UTF_8);
//urlRedirect(req, resp, geoExplorerPortletUrl);
return Response.seeOther(new URI(geoExplorerPortletUrl)).build();
}
ExceptionManager.throwBadRequestException(req, GIS_UUID+" or "+GEO_EXPLORER_LAYER_UUID+" not found or empty in the query string", this.getClass(), help);
return null;
} catch (Exception e) {
logger.error("Exception:", e);
String error = "Sorry, an error occurred on resolving request with UUID "+gisUUID+" and scope "+scope+". Please, contact support!";
ExceptionManager.throwInternalErrorException(req, error, this.getClass(), help);
return null;
}
}
/**
* Gets the gis viewer application url.
*
* @param scope the scope
* @return the gis viewer application url
* @throws Exception the exception
*/
protected String getGisViewerApplicationURL(String scope) throws Exception{
logger.info("Tentative of recovering gis viewer application hostname from cache for scope: "+scope);
String gisViewerAppHostname = GisViewerApplicationHostnameGuavaCache.getCache().get(scope);
if(gisViewerAppHostname==null){
logger.info("Gis viewer application hostname is null, reading from application profile..");
String url = GisViewerApplicationHostnameGuavaCache.loadGisViewerApplicationURL(scope);
GisViewerApplicationHostnameGuavaCache.getCache().put(scope, url);
logger.info("Updated GisViewerApplication cache! Scope "+scope+" linking "+url);
return url;
}else
logger.info("Cache for GisViewerApplication end point is not null using it");
return gisViewerAppHostname;
}
/**
* Gets the gis layer for layer uuid.
*
* @param scope the scope
* @param gisUUID the gis uuid
* @return the gis layer for layer uuid
* @throws Exception the exception
*/
protected GisLayerItem getGisLayerForLayerUUID(String scope, String gisUUID) throws Exception{
try {
GeonetworkInstance gi = getCachedGeonetworkInstance(scope);
GisLayerItem gisLayerItem = MetadataConverter.getWMSOnLineResource(gi, gisUUID);
return gisLayerItem;
//TODO CREATE A BEAN ADDING WMS REQUEST AND LAYER TITLE MetadataConverter.
}catch (GeonetworkInstanceException e){
logger.error("An error occurred when instancing geonetowrk gis layer with UUID "+gisUUID, e);
throw new IllegalArgumentException("Sorry, An error occurred when instancing geonetwork with UUID: "+gisUUID);
} catch (Exception e) {
logger.error("An error occurred when retrieving gis layer with UUID "+gisUUID, e);
throw new IllegalArgumentException("Sorry, An error occurred when retrieving gis layer with UUID "+gisUUID);
}
}
/**
* Gets the cached geonetwork instance.
*
* @param scope the scope
* @return the cached geonetwork instance
* @throws Exception the exception
*/
protected GeonetworkInstance getCachedGeonetworkInstance(String scope) throws Exception{
logger.info("Attempt to get the GeonetworkInstance from cache by scope: "+scope);
GeonetworkInstance geonInstance = GeoentworkInstanceGuavaCache.getCache().get(scope);
if(geonInstance==null){
logger.info("GeonetworkInstance is null in cache, reading from library...");
try {
geonInstance = GeoentworkInstanceGuavaCache.loadGeonetworkInstance(scope);
GeoentworkInstanceGuavaCache.getCache().put(scope, geonInstance);
logger.info("Updated GeonetworkInstance cache! Scope "+scope+" linking "+geonInstance);
} catch (Exception e) {
logger.error("An error occurred on getting GeonetworkInstance for scope: "+scope, e);
throw new Exception("Sorry, An error occurred on getting GeonetworkInstance for scope: "+scope);
}
}else
logger.info("GeonetworkInstance is not null using it");
logger.info("returning GeonetworkInstance: "+geonInstance);
return geonInstance;
}
/**
* Gets the geo explorer application url.
*
* @param scope the scope
* @return the geo explorer application url
* @throws Exception the exception
*/
protected String getGeoExplorerApplicationURL(String scope) throws Exception{
logger.info("Tentative of recovering geo explorer application hostname from cache for scope: "+scope);
String geoExplorerApplicationHostname = GeoExplorerApplicationHostnameGuavaCache.getCache().get(scope);
if(geoExplorerApplicationHostname==null){
logger.info("GeoExplorer application hostname is null, reading from application profile..");
String url = GeoExplorerApplicationHostnameGuavaCache.loadGeoExplorerApplicationURL(scope);
GeoExplorerApplicationHostnameGuavaCache.getCache().put(scope, url);
logger.info("Updated GeoExplorerApplication cache! Scope "+scope+" linking "+url);
return url;
}else
logger.info("Cache for GeoExplorerApplication end point is not null using it");
return geoExplorerApplicationHostname;
}
}

View File

@ -0,0 +1,11 @@
# Property files
#
# author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
# created 02/2013
#
# The generic resource that describes the properties to open
# an item from workspace
#
SECONDARY_TYPE = ApplicationProfile
APP_ID = org.gcube.portlets.user.geoexplorer.server.GeoExplorerServiceImpl

View File

@ -0,0 +1,11 @@
# Property files
#
# author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
# created 02/2013
#
# The generic resource that describes the properties to open
# an item from workspace
#
SECONDARY_TYPE = ApplicationProfile
APP_ID = org.gcube.portlets.user.gisviewerapp.server.GisViewerAppServiceImpl