You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/10/18 23:24:32 UTC

[sling-org-apache-sling-hapi] 16/27: SLING-5707 - Add fromResource to HapiUtil and deprecate fromNode - contributed by Andrei Dulvac, thanks \!

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hapi.git

commit 2bbfb9e2f7359469000428c403b3fb9e6c4a42df
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Tue May 3 08:25:01 2016 +0000

    SLING-5707 - Add fromResource to HapiUtil and deprecate fromNode - contributed by Andrei Dulvac, thanks \!
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1742070 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  4 +-
 src/main/java/org/apache/sling/hapi/HApiUtil.java  | 49 +++++++++++++++-
 .../org/apache/sling/hapi/impl/HApiTypeImpl.java   |  5 ++
 .../org/apache/sling/hapi/impl/HApiUtilImpl.java   | 66 ++++++++++++++--------
 .../java/org/apache/sling/hapi/package-info.java   |  2 +-
 5 files changed, 99 insertions(+), 27 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0562344..572c840 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.hapi</artifactId>
-    <version>1.0.1-SNAPSHOT</version>
+    <version>2.0.0-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
     <name>HApi - Sling Hypermedia API tools</name>
@@ -125,7 +125,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.scripting.sightly</artifactId>
-            <version>1.0.0</version>
+            <version>1.0.18</version>
             <scope>provided</scope>
         </dependency>
     </dependencies>
diff --git a/src/main/java/org/apache/sling/hapi/HApiUtil.java b/src/main/java/org/apache/sling/hapi/HApiUtil.java
index b1cebdd..822c52d 100644
--- a/src/main/java/org/apache/sling/hapi/HApiUtil.java
+++ b/src/main/java/org/apache/sling/hapi/HApiUtil.java
@@ -22,6 +22,7 @@ package org.apache.sling.hapi;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 
 public interface HApiUtil {
@@ -29,7 +30,7 @@ public interface HApiUtil {
     String DEFAULT_RESOURCE_TYPE = "sling/hapi/components/type";
 
     /**
-     * <p>Get a HApi type object from a type identifier.</p>
+     * <p>Get a HApi type jcr node from a type identifier.</p>
      * <p>The JCR node must be [nt:unstructured], a descendant of any of the HAPi search path defined by the
      * {@see HAPI_PATHS} config and the sling:resourceType should be set to the value defined by the {@see HAPI_RESOURCE_TYPE} config</p>
      * <p>The first result is returned</p>
@@ -40,10 +41,25 @@ public interface HApiUtil {
      * @return The first node that matches that type or null if none is found.
      * @throws RepositoryException
      */
+    @Deprecated
     Node getTypeNode(ResourceResolver resolver, String type) throws RepositoryException;
 
 
     /**
+     * <p>Get a HApi type Resource from a type identifier.</p>
+     * <p>The Resource must be [nt:unstructured], a descendant of any of the HAPi search path defined by the
+     * {@see HAPI_PATHS} config and the sling:resourceType should be set to the value defined by the {@see HAPI_RESOURCE_TYPE} config</p>
+     * <p>The first result is returned</p>
+     * @param resolver The sling resource resolver object
+     * @param type The type identifier, which is either in the form of a jcr path,
+     *             same as the path for {@link: ResourceResolver#getResource(String)}. If the path cannot be resolved, type is treated like
+     *             a fully qualified domain name, which has to match the "fqdn" property on the Resource which represents the type.
+     * @return The first Resource that matches that type or null if none is found.
+     * @throws RepositoryException
+     */
+    Resource getTypeResource(ResourceResolver resolver, String type) throws RepositoryException;
+
+    /**
      * <p>Get a HApi type object from a type identifier.</p>
      * <p>The type identifier is resolved to a {@link javax.jcr.Node} and then
      * {@link #fromNode(org.apache.sling.api.resource.ResourceResolver, javax.jcr.Node)} is called.</p>
@@ -86,9 +102,40 @@ public interface HApiUtil {
      * @return The HApiType
      * @throws RepositoryException
      */
+    @Deprecated
     HApiType fromNode(ResourceResolver resolver, Node typeNode) throws RepositoryException;
 
     /**
+     * <p>Get a HApi type object from the {@link org.apache.sling.api.resource.Resource}.</p>
+     * The Resource has the following properties:
+     * <ul>
+     *     <li>name: A 'Name' of the type (mandatory)</li>
+     *     <li>description: A 'String' with the description text for this type (mandatory)</li>
+     *     <li>fqdn: A 'String' with the fully qualified domain name; A namespace like a java package (mandatory)</li>
+     *     <li>extends: A type identifier (either a path or a fqdn); (optional). This defines the parent type of this type</li>
+     *     <li>parameter: A multivalue property to define a list of java-like generic types
+     *     that can be used as types for properties; (optional)</li>
+     * </ul>
+     *
+     * <p>The properties of this type are defined as children resources.</p>
+     * <p>The name of property resource defines the name of the property for this type. </p>
+     * The children property nodes have the following properties:
+     * <ul>
+     *     <li>type: The type identifier (mandatory). Can be of type 'Name' or 'Path'
+     *      See {@link HApiUtil#getTypeNode(org.apache.sling.api.resource.ResourceResolver, String)}
+     *      for the format of this value</li>
+     *     <li>description: A 'String' with the description for this property (mandatory)</li>
+     *     <li>multiple: A 'Boolean' that defines whether this property can exist multiple times on an object of this type (optional)</li>
+     * </ul>
+     *
+     * @param resolver The resource resolver
+     * @param typeResource The sling Resource of the HApi type
+     * @return The HApiType
+     * @throws RepositoryException
+     */
+    HApiType fromResource(ResourceResolver resolver, Resource typeResource) throws RepositoryException;
+
+    /**
      * Get a new instance of AttributeHelper for the type identified by 'type'
      * @param resolver
      * @param type See {@link #getTypeNode(org.apache.sling.api.resource.ResourceResolver, String)}
diff --git a/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java b/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
index 0d25645..f0ec536 100644
--- a/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
+++ b/src/main/java/org/apache/sling/hapi/impl/HApiTypeImpl.java
@@ -166,4 +166,9 @@ public class HApiTypeImpl implements HApiType {
     public void setProperties(Map<String, HApiProperty> properties) {
         this.properties = properties;
     }
+
+    @Override
+    public String toString() {
+        return this.getName() + ": Properties: " + this.getProperties();
+    }
 }
diff --git a/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java b/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
index 043ce0c..d657710 100644
--- a/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
+++ b/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
@@ -22,7 +22,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -41,6 +40,7 @@ import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.hapi.HApiProperty;
 import org.apache.sling.hapi.HApiType;
@@ -83,14 +83,23 @@ public class HApiUtilImpl implements HApiUtil {
      * {@inheritDoc}
      */
     @Override
+    @Deprecated
     public Node getTypeNode(ResourceResolver resolver, String type) throws RepositoryException {
+        return getTypeResource(resolver, type).adaptTo(Node.class);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Resource getTypeResource(ResourceResolver resolver, String type) throws RepositoryException {
         Session session = resolver.adaptTo(Session.class);
 
         // Try to resolve the resource as a path
         Resource res = resolver.getResource(type);
         if (null != res) {
             LOG.debug("res = " + res.getName() + " " + res.getPath());
-            return res.adaptTo(Node.class);
+            return res;
         } else {
             for (String path : new HashSet<String>(Arrays.asList(hApiPaths))) {
                 // Remove trailing slash from path
@@ -111,7 +120,7 @@ public class HApiUtilImpl implements HApiUtil {
 
                 NodeIterator nodeIter = result.getNodes();
                 if (nodeIter.hasNext()) {
-                    return nodeIter.nextNode();
+                    return resolver.getResource(nodeIter.nextNode().getPath());
                 } else {
                     // continue
                 }
@@ -128,12 +137,12 @@ public class HApiUtilImpl implements HApiUtil {
      */
     @Override
     public HApiType fromPath(ResourceResolver resolver, String type) throws RepositoryException {
-        Node typeNode = this.getTypeNode(resolver, type);
-        LOG.debug("typeNode=" + typeNode);
-        if (null == typeNode) {
+        Resource typeResource = this.getTypeResource(resolver, type);
+        LOG.debug("typeResource=" + typeResource);
+        if (null == typeResource) {
             return new AbstractHapiTypeImpl(type);
         } else {
-            return fromNode(resolver, typeNode);
+            return fromResource(resolver, typeResource);
         }
     }
 
@@ -141,15 +150,26 @@ public class HApiUtilImpl implements HApiUtil {
      * {@inheritDoc}
      */
     @Override
+    @Deprecated
     public HApiType fromNode(ResourceResolver resolver, Node typeNode) throws RepositoryException {
         if (null == typeNode) return null;
-        String name = typeNode.getProperty("name").getValue().getString();
-        String description = typeNode.getProperty("description").getValue().getString();
-        String path = typeNode.getPath();
-        String fqdn = typeNode.getProperty("fqdn").getValue().getString();
+        Resource resource = resolver.getResource(typeNode.getPath());
+        return fromResource(resolver, resource);
+    }
 
+    /**
+     * {@inheritDoc}
+     */
+    public HApiType fromResource(ResourceResolver resolver, Resource typeResource) throws RepositoryException {
+        if (null == typeResource) return null;
+
+        ValueMap resProps = typeResource.adaptTo(ValueMap.class);
+        String name = resProps.get("name", (String) null);
+        String description = resProps.get("description", (String) null);
+        String path = typeResource.getPath();
+        String fqdn = resProps.get("fqdn", (String) null);
         // get parameters
-        Value[] parameterValues = typeNode.hasProperty("parameters") ? typeNode.getProperty("parameters").getValues() : new Value[]{};
+        Value[] parameterValues = resProps.get("parameters", new Value[]{});
         List<String> parameters = new ArrayList<String>(parameterValues.length);
 
         for (Value p : Arrays.asList(parameterValues)) {
@@ -158,31 +178,30 @@ public class HApiUtilImpl implements HApiUtil {
         HApiTypeImpl newType = new HApiTypeImpl(name, description, serverContextPath, path, fqdn, parameters, null, null, false);
         TypesCache.getInstance(this).addType(newType);
 
+
         try {
-            // get parent if it exists
+            // Get parent if it exists
             HApiType parent = null;
-            String parentPath = typeNode.hasProperty("extends") ? typeNode.getProperty("extends").getString() : null;
+            String parentPath = resProps.get("extends", (String) null);
             if (null != parentPath) {
                 parent = TypesCache.getInstance(this).getType(resolver, parentPath);
             }
 
             // Get properties
             Map<String, HApiProperty> properties = new HashMap<String, HApiProperty>();
+            for (Resource res : typeResource.getChildren()) {
+                ValueMap resValueMap = res.adaptTo(ValueMap.class);
 
-            // Add the properties from this node
-            Iterator<Node> it = typeNode.getNodes();
-            while (it.hasNext()) {
-                Node propNode = it.next();
-                String propName = propNode.getName();
-                String propDescription = propNode.hasProperty("description") ? propNode.getProperty("description").getString() : "";
-
-                String typePath = propNode.getProperty("type").getValue().getString();
+                String propName = res.getName();
+                String propDescription = resValueMap.get("description", "");
+                String typePath = resValueMap.get("type", (String) null);
                 HApiType propType = TypesCache.getInstance(this).getType(resolver, typePath);
-                Boolean propMultiple = propNode.hasProperty("multiple") ? propNode.getProperty("multiple").getBoolean() : false;
+                Boolean propMultiple = resValueMap.get("multiple", false);
 
                 HApiProperty prop = new HApiPropertyImpl(propName, propDescription, propType, propMultiple);
                 properties.put(prop.getName(), prop);
             }
+            // Set parent and properties
             newType.setParent(parent);
             newType.setProperties(properties);
 
@@ -196,6 +215,7 @@ public class HApiUtilImpl implements HApiUtil {
             throw e;
         }
 
+        LOG.debug("Created type {}", newType);
         return newType;
     }
 
diff --git a/src/main/java/org/apache/sling/hapi/package-info.java b/src/main/java/org/apache/sling/hapi/package-info.java
index fcded2d..24616a4 100644
--- a/src/main/java/org/apache/sling/hapi/package-info.java
+++ b/src/main/java/org/apache/sling/hapi/package-info.java
@@ -17,7 +17,7 @@
  * under the License.
  ******************************************************************************/
 
-@Version("1.0.0")
+@Version("2.0.0")
 package org.apache.sling.hapi;
 
 import aQute.bnd.annotation.Version;

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.