Author: mgrigorov
Date: Mon Oct 10 14:47:50 2011
New Revision: 1181006

URL: http://svn.apache.org/viewvc?rev=1181006&view=rev
Log:
WICKET-4119 FileResourceStream returns unknown content type

Use Application#getMimeType(String filePath) to determine the content type for 
resources.


Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/handler/resource/ResourceStreamRequestHandler.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceStreamResource.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java
    
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/resource/FileResourceStream.java

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/handler/resource/ResourceStreamRequestHandler.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/handler/resource/ResourceStreamRequestHandler.java?rev=1181006&r1=1181005&r2=1181006&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/handler/resource/ResourceStreamRequestHandler.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/handler/resource/ResourceStreamRequestHandler.java
 Mon Oct 10 14:47:50 2011
@@ -109,10 +109,14 @@ public class ResourceStreamRequestHandle
                ResourceStreamResource resource = new 
ResourceStreamResource(resourceStream);
                resource.setFileName(fileName);
                if (contentDisposition != null)
+               {
                        resource.setContentDisposition(contentDisposition);
+               }
                else
+               {
                        
resource.setContentDisposition(Strings.isEmpty(fileName) ? 
ContentDisposition.INLINE
                                : ContentDisposition.ATTACHMENT);
+               }
 
                resource.respond(attributes);
        }

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java?rev=1181006&r1=1181005&r2=1181006&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java
 Mon Oct 10 14:47:50 2011
@@ -175,11 +175,11 @@ public class PackageResource extends Abs
        }
 
        /**
-        * be aware that method takes the current wicket session's locale 
-        * and style into account when locating the stream.
+        * be aware that method takes the current wicket session's locale and 
style into account when
+        * locating the stream.
         * 
         * @return resource stream
-        *
+        * 
         * @see 
org.apache.wicket.request.resource.caching.IStaticCacheableResource#getCacheableResourceStream()
         * @see #getResourceStream()
         */
@@ -190,11 +190,10 @@ public class PackageResource extends Abs
                        .getResourceSettings()
                        .getResourceStreamLocator();
 
-               // determine current resource stream 
+               // determine current resource stream
                // taking client locale and style into account
-               return locator.locate(getScope(), absolutePath,
-                                     getCurrentStyle(), variation, 
getCurrentLocale(),
-                                     null, false);
+               return locator.locate(getScope(), absolutePath, 
getCurrentStyle(), variation,
+                       getCurrentLocale(), null, false);
        }
 
        public Serializable getCacheKey()
@@ -207,8 +206,8 @@ public class PackageResource extends Abs
                        return null;
                }
 
-               return new CacheKey(scopeName, absolutePath, 
-                                   stream.getLocale(), stream.getStyle(), 
stream.getVariation());
+               return new CacheKey(scopeName, absolutePath, 
stream.getLocale(), stream.getStyle(),
+                       stream.getVariation());
        }
 
        /**
@@ -253,8 +252,13 @@ public class PackageResource extends Abs
                                return sendResourceError(resourceResponse, 
HttpServletResponse.SC_NOT_FOUND,
                                        "Unable to find resource");
 
+                       String contentType = resourceStream.getContentType();
+                       if (contentType == null && Application.exists())
+                       {
+                               contentType = 
Application.get().getMimeType(path);
+                       }
                        // set Content-Type (may be null)
-                       
resourceResponse.setContentType(resourceStream.getContentType());
+                       resourceResponse.setContentType(contentType);
 
                        // add Last-Modified header (to support HEAD requests 
and If-Modified-Since)
                        final Time lastModified = 
resourceStream.lastModifiedTime();
@@ -348,35 +352,35 @@ public class PackageResource extends Abs
         * locate resource stream for current resource
         * <p/>
         * Unfortunately this method has changed from scope 'public' in wicket 
1.4 to scope 'protected'
-        * in wicket 1.5. We realized this too late and now changing it would 
break the api. So
-        * in case you need access to this method you have the following 
options:
+        * in wicket 1.5. We realized this too late and now changing it would 
break the api. So in case
+        * you need access to this method you have the following options:
         * 
         * <ul>
-        *     <li>
-        *         copy-paste the code in the method body of {@link 
#getResourceStream()} 
-        *         and wait for wicket 1.6
-        *     </li>
-        *     <li>
-        *         extend PackageResource, passing the package resources 
-        *         attributes and make {@link #getResourceStream()} public 
again:
-        *         
-        *       <pre>
-        *       public class MyPackageResource extends PackageResource
-        *       { 
-        *           public MyPackageResource(Class<?> scope, String name, 
Locale locale, String style,
-        *                 String variation)
-        *         {
-        *           super(scope, name, locale, style, variation);
-        *         }
-        *
-        *         // change access to public here
-        *         public IResourceStream getResourceStream()
-        *         {
-        *           return super.getResourceStream();
-        *         }
-        *       }
-        *       </pre>
-        *     </li>
+        * <li>
+        * copy-paste the code in the method body of {@link 
#getResourceStream()} and wait for wicket
+        * 1.6</li>
+        * <li>
+        * extend PackageResource, passing the package resources attributes and 
make
+        * {@link #getResourceStream()} public again:
+        * 
+        * <pre>
+        * public class MyPackageResource extends PackageResource
+        * {
+        *      public MyPackageResource(Class&lt;?&gt; scope, String name, 
Locale locale, String style,
+        *              String variation)
+        *      {
+        *              super(scope, name, locale, style, variation);
+        *      }
+        * 
+        *      // change access to public here
+        *      public IResourceStream getResourceStream()
+        *      {
+        *              return super.getResourceStream();
+        *      }
+        * }
+        * </pre>
+        * 
+        * </li>
         * </ul>
         * 
         * 
@@ -559,7 +563,8 @@ public class PackageResource extends Abs
                                return false;
                        if (style != null ? !style.equals(cacheKey.style) : 
cacheKey.style != null)
                                return false;
-                       if (variation != null ? 
!variation.equals(cacheKey.variation) : cacheKey.variation != null)
+                       if (variation != null ? 
!variation.equals(cacheKey.variation)
+                               : cacheKey.variation != null)
                                return false;
 
                        return true;

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceStreamResource.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceStreamResource.java?rev=1181006&r1=1181005&r2=1181006&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceStreamResource.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceStreamResource.java
 Mon Oct 10 14:47:50 2011
@@ -21,6 +21,7 @@ import java.io.InputStream;
 
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.wicket.Application;
 import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.lang.Bytes;
 import org.apache.wicket.util.resource.IResourceStream;
@@ -121,7 +122,13 @@ public class ResourceStreamResource exte
                                data.setContentLength(length.bytes());
                        }
                        data.setFileName(fileName);
-                       data.setContentType(stream.getContentType());
+
+                       String contentType = stream.getContentType();
+                       if (contentType == null && fileName != null && 
Application.exists())
+                       {
+                               contentType = 
Application.get().getMimeType(fileName);
+                       }
+                       data.setContentType(contentType);
                        data.setTextEncoding(textEncoding);
 
                        if (stream instanceof IResourceStreamWriter)

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java?rev=1181006&r1=1181005&r2=1181006&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/resource/UrlResourceStream.java
 Mon Oct 10 14:47:50 2011
@@ -22,7 +22,6 @@ import java.net.URL;
 import java.net.URLConnection;
 
 import org.apache.wicket.Application;
-import org.apache.wicket.protocol.http.WebApplication;
 import org.apache.wicket.util.io.Connections;
 import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.lang.Bytes;
@@ -59,7 +58,7 @@ public class UrlResourceStream extends A
 
        /** Last known time the stream was last modified. */
        private Time lastModified;
-       
+
        /**
         * Meta data class for the stream attributes
         */
@@ -108,18 +107,9 @@ public class UrlResourceStream extends A
 
                                if (streamData.contentType == null || 
streamData.contentType.contains("unknown"))
                                {
-                                       if (Application.exists() && 
Application.get() instanceof WebApplication)
+                                       if (Application.exists())
                                        {
-                                               // TODO Post 1.2: General: For 
non webapplication another method
-                                               // should be implemented 
(getMimeType on application?)
-                                               streamData.contentType = 
WebApplication.get()
-                                                       .getServletContext()
-                                                       
.getMimeType(url.getFile());
-                                               if (streamData.contentType == 
null)
-                                               {
-                                                       streamData.contentType 
= URLConnection.getFileNameMap()
-                                                               
.getContentTypeFor(url.getFile());
-                                               }
+                                               streamData.contentType = 
Application.get().getMimeType(url.getFile());
                                        }
                                        else
                                        {
@@ -193,7 +183,7 @@ public class UrlResourceStream extends A
                        if (Objects.equal(time, lastModified) == false)
                        {
                                lastModified = time;
-                               this.updateContentLength();
+                               updateContentLength();
                        }
                        return lastModified;
                }
@@ -210,7 +200,7 @@ public class UrlResourceStream extends A
        {
                StreamData data = getData(false);
 
-               if(data != null)
+               if (data != null)
                {
                        URLConnection connection = url.openConnection();
                        data.contentLength = connection.getContentLength();

Modified: 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/resource/FileResourceStream.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/resource/FileResourceStream.java?rev=1181006&r1=1181005&r2=1181006&view=diff
==============================================================================
--- 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/resource/FileResourceStream.java
 (original)
+++ 
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/resource/FileResourceStream.java
 Mon Oct 10 14:47:50 2011
@@ -20,6 +20,7 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URLConnection;
 
 import org.apache.wicket.util.file.File;
 import org.apache.wicket.util.lang.Bytes;
@@ -33,6 +34,7 @@ import org.apache.wicket.util.time.Time;
  * @see org.apache.wicket.util.watch.IModifiable
  * @author Jonathan Locke
  */
+// TODO Wicket 1.6 - make #file mandatory. Args.notNull(file).
 public class FileResourceStream extends AbstractResourceStream
        implements
                IFixedLocationResourceStream
@@ -84,8 +86,12 @@ public class FileResourceStream extends 
        @Override
        public String getContentType()
        {
-               // Let ResourceStreamRequestTarget handle content-type 
automatically
-               return null;
+               String contentType = null;
+               if (file != null)
+               {
+                       contentType = 
URLConnection.getFileNameMap().getContentTypeFor(file.getName());
+               }
+               return contentType;
        }
 
        /**


Reply via email to