incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1350684 - in /sling/trunk/bundles/jcr/resource/src: main/java/org/apache/sling/jcr/resource/ test/java/org/apache/sling/jcr/resource/internal/
Date Fri, 15 Jun 2012 16:37:01 GMT
Author: cziegeler
Date: Fri Jun 15 16:37:01 2012
New Revision: 1350684

URL: http://svn.apache.org/viewvc?rev=1350684&view=rev
Log:
SLING-2502 : Incorrect escaping of property names in JcrModifiablePropertyMap on save. Applied
patch from Alex Parvulescu

Modified:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrModifiablePropertyMap.java
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java
    sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiablePropertyMapTest.java
    sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrPropertyMapTest.java

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrModifiablePropertyMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrModifiablePropertyMap.java?rev=1350684&r1=1350683&r2=1350684&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrModifiablePropertyMap.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrModifiablePropertyMap.java
Fri Jun 15 16:37:01 2012
@@ -196,7 +196,7 @@ public final class JcrModifiableProperty
             }
 
             for(final String key : this.changedProperties) {
-                final String name = Text.escapeIllegalJcrChars(key);
+                final String name = escapeKeyName(key);
                 if ( !MIXIN_TYPES.equals(name) ) {
                     if ( cache.containsKey(key) ) {
                         final JcrPropertyMapCacheEntry entry = cache.get(key);

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java?rev=1350684&r1=1350683&r2=1350684&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrPropertyMap.java
Fri Jun 15 16:37:01 2012
@@ -336,8 +336,8 @@ public class JcrPropertyMap
             return cache.get(name);
         }
 
-        final String key = Text.escapeIllegalJcrChars(name);
         try {
+            final String key = escapeKeyName(name);
             if (node.hasProperty(key)) {
                 final Property prop = node.getProperty(key);
                 return cacheProperty(prop);
@@ -358,6 +358,30 @@ public class JcrPropertyMap
     }
 
     /**
+     * Handles key name escaping by taking into consideration if it contains a
+     * registered prefix
+     *
+     * @param key
+     * @return escaped key name
+     */
+    protected String escapeKeyName(final String key) throws RepositoryException {
+        final int indexOfPrefix = key.indexOf(':');
+        if (indexOfPrefix != -1 && key.length() > indexOfPrefix + 1) {
+            final String prefix = key.substring(0, indexOfPrefix);
+            for (final String existingPrefix : getNode().getSession()
+                    .getNamespacePrefixes()) {
+                if (existingPrefix.equals(prefix)) {
+                    return prefix
+                            + ":"
+                            + Text.escapeIllegalJcrChars(key
+                                    .substring(indexOfPrefix + 1));
+                }
+            }
+        }
+        return Text.escapeIllegalJcrChars(key);
+    }
+
+    /**
      * Read all properties.
      * @throws IllegalArgumentException if a repository exception occurs
      */

Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiablePropertyMapTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiablePropertyMapTest.java?rev=1350684&r1=1350683&r2=1350684&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiablePropertyMapTest.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrModifiablePropertyMapTest.java
Fri Jun 15 16:37:01 2012
@@ -18,7 +18,6 @@
  */
 package org.apache.sling.jcr.resource.internal;
 
-import java.io.IOException;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -31,6 +30,7 @@ import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.NodeType;
 
+import org.apache.jackrabbit.util.Text;
 import org.apache.sling.api.resource.PersistableValueMap;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.jcr.resource.JcrModifiablePropertyMap;
@@ -77,7 +77,8 @@ public class JcrModifiablePropertyMapTes
     }
 
     public void testPut()
-    throws IOException {
+    throws Exception {
+        this.rootNode.getSession().refresh(false);
         final PersistableValueMap pvm = new JcrModifiablePropertyMap(this.rootNode);
         assertContains(pvm, initialSet());
         assertNull(pvm.get("something"));
@@ -99,7 +100,8 @@ public class JcrModifiablePropertyMapTes
     }
 
     public void testReset()
-    throws IOException {
+    throws Exception {
+        this.rootNode.getSession().refresh(false);
         final PersistableValueMap pvm = new JcrModifiablePropertyMap(this.rootNode);
         assertContains(pvm, initialSet());
         assertNull(pvm.get("something"));
@@ -121,7 +123,8 @@ public class JcrModifiablePropertyMapTes
     }
 
     public void testSerializable()
-    throws IOException {
+    throws Exception {
+        this.rootNode.getSession().refresh(false);
         final PersistableValueMap pvm = new JcrModifiablePropertyMap(this.rootNode);
         assertContains(pvm, initialSet());
         assertNull(pvm.get("something"));
@@ -147,7 +150,8 @@ public class JcrModifiablePropertyMapTes
 
     }
 
-    public void testExceptions() {
+    public void testExceptions() throws Exception {
+        this.rootNode.getSession().refresh(false);
         final PersistableValueMap pvm = new JcrModifiablePropertyMap(this.rootNode);
         try {
             pvm.put(null, "something");
@@ -172,6 +176,7 @@ public class JcrModifiablePropertyMapTes
     }
 
     public void testMixins() throws Exception {
+        this.rootNode.getSession().refresh(false);
         final Node testNode = this.rootNode.addNode("testMixins" + System.currentTimeMillis());
         testNode.getSession().save();
         final PersistableValueMap pvm = new JcrModifiablePropertyMap(testNode);
@@ -197,6 +202,73 @@ public class JcrModifiablePropertyMapTes
         assertEquals(newNodeTypes.size(), exNodeTypes.size() + 1);
     }
 
+    private static final String TEST_PATH = "a<a";
+
+    private static final String VALUE = "value";
+    private static final String VALUE1 = "value";
+    private static final String VALUE2 = "value";
+    private static final String VALUE3 = "my title";
+    private static final String PROP1 = "-prop";
+    private static final String PROP2 = "1prop";
+    private static final String PROP3 = "jcr:title";
+
+    public void testNamesReverse() throws Exception {
+        this.rootNode.getSession().refresh(false);
+
+        final Node testNode = this.rootNode.addNode("nameTest" + System.currentTimeMillis());
+        testNode.getSession().save();
+        final PersistableValueMap pvm = new JcrModifiablePropertyMap(testNode);
+        pvm.put(TEST_PATH, VALUE);
+        pvm.put(PROP1, VALUE1);
+        pvm.put(PROP2, VALUE2);
+        pvm.put(PROP3, VALUE3);
+        pvm.save();
+
+        // read with property map
+        final ValueMap vm = this.createPropertyMap(testNode);
+        assertEquals(VALUE, vm.get(TEST_PATH));
+        assertEquals(VALUE1, vm.get(PROP1));
+        assertEquals(VALUE2, vm.get(PROP2));
+        assertEquals(VALUE3, vm.get(PROP3));
+
+        // read properties
+        assertEquals(VALUE, testNode.getProperty(TEST_PATH).getString());
+        assertEquals(VALUE1, testNode.getProperty(PROP1).getString());
+        assertEquals(VALUE2, testNode.getProperty(PROP2).getString());
+        assertEquals(VALUE3, testNode.getProperty(PROP3).getString());
+    }
+
+    /**
+     * Checks property names encoding, see SLING-2502.
+     */
+    public void testNamesUpdate() throws Exception {
+        this.rootNode.getSession().refresh(false);
+
+        final Node testNode = this.rootNode.addNode("nameUpdateTest"
+                + System.currentTimeMillis());
+        testNode.setProperty(PROP3, VALUE);
+        testNode.getSession().save();
+
+        final PersistableValueMap pvm = new JcrModifiablePropertyMap(testNode);
+        pvm.put(PROP3, VALUE3);
+        pvm.put("jcr:a:b", VALUE3);
+        pvm.put("jcr:", VALUE3);
+        pvm.save();
+
+        // read with property map
+        final ValueMap vm = this.createPropertyMap(testNode);
+        assertEquals(VALUE3, vm.get(PROP3));
+        assertEquals(VALUE3, vm.get("jcr:a:b"));
+        assertEquals(VALUE3, vm.get("jcr:"));
+
+        // read properties
+        assertEquals(VALUE3, testNode.getProperty(PROP3).getString());
+        assertEquals(VALUE3, testNode.getProperty("jcr:"+Text.escapeIllegalJcrChars("a:b")).getString());
+        assertEquals(VALUE3, testNode.getProperty(Text.escapeIllegalJcrChars("jcr:")).getString());
+        assertFalse(testNode.hasProperty(Text.escapeIllegalJcrChars(PROP3)));
+        assertFalse(testNode.hasProperty(Text.escapeIllegalJcrChars("jcr:a:b")));
+    }
+
     protected JcrPropertyMap createPropertyMap(final Node node) {
         return new JcrModifiablePropertyMap(node);
     }

Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrPropertyMapTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrPropertyMapTest.java?rev=1350684&r1=1350683&r2=1350684&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrPropertyMapTest.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrPropertyMapTest.java
Fri Jun 15 16:37:01 2012
@@ -69,6 +69,7 @@ public class JcrPropertyMapTest extends 
     }
 
     public void testJCRType() throws Exception {
+        this.rootNode.getSession().refresh(false);
         testValue(rootNode, "A String");
         testValue(rootNode, Calendar.getInstance());
         testValue(rootNode, 5L);
@@ -78,6 +79,7 @@ public class JcrPropertyMapTest extends 
     }
 
     public void testTypeByClass() throws Exception {
+        this.rootNode.getSession().refresh(false);
         testValue(rootNode, "A String Value", String.class);
 
         testValue(rootNode, 1l, Byte.class);
@@ -98,6 +100,7 @@ public class JcrPropertyMapTest extends 
     }
 
     public void testTypeByDefaultValue() throws Exception {
+        this.rootNode.getSession().refresh(false);
         testValue(rootNode, "A String Value", "default");
 
         testValue(rootNode, 1l, (byte) 10);
@@ -127,6 +130,7 @@ public class JcrPropertyMapTest extends 
     }
 
     public void testDefaultValue() throws Exception {
+        this.rootNode.getSession().refresh(false);
         testDefaultValue(rootNode, "default");
 
         testDefaultValue(rootNode, (byte) 10);
@@ -151,6 +155,7 @@ public class JcrPropertyMapTest extends 
     }
 
     public void testProperty() throws Exception {
+        this.rootNode.getSession().refresh(false);
         ValueMap map = createProperty(rootNode, "Sample Value For Prop");
         Property prop = rootNode.getProperty(PROP_NAME);
 
@@ -169,6 +174,7 @@ public class JcrPropertyMapTest extends 
     }
 
     public void testInputStream() throws Exception {
+        this.rootNode.getSession().refresh(false);
         InputStream instream = new ByteArrayInputStream("this too shall pass".getBytes());
 
         ValueFactory valueFactory = rootNode.getSession().getValueFactory();
@@ -285,20 +291,26 @@ public class JcrPropertyMapTest extends 
     private static final String VALUE = "value";
     private static final String VALUE1 = "value";
     private static final String VALUE2 = "value";
+    private static final String VALUE3 = "my title";
     private static final String PROP1 = "-prop";
     private static final String PROP2 = "1prop";
+    private static final String PROP3 = "jcr:title";
 
     public void testNames() throws Exception {
+        this.rootNode.getSession().refresh(false);
         this.rootNode.setProperty(Text.escapeIllegalJcrChars(TEST_PATH), VALUE);
         this.rootNode.setProperty(PROP1, VALUE1);
         this.rootNode.setProperty(PROP2, VALUE2);
+        this.rootNode.setProperty(PROP3, VALUE3);
         final ValueMap vm = this.createPropertyMap(this.rootNode);
         assertEquals(VALUE, vm.get(TEST_PATH));
         assertEquals(VALUE1, vm.get(PROP1));
         assertEquals(VALUE2, vm.get(PROP2));
+        assertEquals(VALUE3, vm.get(PROP3));
     }
 
     public void testIerators() throws Exception {
+        this.rootNode.getSession().refresh(false);
         this.rootNode.setProperty(Text.escapeIllegalJcrChars(TEST_PATH), VALUE);
         this.rootNode.setProperty(PROP1, VALUE1);
         this.rootNode.setProperty(PROP2, VALUE2);
@@ -313,12 +325,14 @@ public class JcrPropertyMapTest extends 
     }
 
     public void testNamesOld() throws Exception {
+        this.rootNode.getSession().refresh(false);
         this.rootNode.setProperty(ISO9075.encodePath(TEST_PATH), VALUE);
         final ValueMap vm = this.createPropertyMap(this.rootNode);
         assertEquals(VALUE, vm.get(TEST_PATH));
     }
 
     public void testIeratorsOld() throws Exception {
+        this.rootNode.getSession().refresh(false);
         this.rootNode.setProperty(ISO9075.encodePath(TEST_PATH), VALUE);
         final ValueMap vm = this.createPropertyMap(this.rootNode);
         assertTrue(vm.containsKey(TEST_PATH));
@@ -327,6 +341,7 @@ public class JcrPropertyMapTest extends 
     }
 
     public void testDotSlash() throws Exception {
+        this.rootNode.getSession().refresh(false);
         final String prop = "myProp";
         final String value = "value";
         this.rootNode.setProperty(prop, value);



Mime
View raw message