This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new ddefed16af FELIX-6772 : Content type of resources is never set
ddefed16af is described below

commit ddefed16afdd13c5be104bff6712afe34fc4d264
Author: Carsten Ziegeler <cziege...@apache.org>
AuthorDate: Wed Apr 23 07:19:10 2025 +0200

    FELIX-6772 : Content type of resources is never set
---
 .../felix/webconsole/AbstractWebConsolePlugin.java |  19 +-
 .../internal/servlet/AbstractPluginAdapter.java    |  11 +-
 .../webconsole/internal/servlet/MimeTypes.java     | 191 +++++++++++++++++++++
 .../internal/servlet/OsgiManagerHttpContext.java   |   8 +-
 .../felix/webconsole/servlet/AbstractServlet.java  |   9 +-
 5 files changed, 226 insertions(+), 12 deletions(-)

diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
index 5e978bf02c..45c46189f4 100644
--- 
a/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
@@ -70,7 +70,7 @@ public abstract class AbstractWebConsolePlugin extends 
HttpServlet {
      */
     @Deprecated
     public static final String ATTR_FILEUPLOAD = 
"org.apache.felix.webconsole.fileupload";
-    
+
     /**
      * This attribute is not supported anymore
      * @deprecated Use the Servlet API for uploads
@@ -432,7 +432,7 @@ public abstract class AbstractWebConsolePlugin extends 
HttpServlet {
             default: Util.LOGGER.debug(message, t);
         }
     }
-    
+
     /**
      * If the request addresses a resource which may be served by the
      * <code>getResource</code> method of the
@@ -451,7 +451,7 @@ public abstract class AbstractWebConsolePlugin extends 
HttpServlet {
      *
      * @throws IOException If an error occurs accessing or spooling the 
resource.
      */
-    private final boolean spoolResource(final HttpServletRequest request, 
+    private final boolean spoolResource(final HttpServletRequest request,
         final HttpServletResponse response) throws IOException
     {
         try
@@ -532,8 +532,15 @@ public abstract class AbstractWebConsolePlugin extends 
HttpServlet {
                 }
 
                 // describe the contents
-                response.setContentType( getServletContext().getMimeType( pi ) 
);
-                response.setIntHeader( "Content-Length", 
connection.getContentLength() );
+                final String contentType = getServletContext().getMimeType(pi);
+                if ( contentType != null )
+                {
+                    response.setContentType( contentType );
+                }
+                if (connection.getContentLength() != -1)
+                {
+                    response.setContentLength( connection.getContentLength() );
+                }
 
                 // spool the actual contents
                 OutputStream out = response.getOutputStream();
@@ -784,7 +791,7 @@ public abstract class AbstractWebConsolePlugin extends 
HttpServlet {
      * {@link WebConsoleConstants#ATTR_PLUGIN_ROOT} request attribute.
      * <p>
      *
-     * @param request The request whose attribute is returned 
+     * @param request The request whose attribute is returned
      *
      * @return The {@link RequestVariableResolver} for the given request.
      * @since 3.5.0
diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/AbstractPluginAdapter.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/AbstractPluginAdapter.java
index 2bf18ca20f..97332c663b 100644
--- 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/AbstractPluginAdapter.java
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/AbstractPluginAdapter.java
@@ -220,8 +220,15 @@ public abstract class AbstractPluginAdapter extends 
HttpServlet {
             }
 
             // describe the contents
-            response.setContentType( getServletContext().getMimeType( pi ) );
-            response.setIntHeader( "Content-Length", 
connection.getContentLength() );
+            final String contentType = getServletContext().getMimeType(pi);
+            if ( contentType != null )
+            {
+                response.setContentType( contentType );
+            }
+            if (connection.getContentLength() != -1)
+            {
+                response.setContentLength( connection.getContentLength() );
+            }
 
             // spool the actual contents
             final OutputStream out = response.getOutputStream();
diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/MimeTypes.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/MimeTypes.java
new file mode 100644
index 0000000000..88e3b96ce3
--- /dev/null
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/MimeTypes.java
@@ -0,0 +1,191 @@
+/*
+ * 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.felix.webconsole.internal.servlet;
+
+import java.util.Map;
+import java.util.HashMap;
+
+public final class MimeTypes {
+
+    private static final Map<String, String> extMap = new HashMap<String, 
String>();
+    static {
+        extMap.put("abs", "audio/x-mpeg");
+        extMap.put("ai", "application/postscript");
+        extMap.put("aif", "audio/x-aiff");
+        extMap.put("aifc", "audio/x-aiff");
+        extMap.put("aiff", "audio/x-aiff");
+        extMap.put("aim", "application/x-aim");
+        extMap.put("art", "image/x-jg");
+        extMap.put("asf", "video/x-ms-asf");
+        extMap.put("asx", "video/x-ms-asf");
+        extMap.put("au", "audio/basic");
+        extMap.put("avi", "video/x-msvideo");
+        extMap.put("avx", "video/x-rad-screenplay");
+        extMap.put("bcpio", "application/x-bcpio");
+        extMap.put("bin", "application/octet-stream");
+        extMap.put("bmp", "image/bmp");
+        extMap.put("body", "text/html");
+        extMap.put("cdf", "application/x-cdf");
+        extMap.put("cer", "application/x-x509-ca-cert");
+        extMap.put("class", "application/java");
+        extMap.put("cpio", "application/x-cpio");
+        extMap.put("csh", "application/x-csh");
+        extMap.put("css", "text/css");
+        extMap.put("dib", "image/bmp");
+        extMap.put("doc", "application/msword");
+        extMap.put("dtd", "application/xml-dtd");
+        extMap.put("dv", "video/x-dv");
+        extMap.put("dvi", "application/x-dvi");
+        extMap.put("eps", "application/postscript");
+        extMap.put("etx", "text/x-setext");
+        extMap.put("exe", "application/octet-stream");
+        extMap.put("gif", "image/gif");
+        extMap.put("gk", "application/octet-stream");
+        extMap.put("gtar", "application/x-gtar");
+        extMap.put("gz", "application/x-gzip");
+        extMap.put("hdf", "application/x-hdf");
+        extMap.put("hqx", "application/mac-binhex40");
+        extMap.put("htc", "text/x-component");
+        extMap.put("htm", "text/html");
+        extMap.put("html", "text/html");
+        extMap.put("hqx", "application/mac-binhex40");
+        extMap.put("ief", "image/ief");
+        extMap.put("jad", "text/vnd.sun.j2me.app-descriptor");
+        extMap.put("jar", "application/java-archive");
+        extMap.put("java", "text/plain");
+        extMap.put("jnlp", "application/x-java-jnlp-file");
+        extMap.put("jpe", "image/jpeg");
+        extMap.put("jpeg", "image/jpeg");
+        extMap.put("jpg", "image/jpeg");
+        extMap.put("js", "text/javascript");
+        extMap.put("kar", "audio/x-midi");
+        extMap.put("latex", "application/x-latex");
+        extMap.put("m3u", "audio/x-mpegurl");
+        extMap.put("mac", "image/x-macpaint");
+        extMap.put("man", "application/x-troff-man");
+        extMap.put("mathml", "application/mathml+xml");
+        extMap.put("me", "application/x-troff-me");
+        extMap.put("mid", "audio/x-midi");
+        extMap.put("midi", "audio/x-midi");
+        extMap.put("mif", "application/x-mif");
+        extMap.put("mov", "video/quicktime");
+        extMap.put("movie", "video/x-sgi-movie");
+        extMap.put("mp1", "audio/x-mpeg");
+        extMap.put("mp2", "audio/x-mpeg");
+        extMap.put("mp3", "audio/x-mpeg");
+        extMap.put("mpa", "audio/x-mpeg");
+        extMap.put("mpe", "video/mpeg");
+        extMap.put("mpeg", "video/mpeg");
+        extMap.put("mpega", "audio/x-mpeg");
+        extMap.put("mpg", "video/mpeg");
+        extMap.put("mpv2", "video/mpeg2");
+        extMap.put("ms", "application/x-wais-source");
+        extMap.put("nc", "application/x-netcdf");
+        extMap.put("oda", "application/oda");
+        extMap.put("ogg", "application/ogg");
+        extMap.put("pbm", "image/x-portable-bitmap");
+        extMap.put("pct", "image/pict");
+        extMap.put("pdf", "application/pdf");
+        extMap.put("pgm", "image/x-portable-graymap");
+        extMap.put("pic", "image/pict");
+        extMap.put("pict", "image/pict");
+        extMap.put("pls", "audio/x-scpls");
+        extMap.put("png", "image/png");
+        extMap.put("pnm", "image/x-portable-anymap");
+        extMap.put("pnt", "image/x-macpaint");
+        extMap.put("ppm", "image/x-portable-pixmap");
+        extMap.put("ppt", "application/powerpoint");
+        extMap.put("ps", "application/postscript");
+        extMap.put("psd", "image/x-photoshop");
+        extMap.put("qt", "video/quicktime");
+        extMap.put("qti", "image/x-quicktime");
+        extMap.put("qtif", "image/x-quicktime");
+        extMap.put("ras", "image/x-cmu-raster");
+        extMap.put("rdf", "application/rdf+xml");
+        extMap.put("rgb", "image/x-rgb");
+        extMap.put("rm", "application/vnd.rn-realmedia");
+        extMap.put("roff", "application/x-troff");
+        extMap.put("rtf", "application/rtf");
+        extMap.put("rtx", "text/richtext");
+        extMap.put("sh", "application/x-sh");
+        extMap.put("shar", "application/x-shar");
+        extMap.put("shtml", "text/x-server-parsed-html");
+        extMap.put("sit", "application/x-stuffit");
+        extMap.put("smf", "audio/x-midi");
+        extMap.put("snd", "audio/basic");
+        extMap.put("src", "application/x-wais-source");
+        extMap.put("sv4cpio", "application/x-sv4cpio");
+        extMap.put("sv4crc", "application/x-sv4crc");
+        extMap.put("svg", "image/svg+xml");
+        extMap.put("svgz", "image/svg+xml");
+        extMap.put("swf", "application/x-shockwave-flash");
+        extMap.put("t", "application/x-troff");
+        extMap.put("tar", "application/x-tar");
+        extMap.put("tcl", "application/x-tcl");
+        extMap.put("tex", "application/x-tex");
+        extMap.put("texi", "application/x-texinfo");
+        extMap.put("texinfo", "application/x-texinfo");
+        extMap.put("tif", "image/tiff");
+        extMap.put("tiff", "image/tiff");
+        extMap.put("tr", "application/x-troff");
+        extMap.put("tsv", "text/tab-separated-values");
+        extMap.put("txt", "text/plain");
+        extMap.put("ulw", "audio/basic");
+        extMap.put("ustar", "application/x-ustar");
+        extMap.put("xbm", "image/x-xbitmap");
+        extMap.put("xml", "text/xml");
+        extMap.put("xpm", "image/x-xpixmap");
+        extMap.put("xsl", "application/xml");
+        extMap.put("xslt", "application/xslt+xml");
+        extMap.put("xwd", "image/x-xwindowdump");
+        extMap.put("vsd", "application/x-visio");
+        extMap.put("vxml", "application/voicexml+xml");
+        extMap.put("wav", "audio/x-wav");
+        extMap.put("wbmp", "image/vnd.wap.wbmp");
+        extMap.put("wml", "text/vnd.wap.wml");
+        extMap.put("wmlc", "application/vnd.wap.wmlc");
+        extMap.put("wmls", "text/vnd.wap.wmls");
+        extMap.put("wmlscriptc", "application/vnd.wap.wmlscriptc");
+        extMap.put("wrl", "x-world/x-vrml");
+        extMap.put("xht", "application/xhtml+xml");
+        extMap.put("xhtml", "application/xhtml+xml");
+        extMap.put("xls", "application/vnd.ms-excel");
+        extMap.put("xul", "application/vnd.mozilla.xul+xml");
+        extMap.put("Z", "application/x-compress");
+        extMap.put("z", "application/x-compress");
+        extMap.put("zip", "application/zip");
+    }
+
+    public static String getByFile(final String file) {
+        if (file == null) {
+            return null;
+        }
+        final int dot = file.lastIndexOf(".");
+        if (dot < 0) {
+            return null;
+        }
+        final String ext = file.substring(dot + 1).toLowerCase();
+        return getByExtension(ext);
+    }
+
+    public static String getByExtension(final String ext){
+        if (ext == null) {
+            return null;
+        }
+        return extMap.get(ext);
+    }
+}
diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManagerHttpContext.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManagerHttpContext.java
index c5fcc5f95b..608ebd7004 100644
--- 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManagerHttpContext.java
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManagerHttpContext.java
@@ -45,6 +45,7 @@ final class OsgiManagerHttpContext extends 
ServletContextHelper {
         this.webManagerRoot = webManagerRoot;
     }
 
+    @Override
     public URL getResource(final String name) {
         URL url = this.bundle.getResource( name );
         if ( url == null && name.endsWith( "/" ) ) {
@@ -53,6 +54,11 @@ final class OsgiManagerHttpContext extends 
ServletContextHelper {
         return url;
     }
 
+    @Override
+    public String getMimeType(final String name) {
+        return MimeTypes.getByFile(name);
+    }
+
     @Override
     @SuppressWarnings("deprecation")
     public boolean handleSecurity( final HttpServletRequest r, final 
HttpServletResponse response ) {
@@ -103,7 +109,7 @@ final class OsgiManagerHttpContext extends 
ServletContextHelper {
                                public Object getUserObject() {
                                        return result;
                                }
-                
+
             });
             
request.setAttribute(org.apache.felix.webconsole.User.USER_ATTRIBUTE, 
request.getAttribute(User.USER_ATTRIBUTE));
         }
diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/servlet/AbstractServlet.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/servlet/AbstractServlet.java
index da36a50841..9bf9853453 100644
--- 
a/webconsole/src/main/java/org/apache/felix/webconsole/servlet/AbstractServlet.java
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/servlet/AbstractServlet.java
@@ -124,7 +124,7 @@ public abstract class AbstractServlet extends HttpServlet {
      *
      * @throws IOException If an error occurs accessing or spooling the 
resource.
      */
-    protected final void spoolResource(final HttpServletRequest request, final 
HttpServletResponse response) 
+    protected final void spoolResource(final HttpServletRequest request, final 
HttpServletResponse response)
     throws IOException {
         // check for a resource, fail if none
         final URL url = this.getResource(request.getPathInfo());
@@ -162,7 +162,10 @@ public abstract class AbstractServlet extends HttpServlet {
             }
 
             // describe the contents
-            response.setContentType( getServletContext().getMimeType( 
request.getPathInfo() ) );
+            final String contentType = getServletContext().getMimeType( 
request.getPathInfo());
+            if ( contentType != null ) {
+                response.setContentType( contentType );
+            }
             if (connection.getContentLength() != -1) {
                 response.setContentLength( connection.getContentLength() );
             }
@@ -184,7 +187,7 @@ public abstract class AbstractServlet extends HttpServlet {
      * UTF-8 encoding.
      *
      * @param templateFile The absolute path to the template file to read.
-     * @return The contents of the template file as a string 
+     * @return The contents of the template file as a string
      *
      * @throws NullPointerException if <code>templateFile</code> is
      *      <code>null</code>

Reply via email to