jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r1430392 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/security/user/ main/java/org/apache/jackrabbit/oak/security/user/query/ main/java/org/apache/jackrabbit/oak/spi/security/user/util/ test/java/org/apache/j...
Date Tue, 08 Jan 2013 17:15:45 GMT
Author: angela
Date: Tue Jan  8 17:15:44 2013
New Revision: 1430392

URL: http://svn.apache.org/viewvc?rev=1430392&view=rev
Log:
OAK-480 : Authorizable Query

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/QueryUtil.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserQueryManager.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryBuilder.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryEvaluator.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/user/util/UserUtility.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserManagerImplTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserQueryManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserQueryManager.java?rev=1430392&r1=1430391&r2=1430392&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserQueryManager.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserQueryManager.java
Tue Jan  8 17:15:44 2013
@@ -28,12 +28,11 @@ import org.apache.jackrabbit.api.securit
 import org.apache.jackrabbit.oak.api.QueryEngine;
 import org.apache.jackrabbit.oak.api.Result;
 import org.apache.jackrabbit.oak.api.Root;
+import org.apache.jackrabbit.oak.security.user.query.QueryUtil;
 import org.apache.jackrabbit.oak.security.user.query.ResultRowToAuthorizable;
 import org.apache.jackrabbit.oak.security.user.query.XPathQueryBuilder;
 import org.apache.jackrabbit.oak.security.user.query.XPathQueryEvaluator;
 import org.apache.jackrabbit.oak.spi.security.user.AuthorizableType;
-import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
-import org.apache.jackrabbit.oak.spi.security.user.util.UserUtility;
 import org.apache.jackrabbit.util.ISO9075;
 import org.apache.jackrabbit.util.Text;
 import org.slf4j.Logger;
@@ -44,25 +43,14 @@ import org.slf4j.LoggerFactory;
  */
 class UserQueryManager {
 
-    /**
-     * logger instance
-     */
     private static final Logger log = LoggerFactory.getLogger(UserQueryManager.class);
 
     private final UserManagerImpl userManager;
     private final Root root;
 
-    private final String userRoot;
-    private final String groupRoot;
-    private final String authorizableRoot;
-
     UserQueryManager(UserManagerImpl userManager, Root root) {
         this.userManager = userManager;
         this.root = root;
-
-        userRoot = UserUtility.getAuthorizableRootPath(userManager.getConfig(), AuthorizableType.USER);
-        groupRoot = UserUtility.getAuthorizableRootPath(userManager.getConfig(), AuthorizableType.GROUP);
-        authorizableRoot = UserUtility.getAuthorizableRootPath(userManager.getConfig(), AuthorizableType.AUTHORIZABLE);
     }
 
     @Nonnull
@@ -70,7 +58,7 @@ class UserQueryManager {
         // TODO OAK-253: replace usage of XPATH
         XPathQueryBuilder builder = new XPathQueryBuilder();
         query.build(builder);
-        return new XPathQueryEvaluator(builder, userManager, root, userManager.getNamePathMapper()).eval();
+        return new XPathQueryEvaluator(builder, userManager, root, userManager.getNamePathMapper(),
userManager.getConfig()).eval();
     }
 
     @Nonnull
@@ -118,39 +106,40 @@ class UserQueryManager {
     @Nonnull
     private String buildXPathStatement(String relPath, String value, boolean exact, AuthorizableType
type) {
         StringBuilder stmt = new StringBuilder();
-        String searchRoot = getSearchRoot(type);
+        String searchRoot = QueryUtil.getSearchRoot(type, userManager.getConfig());
         if (!"/".equals(searchRoot)) {
             stmt.append(searchRoot);
         }
 
-        String path;
         String propName;
+        String path;
         String ntName;
-        if (relPath.indexOf('/') == -1 && value != null) {
+        if (relPath.indexOf('/') == -1) {
             // search for properties somewhere below an authorizable node
-            path = null;
             propName = userManager.getNamePathMapper().getOakName(relPath);
+            path = null;
             ntName = null;
         } else {
             // FIXME: proper normalization of the relative path
             String oakPath = userManager.getNamePathMapper().getOakPath(relPath);
-            path = Text.getRelativeParent(oakPath, 1);
             propName = Text.getName(oakPath);
-            ntName = getNodeTypeName(type);
+            path = Text.getRelativeParent(oakPath, 1);
+            ntName = QueryUtil.getNodeTypeName(type);
         }
 
         stmt.append("//");
         if (path != null && !path.isEmpty()) {
             stmt.append(path);
+        } else if (ntName != null) {
+            stmt.append("element(*,").append(ntName).append(')');
         } else {
-            if (ntName != null) {
-                stmt.append("element(*,").append(ntName).append(')');
-            } else {
-                stmt.append("element(*)");
-            }
+            stmt.append("element(*)");
         }
 
-        if (value != null) {
+        if (value == null) {
+            // property must exist
+            stmt.append("[@").append(propName).append(']');
+        } else {
             stmt.append('[');
             stmt.append((exact) ? "@" : "jcr:like(@");
             stmt.append(ISO9075.encode(propName));
@@ -168,25 +157,6 @@ class UserQueryManager {
         return stmt.toString();
     }
 
-    /**
-     * @param type
-     * @return The path of search root for the specified authorizable type.
-     */
-    @Nonnull
-    private String getSearchRoot(AuthorizableType type) {
-        String path;
-        if (type == AuthorizableType.USER) {
-            path = userRoot;
-        } else if (type == AuthorizableType.GROUP) {
-            path = groupRoot;
-        } else {
-            path = authorizableRoot;
-        }
-        StringBuilder searchRoot = new StringBuilder();
-        searchRoot.append("/jcr:root").append(path);
-        return searchRoot.toString();
-    }
-
     @Nonnull
     private static String escapeForQuery(String value) {
         StringBuilder ret = new StringBuilder();
@@ -202,15 +172,4 @@ class UserQueryManager {
         }
         return ret.toString();
     }
-
-    @Nonnull
-    private static String getNodeTypeName(AuthorizableType type) {
-        if (type == AuthorizableType.USER) {
-            return UserConstants.NT_REP_USER;
-        } else if (type == AuthorizableType.GROUP) {
-            return UserConstants.NT_REP_GROUP;
-        } else {
-            return UserConstants.NT_REP_AUTHORIZABLE;
-        }
-    }
 }
\ No newline at end of file

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/QueryUtil.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/QueryUtil.java?rev=1430392&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/QueryUtil.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/QueryUtil.java
Tue Jan  8 17:15:44 2013
@@ -0,0 +1,72 @@
+/*
+ * 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.security.user.query;
+
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
+import org.apache.jackrabbit.oak.spi.security.user.AuthorizableType;
+import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
+import org.apache.jackrabbit.oak.spi.security.user.util.UserUtility;
+
+/**
+ * QueryUtil... TODO
+ */
+public class QueryUtil {
+
+    private QueryUtil() {}
+
+    /**
+     * Determine the search root for the given authorizable type based on the
+     * configured root path.
+     *
+     * @param type The authorizable type.
+     * @param config The configuration parameters.
+     * @return The path of search root for the specified authorizable type.
+     */
+    @Nonnull
+    public static String getSearchRoot(AuthorizableType type, ConfigurationParameters config)
{
+        String path;
+        if (type == AuthorizableType.USER) {
+            path = UserUtility.getAuthorizableRootPath(config, AuthorizableType.USER);
+        } else if (type == AuthorizableType.GROUP) {
+            path = UserUtility.getAuthorizableRootPath(config, AuthorizableType.GROUP);
+        } else {
+            path = UserUtility.getAuthorizableRootPath(config, AuthorizableType.AUTHORIZABLE);
+        }
+        StringBuilder searchRoot = new StringBuilder();
+        searchRoot.append("/jcr:root").append(path);
+        return searchRoot.toString();
+    }
+
+    /**
+     * Retrieve the node type name for the specified authorizable type.
+     *
+     * @param type The authorizable type.
+     * @return The corresponding node type name.
+     */
+    @Nonnull
+    public static String getNodeTypeName(AuthorizableType type) {
+        if (type == AuthorizableType.USER) {
+            return UserConstants.NT_REP_USER;
+        } else if (type == AuthorizableType.GROUP) {
+            return UserConstants.NT_REP_GROUP;
+        } else {
+            return UserConstants.NT_REP_AUTHORIZABLE;
+        }
+    }
+}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryBuilder.java?rev=1430392&r1=1430391&r2=1430392&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryBuilder.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryBuilder.java
Tue Jan  8 17:15:44 2013
@@ -19,11 +19,14 @@ package org.apache.jackrabbit.oak.securi
 import javax.jcr.Value;
 
 import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.api.security.user.Group;
 import org.apache.jackrabbit.api.security.user.QueryBuilder;
+import org.apache.jackrabbit.api.security.user.User;
+import org.apache.jackrabbit.oak.spi.security.user.AuthorizableType;
 
 public class XPathQueryBuilder implements QueryBuilder<Condition> {
 
-    private Class<? extends Authorizable> selector = Authorizable.class;
+    private AuthorizableType selectorType = AuthorizableType.AUTHORIZABLE;
     private String groupName;
     private boolean declaredMembersOnly;
     private Condition condition;
@@ -37,7 +40,13 @@ public class XPathQueryBuilder implement
     //-------------------------------------------------------< QueryBuilder >---
     @Override
     public void setSelector(Class<? extends Authorizable> selector) {
-        this.selector = selector;
+        if (User.class.isAssignableFrom(selector)) {
+            selectorType = AuthorizableType.USER;
+        } else if (Group.class.isAssignableFrom(selector)) {
+            selectorType = AuthorizableType.GROUP;
+        } else {
+            selectorType = AuthorizableType.AUTHORIZABLE;
+        }
     }
 
     @Override
@@ -155,8 +164,8 @@ public class XPathQueryBuilder implement
         return new Condition.Property(relPath, op, value);
     }
 
-    Class<? extends Authorizable> getSelector() {
-        return selector;
+    AuthorizableType getSelectorType() {
+        return selectorType;
     }
 
     String getGroupName() {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryEvaluator.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryEvaluator.java?rev=1430392&r1=1430391&r2=1430392&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryEvaluator.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/query/XPathQueryEvaluator.java
Tue Jan  8 17:15:44 2013
@@ -30,11 +30,11 @@ import com.google.common.collect.Iterato
 import org.apache.jackrabbit.api.security.user.Authorizable;
 import org.apache.jackrabbit.api.security.user.Group;
 import org.apache.jackrabbit.api.security.user.QueryBuilder;
-import org.apache.jackrabbit.api.security.user.User;
 import org.apache.jackrabbit.api.security.user.UserManager;
 import org.apache.jackrabbit.oak.api.ResultRow;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.namepath.NamePathMapper;
+import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
 import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
 import org.apache.jackrabbit.util.Text;
 import org.slf4j.Logger;
@@ -51,15 +51,18 @@ public class XPathQueryEvaluator impleme
     private final UserManager userManager;
     private final Root root;
     private final NamePathMapper namePathMapper;
+    private final ConfigurationParameters config;
 
-    private final StringBuilder xPath = new StringBuilder();
+    private final StringBuilder statement = new StringBuilder();
 
     public XPathQueryEvaluator(XPathQueryBuilder builder, UserManager userManager,
-                               Root root, NamePathMapper namePathMapper) {
+                               Root root, NamePathMapper namePathMapper,
+                               ConfigurationParameters config) {
         this.builder = builder;
         this.userManager = userManager;
         this.root = root;
         this.namePathMapper = namePathMapper;
+        this.config = config;
     }
 
     public Iterator<Authorizable> eval() throws RepositoryException {
@@ -67,8 +70,9 @@ public class XPathQueryEvaluator impleme
             return Iterators.emptyIterator();
         }
 
-        xPath.append("//element(*,")
-                .append(getNtName(builder.getSelector()))
+        statement.append(QueryUtil.getSearchRoot(builder.getSelectorType(), config))
+                .append("//element(*,")
+                .append(QueryUtil.getNodeTypeName(builder.getSelectorType()))
                 .append(')');
 
         Value bound = builder.getBound();
@@ -89,14 +93,14 @@ public class XPathQueryEvaluator impleme
         }
 
         if (condition != null) {
-            xPath.append('[');
+            statement.append('[');
             condition.accept(this);
-            xPath.append(']');
+            statement.append(']');
         }
 
         if (sortCol != null) {
             boolean ignoreCase = builder.getSortIgnoreCase();
-            xPath.append(" order by ")
+            statement.append(" order by ")
                     .append(ignoreCase ? "" : "fn:lower-case(")
                     .append(sortCol)
                     .append(ignoreCase ? " " : ") ")
@@ -126,7 +130,7 @@ public class XPathQueryEvaluator impleme
     //---------------------------------------------------< ConditionVisitor >---
     @Override
     public void visit(Condition.Node condition) throws RepositoryException {
-        xPath.append('(')
+        statement.append('(')
                 .append("jcr:like(@")
                 .append(namePathMapper.getJcrName(UserConstants.REP_PRINCIPAL_NAME))
                 .append(",'")
@@ -143,15 +147,15 @@ public class XPathQueryEvaluator impleme
     public void visit(Condition.Property condition) throws RepositoryException {
         RelationOp relOp = condition.getOp();
         if (relOp == RelationOp.EX) {
-            xPath.append(condition.getRelPath());
+            statement.append(condition.getRelPath());
         } else if (relOp == RelationOp.LIKE) {
-            xPath.append("jcr:like(")
+            statement.append("jcr:like(")
                     .append(condition.getRelPath())
                     .append(",'")
                     .append(condition.getPattern())
                     .append("')");
         } else {
-            xPath.append(condition.getRelPath())
+            statement.append(condition.getRelPath())
                     .append(condition.getOp().getOp())
                     .append(format(condition.getValue()));
         }
@@ -159,7 +163,7 @@ public class XPathQueryEvaluator impleme
 
     @Override
     public void visit(Condition.Contains condition) {
-        xPath.append("jcr:contains(")
+        statement.append("jcr:contains(")
                 .append(condition.getRelPath())
                 .append(",'")
                 .append(condition.getSearchExpr())
@@ -168,41 +172,41 @@ public class XPathQueryEvaluator impleme
 
     @Override
     public void visit(Condition.Impersonation condition) {
-        xPath.append("@rep:impersonators='")
+        statement.append("@rep:impersonators='")
                 .append(condition.getName())
                 .append('\'');
     }
 
     @Override
     public void visit(Condition.Not condition) throws RepositoryException {
-        xPath.append("not(");
+        statement.append("not(");
         condition.getCondition().accept(this);
-        xPath.append(')');
+        statement.append(')');
     }
 
     @Override
     public void visit(Condition.And condition) throws RepositoryException {
         int count = 0;
         for (Condition c : condition) {
-            xPath.append(count++ > 0 ? " and " : "");
+            statement.append(count++ > 0 ? " and " : "");
             c.accept(this);
         }
     }
 
     @Override
     public void visit(Condition.Or condition) throws RepositoryException {
-        int pos = xPath.length();
+        int pos = statement.length();
 
         int count = 0;
         for (Condition c : condition) {
-            xPath.append(count++ > 0 ? " or " : "");
+            statement.append(count++ > 0 ? " or " : "");
             c.accept(this);
         }
 
         // Surround or clause with parentheses if it contains more than one term
         if (count > 1) {
-            xPath.insert(pos, '(');
-            xPath.append(')');
+            statement.insert(pos, '(');
+            statement.append(')');
         }
     }
 
@@ -238,18 +242,6 @@ public class XPathQueryEvaluator impleme
         return result.toString();
     }
 
-    @Nonnull
-    private String getNtName(Class<? extends Authorizable> selector) {
-        String ntName;
-        if (User.class.isAssignableFrom(selector)) {
-            ntName = namePathMapper.getJcrName(UserConstants.NT_REP_USER);
-        } else if (Group.class.isAssignableFrom(selector)) {
-            ntName = namePathMapper.getJcrName(UserConstants.NT_REP_GROUP);
-        } else {
-            ntName = namePathMapper.getJcrName(UserConstants.NT_REP_AUTHORIZABLE);
-        }
-        return ntName;
-    }
 
     @Nonnull
     private static String format(Value value) throws RepositoryException {
@@ -284,7 +276,7 @@ public class XPathQueryEvaluator impleme
 
     @Nonnull
     private Iterator<Authorizable> findAuthorizables(long limit, long offset) throws
ParseException {
-        Iterable<? extends ResultRow> resultRows = root.getQueryEngine().executeQuery(xPath.toString(),
Query.XPATH, limit, offset, null, namePathMapper).getRows();
+        Iterable<? extends ResultRow> resultRows = root.getQueryEngine().executeQuery(statement.toString(),
Query.XPATH, limit, offset, null, namePathMapper).getRows();
         Iterator<Authorizable> authorizables = Iterators.transform(resultRows.iterator(),
new ResultRowToAuthorizable(userManager));
         return Iterators.filter(authorizables, Predicates.<Object>notNull());
     }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/user/util/UserUtility.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/user/util/UserUtility.java?rev=1430392&r1=1430391&r2=1430392&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/user/util/UserUtility.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/security/user/util/UserUtility.java
Tue Jan  8 17:15:44 2013
@@ -32,7 +32,7 @@ import static org.apache.jackrabbit.oak.
 /**
  * UserUtils... TODO
  */
-public final class UserUtility implements UserConstants{
+public final class UserUtility implements UserConstants {
 
     @Nonnull
     public static String getAdminId(ConfigurationParameters parameters) {

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserManagerImplTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserManagerImplTest.java?rev=1430392&r1=1430391&r2=1430392&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserManagerImplTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/security/user/UserManagerImplTest.java
Tue Jan  8 17:15:44 2013
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.oak.securi
 
 import java.security.Principal;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import javax.jcr.RepositoryException;
 import javax.jcr.UnsupportedRepositoryOperationException;
@@ -219,4 +220,13 @@ public class UserManagerImplTest extends
             }
         }
     }
+
+    @Test
+    public void testFindWithNullValue() throws RepositoryException {
+        Iterator<Authorizable> result = userMgr.findAuthorizables(UserConstants.REP_PRINCIPAL_NAME,
null);
+        assertTrue(result.hasNext());
+
+        result = userMgr.findAuthorizables("./"+UserConstants.REP_PRINCIPAL_NAME, null);
+        assertTrue(result.hasNext());
+    }
 }
\ No newline at end of file



Mime
View raw message