added a random id to avoid browser cache

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@122524 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-01-26 15:22:37 +00:00
parent bf498fc721
commit 5189be3d57
1 changed files with 52 additions and 51 deletions

View File

@ -1,5 +1,5 @@
/** /**
* *
*/ */
package org.gcube.datatransfer.resolver.gis; package org.gcube.datatransfer.resolver.gis;
@ -8,6 +8,7 @@ import java.io.StringReader;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@ -42,25 +43,25 @@ public class GisResolver extends HttpServlet{
public static final String PARAM_SEPARATOR_REPLACEMENT_VALUE = "%%"; public static final String PARAM_SEPARATOR_REPLACEMENT_VALUE = "%%";
public static final String PARAM_SEPARATOR_REPLACEMENT_KEY = "separtor"; public static final String PARAM_SEPARATOR_REPLACEMENT_KEY = "separtor";
private static final long serialVersionUID = 5605107730993617579L; private static final long serialVersionUID = 5605107730993617579L;
public static final String GIS_UUID = "gis-UUID"; public static final String GIS_UUID = "gis-UUID";
public static final String SCOPE = "scope"; public static final String SCOPE = "scope";
/** The logger. */ /** The logger. */
private static final Logger logger = LoggerFactory.getLogger(GisResolver.class); private static final Logger logger = LoggerFactory.getLogger(GisResolver.class);
protected Map<String, ServerParameters> cachedServerParams; //A cache: scope - geonetwork parameters protected Map<String, ServerParameters> cachedServerParams; //A cache: scope - geonetwork parameters
protected Map<String, String> cachedGisViewerApplHostname; //A cache: scope - GisViewerApp hostname protected Map<String, String> cachedGisViewerApplHostname; //A cache: scope - GisViewerApp hostname
private Timer timer; private Timer timer;
private GisViewerAppGenericResourcePropertyReader gisViewerAppPropertyReader; private GisViewerAppGenericResourcePropertyReader gisViewerAppPropertyReader;
//THIRTY MINUTES //THIRTY MINUTES
public static final long CACHE_RESET_TIME = 30*60*1000; public static final long CACHE_RESET_TIME = 30*60*1000;
//TEN MINUTES //TEN MINUTES
public static final long CACHE_RESET_DELAY = 10*1000; public static final long CACHE_RESET_DELAY = 10*1000;
/* (non-Javadoc) /* (non-Javadoc)
* @see javax.servlet.GenericServlet#init() * @see javax.servlet.GenericServlet#init()
*/ */
@ -78,7 +79,7 @@ public class GisResolver extends HttpServlet{
} }
}, CACHE_RESET_DELAY, CACHE_RESET_TIME); }, CACHE_RESET_DELAY, CACHE_RESET_TIME);
} }
/** /**
* Gets the cached server parameters. * Gets the cached server parameters.
* *
@ -87,13 +88,13 @@ public class GisResolver extends HttpServlet{
* @throws Exception the exception * @throws Exception the exception
*/ */
protected ServerParameters getCachedServerParameters(String scope) throws Exception{ protected ServerParameters getCachedServerParameters(String scope) throws Exception{
if(cachedServerParams==null) if(cachedServerParams==null)
reseCacheServerParameters(); reseCacheServerParameters();
logger.info("Tentative to recovering gis server param from cache to scope "+scope); logger.info("Tentative to recovering gis server param from cache to scope "+scope);
ServerParameters serverParam = cachedServerParams.get(scope); ServerParameters serverParam = cachedServerParams.get(scope);
if(serverParam==null){ if(serverParam==null){
logger.info("Gis server param is null, reading from application profile.."); logger.info("Gis server param is null, reading from application profile..");
GeoRuntimeReader reader = new GeoRuntimeReader(); GeoRuntimeReader reader = new GeoRuntimeReader();
@ -107,12 +108,12 @@ public class GisResolver extends HttpServlet{
} }
}else }else
logger.info("Cache gis server param is not null using it"); logger.info("Cache gis server param is not null using it");
logger.info("returning geonetworkParams "+serverParam); logger.info("returning geonetworkParams "+serverParam);
return serverParam; return serverParam;
} }
/** /**
* Rese cache server parameters. * Rese cache server parameters.
*/ */
@ -120,7 +121,7 @@ public class GisResolver extends HttpServlet{
cachedServerParams = new HashMap<String, ServerParameters>(); cachedServerParams = new HashMap<String, ServerParameters>();
logger.info("Cache server params reset!"); logger.info("Cache server params reset!");
} }
/** /**
* Rese cache gis viewer application hostname. * Rese cache gis viewer application hostname.
*/ */
@ -140,7 +141,7 @@ public class GisResolver extends HttpServlet{
logger.error("Error on reset GisViewerAppEndPoint ",e); logger.error("Error on reset GisViewerAppEndPoint ",e);
} }
} }
/** /**
* Gets the gis viewer application url. * Gets the gis viewer application url.
* *
@ -149,10 +150,10 @@ public class GisResolver extends HttpServlet{
* @throws Exception the exception * @throws Exception the exception
*/ */
protected String getGisViewerApplicationURL(String scope) throws Exception{ protected String getGisViewerApplicationURL(String scope) throws Exception{
if(cachedGisViewerApplHostname==null) if(cachedGisViewerApplHostname==null)
reseCacheGisViewerApplicationHostname(); reseCacheGisViewerApplicationHostname();
String infra = ScopeUtil.getInfrastructureNameFromScope(scope); String infra = ScopeUtil.getInfrastructureNameFromScope(scope);
logger.info("Tentative of recovering gis viewer application hostname from cache for scope: "+scope); logger.info("Tentative of recovering gis viewer application hostname from cache for scope: "+scope);
String gisViewerAppHostname = cachedGisViewerApplHostname.get(infra); String gisViewerAppHostname = cachedGisViewerApplHostname.get(infra);
@ -160,7 +161,7 @@ public class GisResolver extends HttpServlet{
logger.info("Gis viewer application hostname is null, reading from application profile.."); logger.info("Gis viewer application hostname is null, reading from application profile..");
if(gisViewerAppPropertyReader==null) if(gisViewerAppPropertyReader==null)
resetGisViewerAppEndPoint(); resetGisViewerAppEndPoint();
ApplicationProfileReader reader = new ApplicationProfileReader(infra, gisViewerAppPropertyReader.getGenericResource(), gisViewerAppPropertyReader.getAppId()); ApplicationProfileReader reader = new ApplicationProfileReader(infra, gisViewerAppPropertyReader.getGenericResource(), gisViewerAppPropertyReader.getAppId());
String url = reader.getApplicationProfile().getUrl(); String url = reader.getApplicationProfile().getUrl();
cachedGisViewerApplHostname.put(infra, url); cachedGisViewerApplHostname.put(infra, url);
@ -168,19 +169,19 @@ public class GisResolver extends HttpServlet{
return url; return url;
}else }else
logger.info("Cache Gis viewer application is not null using it"); logger.info("Cache Gis viewer application is not null using it");
return gisViewerAppHostname; return gisViewerAppHostname;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/ */
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
logger.info("The http session id is: " + req.getSession().getId()); logger.info("The http session id is: " + req.getSession().getId());
String gisUUID = req.getParameter(GIS_UUID); String gisUUID = req.getParameter(GIS_UUID);
if (gisUUID == null || gisUUID.equals("")) { if (gisUUID == null || gisUUID.equals("")) {
@ -188,9 +189,9 @@ public class GisResolver extends HttpServlet{
sendError(resp, HttpServletResponse.SC_BAD_REQUEST, GIS_UUID+" not found or empty"); sendError(resp, HttpServletResponse.SC_BAD_REQUEST, GIS_UUID+" not found or empty");
return; return;
} }
logger.info("GIS UUID is: " + gisUUID); logger.info("GIS UUID is: " + gisUUID);
String scope = req.getParameter(SCOPE); String scope = req.getParameter(SCOPE);
if (scope == null || scope.equals("")) { if (scope == null || scope.equals("")) {
@ -198,43 +199,43 @@ public class GisResolver extends HttpServlet{
sendError(resp, HttpServletResponse.SC_BAD_REQUEST, SCOPE+" not found or empty"); sendError(resp, HttpServletResponse.SC_BAD_REQUEST, SCOPE+" not found or empty");
return; return;
} }
logger.info("SCOPE is: " + gisUUID); logger.info("SCOPE is: " + gisUUID);
try { try {
ServerParameters geonetworkParams = getCachedServerParameters(scope); ServerParameters geonetworkParams = getCachedServerParameters(scope);
String wmsRequest = getLayerWmsRequest(scope, gisUUID, geonetworkParams); String wmsRequest = getLayerWmsRequest(scope, gisUUID, geonetworkParams);
logger.info("wms url is: " + wmsRequest); logger.info("wms url is: " + wmsRequest);
wmsRequest = URLEncoder.encode(wmsRequest, UTF_8); wmsRequest = URLEncoder.encode(wmsRequest, UTF_8);
logger.info("encoded WMS url is: " + wmsRequest); logger.info("encoded WMS url is: " + wmsRequest);
String gisPortletUrl = getGisViewerApplicationURL(scope); String gisPortletUrl = getGisViewerApplicationURL(scope);
logger.info("Gis Viewer Application url is: " + gisPortletUrl); logger.info("Gis Viewer Application url is: " + gisPortletUrl);
gisPortletUrl+="?wmsrequest="+wmsRequest; gisPortletUrl+="?rid="+new Random().nextLong()+"&wmsrequest="+wmsRequest;
/*resp.setContentType(TEXT_PLAIN); /*resp.setContentType(TEXT_PLAIN);
resp.setCharacterEncoding(UTF_8); resp.setCharacterEncoding(UTF_8);
PrintWriter out = resp.getWriter(); PrintWriter out = resp.getWriter();
out.println(gisPortletUrl); out.println(gisPortletUrl);
logger.info("returning link: " + gisPortletUrl); logger.info("returning link: " + gisPortletUrl);
out.close();*/ out.close();*/
urlRedirect(req, resp, gisPortletUrl); urlRedirect(req, resp, gisPortletUrl);
} catch (IllegalArgumentException e){ } catch (IllegalArgumentException e){
logger.error("IllegalArgumentException:", e); logger.error("IllegalArgumentException:", e);
sendError(resp, HttpServletResponse.SC_BAD_REQUEST, "Illegal argument to carry out the request!"); sendError(resp, HttpServletResponse.SC_BAD_REQUEST, "Illegal argument to carry out the request!");
return; return;
} catch (Exception e) { } catch (Exception e) {
logger.error("Exception:", e); logger.error("Exception:", e);
String error = "Sorry, an error occurred on resolving request with UUID "+gisUUID+" and scope "+scope+". Please, contact support!"; String error = "Sorry, an error occurred on resolving request with UUID "+gisUUID+" and scope "+scope+". Please, contact support!";
sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error); sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error);
return; return;
} }
} }
/** /**
* Encode url with param delimiter. * Encode url with param delimiter.
* *
@ -244,7 +245,7 @@ public class GisResolver extends HttpServlet{
private String encodeURLWithParamDelimiter(String wmsRequest){ private String encodeURLWithParamDelimiter(String wmsRequest){
return wmsRequest.replaceAll("&", PARAM_SEPARATOR_REPLACEMENT_VALUE); return wmsRequest.replaceAll("&", PARAM_SEPARATOR_REPLACEMENT_VALUE);
} }
/** /**
* Decode url with param delimiter. * Decode url with param delimiter.
* *
@ -254,7 +255,7 @@ public class GisResolver extends HttpServlet{
private String decodeURLWithParamDelimiter(String wmsRequest){ private String decodeURLWithParamDelimiter(String wmsRequest){
return wmsRequest.replaceAll(PARAM_SEPARATOR_REPLACEMENT_VALUE, "&"); return wmsRequest.replaceAll(PARAM_SEPARATOR_REPLACEMENT_VALUE, "&");
} }
/** /**
* Append param replacement. * Append param replacement.
* *
@ -264,7 +265,7 @@ public class GisResolver extends HttpServlet{
private String appendParamReplacement(String wmsRequest){ private String appendParamReplacement(String wmsRequest){
return wmsRequest+"&"+PARAM_SEPARATOR_REPLACEMENT_KEY+"="+PARAM_SEPARATOR_REPLACEMENT_VALUE; return wmsRequest+"&"+PARAM_SEPARATOR_REPLACEMENT_KEY+"="+PARAM_SEPARATOR_REPLACEMENT_VALUE;
} }
/** /**
* Gets the layer wms request. * Gets the layer wms request.
* *
@ -275,7 +276,7 @@ public class GisResolver extends HttpServlet{
* @throws Exception the exception * @throws Exception the exception
*/ */
protected String getLayerWmsRequest(String scope, String gisUUID, ServerParameters geonetworkParams) throws Exception{ protected String getLayerWmsRequest(String scope, String gisUUID, ServerParameters geonetworkParams) throws Exception{
try { try {
GeonetworkServiceInterface gntwAccess = new GeonetowrkAccessParameter(scope, geonetworkParams); GeonetworkServiceInterface gntwAccess = new GeonetowrkAccessParameter(scope, geonetworkParams);
return MetadataConverter.getWMSOnLineResource(gntwAccess.getGeonetworkInstance(true), gisUUID); return MetadataConverter.getWMSOnLineResource(gntwAccess.getGeonetworkInstance(true), gisUUID);
@ -287,7 +288,7 @@ public class GisResolver extends HttpServlet{
throw new IllegalArgumentException("Sorry, An error occurred when retrieving gis layer with UUID "+gisUUID); throw new IllegalArgumentException("Sorry, An error occurred when retrieving gis layer with UUID "+gisUUID);
} }
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/ */
@ -295,7 +296,7 @@ public class GisResolver extends HttpServlet{
protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
this.doGet(req, resp); this.doGet(req, resp);
} }
/** /**
* Send error. * Send error.
* *
@ -305,19 +306,19 @@ public class GisResolver extends HttpServlet{
* @throws IOException Signals that an I/O exception has occurred. * @throws IOException Signals that an I/O exception has occurred.
*/ */
protected void sendError(HttpServletResponse response, int status, String message) throws IOException protected void sendError(HttpServletResponse response, int status, String message) throws IOException
{ {
// response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); // response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.setStatus(status); response.setStatus(status);
logger.info("error message: "+message); logger.info("error message: "+message);
logger.info("writing response..."); logger.info("writing response...");
StringReader sr = new StringReader(message); StringReader sr = new StringReader(message);
IOUtils.copy(sr, response.getOutputStream()); IOUtils.copy(sr, response.getOutputStream());
// response.getWriter().write(resultMessage.toString()); // response.getWriter().write(resultMessage.toString());
logger.info("response writed"); logger.info("response writed");
response.flushBuffer(); response.flushBuffer();
} }
/** /**
* Url redirect. * Url redirect.
* *
@ -346,15 +347,15 @@ public class GisResolver extends HttpServlet{
// String servletPath = req.getServletPath(); // /servlet/MyServlet // String servletPath = req.getServletPath(); // /servlet/MyServlet
// String pathInfo = req.getPathInfo(); // /a/b;c=123 // String pathInfo = req.getPathInfo(); // /a/b;c=123
// String queryString = req.getQueryString(); // d=789 // String queryString = req.getQueryString(); // d=789
// Reconstruct original requesting URL // Reconstruct original requesting URL
StringBuffer url = new StringBuffer(); StringBuffer url = new StringBuffer();
url.append(scheme).append("://").append(serverName); url.append(scheme).append("://").append(serverName);
if ((serverPort != 80) && (serverPort != 443)) { if (serverPort != 80 && serverPort != 443) {
url.append(":").append(serverPort); url.append(":").append(serverPort);
} }
logger.trace("server: "+url); logger.trace("server: "+url);
logger.trace("omitted contextPath: "+contextPath); logger.trace("omitted contextPath: "+contextPath);
return url.toString(); return url.toString();
@ -377,7 +378,7 @@ public class GisResolver extends HttpServlet{
// logger.info("Encoded url is: " + wmsRequest); // logger.info("Encoded url is: " + wmsRequest);
// wmsRequest = appendParamReplacement(wmsRequest); // wmsRequest = appendParamReplacement(wmsRequest);
gisPortletUrl+="?wmsrequest="+wmsRequest; gisPortletUrl+="?wmsrequest="+wmsRequest;
System.out.println(gisPortletUrl); System.out.println(gisPortletUrl);
// urlRedirect(req, resp, gisPortletUrl); // urlRedirect(req, resp, gisPortletUrl);
} catch (Exception e) { } catch (Exception e) {