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:
Francesco Mangiacrapa 2016-04-05 13:24:15 +00:00
parent ae7ed9ce32
commit 5fa9dcfaa9
3 changed files with 67 additions and 86 deletions

View File

@ -56,6 +56,9 @@ public class UriResolverRewriteFilter implements Filter{
String path = request.getServletPath();
String scope = getScope(path);
String newURI = SERVLET_GEONETWORK + "?" + GeonetworkResolver.SCOPE + "=" + scope;
if(queryString!=null && !queryString.isEmpty())
newURI+="&"+queryString;
logger.debug("forward "+newURI);
req.getRequestDispatcher(newURI).forward(req, res);
}else{

View File

@ -40,6 +40,10 @@ import org.slf4j.LoggerFactory;
*/
public class GeonetworkResolver extends HttpServlet{
/**
*
*/
public static final String APPLICATION_XML = "application/xml";
/**
*
*/
@ -89,17 +93,20 @@ public class GeonetworkResolver extends HttpServlet{
return;
}
logger.info("SCOPE is: " + scope);
logger.info("SCOPE: " + scope +", Query String: " + req.getQueryString());
try {
ServerParameters geonetworkParams = getGeonetworkCachedServerParameters(scope);
HTTPCallsUtils httpUtils = new HTTPCallsUtils();
boolean authorized = GNAuthentication.login(httpUtils, geonetworkParams.getUrl(), geonetworkParams.getUser(), geonetworkParams.getPassword());
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);
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);
OutputStream out = resp.getOutputStream();
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());
return scope.replaceAll("_", "/");
/**
* Purge scope from query string.
*
* @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)
@ -157,6 +185,8 @@ public class GeonetworkResolver extends HttpServlet{
String gnCSWlURL = geonetworkParams.getUrl()+"/"+CSW_SERVER;
logger.info("Sending CSW request to URL: "+gnCSWlURL);
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);
OutputStream out = resp.getOutputStream();
try{
@ -283,57 +313,12 @@ public class GeonetworkResolver extends HttpServlet{
}
/**
* Gets the request url.
* The main method.
*
* @param req the req
* @return the request url
* @param args the arguments
*/
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) {
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);
// urlRedirect(req, resp, gisPortletUrl);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(purgeScopeFromQueryString("/gcube/devsec/devVRE", "scope=/gcube/devsec/devVRE&version=2.0.2&request=GetCapabilities&service=CSW"));
}
}*/
}

View File

@ -15,7 +15,9 @@ import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
@ -60,6 +62,7 @@ public class HTTPCallsUtils {
private int lastHttpStatus;
private boolean ignoreResponseContentOnSuccess = false;
private String lastContentType;
/**
* Instantiates a new HTTP utils.
@ -132,6 +135,7 @@ public class HTTPCallsUtils {
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
lastHttpStatus = client.executeMethod(httpMethod);
if(lastHttpStatus == HttpStatus.SC_OK) {
setContentType(httpMethod);
InputStream is = httpMethod.getResponseBodyAsStream();
String response = IOUtils.toString(is);
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.
* @return The HTTP response as a String if the HTTP response code was 200 (OK).
* @throws MalformedURLException the malformed url exception
* @param entity the entity
*/
public InputStream getBody(String url) throws MalformedURLException {
GetMethod httpMethod = null;
try {
setAuth(client, url, username, pw);
httpMethod = new GetMethod(url);
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
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();
private void setContentType(HttpMethod method) {
final Header contentTypeHeader=method.getResponseHeader("Content-Type");
lastContentType =contentTypeHeader == null ? null : contentTypeHeader.getValue();
}
return null;
/**
* @return the lastContentType
*/
public String getLastContentType() {
return lastContentType;
}
//==========================================================================
//=== PUT
//==========================================================================
/**
* Gets the client.
*
@ -381,11 +369,12 @@ public class HTTPCallsUtils {
if(ignoreResponseContentOnSuccess)
return "";
String response = IOUtils.toString(httpMethod.getResponseBodyAsStream());
setContentType(httpMethod);
return response;
default:
String badresponse = IOUtils.toString(httpMethod.getResponseBodyAsStream());
String message = getGeoNetworkErrorMessage(badresponse);
setContentType(httpMethod);
logger.warn("Bad response: "+lastHttpStatus
+ " " + httpMethod.getStatusText()
+ " -- " + httpMethod.getName()
@ -441,6 +430,7 @@ public class HTTPCallsUtils {
case HttpURLConnection.HTTP_ACCEPTED:
if(logger.isDebugEnabled())
logger.debug("HTTP "+ httpMethod.getStatusText() + " <-- " + url);
setContentType(httpMethod);
return httpMethod.getResponseBodyAsStream();
default:
logger.warn("Bad response: "+lastHttpStatus
@ -448,6 +438,7 @@ public class HTTPCallsUtils {
+ " -- " + httpMethod.getName()
+ " " +url
);
setContentType(httpMethod);
return httpMethod.getResponseBodyAsStream();
}
} 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)");
}
}
setContentType(httpMethod);
return true;
} else {
setContentType(httpMethod);
logger.info("("+lastHttpStatus+") " + httpMethod.getStatusText() + " -- " + url );
logger.info("Response: '"+response+"'" );
}