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
This commit is contained in:
Francesco Mangiacrapa 2016-01-12 16:34:36 +00:00
parent af3d5aa1ff
commit b8d2257a12
5 changed files with 203 additions and 67 deletions

View File

@ -8,7 +8,7 @@
</parent>
<groupId>org.gcube.data.transfer</groupId>
<artifactId>uri-resolver</artifactId>
<version>1.4.0-SNAPSHOT</version>
<version>1.5.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>

View File

@ -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");
}
}

View File

@ -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<String, ServerParameters>();
logger.info("Cache server params reset!");
}
/**
* Rese cache gis viewer application hostname.
*/
private void reseCacheGisViewerApplicationHostname(){
cachedGisViewerApplHostname = new HashMap<String, String>();
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{
}
/**
* Encode url with param delimiter.
*
* @param wmsRequest
* @return
* @param wmsRequest the wms request
* @return the string
*/
private String encodeURLWithParamDelimiter(String wmsRequest){
return wmsRequest.replaceAll("&", PARAM_SEPARATOR_REPLACEMENT_VALUE);
}
/**
* Decode url with param delimiter.
*
* @param wmsRequest
* @return
* @param wmsRequest the wms request
* @return the string
*/
private String decodeURLWithParamDelimiter(String wmsRequest){
return wmsRequest.replaceAll(PARAM_SEPARATOR_REPLACEMENT_VALUE, "&");
}
/**
* Append param replacement.
*
* @param wmsRequest
* @return
* @param wmsRequest the wms request
* @return the string
*/
private String appendParamReplacement(String wmsRequest){
return wmsRequest+"&"+PARAM_SEPARATOR_REPLACEMENT_KEY+"="+PARAM_SEPARATOR_REPLACEMENT_VALUE;
}
/**
* Gets the layer wms request.
*
* @param scope
* @param gisUUID
* @param geonetworkParams
* @return
* @throws Exception
* @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{
}
/**
* Send error.
*
* @param response
* @param status
* @param message
* @throws IOException
* @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();
}
}
}*/
}

View File

@ -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,8 +25,9 @@ public class MetadataConverter {
public static final String NOT_FOUND = "";
/**
* Gets the geoserver base uri.
*
* @param uri
* @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
*/
@ -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 = "";
try{
// 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);
if(item.getOnLines()!=null){
Collection<? extends OnlineResource> onlineResources = item.getOnLines();
@ -129,14 +136,13 @@ 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";
//
// 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);
}
}*/
}

View File

@ -4,6 +4,16 @@
<web-app>
<display-name>http resolver</display-name>
<filter>
<filter-name>uriResolverRewriteFilter</filter-name>
<filter-class>org.gcube.datatransfer.resolver.UriResolverRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>uriResolverRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>smp</servlet-name>
<display-name>smp</display-name>
@ -25,6 +35,7 @@
<load-on-startup>true</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>smp</servlet-name>
<url-pattern>/smp</url-pattern>
@ -40,5 +51,4 @@
<url-pattern>/id</url-pattern>
</servlet-mapping>
</web-app>