You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by al...@apache.org on 2011/06/24 11:51:50 UTC

svn commit: r1139225 - in /jackrabbit/branches/2.2/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/query/lucene/ test/java/org/apache/jackrabbit/core/ test/java/org/apache/jackrabbit/core/query/

Author: alexparvulescu
Date: Fri Jun 24 09:51:50 2011
New Revision: 1139225

URL: http://svn.apache.org/viewvc?rev=1139225&view=rev
Log:
2.2: merged revision 1139224 (JCR-3001 DescendantSelfAxisQuery may fail with IOException when session has limited access)

Added:
    jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/LimitedAccessQueryTest.java   (with props)
Modified:
    jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/DescendantSelfAxisQuery.java
    jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/NodeImplTest.java
    jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/TestAll.java

Modified: jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/DescendantSelfAxisQuery.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/DescendantSelfAxisQuery.java?rev=1139225&r1=1139224&r2=1139225&view=diff
==============================================================================
--- jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/DescendantSelfAxisQuery.java (original)
+++ jackrabbit/branches/2.2/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/DescendantSelfAxisQuery.java Fri Jun 24 09:51:50 2011
@@ -16,6 +16,19 @@
  */
 package org.apache.jackrabbit.core.query.lucene;
 
+import java.io.IOException;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import javax.jcr.ItemNotFoundException;
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+
+import org.apache.jackrabbit.core.SessionImpl;
+import org.apache.jackrabbit.core.id.NodeId;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.search.Explanation;
 import org.apache.lucene.search.HitCollector;
@@ -23,26 +36,17 @@ import org.apache.lucene.search.Query;
 import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Searcher;
 import org.apache.lucene.search.Similarity;
-import org.apache.lucene.search.Weight;
 import org.apache.lucene.search.Sort;
-import org.apache.jackrabbit.core.SessionImpl;
+import org.apache.lucene.search.Weight;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.jcr.Node;
-import javax.jcr.RepositoryException;
-import java.io.IOException;
-import java.util.BitSet;
-import java.util.Set;
-import java.util.Map;
-import java.util.TreeMap;
-import java.util.Iterator;
-
 /**
  * Implements a lucene <code>Query</code> which filters a sub query by checking
  * whether the nodes selected by that sub query are descendants or self of
  * nodes selected by a context query.
  */
+@SuppressWarnings("serial")
 class DescendantSelfAxisQuery extends Query implements JackrabbitQuery {
 
     /**
@@ -239,13 +243,17 @@ class DescendantSelfAxisQuery extends Qu
                 }
 
                 ScoreNode sn;
-                try {
-                    while ((sn = result.nextScoreNode()) != null) {
-                        Node node = session.getNodeById(sn.getNodeId());
+                while ((sn = result.nextScoreNode()) != null) {
+                    NodeId id = sn.getNodeId();
+                    try {
+                        Node node = session.getNodeById(id);
                         startingPoints.put(node.getPath(), sn);
+                    } catch (ItemNotFoundException e) {
+                        // JCR-3001 node access denied, will just skip it
+                        log.warn("Access denied to node id {}.", id);
+                    } catch (RepositoryException e) {
+                        throw Util.createIOException(e);
                     }
-                } catch (RepositoryException e) {
-                    throw Util.createIOException(e);
                 }
             } finally {
                 result.close();
@@ -296,17 +304,23 @@ class DescendantSelfAxisQuery extends Qu
                     if (currentTraversal != null) {
                         currentTraversal.close();
                     }
-                    if (scoreNodes.hasNext()) {
+                    currentTraversal = null;
+                    // We only need one node, but because of the acls, we'll
+                    // iterate until we find a good one
+                    while (scoreNodes.hasNext()) {
                         ScoreNode sn = scoreNodes.next();
+                        NodeId id = sn.getNodeId();
                         try {
-                            Node node = session.getNodeById(sn.getNodeId());
-                            currentTraversal = new NodeTraversingQueryHits(node,
-                                    getMinLevels() == 0);
+                            Node node = session.getNodeById(id);
+                            currentTraversal = new NodeTraversingQueryHits(
+                                    node, getMinLevels() == 0);
+                            break;
+                        } catch (ItemNotFoundException e) {
+                            // JCR-3001 node access denied, will just skip it
+                            log.warn("Access denied to node id {}.", id);
                         } catch (RepositoryException e) {
                             throw Util.createIOException(e);
                         }
-                    } else {
-                        currentTraversal = null;
                     }
                 }
             };

Modified: jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/NodeImplTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/NodeImplTest.java?rev=1139225&r1=1139224&r2=1139225&view=diff
==============================================================================
--- jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/NodeImplTest.java (original)
+++ jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/NodeImplTest.java Fri Jun 24 09:51:50 2011
@@ -37,6 +37,7 @@ import org.apache.jackrabbit.api.securit
 import org.apache.jackrabbit.commons.JcrUtils;
 import org.apache.jackrabbit.test.AbstractJCRTest;
 import org.apache.jackrabbit.test.NotExecutableException;
+import org.apache.jackrabbit.test.RepositoryHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,7 +53,7 @@ public class NodeImplTest extends Abstra
         }
     }
 
-    private static void changeReadPermission(Principal principal, Node n, boolean allowRead) throws RepositoryException, NotExecutableException {
+    public static void changeReadPermission(Principal principal, Node n, boolean allowRead) throws RepositoryException, NotExecutableException {
         SessionImpl s = (SessionImpl) n.getSession();
         JackrabbitAccessControlList acl = null;
         AccessControlManager acMgr = s.getAccessControlManager();
@@ -84,8 +85,8 @@ public class NodeImplTest extends Abstra
         }
     }
 
-    private Principal getReadOnlyPrincipal() throws RepositoryException, NotExecutableException {
-        SessionImpl s = (SessionImpl) getHelper().getReadOnlySession();
+    public static Principal getReadOnlyPrincipal(RepositoryHelper helper) throws RepositoryException, NotExecutableException {
+        SessionImpl s = (SessionImpl) helper.getReadOnlySession();
         try {
             for (Principal p : s.getSubject().getPrincipals()) {
                 if (!(p instanceof Group)) {
@@ -110,7 +111,7 @@ public class NodeImplTest extends Abstra
         NodeImpl testNode = (NodeImpl) n.addNode(nodeName2);
         testRootNode.save();
 
-        Principal principal = getReadOnlyPrincipal();
+        Principal principal = getReadOnlyPrincipal(getHelper());
         changeReadPermission(principal, n, false);
         changeReadPermission(principal, testNode, true);
 

Added: jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/LimitedAccessQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/LimitedAccessQueryTest.java?rev=1139225&view=auto
==============================================================================
--- jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/LimitedAccessQueryTest.java (added)
+++ jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/LimitedAccessQueryTest.java Fri Jun 24 09:51:50 2011
@@ -0,0 +1,92 @@
+/*
+ * 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.core.query;
+
+import java.security.Principal;
+
+import javax.jcr.ItemNotFoundException;
+import javax.jcr.Node;
+import javax.jcr.Session;
+import javax.jcr.query.Query;
+
+import org.apache.jackrabbit.core.NodeImplTest;
+
+/**
+ * <code>LimitedAccessQueryTest</code> tests queries that include nodes that are
+ * outside their access.
+ */
+public class LimitedAccessQueryTest extends AbstractQueryTest {
+
+    private Session readOnly;
+    private Principal principal;
+
+    private Node a;
+    private Node b;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        a = testRootNode.addNode("a", "nt:unstructured");
+        a.setProperty("p", 1);
+        b = testRootNode.addNode("b", "nt:unstructured");
+        b.setProperty("p", 1);
+        superuser.save();
+
+        principal = NodeImplTest.getReadOnlyPrincipal(getHelper());
+        NodeImplTest.changeReadPermission(principal, a, false);
+        superuser.save();
+
+        readOnly = getHelper().getReadOnlySession();
+
+        // preliminary tests
+        try {
+            readOnly.getNodeByIdentifier(a.getIdentifier());
+            fail("Access to the node '" + a.getPath() + "' has to be denied.");
+        } catch (ItemNotFoundException e) {
+            // good acl
+        }
+
+        try {
+            readOnly.getNodeByIdentifier(b.getIdentifier());
+        } catch (ItemNotFoundException e) {
+            fail(e.getMessage());
+        }
+
+    }
+
+    protected void tearDown() throws Exception {
+        readOnly.logout();
+        NodeImplTest.changeReadPermission(principal, a, true);
+        super.tearDown();
+    }
+
+    /**
+     * this test is for the DescendantSelfAxisQuery class.
+     * 
+     * see <a href="https://issues.apache.org/jira/browse/JCR-3001">JCR-3001</a>
+     * 
+     * @throws Exception
+     */
+    @SuppressWarnings("deprecation")
+    public void testDescendantSelfAxisQuery() throws Exception {
+        String xpath = "/" + testRootNode.getPath() + "//*";
+        checkResult(
+                readOnly.getWorkspace().getQueryManager()
+                        .createQuery(xpath, Query.XPATH).execute(),
+                new Node[] { b });
+    }
+}

Propchange: jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/LimitedAccessQueryTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/TestAll.java?rev=1139225&r1=1139224&r2=1139225&view=diff
==============================================================================
--- jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/TestAll.java (original)
+++ jackrabbit/branches/2.2/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/TestAll.java Fri Jun 24 09:51:50 2011
@@ -68,6 +68,7 @@ public class TestAll extends TestCase {
         suite.addTestSuite(SQL2NodeLocalNameTest.class);
         suite.addTestSuite(SQL2OuterJoinTest.class);
         suite.addTestSuite(SQL2PathEscapingTest.class);
+        suite.addTestSuite(LimitedAccessQueryTest.class);
 
         return suite;
     }