diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/LinkResolver.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/LinkResolver.java new file mode 100644 index 0000000..a3658bb --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/LinkResolver.java @@ -0,0 +1,13 @@ +/** + * + */ +package org.gcube.portlets.user.uriresolvermanager; + +/** + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Oct 14, 2014 + * + */ +public interface LinkResolver { + String getLink(); +} diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolveManager.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java similarity index 59% rename from src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolveManager.java rename to src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java index ce52f9a..45432ef 100644 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolveManager.java +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java @@ -6,38 +6,42 @@ package org.gcube.portlets.user.uriresolvermanager; import java.util.Map; import java.util.Set; +import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException; import org.gcube.portlets.user.uriresolvermanager.type.ApplicationTypePropertyReader; import org.gcube.portlets.user.uriresolvermanager.type.PropertyFileNotFoundException; +import org.gcube.portlets.user.uriresolvermanager.util.UriResolverMapReader; /** * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @Oct 14, 2014 * */ -public class UriResolveManager { +public class UriResolverManager { - private ApplicationTypePropertyReader applicationTypeReader; - private String userApplicationType; - + private UriResolverMapReader uriResolverMapReader; private Map applicationTypes; + private String userScope; + private String userApplicationType; /** * * @param scope * @param applicationType a key Application Type + * @throws IllegalArgumentException */ - public UriResolveManager(String scope, String applicationType){ + public UriResolverManager(String scope, String applicationType) throws IllegalArgumentException{ try { this.applicationTypes = new ApplicationTypePropertyReader().getApplicationTypes(); - this.userApplicationType = applicationType; - - if(!this.applicationTypeReader.getApplicationTypes().containsKey(applicationType)){ - + if(!this.uriResolverMapReader.getApplicationTypes().containsKey(applicationType)){ + throw new IllegalArgumentException("Application type: "+applicationType +" not found in "+getApplicationTypes()); } + this.userApplicationType = applicationType; + this.userScope = scope; + } catch (PropertyFileNotFoundException e) { // TODO Auto-generated catch block @@ -45,6 +49,12 @@ public class UriResolveManager { } } + public String getLink(Map parameters) throws IllegalArgumentException{ + +// if(applicationTypeReader.) + return ""; + } + /** * * @return the Application Types availables diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/ApplicationProfileException.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/ApplicationProfileException.java new file mode 100644 index 0000000..b30b0ce --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/ApplicationProfileException.java @@ -0,0 +1,8 @@ +package org.gcube.portlets.user.uriresolvermanager.util; + +@SuppressWarnings("serial") +public class ApplicationProfileException extends Exception { + public ApplicationProfileException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/ScopeUtil.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/ScopeUtil.java index dde24e2..f2cc17f 100644 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/ScopeUtil.java +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/ScopeUtil.java @@ -37,6 +37,8 @@ public class ScopeUtil { throw new Exception("Infrastructure name not found in "+scope); } + logger.trace("scope is "+ scope +" infra: "+rootScope); + return rootScope; } diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/UriResolverMapReader.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/UriResolverMapReader.java new file mode 100644 index 0000000..935cbe0 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/UriResolverMapReader.java @@ -0,0 +1,158 @@ +package org.gcube.portlets.user.uriresolvermanager.util; + +import static org.gcube.resources.discovery.icclient.ICFactory.client; + +import java.io.StringReader; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +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.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Oct 13, 2014 + * + */ +public class UriResolverMapReader { + + private Logger logger = LoggerFactory.getLogger(UriResolverMapReader.class); + private String secondaryType; + private String scope; + private String resourceName; + + private Map applicationTypes; //A map ApplicationType - Resource Name + + /** + * + * @param scope - the scope to be searched + * @throws Exception + */ + public UriResolverMapReader(String scope) throws Exception { + this.scope = scope; + this.resourceName = "Uri-Resolver-Map"; + this.secondaryType = "UriResolverMap"; + readProfileFromInfrastrucure(); + } + + + /** + * 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 + * @return the applicationProfile profile + * @throws Exception + */ + private void readProfileFromInfrastrucure() throws Exception { + + String queryString = getGcubeGenericQueryString(secondaryType, resourceName); + + logger.info("Trying to fetch applicationProfile profile from the infrastructure for " + secondaryType + " scope: " + scope); + logger.info(queryString); + try { + + String infra = ScopeUtil.getInfrastructureNameFromScope(this.scope); + ScopeProvider.instance.set(infra); + logger.info("scope provider set instance: "+infra); + + Query q = new QueryBox(queryString); + + DiscoveryClient client = client(); + List appUriResolverMap = client.submit(q); + + if (appUriResolverMap == null || appUriResolverMap.size() == 0) + throw new ApplicationProfileException("Your applicationProfile with secondaryType: "+secondaryType+" and name "+resourceName+" is not registered in the infrastructure"); + else { + String elem = appUriResolverMap.get(0); + DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement(); + XPathHelper helper = new XPathHelper(node); + List currValue = null; + currValue = helper.evaluate("/Resource/Profile/Body/access_point/application_type/text()"); + if (currValue != null && currValue.size() > 0) { + logger.trace("Application Types are: "+currValue.size()); + applicationTypes = new HashMap(currValue.size()); + List appTypes = currValue; + for (String at : appTypes) { + logger.trace("Application Type "+at); +// currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Scope/text()"); + currValue = helper.evaluate("/Resource/Profile/Body/access_point[application_type='"+at+"']/resource/text()"); + applicationTypes.put(at, currValue.get(0)); + logger.trace("Stored: "+at +" -> Resource: "+ currValue.get(0)); + } + } + } + + } catch (Exception e) { + logger.error("Error while trying to fetch applicationProfile with secondaryType: "+secondaryType+" and name "+resourceName+" from the infrastructure", e); + throw new ApplicationProfileException("Error while trying to fetch applicationProfile with secondaryType: "+secondaryType+" and name "+resourceName+" from the infrastructure"); + } + + } + + public static String getGcubeGenericQueryString(String secondaryType, String name){ + + return "for $profile in collection('/db/Profiles/GenericResource')//Resource " + + "where $profile/Profile/SecondaryType/string() eq '"+secondaryType+"' and $profile/Profile/Name/string() " + + " eq '" + name + "'" + + "return $profile"; + + } + + + + public String getSecondaryType() { + return secondaryType; + } + + + public String getScope() { + return scope; + } + + public Map getApplicationTypes() { + return applicationTypes; + } + + + public String getResourceName() { + return resourceName; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("UriResolverMapReader [secondaryType="); + builder.append(secondaryType); + builder.append(", scope="); + builder.append(scope); + builder.append(", resourceName="); + builder.append(resourceName); + builder.append(", applicationTypes="); + builder.append(applicationTypes); + builder.append("]"); + return builder.toString(); + } + + /*public static void main(String[] args) { + + String scope ="/gcube"; + try { + UriResolverMapReader resolver = new UriResolverMapReader(scope); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }*/ +}