You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2013/03/01 10:57:53 UTC

svn commit: r1451548 - in /sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource: JcrModifiablePropertyMap.java internal/JcrModifiableValueMap.java internal/NodeUtil.java internal/helper/jcr/JcrResourceProvider.java

Author: cziegeler
Date: Fri Mar  1 09:57:52 2013
New Revision: 1451548

URL: http://svn.apache.org/r1451548
Log:
SLING-2761 : JcrResourceProvider.create should filter protected properties

Added:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java   (with props)
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/internal/JcrModifiableValueMap.java
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.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=1451548&r1=1451547&r2=1451548&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 Mar  1 09:57:52 2013
@@ -25,16 +25,15 @@ import java.util.Set;
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
-import javax.jcr.Value;
-import javax.jcr.nodetype.NodeType;
 
 import org.apache.sling.api.resource.PersistableValueMap;
 import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.jcr.resource.internal.NodeUtil;
 import org.apache.sling.jcr.resource.internal.helper.JcrPropertyMapCacheEntry;
 
 /**
  * This implementation of the value map allows to change
- * the properies and save them later on.
+ * the properties and save them later on.
  */
 public final class JcrModifiablePropertyMap
     extends JcrPropertyMap
@@ -65,6 +64,7 @@ public final class JcrModifiableProperty
     /**
      * @see java.util.Map#clear()
      */
+    @Override
     public void clear() {
         // we have to read all properties first
         this.readFully();
@@ -79,6 +79,7 @@ public final class JcrModifiableProperty
     /**
      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
      */
+    @Override
     public Object put(String aKey, Object value) {
         final String key = checkKey(aKey);
         if ( key.indexOf('/') != -1 ) {
@@ -105,6 +106,7 @@ public final class JcrModifiableProperty
     /**
      * @see java.util.Map#putAll(java.util.Map)
      */
+    @Override
     public void putAll(Map<? extends String, ? extends Object> t) {
         readFully();
         if ( t != null ) {
@@ -120,6 +122,7 @@ public final class JcrModifiableProperty
     /**
      * @see java.util.Map#remove(java.lang.Object)
      */
+    @Override
     public Object remove(Object aKey) {
         final String key = checkKey(aKey.toString());
         readFully();
@@ -144,35 +147,6 @@ public final class JcrModifiableProperty
         this.fullyRead = false;
     }
 
-    /** Property for the mixin node types. */
-    private static final String MIXIN_TYPES = "jcr:mixinTypes";
-
-    /**
-     * Update the mixin node types
-     */
-    private void handleMixinTypes(final Node node, final Value[] mixinTypes) throws RepositoryException {
-        final Set<String> newTypes = new HashSet<String>();
-        if ( mixinTypes != null ) {
-            for(final Value value : mixinTypes ) {
-                newTypes.add(value.getString());
-            }
-        }
-        final Set<String> oldTypes = new HashSet<String>();
-        for(final NodeType mixinType : node.getMixinNodeTypes()) {
-            oldTypes.add(mixinType.getName());
-        }
-        for(final String name : oldTypes) {
-            if ( !newTypes.contains(name) ) {
-                node.removeMixin(name);
-            } else {
-                newTypes.remove(name);
-            }
-        }
-        for(final String name : newTypes) {
-            node.addMixin(name);
-        }
-    }
-
     /**
      * @see org.apache.sling.api.resource.PersistableValueMap#save()
      */
@@ -185,19 +159,19 @@ public final class JcrModifiableProperty
         try {
             final Node node = getNode();
             // check for mixin types
-            if ( this.changedProperties.contains(MIXIN_TYPES) ) {
-                if ( cache.containsKey(MIXIN_TYPES) ) {
-                    final JcrPropertyMapCacheEntry entry = cache.get(MIXIN_TYPES);
-                    handleMixinTypes(node, entry.values);
+            if ( this.changedProperties.contains(NodeUtil.MIXIN_TYPES) ) {
+                if ( cache.containsKey(NodeUtil.MIXIN_TYPES) ) {
+                    final JcrPropertyMapCacheEntry entry = cache.get(NodeUtil.MIXIN_TYPES);
+                    NodeUtil.handleMixinTypes(node, entry.values);
                 } else {
                     // remove all mixin types!
-                    handleMixinTypes(node, null);
+                    NodeUtil.handleMixinTypes(node, null);
                 }
             }
 
             for(final String key : this.changedProperties) {
                 final String name = escapeKeyName(key);
-                if ( !MIXIN_TYPES.equals(name) ) {
+                if ( !NodeUtil.MIXIN_TYPES.equals(name) ) {
                     if ( cache.containsKey(key) ) {
                         final JcrPropertyMapCacheEntry entry = cache.get(key);
                         if ( entry.isMulti ) {
@@ -210,7 +184,7 @@ public final class JcrModifiableProperty
                     }
                 }
             }
-            getNode().getSession().save();
+            node.getSession().save();
             this.reset();
         } catch (final RepositoryException re) {
             throw new PersistenceException("Unable to persist changes.", re, getPath(), null);

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java?rev=1451548&r1=1451547&r2=1451548&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java (original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java Fri Mar  1 09:57:52 2013
@@ -51,7 +51,7 @@ import org.slf4j.LoggerFactory;
 
 /**
  * This implementation of the value map allows to change
- * the properies.
+ * the properties.
  *
  * TODO : This adds a lot of duplicate code - we should consolidate.
  */
@@ -65,13 +65,13 @@ public final class JcrModifiableValueMap
     private final Node node;
 
     /** A cache for the properties. */
-    final Map<String, JcrPropertyMapCacheEntry> cache;
+    private final Map<String, JcrPropertyMapCacheEntry> cache;
 
     /** A cache for the values. */
-    final Map<String, Object> valueCache;
+    private final Map<String, Object> valueCache;
 
-    /** Has the node been read completly? */
-    boolean fullyRead;
+    /** Has the node been read completely? */
+    private boolean fullyRead;
 
     private final ClassLoader dynamicClassLoader;
 
@@ -251,7 +251,7 @@ public final class JcrModifiableValueMap
             final String name = prop.getName();
             String key = null;
             if ( name.indexOf("_x") != -1 ) {
-                // for compatiblity with older versions we use the (wrong)
+                // for compatibility with older versions we use the (wrong)
                 // ISO9075 path encoding
                 key = ISO9075.decode(name);
                 if ( key.equals(name) ) {
@@ -562,7 +562,7 @@ public final class JcrModifiableValueMap
         return type;
     }
 
-    private Map<String, Object> transformEntries( Map<String, JcrPropertyMapCacheEntry> map) {
+    private Map<String, Object> transformEntries(final Map<String, JcrPropertyMapCacheEntry> map) {
 
         Map<String, Object> transformedEntries = new LinkedHashMap<String, Object>(map.size());
         for ( Map.Entry<String, JcrPropertyMapCacheEntry> entry : map.entrySet() )
@@ -608,7 +608,7 @@ public final class JcrModifiableValueMap
     /**
      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
      */
-    public Object put(String aKey, Object value) {
+    public Object put(final String aKey, final Object value) {
         final String key = checkKey(aKey);
         if ( key.indexOf('/') != -1 ) {
             throw new IllegalArgumentException("Invalid key: " + key);
@@ -622,10 +622,16 @@ public final class JcrModifiableValueMap
             final JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(value, getNode().getSession());
             this.cache.put(key, entry);
             final String name = escapeKeyName(key);
-            if ( entry.isMulti ) {
-                node.setProperty(name, entry.values);
+            if ( NodeUtil.MIXIN_TYPES.equals(name) ) {
+                NodeUtil.handleMixinTypes(node, entry.values);
+            } else if ( "jcr:primaryType".equals(name) ) {
+                node.setPrimaryType(entry.values[0].getString());
             } else {
-                node.setProperty(name, entry.values[0]);
+                if ( entry.isMulti ) {
+                    node.setProperty(name, entry.values);
+                } else {
+                    node.setProperty(name, entry.values[0]);
+                }
             }
         } catch (final RepositoryException re) {
             throw new IllegalArgumentException("Value for key " + key + " can't be put into node: " + value, re);
@@ -638,8 +644,7 @@ public final class JcrModifiableValueMap
     /**
      * @see java.util.Map#putAll(java.util.Map)
      */
-    public void putAll(Map<? extends String, ? extends Object> t) {
-        readFully();
+    public void putAll(final Map<? extends String, ? extends Object> t) {
         if ( t != null ) {
             final Iterator<?> i = t.entrySet().iterator();
             while (i.hasNext() ) {
@@ -653,7 +658,7 @@ public final class JcrModifiableValueMap
     /**
      * @see java.util.Map#remove(java.lang.Object)
      */
-    public Object remove(Object aKey) {
+    public Object remove(final Object aKey) {
         final String key = checkKey(aKey.toString());
         readFully();
         final Object oldValue = this.cache.remove(key);

Added: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java?rev=1451548&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java (added)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java Fri Mar  1 09:57:52 2013
@@ -0,0 +1,63 @@
+/*
+ * 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.jcr.resource.internal;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.nodetype.NodeType;
+
+public abstract class NodeUtil {
+
+    /** Property for the mixin node types. */
+    public static final String MIXIN_TYPES = "jcr:mixinTypes";
+
+    /** Property for the node type. */
+    public static final String NODE_TYPE = "jcr:primaryType";
+
+    /**
+     * Update the mixin node types
+     */
+    public static void handleMixinTypes(final Node node, final Value[] mixinTypes)
+    throws RepositoryException {
+        final Set<String> newTypes = new HashSet<String>();
+        if ( mixinTypes != null ) {
+            for(final Value value : mixinTypes ) {
+                newTypes.add(value.getString());
+            }
+        }
+        final Set<String> oldTypes = new HashSet<String>();
+        for(final NodeType mixinType : node.getMixinNodeTypes()) {
+            oldTypes.add(mixinType.getName());
+        }
+        for(final String name : oldTypes) {
+            if ( !newTypes.contains(name) ) {
+                node.removeMixin(name);
+            } else {
+                newTypes.remove(name);
+            }
+        }
+        for(final String name : newTypes) {
+            node.addMixin(name);
+        }
+    }
+}

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java?rev=1451548&r1=1451547&r2=1451548&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java (original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java Fri Mar  1 09:57:52 2013
@@ -53,6 +53,7 @@ import org.apache.sling.api.resource.Val
 import org.apache.sling.api.wrappers.ValueMapDecorator;
 import org.apache.sling.jcr.resource.JcrResourceUtil;
 import org.apache.sling.jcr.resource.internal.JcrModifiableValueMap;
+import org.apache.sling.jcr.resource.internal.NodeUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -386,7 +387,7 @@ public class JcrResourceProvider
     public Resource create(final ResourceResolver resolver, final String path, final Map<String, Object> properties)
     throws PersistenceException {
         // check for node type
-        final Object nodeObj = (properties != null ? properties.get("jcr:primaryType") : null);
+        final Object nodeObj = (properties != null ? properties.get(NodeUtil.NODE_TYPE) : null);
         final String nodeType = (nodeObj != null ? nodeObj.toString() : null);
         try {
             final int lastPos = path.lastIndexOf('/');
@@ -407,8 +408,13 @@ public class JcrResourceProvider
             if ( properties != null ) {
                 // create modifiable map
                 final JcrModifiableValueMap jcrMap = new JcrModifiableValueMap(node, this.dynamicClassLoader);
+                // check mixin types first
+                final Object value = properties.get(NodeUtil.MIXIN_TYPES);
+                if ( value != null ) {
+                    jcrMap.put(NodeUtil.MIXIN_TYPES, value);
+                }
                 for(final Map.Entry<String, Object> entry : properties.entrySet()) {
-                    if ( !"jcr:primaryType".equals(entry.getKey()) ) {
+                    if ( !NodeUtil.NODE_TYPE.equals(entry.getKey()) && !NodeUtil.MIXIN_TYPES.equals(entry.getKey())) {
                         try {
                             jcrMap.put(entry.getKey(), entry.getValue());
                         } catch (final IllegalArgumentException iae) {