You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by re...@apache.org on 2021/12/08 14:44:58 UTC

svn commit: r1895700 - in /jackrabbit/trunk/jackrabbit-spi2dav/src: main/java/org/apache/jackrabbit/spi2dav/IdURICache.java test/java/org/apache/jackrabbit/spi2dav/IdURICacheTest.java

Author: reschke
Date: Wed Dec  8 14:44:58 2021
New Revision: 1895700

URL: http://svn.apache.org/viewvc?rev=1895700&view=rev
Log:
JCR-4749: improve diagnostics

Added:
    jackrabbit/trunk/jackrabbit-spi2dav/src/test/java/org/apache/jackrabbit/spi2dav/IdURICacheTest.java   (with props)
Modified:
    jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/IdURICache.java

Modified: jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/IdURICache.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/IdURICache.java?rev=1895700&r1=1895699&r2=1895700&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/IdURICache.java (original)
+++ jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/IdURICache.java Wed Dec  8 14:44:58 2021
@@ -60,10 +60,7 @@ class IdURICache {
     }
 
     public void add(String uri, ItemId itemId) {
-        if (!uri.startsWith(workspaceUri)) {
-            throw new IllegalArgumentException("Workspace mismatch: '" + uri + "' not under '" + workspaceUri + "'");
-        }
-        String cleanUri = getCleanUri(uri);
+        String cleanUri = checkedIsUnderWorkspace(getCleanUri(uri));
         uriToIdCache.put(cleanUri, itemId);
         idToUriCache.put(itemId, cleanUri);
         log.debug("Added: ItemId = " + itemId + " URI = " + cleanUri);
@@ -91,6 +88,27 @@ class IdURICache {
         uriToIdCache.clear();
     }
 
+    private String checkedIsUnderWorkspace(String uri) {
+        if (uri.startsWith(workspaceUri)) {
+            return uri;
+        } else {
+            int ml = Math.max(uri.length(), workspaceUri.length());
+            int match = 0;
+            for (int i = 0; i < ml; i++) {
+                if (uri.charAt(i) != workspaceUri.charAt(i)) {
+                    break;
+                }
+                match = i;
+            }
+            String diags = "";
+            if (uri.length() > match) {
+                String expected = (workspaceUri.length() > match) ? String.format(", expected: '%s'", workspaceUri.substring(match + 1)): ""; 
+                diags = String.format(" (position %d: '{%s}%s'%s)", match, uri.substring(0, match + 1), uri.substring(match + 1), expected);
+            }
+            throw new IllegalArgumentException("Workspace mismatch: '" + uri + "' not under workspace '" + workspaceUri + "'" + diags);
+        }
+    }
+
     private static String getCleanUri(String uri) {
         if (uri.endsWith("/")) {
             return uri.substring(0, uri.length() - 1);

Added: jackrabbit/trunk/jackrabbit-spi2dav/src/test/java/org/apache/jackrabbit/spi2dav/IdURICacheTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi2dav/src/test/java/org/apache/jackrabbit/spi2dav/IdURICacheTest.java?rev=1895700&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi2dav/src/test/java/org/apache/jackrabbit/spi2dav/IdURICacheTest.java (added)
+++ jackrabbit/trunk/jackrabbit-spi2dav/src/test/java/org/apache/jackrabbit/spi2dav/IdURICacheTest.java Wed Dec  8 14:44:58 2021
@@ -0,0 +1,93 @@
+/*
+ * 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.spi2dav;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+public class IdURICacheTest {
+
+    @Test
+    public void testException() {
+
+        String wspuri = "https://example.org/foo/";
+        IdURICache cache = new IdURICache(wspuri);
+        String test;
+
+        // port number
+        test = "https://example.org:443/foo/x";
+        try {
+            cache.add(test, null);
+            fail("should throw");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Workspace mismatch: '" + test + "' not under workspace '" + wspuri
+                    + "' (position 18: '{https://example.org}:443/foo/x', expected: '/foo/')", ex.getMessage());
+        }
+
+        // protocol
+        test = "http://example.org/foo/x";
+        try {
+            cache.add(test, null);
+            fail("should throw");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Workspace mismatch: '" + test + "' not under workspace '" + wspuri
+                    + "' (position 3: '{http}://example.org/foo/x', expected: 's://example.org/foo/')", ex.getMessage());
+        }
+
+        // hostname
+        test = "https://example.com/foo/x";
+        try {
+            cache.add(test, null);
+            fail("should throw");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Workspace mismatch: '" + test + "' not under workspace '" + wspuri
+                    + "' (position 15: '{https://example.}com/foo/x', expected: 'org/foo/')", ex.getMessage());
+        }
+
+        // root path
+        test = "https://example.org/bar/x";
+        try {
+            cache.add(test, null);
+            fail("should throw");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Workspace mismatch: '" + test + "' not under workspace '" + wspuri
+                    + "' (position 19: '{https://example.org/}bar/x', expected: 'foo/')", ex.getMessage());
+        }
+
+        // too short
+        test = "https://example.org/fo/x";
+        try {
+            cache.add(test, null);
+            fail("should throw");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Workspace mismatch: '" + test + "' not under workspace '" + wspuri
+                    + "' (position 21: '{https://example.org/fo}/x', expected: 'o/')", ex.getMessage());
+        }
+
+        // way too short
+        test = "https://x.org/foo/x";
+        try {
+            cache.add(test, null);
+            fail("should throw");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Workspace mismatch: '" + test + "' not under workspace '" + wspuri
+                    + "' (position 7: '{https://}x.org/foo/x', expected: 'example.org/foo/')", ex.getMessage());
+        }
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-spi2dav/src/test/java/org/apache/jackrabbit/spi2dav/IdURICacheTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-spi2dav/src/test/java/org/apache/jackrabbit/spi2dav/IdURICacheTest.java
------------------------------------------------------------------------------
    svn:executable = *