Return-Path: X-Original-To: apmail-sling-commits-archive@www.apache.org Delivered-To: apmail-sling-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 85AF56F90 for ; Thu, 30 Jun 2011 09:27:41 +0000 (UTC) Received: (qmail 47500 invoked by uid 500); 30 Jun 2011 09:27:41 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 47389 invoked by uid 500); 30 Jun 2011 09:27:30 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 47366 invoked by uid 99); 30 Jun 2011 09:27:26 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 30 Jun 2011 09:27:26 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 30 Jun 2011 09:27:22 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 6C463238896F; Thu, 30 Jun 2011 09:27:01 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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... Date: Thu, 30 Jun 2011 09:27:01 -0000 To: commits@sling.apache.org From: fmeschbe@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110630092701.6C463238896F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 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 paramName as an absolute (unnormalized) - * property path by prepending the response path (response.getPath) - * to the parameter name if not already absolute. + * Returns true 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 paramName as an absolute (unnormalized) property + * path by prepending the response path (response.getPath) 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 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