You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2015/11/17 15:07:42 UTC

svn commit: r1714786 - in /sling/trunk/bundles: api/src/main/java/org/apache/sling/api/scripting/ scripting/core/ scripting/core/src/main/java/org/apache/sling/scripting/core/impl/

Author: radu
Date: Tue Nov 17 14:07:42 2015
New Revision: 1714786

URL: http://svn.apache.org/viewvc?rev=1714786&view=rev
Log:
SLING-5301 - Add the current resource resolver to the script bindings

* defined the constant for obtaining the resource resolver from the script bindings as SlingBindings#RESOLVER
* made sure that the resolver is added to the bindings in the DefaultSlingScript#verifyBindings method

Modified:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingBindings.java
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/package-info.java
    sling/trunk/bundles/scripting/core/pom.xml
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingBindings.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingBindings.java?rev=1714786&r1=1714785&r2=1714786&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingBindings.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingBindings.java Tue Nov 17 14:07:42 2015
@@ -27,6 +27,7 @@ import javax.annotation.CheckForNull;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
 import org.slf4j.Logger;
 
 /**
@@ -100,6 +101,24 @@ public class SlingBindings extends HashM
     public static final String RESOURCE = "resource";
 
     /**
+     * <p>
+     * The name of the global scripting variable providing the
+     * {@link org.apache.sling.api.resource.ResourceResolver} object (value is
+     * "resolver"). The value of the scripting variable is the same as that
+     * returned by the {@code SlingScriptHelper.getRequest().getResourceResolver()}
+     * method.
+     * </p>
+     * <p>
+     * This bound variable is optional. If existing, the resource resolver must be
+     * bound to the same resolver as returned by the {@code
+     * SlingHttpServletRequest.getResource().getResourceResolver} method. If this
+     * variable is not bound, the script implementation will bind it before actually
+     * evaluating the script.
+     * </p>
+     */
+    public static final String RESOLVER = "resolver";
+
+    /**
      * The name of the global scripting variable providing the
      * <code>java.io.PrintWriter</code> object to return the response content
      * (value is "out"). The value of the scripting variable is the same as that
@@ -270,6 +289,24 @@ public class SlingBindings extends HashM
     }
 
     /**
+     * Sets the {@link #RESOLVER} property to the provided {@code resourceResolver} if not {@code null}.
+     * @param resourceResolver the Resource Resolver
+     */
+    public void setResourceResolver(ResourceResolver resourceResolver) {
+        this.safePut(RESOLVER, resourceResolver);
+    }
+
+    /**
+     * Returns the {@link #RESOLVER} property if not <code>null</code> and a
+     * <code>ResourceResolver</code> instance. Otherwise <code>null</code> is
+     * returned.
+     * @return the bound {@link ResourceResolver} if one exists, <code>null</code> otherwise
+     */
+    public @CheckForNull ResourceResolver getResourceResolver() {
+        return this.get(RESOLVER, ResourceResolver.class);
+    }
+
+    /**
      * Sets the {@link #RESPONSE} property to <code>response</code> if not
      * <code>null</code>.
      */

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/package-info.java?rev=1714786&r1=1714785&r2=1714786&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/package-info.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/package-info.java Tue Nov 17 14:07:42 2015
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-@Version("2.1.1")
+@Version("2.2.0")
 package org.apache.sling.api.scripting;
 
 import aQute.bnd.annotation.Version;

Modified: sling/trunk/bundles/scripting/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/pom.xml?rev=1714786&r1=1714785&r2=1714786&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/pom.xml (original)
+++ sling/trunk/bundles/scripting/core/pom.xml Tue Nov 17 14:07:42 2015
@@ -130,7 +130,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.1.0</version>
+            <version>2.9.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java?rev=1714786&r1=1714785&r2=1714786&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java (original)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/DefaultSlingScript.java Tue Nov 17 14:07:42 2015
@@ -24,11 +24,11 @@ import static org.apache.sling.api.scrip
 import static org.apache.sling.api.scripting.SlingBindings.READER;
 import static org.apache.sling.api.scripting.SlingBindings.REQUEST;
 import static org.apache.sling.api.scripting.SlingBindings.RESOURCE;
+import static org.apache.sling.api.scripting.SlingBindings.RESOLVER;
 import static org.apache.sling.api.scripting.SlingBindings.RESPONSE;
 import static org.apache.sling.api.scripting.SlingBindings.SLING;
 
 import java.io.BufferedReader;
-import java.io.FilterReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -77,7 +77,6 @@ import org.apache.sling.api.scripting.Sl
 import org.apache.sling.scripting.api.BindingsValuesProvider;
 import org.apache.sling.scripting.api.CachedScript;
 import org.apache.sling.scripting.api.ScriptCache;
-import org.apache.sling.scripting.api.ScriptNameAware;
 import org.apache.sling.scripting.core.ScriptNameAwareReader;
 import org.apache.sling.scripting.core.impl.helper.CachedScriptImpl;
 import org.apache.sling.scripting.core.impl.helper.ProtectedBindings;
@@ -95,7 +94,7 @@ class DefaultSlingScript implements Slin
 
     /** The set of protected keys. */
     private static final Set<String> PROTECTED_KEYS =
-        new HashSet<String>(Arrays.asList(REQUEST, RESPONSE, READER, SLING, RESOURCE, OUT, LOG));
+        new HashSet<String>(Arrays.asList(REQUEST, RESPONSE, READER, SLING, RESOURCE, RESOLVER, OUT, LOG));
 
     /** The resource pointing to the script. */
 
@@ -662,6 +661,11 @@ class DefaultSlingScript implements Slin
                 throw fail(RESOURCE, "Wrong type");
             }
 
+            Object resolverObject = slingBindings.get(RESOLVER);
+            if (resolverObject != null && !(resolverObject instanceof ResourceResolver)) {
+                throw fail(RESOLVER, "Wrong type");
+            }
+
             Object writerObject = slingBindings.get(OUT);
             if (writerObject != null && !(writerObject instanceof PrintWriter)) {
                 throw fail(OUT, "Wrong type");
@@ -686,6 +690,11 @@ class DefaultSlingScript implements Slin
                         "Not the same as resource of the SlingScriptHelper request");
                 }
 
+                if (resolverObject != null && sling.getRequest().getResourceResolver() != resolverObject) {
+                    throw fail(RESOLVER,
+                        "Not the same as the resource resolver of the SlingScriptHelper request's resolver");
+                }
+
                 if (writerObject != null
                     && sling.getResponse().getWriter() != writerObject) {
                     throw fail(OUT,
@@ -698,6 +707,7 @@ class DefaultSlingScript implements Slin
             bindings.put(READER, sling.getRequest().getReader());
             bindings.put(RESPONSE, sling.getResponse());
             bindings.put(RESOURCE, sling.getRequest().getResource());
+            bindings.put(RESOLVER, sling.getRequest().getResourceResolver());
             bindings.put(OUT, sling.getResponse().getWriter());
         }
 
@@ -904,4 +914,4 @@ class DefaultSlingScript implements Slin
             return this.resolver;
         }
     }
-}
\ No newline at end of file
+}