jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r1401283 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user: UserManagerImpl.java UserQueryManager.java query/XPathQueryEvaluator.java
Date Tue, 23 Oct 2012 13:06:45 GMT
Author: angela
Date: Tue Oct 23 13:06:44 2012
New Revision: 1401283

URL: http://svn.apache.org/viewvc?rev=1401283&view=rev
Log:
OAK-50 : Implement User Management (WIP)

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserManagerImpl.java
    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/XPathQueryEvaluator.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserManagerImpl.java?rev=1401283&r1=1401282&r2=1401283&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserManagerImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserManagerImpl.java
Tue Oct 23 13:06:44 2012
@@ -422,7 +422,7 @@ public class UserManagerImpl implements 
 
     private UserQueryManager getQueryManager() throws RepositoryException {
         if (queryManager == null) {
-            queryManager = new UserQueryManager(this, session, root);
+            queryManager = new UserQueryManager(this, root);
         }
         return queryManager;
     }

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=1401283&r1=1401282&r2=1401283&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 Oct 23 13:06:44 2012
@@ -16,21 +16,26 @@
  */
 package org.apache.jackrabbit.oak.security.user;
 
+import java.text.ParseException;
+import java.util.Collections;
 import java.util.Iterator;
+import java.util.Map;
 import javax.annotation.Nonnull;
-import javax.jcr.Node;
-import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-import javax.jcr.query.QueryManager;
 
 import com.google.common.base.Function;
+import com.google.common.base.Predicates;
 import com.google.common.collect.Iterators;
 import org.apache.jackrabbit.api.security.user.Authorizable;
 import org.apache.jackrabbit.api.security.user.Query;
+import org.apache.jackrabbit.oak.api.PropertyValue;
+import org.apache.jackrabbit.oak.api.Result;
+import org.apache.jackrabbit.oak.api.ResultRow;
 import org.apache.jackrabbit.oak.api.Root;
+import org.apache.jackrabbit.oak.api.SessionQueryEngine;
 import org.apache.jackrabbit.oak.security.user.query.XPathQueryBuilder;
 import org.apache.jackrabbit.oak.security.user.query.XPathQueryEvaluator;
+import org.apache.jackrabbit.oak.spi.query.PropertyValues;
 import org.apache.jackrabbit.oak.spi.security.user.AuthorizableType;
 import org.apache.jackrabbit.oak.spi.security.user.UserConstants;
 import org.apache.jackrabbit.util.ISO9075;
@@ -50,20 +55,17 @@ class UserQueryManager {
 
     private final UserManagerImpl userManager;
     private final Root root;
-    private final QueryManager queryManager;
 
     private final String userRoot;
     private final String groupRoot;
     private final String authorizableRoot;
 
-    // TODO: replace usage of jcr-query-manager by oak query manager and drop session from
constructor.
-    UserQueryManager(UserManagerImpl userManager, Session session, Root root) throws RepositoryException
{
+    UserQueryManager(UserManagerImpl userManager, Root root) throws RepositoryException {
         this.userManager = userManager;
         this.root = root;
-        this.queryManager = (session != null) ? session.getWorkspace().getQueryManager()
: null;
 
         this.userRoot = userManager.getConfig().getConfigValue(UserConstants.PARAM_USER_PATH,
UserConstants.DEFAULT_USER_PATH);
-        this.groupRoot = userManager.getConfig().getConfigValue(UserConstants.PARAM_GROUP_PATH,
UserConstants.DEFAULT_GROUP_PATH);;
+        this.groupRoot = userManager.getConfig().getConfigValue(UserConstants.PARAM_GROUP_PATH,
UserConstants.DEFAULT_GROUP_PATH);
 
         String parent = userRoot;
         while (!Text.isDescendant(parent, groupRoot)) {
@@ -72,16 +74,11 @@ class UserQueryManager {
         authorizableRoot = parent;
     }
 
+    @Nonnull
     Iterator<Authorizable> find(Query query) throws RepositoryException {
-        // TODO: create query builder depending query-language configured with user-mgt configuration.
-        if (queryManager != null) {
-            XPathQueryBuilder builder = new XPathQueryBuilder();
-            query.build(builder);
-            return new XPathQueryEvaluator(builder, userManager, queryManager, userManager.getNamePathMapper()).eval();
-        } else {
-            // TODO: implement
-            throw new UnsupportedOperationException("not implemented");
-        }
+        XPathQueryBuilder builder = new XPathQueryBuilder();
+        query.build(builder);
+        return new XPathQueryEvaluator(builder, userManager, root, userManager.getNamePathMapper()).eval();
     }
 
     @Nonnull
@@ -108,21 +105,24 @@ class UserQueryManager {
      * @return An iterator of authorizable trees that match the specified
      * search parameters and filters or an empty iterator if no result can be
      * found.
+     * @throws javax.jcr.RepositoryException If an error occurs.
      */
     @Nonnull
     Iterator<Authorizable> findAuthorizables(String relPath, String value,
-                                             boolean exact, AuthorizableType type)
-            throws RepositoryException {
-        // TODO replace usage of jcr query manager by oak-query api.
+                                             boolean exact, AuthorizableType type) throws
RepositoryException {
         String statement = buildXPathStatement(relPath, value, exact, type);
-        if (queryManager != null) {
-            NodeIterator results = queryManager.createQuery(statement, javax.jcr.query.Query.XPATH).execute().getNodes();
-            return Iterators.transform(results, new NodeToAuthorizable());
-        } else {
-            throw new UnsupportedOperationException("not yet implemented");
+        SessionQueryEngine queryEngine = root.getQueryEngine();
+        try {
+            Map<String,PropertyValue> bindings = (value != null) ? Collections.singletonMap("propValue",
PropertyValues.newString(value)) : null;
+            Result result = queryEngine.executeQuery(statement, javax.jcr.query.Query.XPATH,
Long.MAX_VALUE, 0, bindings, root, userManager.getNamePathMapper());
+            return Iterators.filter(Iterators.transform(result.getRows().iterator(), new
ResultRowToAuthorizable()), Predicates.<Object>notNull());
+        } catch (ParseException e) {
+            throw new RepositoryException(e);
         }
     }
 
+    //------------------------------------------------------------< private >---
+    @Nonnull
     private String buildXPathStatement(String relPath, String value, boolean exact, AuthorizableType
type) {
         StringBuilder stmt = new StringBuilder();
         String searchRoot = getSearchRoot(type);
@@ -180,6 +180,7 @@ class UserQueryManager {
      * @param type
      * @return The path of search root for the specified authorizable type.
      */
+    @Nonnull
     private String getSearchRoot(AuthorizableType type) {
         if (type == AuthorizableType.USER) {
             return userRoot;
@@ -190,6 +191,7 @@ class UserQueryManager {
         }
     }
 
+    @Nonnull
     private static String escapeForQuery(String value) {
         StringBuilder ret = new StringBuilder();
         for (int i = 0; i < value.length(); i++) {
@@ -205,6 +207,7 @@ class UserQueryManager {
         return ret.toString();
     }
 
+    @Nonnull
     private static String getNodeTypeName(AuthorizableType type) {
         if (type == AuthorizableType.USER) {
             return UserConstants.NT_REP_USER;
@@ -215,13 +218,13 @@ class UserQueryManager {
         }
     }
 
-    private class NodeToAuthorizable implements Function<Node, Authorizable> {
+    private class ResultRowToAuthorizable implements Function<ResultRow, Authorizable>
{
         @Override
-        public Authorizable apply(Node node) {
+        public Authorizable apply(ResultRow row) {
             try {
-                return userManager.getAuthorizable(node.getPath());
+                return userManager.getAuthorizable(row.getPath());
             } catch (RepositoryException e) {
-                log.debug("Failed to access authorizable " + node);
+                log.debug("Failed to access authorizable " + row.getPath());
                 return null;
             }
         }

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=1401283&r1=1401282&r2=1401283&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 Oct 23 13:06:44 2012
@@ -16,15 +16,13 @@
  */
 package org.apache.jackrabbit.oak.security.user.query;
 
+import java.text.ParseException;
 import java.util.Iterator;
-
 import javax.annotation.Nonnull;
-import javax.jcr.Node;
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import javax.jcr.query.Query;
-import javax.jcr.query.QueryManager;
 
 import com.google.common.base.Function;
 import com.google.common.base.Predicate;
@@ -35,6 +33,8 @@ import org.apache.jackrabbit.api.securit
 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.user.UserConstants;
 import org.apache.jackrabbit.util.Text;
@@ -44,38 +44,36 @@ import org.slf4j.LoggerFactory;
 /**
  * This evaluator for {@link org.apache.jackrabbit.api.security.user.Query}s use XPath
  * and some minimal client side filtering.
- *
- * FIXME: replace usage of jcr-query manager by oak-api SessionQueryEngine.
  */
 public class XPathQueryEvaluator implements ConditionVisitor {
     static final Logger log = LoggerFactory.getLogger(XPathQueryEvaluator.class);
 
     private final XPathQueryBuilder builder;
     private final UserManager userManager;
-    private final QueryManager queryManager;
+    private final Root root;
     private final NamePathMapper namePathMapper;
 
     private final StringBuilder xPath = new StringBuilder();
 
     public XPathQueryEvaluator(XPathQueryBuilder builder, UserManager userManager,
-                               QueryManager queryManager, NamePathMapper namePathMapper)
{
+                               Root root, NamePathMapper namePathMapper) {
         this.builder = builder;
         this.userManager = userManager;
-        this.queryManager = queryManager;
+        this.root = root;
         this.namePathMapper = namePathMapper;
     }
 
     public Iterator<Authorizable> eval() throws RepositoryException {
+        // shortcut
+        if (builder.getMaxCount() == 0) {
+            return Iterators.emptyIterator();
+        }
+
         xPath.append("//element(*,")
                 .append(getNtName(builder.getSelector()))
                 .append(')');
 
         Value bound = builder.getBound();
-        long offset = builder.getOffset();
-        if (bound != null && offset > 0) {
-            log.warn("Found bound {} and offset {} in limit. Discarding offset.", bound,
offset);
-            offset = 0;
-        }
 
         Condition condition = builder.getCondition();
         String sortCol = builder.getSortProperty();
@@ -106,27 +104,28 @@ public class XPathQueryEvaluator impleme
                     .append(sortDir.getDirection());
         }
 
-        Query query = queryManager.createQuery(xPath.toString(), Query.XPATH);
-        long maxCount = builder.getMaxCount();
-        if (maxCount == 0) {
-            return Iterators.emptyIterator();
+        try {
+            if (builder.getGroupName() == null) {
+                long offset = builder.getOffset();
+                if (bound != null && offset > 0) {
+                    log.warn("Found bound {} and offset {} in limit. Discarding offset.",
bound, offset);
+                    offset = 0;
+                }
+                return findAuthorizables(builder.getMaxCount(), offset);
+            } else {
+                // filtering by group name not included in query -> enforce offset
+                // and limit on the result set.
+                Iterator<Authorizable> result = findAuthorizables(Long.MAX_VALUE, 0);
+                Iterator<Authorizable> filtered = filter(result, builder.getGroupName(),
builder.isDeclaredMembersOnly());
+                return ResultIterator.create(builder.getOffset(), builder.getMaxCount(),
filtered);
+            }
+        } catch (ParseException e) {
+            throw new RepositoryException(e);
         }
 
         // If we are scoped to a group and have a limit, we have to apply the limit
         // here (inefficient!) otherwise we can apply the limit in the query
-        if (builder.getGroupName() == null) {
-            if (offset > 0) {
-                query.setOffset(offset);
-            }
-            if (maxCount > 0) {
-                query.setLimit(maxCount);
-            }
-            return toAuthorizables(execute(query));
-        } else {
-            Iterator<Authorizable> result = toAuthorizables(execute(query));
-            Iterator<Authorizable> filtered = filter(result, builder.getGroupName(),
builder.isDeclaredMembersOnly());
-            return ResultIterator.create(offset, maxCount, filtered);
-        }
+
     }
 
     //---------------------------------------------------< ConditionVisitor >---
@@ -294,26 +293,22 @@ public class XPathQueryEvaluator impleme
     }
 
     @Nonnull
-    @SuppressWarnings("unchecked")
-    private static Iterator<Node> execute(Query query) throws RepositoryException {
-        return query.execute().getNodes();
-    }
+    private Iterator<Authorizable> findAuthorizables(long limit, long offset) throws
ParseException {
+        Iterable<? extends ResultRow> resultRows = root.getQueryEngine().executeQuery(xPath.toString(),
Query.XPATH, limit, offset, null, root, namePathMapper).getRows();
 
-    @Nonnull
-    private Iterator<Authorizable> toAuthorizables(Iterator<Node> nodes) {
-        Function<Node, Authorizable> transformer = new Function<Node, Authorizable>()
{
-            public Authorizable apply(Node node) {
+        Function<ResultRow, Authorizable> transformer = new Function<ResultRow,
Authorizable>() {
+            public Authorizable apply(ResultRow resultRow) {
                 try {
-                    return userManager.getAuthorizableByPath(node.getPath());
+                    return userManager.getAuthorizableByPath(resultRow.getPath());
                 } catch (RepositoryException e) {
-                    log.warn("Cannot create authorizable from node {}", node);
+                    log.warn("Cannot create authorizable from result row {}", resultRow);
                     log.debug(e.getMessage(), e);
                     return null;
                 }
             }
         };
 
-        return Iterators.transform(nodes, transformer);
+        return Iterators.filter(Iterators.transform(resultRows.iterator(), transformer),
Predicates.<Object>notNull());
     }
 
     @Nonnull



Mime
View raw message