Author: sseifert
Date: Wed Jun 21 20:06:10 2017
New Revision: 1799508

URL: http://svn.apache.org/viewvc?rev=1799508&view=rev
Log:
SLING-6976 ContentLoader.binaryFile() and ContentLoader.binaryResource() should 
try to derive mimetype from classpath resource rather than from target resource 
name

Modified:
    
sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java

Modified: 
sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java?rev=1799508&r1=1799507&r2=1799508&view=diff
==============================================================================
--- 
sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
 (original)
+++ 
sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
 Wed Jun 21 20:06:10 2017
@@ -211,7 +211,7 @@ public final class ContentLoader {
     /**
      * Import binary file as nt:file binary node into repository. Auto-creates
      * parent hierarchies as nt:unstrucured nodes if missing. Mime type is
-     * auto-detected from resource name.
+     * auto-detected from either {@code classpathResource} or {@code path}.
      * @param classpathResource Classpath resource URL for binary file.
      * @param path Path to mount binary data to (parent nodes created
      *            automatically)
@@ -223,7 +223,7 @@ public final class ContentLoader {
             throw new IllegalArgumentException("Classpath resource not found: 
" + classpathResource);
         }
         try {
-            return binaryFile(is, path, detectMimeTypeFromName(path));
+            return binaryFile(is, path, 
detectMimeTypeFromNames(classpathResource, path));
         } finally {
             try {
                 is.close();
@@ -268,7 +268,7 @@ public final class ContentLoader {
      * @return Resource with binary data
      */
     public Resource binaryFile(InputStream inputStream, String path) {
-        return binaryFile(inputStream, path, detectMimeTypeFromName(path));
+        return binaryFile(inputStream, path, detectMimeTypeFromNames(path));
     }
 
     /**
@@ -300,7 +300,7 @@ public final class ContentLoader {
      * @return Resource with binary data
      */
     public Resource binaryFile(InputStream inputStream, Resource 
parentResource, String name) {
-        return binaryFile(inputStream, parentResource, name, 
detectMimeTypeFromName(name));
+        return binaryFile(inputStream, parentResource, name, 
detectMimeTypeFromNames(name));
     }
 
     /**
@@ -332,7 +332,7 @@ public final class ContentLoader {
     /**
      * Import binary file as nt:resource binary node into repository.
      * Auto-creates parent hierarchies as nt:unstrucured nodes if missing. Mime
-     * type is auto-detected from resource name.
+     * type is auto-detected from {@code classpathResource} or {@code path}.
      * @param classpathResource Classpath resource URL for binary file.
      * @param path Path to mount binary data to (parent nodes created
      *            automatically)
@@ -344,7 +344,7 @@ public final class ContentLoader {
             throw new IllegalArgumentException("Classpath resource not found: 
" + classpathResource);
         }
         try {
-            return binaryResource(is, path, detectMimeTypeFromName(path));
+            return binaryResource(is, path, 
detectMimeTypeFromNames(classpathResource, path));
         } finally {
             try {
                 is.close();
@@ -389,7 +389,7 @@ public final class ContentLoader {
      * @return Resource with binary data
      */
     public Resource binaryResource(InputStream inputStream, String path) {
-        return binaryResource(inputStream, path, detectMimeTypeFromName(path));
+        return binaryResource(inputStream, path, 
detectMimeTypeFromNames(path));
     }
 
     /**
@@ -421,7 +421,7 @@ public final class ContentLoader {
      * @return Resource with binary data
      */
     public Resource binaryResource(InputStream inputStream, Resource 
parentResource, String name) {
-        return binaryResource(inputStream, parentResource, name, 
detectMimeTypeFromName(name));
+        return binaryResource(inputStream, parentResource, name, 
detectMimeTypeFromNames(name));
     }
 
     /**
@@ -448,19 +448,22 @@ public final class ContentLoader {
     }
 
     /**
-     * Detected mime type from name (file extension) using Mime Type service.
+     * Detected mime type from any of the given names (evaluating the file 
extension) using Mime Type service.
      * Fallback to application/octet-stream.
-     * @param name Node name
+     * @param names The names from which to derive the mime type
      * @return Mime type (never null)
      */
-    private String detectMimeTypeFromName(String name) {
+    private String detectMimeTypeFromNames(String... names) {
         String mimeType = null;
-        String fileExtension = StringUtils.substringAfterLast(name, ".");
-        if (bundleContext != null && StringUtils.isNotEmpty(fileExtension)) {
-            ServiceReference<MimeTypeService> ref = 
bundleContext.getServiceReference(MimeTypeService.class);
-            if (ref != null) {
-                MimeTypeService mimeTypeService = 
(MimeTypeService)bundleContext.getService(ref);
-                mimeType = mimeTypeService.getMimeType(fileExtension);
+        for (String name : names) {
+            String fileExtension = StringUtils.substringAfterLast(name, ".");
+            if (bundleContext != null && 
StringUtils.isNotEmpty(fileExtension)) {
+                ServiceReference<MimeTypeService> ref = 
bundleContext.getServiceReference(MimeTypeService.class);
+                if (ref != null) {
+                    MimeTypeService mimeTypeService = 
(MimeTypeService)bundleContext.getService(ref);
+                    mimeType = mimeTypeService.getMimeType(fileExtension);
+                    break;
+                }
             }
         }
         return StringUtils.defaultString(mimeType, CONTENTTYPE_OCTET_STREAM);


Reply via email to