rombert closed pull request #1: SLING-7528: remove broken attempt to handle URIs
URL: https://github.com/apache/sling-org-apache-sling-jcr-webdav/pull/1
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pom.xml b/pom.xml
index f2834f1..74c2f27 100644
--- a/pom.xml
+++ b/pom.xml
@@ -177,5 +177,9 @@
             <version>2.0.8</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git 
a/src/main/java/org/apache/sling/jcr/webdav/impl/helper/SlingTikaDetector.java 
b/src/main/java/org/apache/sling/jcr/webdav/impl/helper/SlingTikaDetector.java
index 69f2828..74aa8d9 100644
--- 
a/src/main/java/org/apache/sling/jcr/webdav/impl/helper/SlingTikaDetector.java
+++ 
b/src/main/java/org/apache/sling/jcr/webdav/impl/helper/SlingTikaDetector.java
@@ -17,8 +17,7 @@
 package org.apache.sling.jcr.webdav.impl.helper;
 
 import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
+
 import org.apache.sling.commons.mime.MimeTypeService;
 import org.apache.tika.detect.Detector;
 import org.apache.tika.metadata.Metadata;
@@ -41,38 +40,11 @@ public MediaType detect(InputStream rawData, Metadata 
metadata) {
         // Look for a resource name in the input metadata
         String name = metadata.get(Metadata.RESOURCE_NAME_KEY);
         if (name != null) {
-            // If the name is a URL, skip the trailing query and fragment parts
-            int question = name.indexOf('?');
-            if (question != -1) {
-                name = name.substring(0, question);
-            }
-            int hash = name.indexOf('#');
-            if (hash != -1) {
-                name = name.substring(0, hash);
-            }
-
-            // If the name is a URL or a path, skip all but the last component
+            // If the name is a path, skip all but the last component
             int slash = name.lastIndexOf('/');
             if (slash != -1) {
                 name = name.substring(slash + 1);
             }
-            int backslash = name.lastIndexOf('\\');
-            if (backslash != -1) {
-                name = name.substring(backslash + 1);
-            }
-
-            // Decode any potential URL encoding
-            int percent = name.indexOf('%');
-            if (percent != -1) {
-                try {
-                    name = URLDecoder.decode(name, "UTF-8");
-                } catch (UnsupportedEncodingException e) {
-                    throw new AssertionError("UTF-8 not supported");
-                }
-            }
-
-            // Skip any leading or trailing whitespace
-            name = name.trim();
             if (name.length() > 0) {
                 // Match the name against the registered patterns
                 String type = mimeTypeService.getMimeType(name);
@@ -84,5 +56,4 @@ public MediaType detect(InputStream rawData, Metadata 
metadata) {
 
         return MediaType.OCTET_STREAM;
     }
-
 }
diff --git 
a/src/test/java/org/apache/sling/jcr/webdav/impl/helper/SlingTikaDetectorTest.java
 
b/src/test/java/org/apache/sling/jcr/webdav/impl/helper/SlingTikaDetectorTest.java
new file mode 100644
index 0000000..d89608e
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/jcr/webdav/impl/helper/SlingTikaDetectorTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.sling.jcr.webdav.impl.helper;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Locale;
+
+import org.apache.sling.commons.mime.MimeTypeService;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.mime.MediaType;
+import org.junit.Test;
+
+public class SlingTikaDetectorTest {
+
+    private static final ByteArrayInputStream EMPTY_INPUT_STREAM = new 
ByteArrayInputStream(new byte[0]);
+
+    private MimeTypeService mimeTypeService = new MimeTypeService() {
+
+        @Override
+        public void registerMimeType(String arg0, String... arg1) {
+        }
+
+        @Override
+        public void registerMimeType(InputStream arg0) throws IOException {
+        }
+
+        @Override
+        public String getMimeType(String name) {
+            if (name.toLowerCase(Locale.ENGLISH).endsWith(".html")) {
+                return "text/html";
+            } else {
+                return null;
+            }
+        }
+
+        @Override
+        public String getExtension(String arg0) {
+            return null;
+        }
+    };
+
+    private SlingTikaDetector detector = new 
SlingTikaDetector(mimeTypeService);
+
+    @Test
+    public void noName() {
+        Metadata metadata = new Metadata();
+        assertEquals(MediaType.OCTET_STREAM, 
detector.detect(EMPTY_INPUT_STREAM, metadata));
+    }
+
+    @Test
+    public void withPercentInName() {
+        // see SLING-7528: checks that there is no percent-unescaping
+        Metadata metadata = new Metadata();
+        metadata.add(Metadata.RESOURCE_NAME_KEY, "a/b%c.html");
+        assertEquals(MediaType.TEXT_HTML, detector.detect(EMPTY_INPUT_STREAM, 
metadata));
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to