From b8d2257a1280a997ba4906b41703e727b68d2aed Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Tue, 12 Jan 2016 16:34:36 +0000 Subject: [PATCH] 1925: Uri-Resolver: resolve gCubeStorage smp uri like http uri Task-Url: https://support.d4science.org/issues/1925 Added Filter to rewrite public link updated pom version at 1.5.0 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@122206 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 2 +- .../resolver/UriResolverRewriteFilter.java | 78 +++++++++++++ .../resolver/gis/GisResolver.java | 106 +++++++++++++----- .../resolver/gis/MetadataConverter.java | 60 +++++----- src/main/webapp/WEB-INF/web.xml | 24 ++-- 5 files changed, 203 insertions(+), 67 deletions(-) create mode 100644 src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java diff --git a/pom.xml b/pom.xml index 6a192bb..da0fcbf 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.gcube.data.transfer uri-resolver - 1.4.0-SNAPSHOT + 1.5.0-SNAPSHOT war diff --git a/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java b/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java new file mode 100644 index 0000000..44711f4 --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java @@ -0,0 +1,78 @@ +/** + * + */ +package org.gcube.datatransfer.resolver; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Nov 20, 2015 + */ +public class UriResolverRewriteFilter implements Filter{ + + protected static final String SMP_ID = "smp-id"; + protected static final String SERVLET_RESOLVER_BY_ID = "id"; + protected static final Logger logger = LoggerFactory.getLogger(UriResolverRewriteFilter.class); + /* (non-Javadoc) + * @see javax.servlet.Filter#destroy() + */ + @Override + public void destroy() { + logger.trace("run destroy"); + } + + /* (non-Javadoc) + * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) + */ + @Override + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { + + HttpServletRequest request = (HttpServletRequest) req; + String requestURI = request.getRequestURI(); + String queryString = request.getQueryString(); + logger.debug("Request URI: " + requestURI + ", QueryString: " +queryString); + if (queryString == null) { // IS A /XXXXX + logger.debug("QueryString is null, is It a new SMP public uri by ID?"); + int lastSlash = requestURI.lastIndexOf("/"); + if ((lastSlash + 1) == requestURI.length()) { + logger.debug("'/' is last index, doFilter Request"); + // req.getRequestDispatcher("/").forward(req, res); + chain.doFilter(req, res); + } + else { + String toStorageID = requestURI.substring(lastSlash + 1, requestURI.length()); + // String newURI = requestURI.replace(toReplace, + // SERVLET_RESOLVER_BY_ID+"?"+SMP_ID+"="+toReplace); + String newURI = SERVLET_RESOLVER_BY_ID + "?" + SMP_ID + "=" + toStorageID; + logger.debug("forward to: " + requestURI + newURI); + req.getRequestDispatcher(newURI).forward(req, res); + } + } + else { + logger.debug("is NOT a SMP public uri by ID, doFilter Request"); + chain.doFilter(req, res); + } + } + + /* (non-Javadoc) + * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) + */ + @Override + public void init(FilterConfig arg0) throws ServletException { + logger.trace("run init"); + } + +} diff --git a/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java b/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java index f2c3477..d33287f 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java @@ -28,10 +28,12 @@ import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundExceptio import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * @Oct 7, 2014 + * The Class GisResolver. * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jan 7, 2016 */ public class GisResolver extends HttpServlet{ @@ -72,6 +74,9 @@ public class GisResolver extends HttpServlet{ //TEN MINUTES public static final long CACHE_RESET_DELAY = 10*1000; + /* (non-Javadoc) + * @see javax.servlet.GenericServlet#init() + */ @Override public void init() throws ServletException { super.init(); @@ -81,12 +86,19 @@ public class GisResolver extends HttpServlet{ public void run() { logger.info("Resetting cache..."); reseCacheServerParameters(); - reseGisViewerAppEndPoint(); + resetGisViewerAppEndPoint(); reseCacheGisViewerApplicationHostname(); } }, CACHE_RESET_DELAY, CACHE_RESET_TIME); } + /** + * Gets the cached server parameters. + * + * @param scope the scope + * @return the cached server parameters + * @throws Exception the exception + */ protected ServerParameters getCachedServerParameters(String scope) throws Exception{ if(cachedServerParams==null) @@ -114,17 +126,26 @@ public class GisResolver extends HttpServlet{ return serverParam; } + /** + * Rese cache server parameters. + */ private void reseCacheServerParameters(){ cachedServerParams = new HashMap(); logger.info("Cache server params reset!"); } + /** + * Rese cache gis viewer application hostname. + */ private void reseCacheGisViewerApplicationHostname(){ cachedGisViewerApplHostname = new HashMap(); logger.info("Cache Gis Viewer Hostname reset!"); } - - private void reseGisViewerAppEndPoint(){ + + /** + * Reset gis viewer app end point. + */ + private void resetGisViewerAppEndPoint(){ try { gisViewerAppPropertyReader = new GisViewerAppGenericResourcePropertyReader(); logger.info("GisViewerApp end point updated!"); @@ -133,18 +154,25 @@ public class GisResolver extends HttpServlet{ } } + /** + * 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{ if(cachedGisViewerApplHostname==null) reseCacheGisViewerApplicationHostname(); String infra = ScopeUtil.getInfrastructureNameFromScope(scope); - logger.info("Tentative to recovering gis viewer application hostname from cache to scope "+scope); + logger.info("Tentative of recovering gis viewer application hostname from cache for scope: "+scope); String gisViewerAppHostname = cachedGisViewerApplHostname.get(infra); if(gisViewerAppHostname==null){ logger.info("Gis viewer application hostname is null, reading from application profile.."); if(gisViewerAppPropertyReader==null) - reseGisViewerAppEndPoint(); + resetGisViewerAppEndPoint(); ApplicationProfileReader reader = new ApplicationProfileReader(infra, gisViewerAppPropertyReader.getGenericResource(), gisViewerAppPropertyReader.getAppId()); String url = reader.getApplicationProfile().getUrl(); @@ -221,39 +249,43 @@ public class GisResolver extends HttpServlet{ } /** - * - * @param wmsRequest - * @return + * Encode url with param delimiter. + * + * @param wmsRequest the wms request + * @return the string */ private String encodeURLWithParamDelimiter(String wmsRequest){ return wmsRequest.replaceAll("&", PARAM_SEPARATOR_REPLACEMENT_VALUE); } /** - * - * @param wmsRequest - * @return + * Decode url with param delimiter. + * + * @param wmsRequest the wms request + * @return the string */ private String decodeURLWithParamDelimiter(String wmsRequest){ return wmsRequest.replaceAll(PARAM_SEPARATOR_REPLACEMENT_VALUE, "&"); } /** - * - * @param wmsRequest - * @return + * Append param replacement. + * + * @param wmsRequest the wms request + * @return the string */ private String appendParamReplacement(String wmsRequest){ return wmsRequest+"&"+PARAM_SEPARATOR_REPLACEMENT_KEY+"="+PARAM_SEPARATOR_REPLACEMENT_VALUE; } /** - * - * @param scope - * @param gisUUID - * @param geonetworkParams - * @return - * @throws Exception + * Gets the layer wms request. + * + * @param scope the scope + * @param gisUUID the gis uuid + * @param geonetworkParams the geonetwork params + * @return the layer wms request + * @throws Exception the exception */ protected String getLayerWmsRequest(String scope, String gisUUID, ServerParameters geonetworkParams) throws Exception{ @@ -278,11 +310,12 @@ public class GisResolver extends HttpServlet{ } /** - * - * @param response - * @param status - * @param message - * @throws IOException + * Send error. + * + * @param response the response + * @param status the status + * @param message the message + * @throws IOException Signals that an I/O exception has occurred. */ protected void sendError(HttpServletResponse response, int status, String message) throws IOException { @@ -298,11 +331,25 @@ public class GisResolver extends HttpServlet{ response.flushBuffer(); } + /** + * Url redirect. + * + * @param req the req + * @param response the response + * @param redirectTo the redirect to + * @throws IOException Signals that an I/O exception has occurred. + */ protected void urlRedirect(HttpServletRequest req, HttpServletResponse response, String redirectTo) throws IOException { response.sendRedirect(response.encodeRedirectURL(redirectTo)); return; } + /** + * Gets the request url. + * + * @param req the req + * @return the request url + */ public static String getRequestURL(HttpServletRequest req) { String scheme = req.getScheme(); // http @@ -325,7 +372,7 @@ public class GisResolver extends HttpServlet{ logger.trace("omitted contextPath: "+contextPath); return url.toString(); } - + /* public static void main(String[] args) { GisResolver gisResolver = new GisResolver(); String scope = "/gcube/devsec/devVRE"; @@ -350,6 +397,5 @@ public class GisResolver extends HttpServlet{ // TODO Auto-generated catch block e.printStackTrace(); } - - } + }*/ } diff --git a/src/main/java/org/gcube/datatransfer/resolver/gis/MetadataConverter.java b/src/main/java/org/gcube/datatransfer/resolver/gis/MetadataConverter.java index f5a9ab4..e0bc5e1 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/gis/MetadataConverter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/gis/MetadataConverter.java @@ -10,10 +10,12 @@ import org.opengis.metadata.distribution.DigitalTransferOptions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * @Apr 26, 2013 + * The Class MetadataConverter. * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jan 7, 2016 */ public class MetadataConverter { @@ -23,9 +25,10 @@ public class MetadataConverter { public static final String NOT_FOUND = ""; /** + * Gets the geoserver base uri. * - * @param uri - * @return the input uri without the parameters, (the uri substring from start to index of '?' char (if exists)) if geoserver base url not found, + * @param uri the uri + * @return the input uri without the parameters, (the uri substring from start to index of '?' char (if exists)) if geoserver base url not found, * geoserver url otherwise */ public static GeoserverBaseUri getGeoserverBaseUri(String uri){ @@ -95,28 +98,32 @@ public class MetadataConverter { } } + /** + * Gets the WMS on line resource. + * + * @param geonetowrkInstance the geonetowrk instance + * @param uuid the uuid + * @return the WMS on line resource + * @throws Exception the exception + */ public static String getWMSOnLineResource(GeonetworkInstance geonetowrkInstance, String uuid) throws Exception{ String fullWmsPath = ""; - boolean isOwsService = false; boolean foundGeoserverUrl = false; - GeoserverBaseUri tempBaseUri = null; - - //GEOSERVER URL - String geoserverBaseUrlOnlineResource = ""; - //LAYER NAME String layerName = ""; +// boolean isOwsService = false; +// GeoserverBaseUri tempBaseUri = null; + //GEOSERVER URL +// String geoserverBaseUrlOnlineResource = ""; + try{ - logger.trace("geonetowrkInstance is null? "+(geonetowrkInstance==null)); Metadata meta = geonetowrkInstance.getGeonetworkPublisher().getById(uuid); if(meta.getDistributionInfo()!=null && meta.getDistributionInfo()!=null){ - + for (DigitalTransferOptions item: meta.getDistributionInfo().getTransferOptions()) { - - // System.out.println(++i +" item DigitalTransferOptions options: "+item); - + // System.out.println(++i +" item DigitalTransferOptions options: "+item); if(item.getOnLines()!=null){ Collection onlineResources = item.getOnLines(); @@ -128,15 +135,14 @@ public class MetadataConverter { int indexServiceWMS = geoserverUrl.toLowerCase().lastIndexOf(SERVICE_WMS); fullWmsPath = geoserverUrl; - - + //IS OWS OR WMS? if(indexServiceWMS>-1){ logger.info("found "+SERVICE_WMS+" url "+geoserverUrl); - isOwsService = geoserverUrl.contains("ows"); - - tempBaseUri = getGeoserverBaseUri(geoserverUrl); - +// isOwsService = geoserverUrl.contains("ows"); +// tempBaseUri = getGeoserverBaseUri(geoserverUrl); + geoserverUrl.contains("ows"); + getGeoserverBaseUri(geoserverUrl); if(!geoserverUrl.contains("layers") && !geoserverUrl.contains("LAYERS")){ logger.info("geoserverUrl does not contain 'layers' param, reading"); layerName= onlineResource.getName()!=null? onlineResource.getName():""; @@ -146,7 +152,6 @@ public class MetadataConverter { fullWmsPath+="&layers="+layerName; } } - break; } @@ -154,24 +159,21 @@ public class MetadataConverter { logger.trace(SERVICE_WMS+" not found for "+uuid); } } - } } - } - }catch(Exception e){ - logger.error("getWMSOnLineResource with UUID "+uuid + " has throw exception: ",e); + logger.error("getWMSOnLineResource with UUID "+uuid + " has thrown exception: ",e); throw new Exception("An error occurred when converting layer with UUID "+uuid); } logger.trace("returning: "+fullWmsPath); return fullWmsPath; } + /* public static void main(String[] args) throws Exception { -// String geoserver = "http://www.fao.org/figis/a/wms/?service=WMS&version=1.1.0&request=GetMap&layers=area:FAO_AREAS&styles=Species_prob, puppa&bbox=-180.0,-88.0,180.0,90.0000000694&width=667&height=330&srs=EPSG:4326&format=image%2Fpng"; -// +// String geoserver = "http://www.fao.org/figis/a/wms/?service=WMS&version=1.1.0&request=GetMap&layers=area:FAO_AREAS&styles=Species_prob, puppa&bbox=-180.0,-88.0,180.0,90.0000000694&width=667&height=330&srs=EPSG:4326&format=image%2Fpng"; // System.out.println(MetadataConverter.getGeoserverBaseUri(geoserver)); String user ="admin"; @@ -185,5 +187,5 @@ public class MetadataConverter { String onLineResource = getWMSOnLineResource(geonetowrkInstance, uuid); System.out.println(onLineResource); - } + }*/ } diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index bc6bfe6..4a66251 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -4,41 +4,51 @@ http resolver + + uriResolverRewriteFilter + org.gcube.datatransfer.resolver.UriResolverRewriteFilter + + + + uriResolverRewriteFilter + /* + + smp smp org.gcube.datatransfer.resolver.http.HttpResolver true - + id id org.gcube.datatransfer.resolver.http.StorageIDResolver true - + gisResolver gisResolver org.gcube.datatransfer.resolver.gis.GisResolver true - + + smp /smp - + gisResolver /gis - + id /id - - +