You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2011/06/30 11:27:01 UTC

svn commit: r1141456 - in /sling/trunk/bundles/servlets/post/src: main/java/org/apache/sling/servlets/post/impl/ main/java/org/apache/sling/servlets/post/impl/operations/ main/resources/OSGI-INF/metatype/ test/java/org/apache/sling/servlets/post/impl/o...

Author: fmeschbe
Date: Thu Jun 30 09:27:00 2011
New Revision: 1141456

URL: http://svn.apache.org/viewvc?rev=1141456&view=rev
Log:
SLING-2120 Add configuration property to specify a regular expression for parameters to ignore when writing back to the repository

Added:
    sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/operations/
    sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperationTest.java   (with props)
Modified:
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
    sling/trunk/bundles/servlets/post/src/main/resources/OSGI-INF/metatype/metatype.properties

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java?rev=1141456&r1=1141455&r2=1141456&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java (original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java Thu Jun 30 09:27:00 2011
@@ -22,6 +22,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.regex.Pattern;
 
 import javax.servlet.Servlet;
 import javax.servlet.ServletException;
@@ -129,6 +130,11 @@ public class SlingPostServlet extends Sl
 
     private static final String PARAM_AUTO_CHECKIN = ":autoCheckin";
 
+    private static final String DEFAULT_IGNORED_PARAMETER_NAME_PATTERN = "j_.*";
+
+    @Property(value = DEFAULT_IGNORED_PARAMETER_NAME_PATTERN)
+    private static final String PROP_IGNORED_PARAMETER_NAME_PATTERN = "servlet.post.ignorePattern";
+
     private ModifyOperation modifyOperation;
 
     private ServiceRegistration[] internalOperations;
@@ -405,9 +411,16 @@ public class SlingPostServlet extends Sl
         NodeNameGenerator nodeNameGenerator = new DefaultNodeNameGenerator(
             nameHints, nameMax);
 
+        final String paramMatch = OsgiUtil.toString(
+            configuration.get(PROP_IGNORED_PARAMETER_NAME_PATTERN),
+            DEFAULT_IGNORED_PARAMETER_NAME_PATTERN);
+        final Pattern paramMatchPattern = Pattern.compile(paramMatch);
+
         this.modifyOperation.setDateParser(dateParser);
         this.modifyOperation.setDefaultNodeNameGenerator(nodeNameGenerator);
         this.importOperation.setDefaultNodeNameGenerator(nodeNameGenerator);
+        this.modifyOperation.setIgnoredParameterNamePattern(paramMatchPattern);
+        this.importOperation.setIgnoredParameterNamePattern(paramMatchPattern);
     }
 
     @Override

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java?rev=1141456&r1=1141455&r2=1141456&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java (original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java Thu Jun 30 09:27:00 2011
@@ -23,7 +23,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
+import java.util.regex.Pattern;
 import javax.jcr.Item;
 import javax.jcr.Node;
 import javax.jcr.PathNotFoundException;
@@ -59,8 +59,14 @@ abstract class AbstractCreateOperation e
      */
     private NodeNameGenerator[] extraNodeNameGenerators;
 
+    /**
+     * regular expression for parameters to ignore
+     */
+    private Pattern ignoredParameterNamePattern;
+
     protected AbstractCreateOperation() {
 		this.defaultNodeNameGenerator = new DefaultNodeNameGenerator();
+		this.ignoredParameterNamePattern = null;
     }
 
     public void setDefaultNodeNameGenerator(
@@ -73,6 +79,11 @@ abstract class AbstractCreateOperation e
         this.extraNodeNameGenerators = extraNodeNameGenerators;
     }
 
+    public void setIgnoredParameterNamePattern(
+            final Pattern ignoredParameterNamePattern) {
+        this.ignoredParameterNamePattern = ignoredParameterNamePattern;
+    }
+
     /**
      * Create node(s) according to current request
      *
@@ -150,14 +161,10 @@ abstract class AbstractCreateOperation e
         for (Map.Entry<String, RequestParameter[]> e : request.getRequestParameterMap().entrySet()) {
             final String paramName = e.getKey();
 
-            // do not store parameters with names starting with sling:post
-            if (paramName.startsWith(SlingPostConstants.RP_PREFIX)) {
-                continue;
-            }
-            // SLING-298: skip form encoding parameter
-            if (paramName.equals("_charset_")) {
+            if (ignoreParameter(paramName)) {
                 continue;
             }
+
             // skip parameters that do not start with the save prefix
             if (requireItemPrefix && !hasItemPathPrefix(paramName)) {
                 continue;
@@ -325,9 +332,33 @@ abstract class AbstractCreateOperation e
     }
 
     /**
-     * Returns the <code>paramName</code> as an absolute (unnormalized)
-     * property path by prepending the response path (<code>response.getPath</code>)
-     * to the parameter name if not already absolute.
+     * Returns <code>true</code> if the parameter of the given name should be
+     * ignored.
+     */
+    private boolean ignoreParameter(final String paramName) {
+        // do not store parameters with names starting with sling:post
+        if (paramName.startsWith(SlingPostConstants.RP_PREFIX)) {
+            return true;
+        }
+
+        // SLING-298: skip form encoding parameter
+        if (paramName.equals("_charset_")) {
+            return true;
+        }
+
+        // SLING-2120: ignore parameter match ignoredParameterNamePattern
+        if (this.ignoredParameterNamePattern != null
+            && this.ignoredParameterNamePattern.matcher(paramName).matches()) {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Returns the <code>paramName</code> as an absolute (unnormalized) property
+     * path by prepending the response path (<code>response.getPath</code>) to
+     * the parameter name if not already absolute.
      */
     private String toPropertyPath(String paramName, PostResponse response) {
         if (!paramName.startsWith("/")) {

Modified: sling/trunk/bundles/servlets/post/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1141456&r1=1141455&r2=1141456&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/resources/OSGI-INF/metatype/metatype.properties (original)
+++ sling/trunk/bundles/servlets/post/src/main/resources/OSGI-INF/metatype/metatype.properties Thu Jun 30 09:27:00 2011
@@ -57,4 +57,9 @@ servlet.post.autoCheckout.description = 
  checked out when necessary. By default, false.
 servlet.post.autoCheckin.name = Auto Checkin Nodes
 servlet.post.autoCheckin.description = If true, nodes which are checked out \
- by the post servlet are checked in. By default, true.
\ No newline at end of file
+ by the post servlet are checked in. By default, true.
+servlet.post.ignorePattern.name = Ignored Parameters
+servlet.post.ignorePattern.description = Configures a regular expression \
+ pattern to select request parameters which should be ignored when wrinting \
+ content to the repository. By default this is "j_.*" thus ignoring all \
+ request parameters starting with j_ such as j_username.
\ No newline at end of file

Added: sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperationTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperationTest.java?rev=1141456&view=auto
==============================================================================
--- sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperationTest.java (added)
+++ sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperationTest.java Thu Jun 30 09:27:00 2011
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.servlets.post.impl.operations;
+
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import junit.framework.TestCase;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.servlets.post.Modification;
+import org.apache.sling.servlets.post.PostResponse;
+
+public class AbstractCreateOperationTest extends TestCase {
+
+    private AbstractCreateOperation op = new AbstractCreateOperation() {
+
+        @Override
+        protected void doRun(SlingHttpServletRequest request,
+                PostResponse response, List<Modification> changes) {
+            // none here
+        }
+    };
+
+    public void test_ignoreParameter() throws Exception {
+        Method ip = getMethod("ignoreParameter", String.class);
+
+        // default setup without matching regexp
+        assertEquals(true, ip.invoke(op, "_charset_"));
+        assertEquals(true, ip.invoke(op, ":operation"));
+        assertEquals(false, ip.invoke(op, "j_username"));
+        assertEquals(false, ip.invoke(op, "j_password"));
+        assertEquals(false, ip.invoke(op, "some_random_j_name"));
+
+        // setup: j_.*
+        op.setIgnoredParameterNamePattern(Pattern.compile("j_.*"));
+        assertEquals(true, ip.invoke(op, "_charset_"));
+        assertEquals(true, ip.invoke(op, ":operation"));
+        assertEquals(true, ip.invoke(op, "j_username"));
+        assertEquals(true, ip.invoke(op, "j_password"));
+        assertEquals(false, ip.invoke(op, "some_random_j_name"));
+
+        // setup: .*j_.*
+        op.setIgnoredParameterNamePattern(Pattern.compile(".*j_.*"));
+        assertEquals(true, ip.invoke(op, "_charset_"));
+        assertEquals(true, ip.invoke(op, ":operation"));
+        assertEquals(true, ip.invoke(op, "j_username"));
+        assertEquals(true, ip.invoke(op, "j_password"));
+        assertEquals(true, ip.invoke(op, "some_random_j_name"));
+
+        // setup: .+j_.*
+        op.setIgnoredParameterNamePattern(Pattern.compile(".+j_.*"));
+        assertEquals(true, ip.invoke(op, "_charset_"));
+        assertEquals(true, ip.invoke(op, ":operation"));
+        assertEquals(false, ip.invoke(op, "j_username"));
+        assertEquals(false, ip.invoke(op, "j_password"));
+        assertEquals(true, ip.invoke(op, "some_random_j_name"));
+    }
+
+    private Method getMethod(String name, Class... parameterTypes) {
+        try {
+            Method m = AbstractCreateOperation.class.getDeclaredMethod(name,
+                parameterTypes);
+            m.setAccessible(true);
+            return m;
+        } catch (Throwable t) {
+            fail(t.toString());
+            return null; // compiler wants this
+        }
+    }
+
+}

Propchange: sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperationTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url