From 0bb83e873613fe1a452299a9c75f6610c682dcb1 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Mon, 3 Jul 2017 14:25:14 +0000 Subject: [PATCH] Updated pom version at 1.13 Completed Feature #9108 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@150696 82a268e6-3cf1-43bd-a215-b396298e98cf --- distro/changelog.xml | 5 + pom.xml | 2 +- .../resolver/UriResolverRewriteFilter.java | 2 +- .../ApplicationProfileReader.java | 90 ++++++--------- .../resolver/gis/GisResolver.java | 2 +- src/test/java/GcoreEndpointReader.java | 104 ++++++++++++++++++ src/test/java/GeonetworkQueryTest.java | 45 +++++++- 7 files changed, 186 insertions(+), 64 deletions(-) create mode 100644 src/test/java/GcoreEndpointReader.java diff --git a/distro/changelog.xml b/distro/changelog.xml index 79a013a..c56aeea 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -86,4 +86,9 @@ [Feature #8494] Remove GeoServices query from URI-Resolver + + [Feature #9108] Edit the GenericResource (ApplicationProfile-Gis Viewer Application) + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 4c383d9..e5feb6e 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.gcube.data.transfer uri-resolver - 1.12.0-SNAPSHOT + 1.13.0-SNAPSHOT war The URI Resolver is an HTTP URI resolver implemented as an HTTP servlet which gives access trough HTTP to different protocols URIs. diff --git a/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java b/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java index 5765c38..00d8605 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java @@ -183,7 +183,7 @@ public class UriResolverRewriteFilter implements Filter{ } } else { - logger.debug("is NOT a SMP public uri by ID, doFilter Request"); + logger.debug("It is NOT a SMP public uri by ID, doFilter Request"); chain.doFilter(multiReadRequest, response); } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java b/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java index 9d3e8b3..90fd0e2 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java +++ b/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java @@ -8,7 +8,6 @@ import java.util.List; import javax.xml.parsers.DocumentBuilder; 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; @@ -28,7 +27,11 @@ import org.xml.sax.InputSource; */ public class ApplicationProfileReader { - protected static final String RESOURCE_PROFILE_BODY_END_POINT_URL_TEXT = "/Resource/Profile/Body/EndPoint/URL/text()"; + /** + * + */ + private static final String RESOURCE_PROFILE_BODY_END_POINT_URL = "/Resource/Profile/Body/EndPoint/URL"; + protected static final String RESOURCE_PROFILE_BODY_END_POINT_URL_TEXT = RESOURCE_PROFILE_BODY_END_POINT_URL+"/text()"; protected static final String RESOURCE_PROFILE_BODY_END_POINT_SCOPE_TEXT = "/Resource/Profile/Body/EndPoint/Scope/text()"; protected static final String RESOURCE_PROFILE_BODY_TEXT = "/Resource/Profile/Body/text()"; protected static final String RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT = "/Resource/Profile/Body/ThumbnailURL/text()"; @@ -85,25 +88,28 @@ public class ApplicationProfileReader { originalScope = ScopeProvider.instance.get(); String discoveryScope = useRootScope?ScopeUtil.getInfrastructureNameFromScope(scope):scope; ScopeProvider.instance.set(discoveryScope); - logger.info("Trying to fetch ApplicationProfile in the infra scope: "+discoveryScope+", SecondaryType: " + secondaryType + ", AppId: " + appId); + logger.info("Trying to fetch Generic Resource in the scope: "+discoveryScope+", SecondaryType: " + secondaryType + ", AppId: " + appId); Query q = new QueryBox(queryString); DiscoveryClient client = client(); List appProfile = client.submit(q); if (appProfile == null || appProfile.size() == 0) - throw new ApplicationProfileNotFoundException("ApplicationProfile with SecondaryType: " + secondaryType + ", AppId: " + appId +" is not registered in the infra scope: "+discoveryScope); + throw new ApplicationProfileNotFoundException("Generic Resource with SecondaryType: " + secondaryType + ", AppId: " + appId +" is not registered in the scope: "+discoveryScope); else { String elem = appProfile.get(0); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement(); XPathHelper helper = new XPathHelper(node); + //set scope + appProf.setScope(discoveryScope); + List currValue = null; currValue = helper.evaluate(RESOURCE_PROFILE_NAME_TEXT); if (currValue != null && currValue.size() > 0) { appProf.setName(currValue.get(0)); } - else throw new ApplicationProfileNotFoundException("Your applicationProfile NAME was not found in the profile"); + else throw new ApplicationProfileNotFoundException("Your ApplicationProfile NAME was not found in the profile"); currValue = helper.evaluate(RESOURCE_PROFILE_DESCRIPTION_TEXT); if (currValue != null && currValue.size() > 0) { @@ -115,7 +121,7 @@ public class ApplicationProfileReader { if (currValue != null && currValue.size() > 0) { appProf.setKey(currValue.get(0)); } - else throw new ApplicationProfileNotFoundException("Your applicationProfile ID n was not found in the profile, consider adding element in "); + else throw new ApplicationProfileNotFoundException("Your ApplicationProfile ID was not found in the profile, consider adding element in "); currValue = helper.evaluate(RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT); if (currValue != null && currValue.size() > 0) { @@ -125,50 +131,20 @@ public class ApplicationProfileReader { logger.warn("Null or empty element in " + appProf.getName()); } - currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Scope/text()"); + //currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Scope/text()"); + + //RETRIEVE URL + currValue = helper.evaluate(RESOURCE_PROFILE_BODY_END_POINT_URL_TEXT); if (currValue != null && currValue.size() > 0) { - List scopes = currValue; - String currentScope = scopes.get(0); - - int slashCount = StringUtils.countMatches(currentScope, "/"); - - if(slashCount < 3){//CASE not VRE - set session scope - logger.info("Scope "+ scope.toString() + " is not a VRE"); - - List listSessionScope = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Sessionscope/text()"); //get session scope of i+1-mo scope - - if(listSessionScope!=null && listSessionScope.size()>0){ //If sessions scope exists - - logger.trace("setting session scope "+ listSessionScope.get(0)); - appProf.setScope(listSessionScope.get(0)); - } - else{ - logger.trace("session scope not exists setting scope "+ scope.toString()); - appProf.setScope(scope.toString()); - } - } - else{ //CASE IS A VRE - logger.info("Scope "+ scope.toString() + " is a VRE"); - appProf.setScope(scope.toString()); - - } - - //RETRIEVE URL - currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/URL/text()"); - - if (currValue != null && currValue.size() > 0) { - String url = currValue.get(0); - // System.out.println("URL "+url); - if(url!=null) - appProf.setUrl(url); - else - throw new ApplicationProfileNotFoundException("Your applicationProfile URL was not found in the profile for Scope: " + scope.toString()); - } - else throw new ApplicationProfileNotFoundException("Your applicationProfile URL was not found in the profile for Scope: " + scope.toString()); - + String url = currValue.get(0); +// System.out.println("URL "+url); + if(url!=null) + appProf.setUrl(url); + else + throw new ApplicationProfileNotFoundException("Your ApplicationProfile URL was not found in the profile for Scope: " + scope.toString() +", consider adding element in "); } - else throw new ApplicationProfileNotFoundException("Your applicationProfile with scope "+scope.toString()+" was not found in the profile, consider adding element in "); + else throw new ApplicationProfileNotFoundException("ApplicationProfile with SecondaryType: " + secondaryType + ", AppId: " + appId +" in the scope: "+discoveryScope +" does not contain "+RESOURCE_PROFILE_BODY_END_POINT_URL +" property, please add it"); return appProf; } @@ -206,16 +182,16 @@ public class ApplicationProfileReader { return builder.toString(); } - /* - public static void main(String[] args) { - String portletClassName = "org.gcube.portlets.user.gisviewerapp.server.GisViewerAppServiceImpl"; - String scope ="/gcube"; - String secondaryType = "ApplicationProfile"; - ApplicationProfileReader reader = new ApplicationProfileReader(scope, secondaryType, portletClassName); - - System.out.println(reader); - - }*/ +// public static void main(String[] args) { +// +// String portletClassName = "org.gcube.portlets.user.gisviewerapp.server.GisViewerAppServiceImpl"; +// String scope ="/gcube/devNext/NextNext"; +// String secondaryType = "ApplicationProfile"; +// ApplicationProfileReader reader = new ApplicationProfileReader(scope, secondaryType, portletClassName, true); +// +// System.out.println(reader); +// +// } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java b/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java index 1c30740..3fd92f6 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java @@ -210,7 +210,7 @@ public class GisResolver extends HttpServlet{ if(gisViewerAppPropertyReader==null) resetGisViewerAppEndPoint(); - ApplicationProfileReader reader = new ApplicationProfileReader(scope, gisViewerAppPropertyReader.getGenericResource(), gisViewerAppPropertyReader.getAppId(), true); + ApplicationProfileReader reader = new ApplicationProfileReader(scope, gisViewerAppPropertyReader.getGenericResource(), gisViewerAppPropertyReader.getAppId(), false); String url = reader.getApplicationProfile().getUrl(); cachedGisViewerApplHostname.put(scope, url); logger.info("Updated GisViewerApplication cache! Scope "+scope+" linking "+url); diff --git a/src/test/java/GcoreEndpointReader.java b/src/test/java/GcoreEndpointReader.java new file mode 100644 index 0000000..29d4a83 --- /dev/null +++ b/src/test/java/GcoreEndpointReader.java @@ -0,0 +1,104 @@ +import static org.gcube.resources.discovery.icclient.ICFactory.client; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.io.Serializable; +import java.util.List; + +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 10, 2016 + */ +public class GcoreEndpointReader implements Serializable{ + + private static final long serialVersionUID = 7631710353375893823L; + private static final String ckanResource = "org.gcube.data.access.ckanconnector.CkanConnector"; + private static final String serviceName = "CkanConnector"; + private static final String serviceClass = "DataAccess"; + + private static final Logger logger = LoggerFactory.getLogger(GcoreEndpointReader.class); + private String ckanResourceEntyName; + + /** + * Instantiates a new gcore endpoint reader. + * + * @param scope the scope + * @throws Exception the exception + */ + public GcoreEndpointReader(String scope) throws Exception { + + String currentScope = ScopeProvider.instance.get(); + try{ + + logger.info("set scope "+scope); + ScopeProvider.instance.set(scope); + + SimpleQuery query = queryFor(GCoreEndpoint.class); + query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'",serviceClass)); + query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'"); + query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'",serviceName)); + query.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""+ckanResource+"\"]/text()"); + + logger.debug("submitting quey "+query.toString()); + + DiscoveryClient client = client(); + List endpoints = client.submit(query); + if (endpoints == null || endpoints.isEmpty()) throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+scope); + + + this.ckanResourceEntyName = endpoints.get(0); + if(ckanResourceEntyName==null) + throw new Exception("Endpoint:"+ckanResource+", is null for serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+scope); + + logger.info("found entyname "+ckanResourceEntyName+" for ckanResource: "+ckanResource); + + /*Group accessPoints = se.profile().endpoints(); + if(accessPoints.size()==0) throw new Exception("Endpoint in serviceName serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+scope +" not found"); + + Endpoint ep = accessPoints.iterator().next(); + + String epName = ep.name(); + + System.out.println(epName);*/ + + }catch(Exception e){ + String error = "An error occurred during GCoreEndpoint discovery, serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+scope +"."; + logger.error(error, e); + throw new Exception(error); + }finally{ + logger.info("scope provider reset"); + ScopeProvider.instance.set(currentScope); + } + } + + /** + * @return the ckanResourceEntyName + */ + public String getCkanResourceEntyName() { + + return ckanResourceEntyName; + } + + private static String scope = "/d4science.research-infrastructures.eu"; + + + public static void main(String[] args) { + + try { + + GcoreEndpointReader reader = new GcoreEndpointReader(scope); + } + catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/src/test/java/GeonetworkQueryTest.java b/src/test/java/GeonetworkQueryTest.java index 227d396..01b75fe 100644 --- a/src/test/java/GeonetworkQueryTest.java +++ b/src/test/java/GeonetworkQueryTest.java @@ -11,6 +11,7 @@ import org.gcube.spatial.data.geonetwork.configuration.Configuration; import org.gcube.spatial.data.geonetwork.model.Account; import org.gcube.spatial.data.geonetwork.model.Account.Type; import org.junit.Test; +import org.opengis.metadata.Metadata; /** * @@ -21,15 +22,21 @@ public class GeonetworkQueryTest { private static final int MAX = 10; + private static final String UUID = "8a878105-ef06-4b1f-843f-120fc525b22b"; + //private String[] scopes = {"/gcube/devNext/NextNext"}; - private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/SIASPA"}; + //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/SIASPA"}; //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/fisheriesandecosystematmii"}; //private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research"}; - //private String[] scopesProd = {"/d4science.research-infrastructures.eu"}; +// private String[] scopesProd = {"/d4science.research-infrastructures.eu"}; + + private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/FAO_TunaAtlas"}; + + //private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research/Blue-Datathon"}; private LoginLevel loginLevel = LoginLevel.CKAN; @@ -84,6 +91,38 @@ public class GeonetworkQueryTest { } } + //@Test + public void getLayerByUUID() throws Exception{ + try{ + for(String scope:scopesProd){ + ScopeProvider.instance.set(scope); + GeoNetworkPublisher reader=GeoNetwork.get(); + + Configuration config = reader.getConfiguration(); + Account account=config.getScopeConfiguration().getAccounts().get(accountType); + + //System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword()); + System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword()); + + Metadata meta = reader.getById(UUID); + + + try{ + String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword()); + System.out.println("Decrypted Password: "+decryptedPassword); + }catch(Exception e){ + System.out.println("ignoring exception during pwd decrypting"); + } + + System.out.println("SCOPE "+scope+" found meta "+meta); + + } + }catch(Exception e){ + System.err.println("Error on getting layer by UUID: "+UUID); + e.printStackTrace(); + } + } + //@Test public void getLayersBySearch() throws Exception{ @@ -92,8 +131,6 @@ public class GeonetworkQueryTest { ScopeProvider.instance.set(scope); GeoNetworkPublisher reader=GeoNetwork.get(); - - Configuration config = reader.getConfiguration(); Account account=config.getScopeConfiguration().getAccounts().get(accountType);