/** * */ 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.LoadingGeoExplorerApplicationURLCache; import org.gcube.datatransfer.resolver.caches.LoadingGeonetworkInstanceCache; import org.gcube.datatransfer.resolver.caches.LoadingGisViewerApplicationURLCache; 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 = LoadingGisViewerApplicationURLCache.getCache().get(scope); if(gisViewerAppHostname==null){ // logger.info("Gis viewer application hostname is null, reading from application profile.."); // String url = LoadingGisViewerApplicationURLCache.loadGisViewerApplicationURL(scope); // LoadingGisViewerApplicationURLCache.getCache().put(scope, url); // logger.info("Updated GisViewerApplication cache! Scope "+scope+" linking "+url); // return url; throw new Exception("GisViewer Application not found in the scope: "+scope); }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 = LoadingGeonetworkInstanceCache.getCache().get(scope); if(geonInstance==null){ logger.info("GeonetworkInstance is null in cache, reading from library..."); try { geonInstance = LoadingGeonetworkInstanceCache.loadGeonetworkInstance(scope); LoadingGeonetworkInstanceCache.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 = LoadingGeoExplorerApplicationURLCache.getCache().get(scope); if(geoExplorerApplicationHostname==null){ logger.info("GeoExplorer application hostname is null, reading from application profile.."); String url = LoadingGeoExplorerApplicationURLCache.loadGeoExplorerApplicationURL(scope); LoadingGeoExplorerApplicationURLCache.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; } }