You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2018/03/23 10:58:39 UTC
[sling-org-apache-sling-jcr-webdav] branch master updated:
SLING-7528: org.apache.sling.jcr.webdav fails for files containing "%"
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-webdav.git
The following commit(s) were added to refs/heads/master by this push:
new d8cdc71 SLING-7528: org.apache.sling.jcr.webdav fails for files containing "%"
d8cdc71 is described below
commit d8cdc71a58211f04ebd9d7e531cb842fdeb65bcb
Author: Julian Reschke <ju...@gmx.de>
AuthorDate: Fri Mar 2 11:14:07 2018 +0100
SLING-7528: org.apache.sling.jcr.webdav fails for files containing "%"
remove URI-related decoding attempts, add minimal test case
This closes #1
---
pom.xml | 4 ++
.../jcr/webdav/impl/helper/SlingTikaDetector.java | 33 +---------
.../webdav/impl/helper/SlingTikaDetectorTest.java | 77 ++++++++++++++++++++++
3 files changed, 83 insertions(+), 31 deletions(-)
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 class SlingTikaDetector implements Detector {
// 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 class SlingTikaDetector implements Detector {
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));
+ }
+}
--
To stop receiving notification emails like this one, please contact
rombert@apache.org.