You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ju...@apache.org on 2012/04/23 18:12:59 UTC

svn commit: r1329310 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins: ./ name/ name/NameValidator.java name/NameValidatorProvider.java type/ type/TypeValidator.java type/TypeValidatorProvider.java

Author: jukka
Date: Mon Apr 23 16:12:59 2012
New Revision: 1329310

URL: http://svn.apache.org/viewvc?rev=1329310&view=rev
Log:
OAK-68: Extension point for commit validation

Add basic JCR name and type validators as example plugins

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidator.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidatorProvider.java

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidator.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidator.java?rev=1329310&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidator.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidator.java Mon Apr 23 16:12:59 2012
@@ -0,0 +1,104 @@
+/*
+ * 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.jackrabbit.oak.plugins.name;
+
+import java.util.Set;
+
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.kernel.NodeState;
+import org.apache.jackrabbit.oak.kernel.NodeValidator;
+
+class NameValidator implements NodeValidator {
+
+    private final Set<String> prefixes;
+
+    public NameValidator(Set<String> prefixes) {
+        this.prefixes = prefixes;
+    }
+
+    protected void checkValidName(String name) throws CommitFailedException {
+        String prefix = null;
+        String local = name;
+
+        int colon = name.indexOf(':');
+        if (colon != -1) {
+            prefix = name.substring(0, colon);
+            local = name.substring(colon + 1);
+        }
+
+        if (!(prefix == null || prefixes.contains(prefix))
+                || !isValidLocalName(local)) {
+            throw new CommitFailedException(
+                    "Self or parent paths (. or ..) are not valid as names");
+        }
+    }
+
+    private boolean isValidLocalName(String local) {
+        if (".".equals(local) || "..".equals(local)) {
+            return false;
+        }
+
+        for (int i = 0; i < local.length(); i++) {
+            char ch = local.charAt(i);
+            if ("/:[]|*".indexOf(ch) != -1) { // TODO: XMLChar check
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    //-------------------------------------------------------< NodeValidator >
+
+    @Override
+    public void propertyAdded(PropertyState after)
+            throws CommitFailedException {
+        checkValidName(after.getName());
+    }
+
+    @Override
+    public void propertyChanged(PropertyState before, PropertyState after) {
+        // do nothing
+    }
+
+    @Override
+    public void propertyDeleted(PropertyState before) {
+        // do nothing
+    }
+
+    @Override
+    public NodeValidator childNodeAdded(String name, NodeState after)
+            throws CommitFailedException {
+        checkValidName(name);
+        return this;
+    }
+
+    @Override
+    public NodeValidator childNodeChanged(
+            String name, NodeState before, NodeState after)
+            throws CommitFailedException {
+        return this;
+    }
+
+    @Override
+    public NodeValidator childNodeDeleted(String name, NodeState before) {
+        // do nothing
+        return null;
+    }
+
+}

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java?rev=1329310&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java Mon Apr 23 16:12:59 2012
@@ -0,0 +1,55 @@
+/*
+ * 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.jackrabbit.oak.plugins.name;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.kernel.NodeState;
+import org.apache.jackrabbit.oak.kernel.NodeValidator;
+import org.apache.jackrabbit.oak.kernel.NodeValidatorProvider;
+
+public class NameValidatorProvider implements NodeValidatorProvider {
+
+    @Override
+    public NodeValidator getRootValidator(NodeState before, NodeState after) {
+        Set<String> prefixes = new HashSet<String>();
+
+        // Default JCR prefixes are always available
+        prefixes.add("jcr");
+        prefixes.add("nt");
+        prefixes.add("mix");
+
+        // Jackrabbit 2.x prefixes are always available
+        prefixes.add("rep");
+
+        // Find any extra prefixes from /jcr:system/jcr:namespaceRegistry
+        NodeState system = after.getChildNode("jcr:system");
+        if (system != null) {
+            NodeState registry = system.getChildNode("jcr:namespaces");
+            if (registry != null) {
+                for (PropertyState property : registry.getProperties()) {
+                    prefixes.add(property.getName());
+                }
+            }
+        }
+
+        return new NameValidator(prefixes);
+    }
+
+}

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java?rev=1329310&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java Mon Apr 23 16:12:59 2012
@@ -0,0 +1,97 @@
+/*
+ * 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.jackrabbit.oak.plugins.type;
+
+import java.util.Collections;
+import java.util.Set;
+
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Scalar;
+import org.apache.jackrabbit.oak.kernel.NodeState;
+import org.apache.jackrabbit.oak.kernel.NodeValidator;
+
+class TypeValidator implements NodeValidator {
+
+    private final Set<String> types;
+
+    public TypeValidator(Set<String> types) {
+        this.types = types;
+    }
+
+    private void checkTypeExists(PropertyState after)
+            throws CommitFailedException {
+        Iterable<Scalar> scalars = Collections.emptyList();
+        if ("jcr:primaryType".equals(after.getName())) {
+            scalars = Collections.singletonList(after.getScalar());
+        } else if ("jcr:mixinTypes".equals(after.getName())) {
+            scalars = after.getArray();
+        }
+        for (Scalar scalar : scalars) {
+            String value = scalar.getString();
+            if (!types.contains(value)) {
+                throw new CommitFailedException("Unknown node type: " + value);
+            }
+        }
+    }
+
+    //-------------------------------------------------------< NodeValidator >
+
+    @Override
+    public void propertyAdded(PropertyState after)
+            throws CommitFailedException {
+        checkTypeExists(after);
+        // TODO: validate added property
+    }
+
+    @Override
+    public void propertyChanged(PropertyState before, PropertyState after)
+            throws CommitFailedException {
+        checkTypeExists(after);
+        // TODO: validate changed property
+    }
+
+    @Override
+    public void propertyDeleted(PropertyState before)
+            throws CommitFailedException {
+        // TODO: validate removed property
+    }
+
+    @Override
+    public NodeValidator childNodeAdded(String name, NodeState after)
+            throws CommitFailedException {
+        // TODO: validate added child node
+        // TODO: get the type for validating the child contents
+        return this;
+    }
+
+    @Override
+    public NodeValidator childNodeChanged(
+            String name, NodeState before, NodeState after)
+            throws CommitFailedException {
+        // TODO: validate changed child node
+        // TODO: get the type to validating the child contents
+        return this;
+    }
+
+    @Override
+    public NodeValidator childNodeDeleted(String name, NodeState before) {
+        // TODO: validate removed child node
+        return null;
+    }
+
+}

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidatorProvider.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidatorProvider.java?rev=1329310&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidatorProvider.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidatorProvider.java Mon Apr 23 16:12:59 2012
@@ -0,0 +1,105 @@
+/*
+ * 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.jackrabbit.oak.plugins.type;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.jackrabbit.oak.kernel.ChildNodeEntry;
+import org.apache.jackrabbit.oak.kernel.NodeState;
+import org.apache.jackrabbit.oak.kernel.NodeValidator;
+import org.apache.jackrabbit.oak.kernel.NodeValidatorProvider;
+
+public class TypeValidatorProvider implements NodeValidatorProvider {
+
+    @Override
+    public NodeValidator getRootValidator(NodeState before, NodeState after) {
+        Set<String> types = new HashSet<String>();
+
+        // Default JCR types are always available
+        types.add("nt:base");
+        types.add("nt:hierarchyNode");
+        types.add("nt:file");
+        types.add("nt:linkedFile");
+        types.add("nt:folder");
+        types.add("nt:resource");
+        types.add("mix:title");
+        types.add("mix:created");
+        types.add("mix:lastModified");
+        types.add("mix:language");
+        types.add("mix:mimeType");
+        types.add("nt:address");
+        types.add("mix:etag");
+        types.add("nt:unstructured");
+        types.add("mix:referenceable");
+        types.add("mix:lockable");
+        types.add("mix:shareable");
+        types.add("mix:simpleVersionable");
+        types.add("mix:versionable");
+        types.add("nt:versionHistory");
+        types.add("nt:versionLabels");
+        types.add("nt:version");
+        types.add("nt:frozenNode");
+        types.add("nt:versionedChild");
+        types.add("nt:activity");
+        types.add("nt:configuration");
+        types.add("nt:nodeType");
+        types.add("nt:propertyDefinition");
+        types.add("nt:childNodeDefinition");
+        types.add("nt:query");
+        types.add("mix:lifecycle");
+
+        // Jackrabbit 2.x types are always available
+        types.add("rep:root");
+        types.add("rep:system");
+        types.add("rep:nodeTypes");
+        types.add("rep:versionStorage");
+        types.add("rep:Activities");
+        types.add("rep:Configurations");
+        types.add("rep:VersionReference");
+        types.add("rep:AccessControllable");
+        types.add("rep:RepoAccessControllable");
+        types.add("rep:Policy");
+        types.add("rep:ACL");
+        types.add("rep:ACE");
+        types.add("rep:GrantACE");
+        types.add("rep:DenyACE");
+        types.add("rep:AccessControl");
+        types.add("rep:PrincipalAccessControl");
+        types.add("rep:Authorizable");
+        types.add("rep:Impersonatable");
+        types.add("rep:User");
+        types.add("rep:Group");
+        types.add("rep:AuthorizableFolder");
+        types.add("rep:Members");
+        types.add("rep:RetentionManageable");
+
+        // Find any extra types from /jcr:system/jcr:nodeTypes
+        NodeState system = after.getChildNode("jcr:system");
+        if (system != null) {
+            NodeState registry = system.getChildNode("jcr:nodeTypes");
+            if (registry != null) {
+                for (ChildNodeEntry child : registry.getChildNodeEntries(0, -1)) {
+                    types.add(child.getName());
+                }
+            }
+        }
+
+        return new TypeValidator(types);
+    }
+
+}