3135: Uri Resolver enhancements: create a Genetwork Resolver
Task-Url: https://support.d4science.org/issues/3135 Fixed issue on Get Request, added query string parameters purged by "scope" git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@126766 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
ae7ed9ce32
commit
5fa9dcfaa9
|
@ -56,6 +56,9 @@ public class UriResolverRewriteFilter implements Filter{
|
||||||
String path = request.getServletPath();
|
String path = request.getServletPath();
|
||||||
String scope = getScope(path);
|
String scope = getScope(path);
|
||||||
String newURI = SERVLET_GEONETWORK + "?" + GeonetworkResolver.SCOPE + "=" + scope;
|
String newURI = SERVLET_GEONETWORK + "?" + GeonetworkResolver.SCOPE + "=" + scope;
|
||||||
|
if(queryString!=null && !queryString.isEmpty())
|
||||||
|
newURI+="&"+queryString;
|
||||||
|
|
||||||
logger.debug("forward "+newURI);
|
logger.debug("forward "+newURI);
|
||||||
req.getRequestDispatcher(newURI).forward(req, res);
|
req.getRequestDispatcher(newURI).forward(req, res);
|
||||||
}else{
|
}else{
|
||||||
|
|
|
@ -40,6 +40,10 @@ import org.slf4j.LoggerFactory;
|
||||||
*/
|
*/
|
||||||
public class GeonetworkResolver extends HttpServlet{
|
public class GeonetworkResolver extends HttpServlet{
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static final String APPLICATION_XML = "application/xml";
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -89,17 +93,20 @@ public class GeonetworkResolver extends HttpServlet{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("SCOPE is: " + scope);
|
logger.info("SCOPE: " + scope +", Query String: " + req.getQueryString());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
ServerParameters geonetworkParams = getGeonetworkCachedServerParameters(scope);
|
ServerParameters geonetworkParams = getGeonetworkCachedServerParameters(scope);
|
||||||
HTTPCallsUtils httpUtils = new HTTPCallsUtils();
|
HTTPCallsUtils httpUtils = new HTTPCallsUtils();
|
||||||
boolean authorized = GNAuthentication.login(httpUtils, geonetworkParams.getUrl(), geonetworkParams.getUser(), geonetworkParams.getPassword());
|
boolean authorized = GNAuthentication.login(httpUtils, geonetworkParams.getUrl(), geonetworkParams.getUser(), geonetworkParams.getPassword());
|
||||||
logger.trace("Authorized on "+geonetworkParams +" ? "+authorized);
|
logger.trace("Authorized on "+geonetworkParams +" ? "+authorized);
|
||||||
String gnGetlURL = geonetworkParams.getUrl()+"/"+CSW_SERVER+"?"+req.getQueryString();
|
String newQueryString = purgeScopeFromQueryString(scope, req.getQueryString());
|
||||||
|
logger.info("Purged query string: "+newQueryString);
|
||||||
|
String gnGetlURL = newQueryString==null || newQueryString.isEmpty()? geonetworkParams.getUrl()+"/"+CSW_SERVER : geonetworkParams.getUrl()+"/"+CSW_SERVER+"?"+newQueryString;
|
||||||
logger.info("Sending get request to URL: "+gnGetlURL);
|
logger.info("Sending get request to URL: "+gnGetlURL);
|
||||||
String response = httpUtils.get(geonetworkParams.getUrl()+CSW_SERVER+"?"+req.getQueryString());
|
String response = httpUtils.get(gnGetlURL);
|
||||||
|
logger.info("Response return Content-Type: "+httpUtils.getLastContentType());
|
||||||
|
resp.setContentType(httpUtils.getLastContentType());
|
||||||
InputStream in = IOUtils.toInputStream(response);
|
InputStream in = IOUtils.toInputStream(response);
|
||||||
OutputStream out = resp.getOutputStream();
|
OutputStream out = resp.getOutputStream();
|
||||||
try{
|
try{
|
||||||
|
@ -125,11 +132,32 @@ public class GeonetworkResolver extends HttpServlet{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getScope(String servletPath){
|
|
||||||
String path = servletPath;
|
|
||||||
logger.debug("Read servlet context: "+path);
|
/**
|
||||||
String scope = path.substring(path.lastIndexOf("/")+1, path.length());
|
* Purge scope from query string.
|
||||||
return scope.replaceAll("_", "/");
|
*
|
||||||
|
* @param scope the scope
|
||||||
|
* @param queryString the query string
|
||||||
|
* @return the string
|
||||||
|
*/
|
||||||
|
private static String purgeScopeFromQueryString(String scope, String queryString){
|
||||||
|
// SCOPE is: /gcube/devsec/devVRE
|
||||||
|
// [INFO ] 2016-04-05 15:01:42,808 org.gcube.datatransfer.resolver.gis.geonetwork.GeonetworkResolver -
|
||||||
|
// Query String is: scope=/gcube/devsec/devVRE&version=2.0.2&request=GetCapabilities&service=CSW
|
||||||
|
int start = queryString.indexOf(SCOPE+"=");
|
||||||
|
|
||||||
|
if(start>=0){
|
||||||
|
int end = queryString.indexOf("&", start);
|
||||||
|
|
||||||
|
if(end==-1 && queryString.length()==(SCOPE+"="+scope).length()){ //SCOPE IS THE UNIQUE PARAMETER INTO QUETY STRING
|
||||||
|
logger.debug("Scope is the unique parameter, returning empty query string");
|
||||||
|
return "";
|
||||||
|
}else if(end<queryString.length())
|
||||||
|
return queryString.substring(end+1, queryString.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
return queryString;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
|
@ -157,6 +185,8 @@ public class GeonetworkResolver extends HttpServlet{
|
||||||
String gnCSWlURL = geonetworkParams.getUrl()+"/"+CSW_SERVER;
|
String gnCSWlURL = geonetworkParams.getUrl()+"/"+CSW_SERVER;
|
||||||
logger.info("Sending CSW request to URL: "+gnCSWlURL);
|
logger.info("Sending CSW request to URL: "+gnCSWlURL);
|
||||||
String response = httpUtils.post(gnCSWlURL, req.getInputStream(), req.getContentType());
|
String response = httpUtils.post(gnCSWlURL, req.getInputStream(), req.getContentType());
|
||||||
|
logger.info("Response return Content-Type: "+httpUtils.getLastContentType());
|
||||||
|
resp.setContentType(httpUtils.getLastContentType());
|
||||||
InputStream in = IOUtils.toInputStream(response);
|
InputStream in = IOUtils.toInputStream(response);
|
||||||
OutputStream out = resp.getOutputStream();
|
OutputStream out = resp.getOutputStream();
|
||||||
try{
|
try{
|
||||||
|
@ -283,57 +313,12 @@ public class GeonetworkResolver extends HttpServlet{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the request url.
|
* The main method.
|
||||||
*
|
*
|
||||||
* @param req the req
|
* @param args the arguments
|
||||||
* @return the request url
|
|
||||||
*/
|
*/
|
||||||
public static String getRequestURL(HttpServletRequest req) {
|
|
||||||
|
|
||||||
String scheme = req.getScheme(); // http
|
|
||||||
String serverName = req.getServerName(); // hostname.com
|
|
||||||
int serverPort = req.getServerPort(); // 80
|
|
||||||
String contextPath = req.getContextPath(); // /mywebapp
|
|
||||||
// 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) {
|
|
||||||
url.append(":").append(serverPort);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.trace("server: "+url);
|
|
||||||
logger.trace("omitted contextPath: "+contextPath);
|
|
||||||
return url.toString();
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
GisResolver gisResolver = new GisResolver();
|
|
||||||
String scope = "/gcube/devsec/devVRE";
|
|
||||||
String UUID = "177e1c3c-4a22-4ad9-b015-bfc443d16cb8";
|
|
||||||
try {
|
|
||||||
ServerParameters geonetworkParams = gisResolver.getCachedServerParameters(scope);
|
|
||||||
String wmsRequest = gisResolver.getLayerWmsRequest(scope, UUID, geonetworkParams);
|
|
||||||
logger.info("Final url is: " + wmsRequest);
|
|
||||||
wmsRequest = URLEncoder.encode(wmsRequest, UTF_8);
|
|
||||||
logger.info("Encoded WMS request is: " + wmsRequest);
|
|
||||||
String gisPortletUrl = gisResolver.getGisViewerApplicationURL(scope);
|
|
||||||
logger.info("Gis Viewer Application url is: " + gisPortletUrl);
|
|
||||||
// logger.info("WmsRequest is: " + wmsRequest);
|
|
||||||
// wmsRequest = encodeURLWithParamDelimiter(wmsRequest);
|
|
||||||
// logger.info("Encoded url is: " + wmsRequest);
|
|
||||||
// wmsRequest = appendParamReplacement(wmsRequest);
|
|
||||||
gisPortletUrl+="?wmsrequest="+wmsRequest;
|
|
||||||
|
|
||||||
System.out.println(gisPortletUrl);
|
System.out.println(purgeScopeFromQueryString("/gcube/devsec/devVRE", "scope=/gcube/devsec/devVRE&version=2.0.2&request=GetCapabilities&service=CSW"));
|
||||||
// urlRedirect(req, resp, gisPortletUrl);
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,9 @@ import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import org.apache.commons.httpclient.Credentials;
|
import org.apache.commons.httpclient.Credentials;
|
||||||
|
import org.apache.commons.httpclient.Header;
|
||||||
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.HttpClient;
|
||||||
|
import org.apache.commons.httpclient.HttpMethod;
|
||||||
import org.apache.commons.httpclient.HttpStatus;
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
import org.apache.commons.httpclient.UsernamePasswordCredentials;
|
import org.apache.commons.httpclient.UsernamePasswordCredentials;
|
||||||
import org.apache.commons.httpclient.auth.AuthScope;
|
import org.apache.commons.httpclient.auth.AuthScope;
|
||||||
|
@ -60,6 +62,7 @@ public class HTTPCallsUtils {
|
||||||
|
|
||||||
private int lastHttpStatus;
|
private int lastHttpStatus;
|
||||||
private boolean ignoreResponseContentOnSuccess = false;
|
private boolean ignoreResponseContentOnSuccess = false;
|
||||||
|
private String lastContentType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new HTTP utils.
|
* Instantiates a new HTTP utils.
|
||||||
|
@ -132,6 +135,7 @@ public class HTTPCallsUtils {
|
||||||
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
|
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
|
||||||
lastHttpStatus = client.executeMethod(httpMethod);
|
lastHttpStatus = client.executeMethod(httpMethod);
|
||||||
if(lastHttpStatus == HttpStatus.SC_OK) {
|
if(lastHttpStatus == HttpStatus.SC_OK) {
|
||||||
|
setContentType(httpMethod);
|
||||||
InputStream is = httpMethod.getResponseBodyAsStream();
|
InputStream is = httpMethod.getResponseBodyAsStream();
|
||||||
String response = IOUtils.toString(is);
|
String response = IOUtils.toString(is);
|
||||||
if(response.trim().length()==0) { // sometime gs rest fails
|
if(response.trim().length()==0) { // sometime gs rest fails
|
||||||
|
@ -156,42 +160,26 @@ public class HTTPCallsUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs an HTTP GET on the given URL and return reponse body.
|
* Show content type.
|
||||||
*
|
*
|
||||||
* @param url The URL where to connect to.
|
* @param entity the entity
|
||||||
* @return The HTTP response as a String if the HTTP response code was 200 (OK).
|
|
||||||
* @throws MalformedURLException the malformed url exception
|
|
||||||
*/
|
*/
|
||||||
public InputStream getBody(String url) throws MalformedURLException {
|
private void setContentType(HttpMethod method) {
|
||||||
|
final Header contentTypeHeader=method.getResponseHeader("Content-Type");
|
||||||
|
lastContentType =contentTypeHeader == null ? null : contentTypeHeader.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
GetMethod httpMethod = null;
|
/**
|
||||||
try {
|
* @return the lastContentType
|
||||||
setAuth(client, url, username, pw);
|
*/
|
||||||
httpMethod = new GetMethod(url);
|
public String getLastContentType() {
|
||||||
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
|
return lastContentType;
|
||||||
lastHttpStatus = client.executeMethod(httpMethod);
|
|
||||||
if(lastHttpStatus == HttpStatus.SC_OK) {
|
|
||||||
return httpMethod.getResponseBodyAsStream();
|
|
||||||
} else {
|
|
||||||
logger.info("("+lastHttpStatus+") " + HttpStatus.getStatusText(lastHttpStatus) + " -- " + url );
|
|
||||||
}
|
|
||||||
} catch (ConnectException e) {
|
|
||||||
logger.info("Couldn't connect to ["+url+"]");
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.info("Error talking to ["+url+"]", e);
|
|
||||||
} finally {
|
|
||||||
if(httpMethod != null)
|
|
||||||
httpMethod.releaseConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//==========================================================================
|
//==========================================================================
|
||||||
//=== PUT
|
//=== PUT
|
||||||
//==========================================================================
|
//==========================================================================
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the client.
|
* Gets the client.
|
||||||
*
|
*
|
||||||
|
@ -381,11 +369,12 @@ public class HTTPCallsUtils {
|
||||||
if(ignoreResponseContentOnSuccess)
|
if(ignoreResponseContentOnSuccess)
|
||||||
return "";
|
return "";
|
||||||
String response = IOUtils.toString(httpMethod.getResponseBodyAsStream());
|
String response = IOUtils.toString(httpMethod.getResponseBodyAsStream());
|
||||||
|
setContentType(httpMethod);
|
||||||
return response;
|
return response;
|
||||||
default:
|
default:
|
||||||
String badresponse = IOUtils.toString(httpMethod.getResponseBodyAsStream());
|
String badresponse = IOUtils.toString(httpMethod.getResponseBodyAsStream());
|
||||||
String message = getGeoNetworkErrorMessage(badresponse);
|
String message = getGeoNetworkErrorMessage(badresponse);
|
||||||
|
setContentType(httpMethod);
|
||||||
logger.warn("Bad response: "+lastHttpStatus
|
logger.warn("Bad response: "+lastHttpStatus
|
||||||
+ " " + httpMethod.getStatusText()
|
+ " " + httpMethod.getStatusText()
|
||||||
+ " -- " + httpMethod.getName()
|
+ " -- " + httpMethod.getName()
|
||||||
|
@ -441,6 +430,7 @@ public class HTTPCallsUtils {
|
||||||
case HttpURLConnection.HTTP_ACCEPTED:
|
case HttpURLConnection.HTTP_ACCEPTED:
|
||||||
if(logger.isDebugEnabled())
|
if(logger.isDebugEnabled())
|
||||||
logger.debug("HTTP "+ httpMethod.getStatusText() + " <-- " + url);
|
logger.debug("HTTP "+ httpMethod.getStatusText() + " <-- " + url);
|
||||||
|
setContentType(httpMethod);
|
||||||
return httpMethod.getResponseBodyAsStream();
|
return httpMethod.getResponseBodyAsStream();
|
||||||
default:
|
default:
|
||||||
logger.warn("Bad response: "+lastHttpStatus
|
logger.warn("Bad response: "+lastHttpStatus
|
||||||
|
@ -448,6 +438,7 @@ public class HTTPCallsUtils {
|
||||||
+ " -- " + httpMethod.getName()
|
+ " -- " + httpMethod.getName()
|
||||||
+ " " +url
|
+ " " +url
|
||||||
);
|
);
|
||||||
|
setContentType(httpMethod);
|
||||||
return httpMethod.getResponseBodyAsStream();
|
return httpMethod.getResponseBodyAsStream();
|
||||||
}
|
}
|
||||||
} catch (ConnectException e) {
|
} catch (ConnectException e) {
|
||||||
|
@ -491,8 +482,10 @@ public class HTTPCallsUtils {
|
||||||
logger.debug("ResponseBody is empty (this may be not an error since we just performed a DELETE call)");
|
logger.debug("ResponseBody is empty (this may be not an error since we just performed a DELETE call)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
setContentType(httpMethod);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
setContentType(httpMethod);
|
||||||
logger.info("("+lastHttpStatus+") " + httpMethod.getStatusText() + " -- " + url );
|
logger.info("("+lastHttpStatus+") " + httpMethod.getStatusText() + " -- " + url );
|
||||||
logger.info("Response: '"+response+"'" );
|
logger.info("Response: '"+response+"'" );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue