From 87c5fcf09e300d494c10e14aeed560d539a54a11 Mon Sep 17 00:00:00 2001 From: lucio Date: Tue, 7 Apr 2020 19:17:37 +0200 Subject: [PATCH] - Maps taken from AuthorizationEntry if not null --- .classpath | 1 - .project | 4 +- pom.xml | 23 +++++--- .../resources/discovery/icclient/Helper.java | 47 +++++----------- .../discovery/icclient/ICClient.java | 21 +++++++- .../discovery/icclient/ICFactory.java | 5 +- src/test/java/org/acme/APIClient.java | 54 ++++++++++++++++++- 7 files changed, 104 insertions(+), 51 deletions(-) diff --git a/.classpath b/.classpath index 502bd4a..b8c44f6 100644 --- a/.classpath +++ b/.classpath @@ -16,7 +16,6 @@ - diff --git a/.project b/.project index 55fc0f8..0f1b541 100644 --- a/.project +++ b/.project @@ -16,12 +16,12 @@ - org.eclipse.m2e.core.maven2Builder + org.eclipse.wst.validation.validationbuilder - org.eclipse.wst.validation.validationbuilder + org.eclipse.m2e.core.maven2Builder diff --git a/pom.xml b/pom.xml index 4eb76c6..f2affa1 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - 4.0.0 @@ -9,15 +10,15 @@ org.gcube.resources.discovery ic-client - 1.0.5-SNAPSHOT + 1.1.0-SNAPSHOT Information Collector Client Client API for the Information Collector service - - scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git - scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git - https://code-repo.d4science.org/gCubeSystem/${project.artifactId} - + + scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git + scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git + https://code-repo.d4science.org/gCubeSystem/${project.artifactId} + distro @@ -26,12 +27,18 @@ + + org.gcube.common + authorization-client + [2.0.0-SNAPSHOT,3.0.0-SNAPSHOT) + + org.gcube.core common-scope [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) - + org.gcube.resources.discovery discovery-client diff --git a/src/main/java/org/gcube/resources/discovery/icclient/Helper.java b/src/main/java/org/gcube/resources/discovery/icclient/Helper.java index e575849..5b68627 100644 --- a/src/main/java/org/gcube/resources/discovery/icclient/Helper.java +++ b/src/main/java/org/gcube/resources/discovery/icclient/Helper.java @@ -1,6 +1,6 @@ package org.gcube.resources.discovery.icclient; -import static org.gcube.common.scope.impl.ScopeBean.Type.*; +import static org.gcube.common.scope.impl.ScopeBean.Type.VRE; import java.util.ArrayList; import java.util.HashMap; @@ -24,9 +24,8 @@ public class Helper { public static String queryAddAuthenticationControl(String expression) throws MalformedQueryException { - String scope = ScopeProvider.instance.get(); - - if (scope==null || !new ScopeBean(scope).is(VRE)) + String context = ScopeProvider.instance.get(); + if (context==null || !new ScopeBean(context).is(VRE)) return expression; int wherePathIndex=0; @@ -41,10 +40,9 @@ public class Helper { HashMap varReplacementMap = new HashMap(); - String forPropertiesString=" *VAR* in *COLLECTION*/Data "; ///child::*[local-name()='Scope'] String forString=" *VAR* in *COLLECTION*/Scopes "; - String authString=" (functx:is-value-in-sequence('"+scope+"',*VAR*/child::*[local-name()='Scope']/text()) or functx:is-value-in-sequence('"+scope.substring(0,scope.lastIndexOf("/"))+"',*VAR*/child::*[local-name()='Scope']/text())) "; - String authStringNormal=" (functx:is-value-in-sequence('"+scope+"',*VAR*/child::*[local-name()='Scope']/text())) "; + String authString=" (functx:is-value-in-sequence('"+context+"',*VAR*/child::*[local-name()='Scope']/text()) or functx:is-value-in-sequence('"+context.substring(0,context.lastIndexOf("/"))+"',*VAR*/child::*[local-name()='Scope']/text())) "; + String authStringNormal=" (functx:is-value-in-sequence('"+context+"',*VAR*/child::*[local-name()='Scope']/text())) "; String queryFiltered; List collInsert= new ArrayList(); @@ -57,7 +55,6 @@ public class Helper { Pattern collectionPattern= Pattern.compile("[^\\s]*\\s*in\\s*collection\\s*[^\\s,]*"); Pattern varPattern= Pattern.compile("[^\\s]*"); Pattern resourcePattern= Pattern.compile("\\scollection\\s*\\([^\\)]*.*/Resource", Pattern.DOTALL); - Pattern propertiesPattern=Pattern.compile("\\scollection\\s*\\([^\\)]*.*/Document", Pattern.DOTALL); Matcher varMat; Matcher resourceMat; Matcher collMat=collectionPattern.matcher(expression); @@ -74,38 +71,19 @@ public class Helper { log.warn("error parsing collection statement"); } varMat= varPattern.matcher(temp); - boolean propBool=false; - if (temp.contains("/Properties")) - { - resourceMat= propertiesPattern.matcher(temp); - propBool=true; - } - else resourceMat=resourcePattern.matcher(temp); + resourceMat=resourcePattern.matcher(temp); varMat.lookingAt(); resourceMat.find(); String tempPath=""; try{ tempPath= temp.substring(resourceMat.end()); + String resourceMatString= resourceMat.group(); + String oldVar = varMat.group(); + String newVar = "$entry"+collInsert.size()+"ValueAuth"; + forStringTemp=forString.replace("*VAR*",newVar ).replace("*COLLECTION*",resourceMatString ); + collInsert.add(resourceMatString.contains("/Profiles/RunningInstance") || resourceMatString.contains("/Profiles/GHN") || resourceMatString.contains("/Profiles/Service")); + varReplacementMap.put(oldVar, newVar+"/.."+tempPath); - if (propBool) - { - String resourceMatString= resourceMat.group(); - forStringTemp=forPropertiesString.replace("*VAR*","$entry"+collInsert.size()+"ValueAuth" ).replace("*COLLECTION*", resourceMatString ); - String oldVar = varMat.group(); - String newVar = "$entry"+collInsert.size()+"ValueAuth"; - collInsert.add(true); - - varReplacementMap.put(oldVar, newVar+"/.."+tempPath); - } - else - { - String resourceMatString= resourceMat.group(); - String oldVar = varMat.group(); - String newVar = "$entry"+collInsert.size()+"ValueAuth"; - forStringTemp=forString.replace("*VAR*",newVar ).replace("*COLLECTION*",resourceMatString ); - collInsert.add(resourceMatString.contains("/Profiles/RunningInstance") || resourceMatString.contains("/Profiles/GHN") || resourceMatString.contains("/Profiles/Service")); - varReplacementMap.put(oldVar, newVar+"/.."+tempPath); - } }catch(IllegalStateException e){ log.debug("error parsing statement");} expression=expression.substring(0, collIndexStart)+ forStringTemp +expression.substring(collIndexEnd); collMat=collectionPattern.matcher(expression); @@ -198,7 +176,6 @@ public class Helper { queryFiltered = queryFiltered.replaceAll("text\\(\\) ne ","text() != "); log.info("submitting filtered query: {}",queryFiltered); - return queryFiltered; } diff --git a/src/main/java/org/gcube/resources/discovery/icclient/ICClient.java b/src/main/java/org/gcube/resources/discovery/icclient/ICClient.java index 214a7b0..3cb60a3 100644 --- a/src/main/java/org/gcube/resources/discovery/icclient/ICClient.java +++ b/src/main/java/org/gcube/resources/discovery/icclient/ICClient.java @@ -1,5 +1,6 @@ package org.gcube.resources.discovery.icclient; +import static org.gcube.common.authorization.client.Constants.authorizationService; import static org.gcube.common.clients.stubs.jaxws.StubFactory.*; import static org.gcube.resources.discovery.icclient.stubs.CollectorConstants.*; @@ -11,7 +12,11 @@ import java.util.regex.Pattern; import javax.xml.ws.soap.SOAPFaultException; +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.clients.stubs.jaxws.JAXWSUtils; +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ServiceMap; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.api.DiscoveryException; @@ -70,8 +75,20 @@ public class ICClient implements DiscoveryClient { //helper private CollectorStub getStub() { - - ServiceMap serviceMap = ServiceMap.instance; + + ServiceMap serviceMap = null; + + if (SecurityTokenProvider.instance.get()!=null) { + try { + AuthorizationEntry entry = authorizationService().get(SecurityTokenProvider.instance.get()); + serviceMap = entry.getMap(); + if (serviceMap==null) ScopeProvider.instance.set(entry.getContext()); + } catch (Throwable e) { + throw new IllegalArgumentException(e); + } + } + + if (serviceMap ==null) serviceMap = ServiceMap.instance; String address = serviceMap.endpoint(localname); log.info("connecting to {}",address); if (serviceMap.version().equals("2.0.0")) diff --git a/src/main/java/org/gcube/resources/discovery/icclient/ICFactory.java b/src/main/java/org/gcube/resources/discovery/icclient/ICFactory.java index 6cebd3a..3d27f79 100644 --- a/src/main/java/org/gcube/resources/discovery/icclient/ICFactory.java +++ b/src/main/java/org/gcube/resources/discovery/icclient/ICFactory.java @@ -9,6 +9,7 @@ import java.util.Map; import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.GenericResource; import org.gcube.common.resources.gcore.HostingNode; +import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceInstance; import org.gcube.common.resources.gcore.Software; @@ -42,7 +43,9 @@ public class ICFactory { // registers parameters for known queries static { - + + register(Resource.class, + params().add(ns, nsDeclaration).add(range, format(profile_range, "")).build()); register(GenericResource.class, params().add(ns, nsDeclaration).add(range, format(profile_range, "GenericResource")).build()); register(ServiceEndpoint.class, diff --git a/src/test/java/org/acme/APIClient.java b/src/test/java/org/acme/APIClient.java index 957e159..07c230a 100644 --- a/src/test/java/org/acme/APIClient.java +++ b/src/test/java/org/acme/APIClient.java @@ -1,8 +1,11 @@ package org.acme; -import static java.lang.String.*; -import static org.gcube.resources.discovery.icclient.ICFactory.*; +import static java.lang.String.format; +import static org.gcube.resources.discovery.icclient.ICFactory.client; +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; +import java.io.StringReader; import java.net.URI; import java.util.List; @@ -12,11 +15,16 @@ import javax.xml.bind.annotation.XmlRootElement; import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.GenericResource; +import org.gcube.common.resources.gcore.HostingNode; +import org.gcube.common.resources.gcore.Resource; +import org.gcube.common.resources.gcore.Resources; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceInstance; +import org.gcube.common.resources.gcore.Software; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.api.ResultParser; import org.gcube.resources.discovery.client.queries.api.Query; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.client.queries.impl.XQuery; @@ -149,6 +157,48 @@ public class APIClient { } + @Test + public void getResources() { + + XQuery query = ICFactory.queryFor(Resource.class); + query.addCondition(String.format("$resource/ID/text() eq '%s'","bdaccb35-7f27-45a6-8ca9-11d467cb9233")); + query.setResult("{$resource/Type}{$resource}"); + DiscoveryClient client = ICFactory.clientWith(new ResourceParser()); + List resources = client.submit(query); + resources.forEach(res-> System.out.println(res.id())); + + } + + public static class ResourceParser implements ResultParser{ + + private enum ResourceType { + GenericResource(GenericResource.class), + RuntimeResource(ServiceEndpoint.class), + RunningInstance(GCoreEndpoint.class), + Service(Software.class), + GHN(HostingNode.class); + + private Class managerClass; + + ResourceType(Class managerClass) { + this.managerClass = managerClass; + } + + public Class getManagerClass(){ + return this.managerClass; + } + } + + @Override + public Resource parse(String res) throws Exception { + String type = res.replaceAll("\\s*([^<]*).*", "$1"); + String resource = res.replaceAll(".*(.*).*", "$1"); + Class classForUnmrshalling= ResourceType.valueOf(type).getManagerClass(); + return Resources.unmarshal(classForUnmrshalling, new StringReader(resource)); + } + + } + @Test public void someServiceEndpointsAccessData() {