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