4953: Uri Resolver: remove jumps of scope from Readers (for Generic and Runtime resource)

Task-Url: https://support.d4science.org/issues/4953

All jumps has been removed
When is need to set scope in order to discovery a certain resource in a specific scope in the end the original scope is restored

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@131174 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-09-07 13:07:46 +00:00
parent 406af9e14d
commit e082f323c7
10 changed files with 316 additions and 78 deletions

View File

@ -11,18 +11,18 @@ import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.gcube.common.resources.gcore.utils.XPathHelper;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.Query;
import org.gcube.resources.discovery.client.queries.impl.QueryBox;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
/**
* The Class ApplicationProfileReader.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Oct 13, 2014
*
* Sep 6, 2016
*/
public class ApplicationProfileReader {
@ -42,9 +42,10 @@ public class ApplicationProfileReader {
/**
* Instantiates a new application profile reader.
*
* @param scope - the scope to be searched
* @param genericResource - the name of generic resource
* @param secondaryType the secondary type
* @param portletClassName - the AppId of generic resource
*/
public ApplicationProfileReader(String scope, String secondaryType, String portletClassName) {
@ -54,13 +55,18 @@ public class ApplicationProfileReader {
this.applicationProfile = readProfileFromInfrastrucure();
}
/**
* Gets the application profile.
*
* @return the application profile
*/
public ApplicationProfile getApplicationProfile() {
return applicationProfile;
}
/**
* this method looks up the applicationProfile profile among the ones available in the infrastructure
* @param portletClassName your servlet class name will be used ad unique identifier for your applicationProfile
* this method looks up the applicationProfile profile among the ones available in the infrastructure.
*
* @return the applicationProfile profile
*/
private ApplicationProfile readProfileFromInfrastrucure() {
@ -68,21 +74,16 @@ public class ApplicationProfileReader {
ApplicationProfile appProf = new ApplicationProfile();
String queryString = GcubeQuery.getGcubeGenericQueryString(secondaryType, appId);
logger.info("Trying to fetch applicationProfile profile from the infrastructure for " + secondaryType + " scope: " + scope);
logger.info("Trying to fetch ApplicationProfile in the scope: "+scope+", SecondaryType: " + secondaryType + ", AppId: " + appId);
try {
String infra = ScopeUtil.getInfrastructureNameFromScope(this.scope);
ScopeProvider.instance.set(infra);
logger.info("scope provider set instance: "+infra);
Query q = new QueryBox(queryString);
DiscoveryClient<String> client = client();
List<String> appProfile = client.submit(q);
if (appProfile == null || appProfile.size() == 0)
throw new ApplicationProfileNotFoundException("Your applicationProfile is not registered in the infrastructure");
throw new ApplicationProfileNotFoundException("ApplicationProfile with SecondaryType: " + secondaryType + ", AppId: " + appId +" is not registered in the scope: "+scope);
else {
String elem = appProfile.get(0);
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
@ -116,7 +117,6 @@ public class ApplicationProfileReader {
logger.warn("Null or empty <ThumbnailURL> element in <Body>" + appProf.getName());
}
currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Scope/text()");
if (currValue != null && currValue.size() > 0) {
@ -168,13 +168,13 @@ public class ApplicationProfileReader {
} catch (Exception e) {
logger.error("Error while trying to fetch applicationProfile profile from the infrastructure", e);
return null;
}finally{
logger.info("Resetting scope provider...");
ScopeProvider.instance.reset();
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();

View File

@ -1,5 +1,5 @@
/**
*
*
*/
package org.gcube.datatransfer.resolver.applicationprofile;
@ -15,29 +15,29 @@ public class ScopeUtil {
private static final String SCOPE_SEPARATOR = "/";
public static final Logger logger = LoggerFactory.getLogger(ScopeUtil.class);
public static String getInfrastructureNameFromScope(String scope) throws Exception{
if(scope==null || scope.isEmpty()){
throw new Exception("Scope is null or empty");
}
if(!scope.startsWith(SCOPE_SEPARATOR)){
logger.warn("Input scope: "+scope+" not have / is a really scope?");
scope = SCOPE_SEPARATOR+scope;
logger.warn("Tentative as scope: "+scope);
}
String[] splitScope = scope.split(SCOPE_SEPARATOR);
String rootScope = SCOPE_SEPARATOR + splitScope[1];
if(rootScope.length()<2){
throw new Exception("Infrastructure name not found in "+scope);
}
return rootScope;
}
// public static String getInfrastructureNameFromScope(String scope) throws Exception{
//
// if(scope==null || scope.isEmpty()){
// throw new Exception("Scope is null or empty");
// }
//
// if(!scope.startsWith(SCOPE_SEPARATOR)){
// logger.warn("Input scope: "+scope+" not have / is a really scope?");
// scope = SCOPE_SEPARATOR+scope;
// logger.warn("Tentative as scope: "+scope);
// }
//
// String[] splitScope = scope.split(SCOPE_SEPARATOR);
//
// String rootScope = SCOPE_SEPARATOR + splitScope[1];
//
// if(rootScope.length()<2){
// throw new Exception("Infrastructure name not found in "+scope);
// }
//
// return rootScope;
//
// }
}

View File

@ -40,13 +40,15 @@ public class GeoRuntimeReader {
{
ServerParameters parameters = new ServerParameters();
String originalScope = null;
try{
boolean isGeoserver = geoservice.equals(GEO_SERVICE.GEOSERVER);
String resourceName = isGeoserver ? GEOSERVER_RESOURCE_NAME : GEONETWORK_RESOURCE_NAME;
originalScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
SimpleQuery query = queryFor(ServiceEndpoint.class);
@ -91,8 +93,13 @@ public class GeoRuntimeReader {
}catch (Exception e) {
logger.error("Sorry, an error occurred on reading parameters in Runtime Resources",e);
}finally{
logger.info("Resetting scope provider...");
ScopeProvider.instance.reset();
if(originalScope!=null && !originalScope.isEmpty()){
ScopeProvider.instance.set(originalScope);
logger.info("scope provider setted to orginal scope: "+originalScope);
}else{
ScopeProvider.instance.reset();
logger.info("scope provider reset");
}
}
return parameters;

View File

@ -68,7 +68,9 @@ public class GeonetworkInstance {
public GeonetworkInstance(String scope, boolean authenticate, LoginLevel level) throws GeonetworkInstanceException {
logger.debug("Instancing GeonetworkInstance with scope: "+scope + ", authenticate: "+authenticate +", login level: "+level);
this.scope = scope;
String originalScope = null;
try {
originalScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
logger.info("setting scope "+scope);
createInstanceGeonetworkPublisher(authenticate, level);
@ -77,8 +79,13 @@ public class GeonetworkInstance {
logger.warn(message, e);
throw new GeonetworkInstanceException(message);
}finally{
logger.info("resetting scope");
ScopeProvider.instance.reset();
if(originalScope!=null && !originalScope.isEmpty()){
ScopeProvider.instance.set(originalScope);
logger.info("scope provider setted to orginal scope: "+originalScope);
}else{
ScopeProvider.instance.reset();
logger.info("scope provider reset");
}
}
}

View File

@ -19,7 +19,6 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader;
import org.gcube.datatransfer.resolver.applicationprofile.ScopeUtil;
import org.gcube.datatransfer.resolver.gis.GeoRuntimeReader.GEO_SERVICE;
import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter.GeonetworkLoginLevel;
import org.gcube.datatransfer.resolver.gis.entity.ServerParameters;
@ -155,17 +154,16 @@ public class GisResolver extends HttpServlet{
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);
String gisViewerAppHostname = cachedGisViewerApplHostname.get(scope);
if(gisViewerAppHostname==null){
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());
ApplicationProfileReader reader = new ApplicationProfileReader(scope, gisViewerAppPropertyReader.getGenericResource(), gisViewerAppPropertyReader.getAppId());
String url = reader.getApplicationProfile().getUrl();
cachedGisViewerApplHostname.put(infra, url);
cachedGisViewerApplHostname.put(scope, url);
logger.info("Updated GisViewerApplication cache! Scope "+scope+" linking "+url);
return url;
}else
@ -363,7 +361,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";
@ -388,5 +386,5 @@ public class GisResolver extends HttpServlet{
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}
}

View File

@ -35,6 +35,7 @@ import org.gcube.datatransfer.resolver.gis.GeonetworkInstance;
import org.gcube.datatransfer.resolver.gis.GeonetworkServiceInterface;
import org.gcube.datatransfer.resolver.gis.entity.ServerParameters;
import org.gcube.datatransfer.resolver.gis.exception.IllegalArgumentException;
import org.gcube.datatransfer.resolver.gis.geonetwork.HTTPCallsUtils.HttpResponse;
import org.gcube.datatransfer.resolver.gis.util.GetResponseRecordFilter;
import org.gcube.spatial.data.geonetwork.configuration.Configuration;
import org.gcube.spatial.data.geonetwork.model.Account;
@ -153,19 +154,34 @@ public class GeonetworkResolver extends HttpServlet{
logger.trace("Purged query string from "+remainValue+" is: "+newQueryString);
String gnGetlURL = newQueryString==null || newQueryString.isEmpty()? baseURL : baseURL+"?"+newQueryString;
logger.info("Sending get request to URL: "+gnGetlURL);
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{
int bytes = IOUtils.copy(in, out);
if(bytes==0)
logger.warn("ResponseBody is empty, returning empty resp");
}catch(Exception e){
logger.error("Error on copy response:", e);
}finally{
IOUtils.closeQuietly(in);
HttpResponse response = httpUtils.get(gnGetlURL);
switch(response.getStatus()){
case HttpServletResponse.SC_OK:
logger.info("Response return Content-Type: "+httpUtils.getLastContentType());
resp.setContentType(httpUtils.getLastContentType());
InputStream in = IOUtils.toInputStream(response.getResponse());
OutputStream out = resp.getOutputStream();
try{
int bytes = IOUtils.copy(in, out);
if(bytes==0)
logger.warn("ResponseBody is empty, returning empty resp");
}catch(Exception e){
logger.error("Error on copy response:", e);
}finally{
IOUtils.closeQuietly(in);
}
break;
case HttpServletResponse.SC_FORBIDDEN:
sendError(resp, response.getStatus(), "Sorry, you are not authorized to perform this request");
break;
default:
sendError(resp, response.getStatus(), "Sorry, an error occurred on resolving geonetwork request with scope "+scopeValue);
}
} catch (IllegalArgumentException e){
@ -252,6 +268,7 @@ public class GeonetworkResolver extends HttpServlet{
String noAuthentication = req.getParameter(PARAMETER_NO_AUTHENTICATION);
boolean filterPublicMetadataIDsB = false;
boolean noAuthenticationB = false;
String originalScope = null;
if (scope == null || scope.equals("")) {
logger.debug("Scope not found");
@ -279,6 +296,8 @@ public class GeonetworkResolver extends HttpServlet{
GeonetworkServiceInterface gntwAccess = new GeonetworkAccessParameter(scope, null);
GeonetworkInstance gnInstance = gntwAccess.getGeonetworkInstance();
originalScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
logger.info("set scope provider "+scope);
Configuration config = gnInstance.getGeonetworkPublisher().getConfiguration();
@ -380,8 +399,13 @@ public class GeonetworkResolver extends HttpServlet{
sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error);
return;
}finally{
ScopeProvider.instance.reset();
logger.info("scope provider reset");
if(originalScope!=null && !originalScope.isEmpty()){
ScopeProvider.instance.set(originalScope);
logger.info("scope provider setted to orginal scope: "+originalScope);
}else{
ScopeProvider.instance.reset();
logger.info("scope provider reset");
}
}
}

View File

@ -130,7 +130,7 @@ public class HTTPCallsUtils {
* @return The HTTP response as a String if the HTTP response code was 200 (OK).
* @throws MalformedURLException the malformed url exception
*/
public String get(String url) throws MalformedURLException {
public HttpResponse get(String url) throws MalformedURLException {
GetMethod httpMethod = null;
try {
@ -146,7 +146,7 @@ public class HTTPCallsUtils {
logger.warn("ResponseBody is empty");
return null;
} else {
return response;
return new HttpResponse(HttpStatus.SC_OK, response);
}
} else {
logger.info("("+lastHttpStatus+") " + HttpStatus.getStatusText(lastHttpStatus) + " -- " + url );
@ -160,13 +160,13 @@ public class HTTPCallsUtils {
httpMethod.releaseConnection();
}
return null;
return new HttpResponse(lastHttpStatus, null);
}
/**
* Show content type.
*
* @param entity the entity
* @param method the new content type
*/
private void setContentType(HttpMethod method) {
final Header contentTypeHeader=method.getResponseHeader("Content-Type");
@ -174,6 +174,8 @@ public class HTTPCallsUtils {
}
/**
* Gets the last content type.
*
* @return the lastContentType
*/
public String getLastContentType() {
@ -342,6 +344,7 @@ public class HTTPCallsUtils {
*
* @param url The URL where to connect to.
* @param requestEntity The request to be sent.
* @param parameterMap the parameter map
* @return The HTTP response as a String if the HTTP response code was 200 (OK).
* the HTTP response or <TT>null</TT> on errors.
*/
@ -667,4 +670,97 @@ public class HTTPCallsUtils {
}
}
/**
* The Class HttpResponse.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 7, 2016
*/
public class HttpResponse {
int status;
String response;
/**
* Instantiates a new http response.
*/
public HttpResponse() {
// TODO Auto-generated constructor stub
}
/**
* Instantiates a new http response.
*
* @param status the status
* @param response the response
*/
public HttpResponse(int status, String response) {
super();
this.status = status;
this.response = response;
}
/**
* Gets the status.
*
* @return the status
*/
public int getStatus() {
return status;
}
/**
* Gets the response.
*
* @return the response
*/
public String getResponse() {
return response;
}
/**
* Sets the status.
*
* @param status the status to set
*/
public void setStatus(int status) {
this.status = status;
}
/**
* Sets the response.
*
* @param response the response to set
*/
public void setResponse(String response) {
this.response = response;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("HttpResponse [status=");
builder.append(status);
builder.append(", response=");
builder.append(response);
builder.append("]");
return builder.toString();
}
}
}

View File

@ -1,6 +1,15 @@
<html>
<body>
<h2>The URI resolver </h2>
<p>See wiki page: <a href="https://gcube.wiki.gcube-system.org/gcube/index.php/URI_Resolver">URI Resolver gCube Wiki</a></p>
</body>
</html>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; url=https://dev4.d4science.org/ckan">
<script type="text/javascript">
window.location.href = "https://dev4.d4science.org/ckan"
</script>
<title>Page Redirection</title>
</head>
<body>
<!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
If you are not redirected automatically, follow the <a href='https://dev4.d4science.org/ckan'>link to example</a>
</body>
</html>

View File

@ -0,0 +1,58 @@
import it.geosolutions.geonetwork.util.GNSearchRequest;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.spatial.data.geonetwork.GeoNetwork;
import org.gcube.spatial.data.geonetwork.GeoNetworkReader;
import org.gcube.spatial.data.geonetwork.LoginLevel;
import org.junit.Test;
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Aug 31, 2016
*/
public class GeonetworkQueryTest {
private String[] scopes = {"/gcube/devsec/devVRE"};
private String[] scopesProd = {"/d4science.research-infrastructures.eu"};
@Test
public void getCount() throws Exception{
try{
for(String scope:scopes){
ScopeProvider.instance.set(scope);
GeoNetworkReader reader=GeoNetwork.get();
final GNSearchRequest req=new GNSearchRequest();
// req.addParam("keyword", "Thredds");
req.addParam(GNSearchRequest.Param.any,"");
int publicCount=reader.query(req).getCount();
reader.login(LoginLevel.CKAN);
int totalCount=reader.query(req).getCount();
System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
}
}catch(Exception e){
e.printStackTrace();
}
}
// @Test
public void getCountProd() throws Exception{
try{
for(String scope:scopesProd){
ScopeProvider.instance.set(scope);
GeoNetworkReader reader=GeoNetwork.get();
final GNSearchRequest req=new GNSearchRequest();
// req.addParam("keyword", "Thredds");
req.addParam(GNSearchRequest.Param.any,"Thredds");
int publicCount=reader.query(req).getCount();
reader.login(LoginLevel.SCOPE);
int totalCount=reader.query(req).getCount();
System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,39 @@
/**
*
*/
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 7, 2016
*/
public class GisResolverTest {
// public void resolve(){
// 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);
// System.out.println("Final url is: " + wmsRequest);
// wmsRequest = URLEncoder.encode(wmsRequest, UTF_8);
// System.out.println("Encoded WMS request is: " + wmsRequest);
// String gisPortletUrl = gisResolver.getGisViewerApplicationURL(scope);
// System.out.println("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();
// }
// }
}