Skip to content

Commit ddefed1

Browse files
committed
FELIX-6772 : Content type of resources is never set
1 parent bf81066 commit ddefed1

File tree

5 files changed

+226
-12
lines changed

5 files changed

+226
-12
lines changed

webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public abstract class AbstractWebConsolePlugin extends HttpServlet {
7070
*/
7171
@Deprecated
7272
public static final String ATTR_FILEUPLOAD = "org.apache.felix.webconsole.fileupload";
73-
73+
7474
/**
7575
* This attribute is not supported anymore
7676
* @deprecated Use the Servlet API for uploads
@@ -432,7 +432,7 @@ public void log(final int level, final String message, final Throwable t ) {
432432
default: Util.LOGGER.debug(message, t);
433433
}
434434
}
435-
435+
436436
/**
437437
* If the request addresses a resource which may be served by the
438438
* <code>getResource</code> method of the
@@ -451,7 +451,7 @@ public void log(final int level, final String message, final Throwable t ) {
451451
*
452452
* @throws IOException If an error occurs accessing or spooling the resource.
453453
*/
454-
private final boolean spoolResource(final HttpServletRequest request,
454+
private final boolean spoolResource(final HttpServletRequest request,
455455
final HttpServletResponse response) throws IOException
456456
{
457457
try
@@ -532,8 +532,15 @@ final boolean spoolResource0( HttpServletRequest request, HttpServletResponse re
532532
}
533533

534534
// describe the contents
535-
response.setContentType( getServletContext().getMimeType( pi ) );
536-
response.setIntHeader( "Content-Length", connection.getContentLength() );
535+
final String contentType = getServletContext().getMimeType(pi);
536+
if ( contentType != null )
537+
{
538+
response.setContentType( contentType );
539+
}
540+
if (connection.getContentLength() != -1)
541+
{
542+
response.setContentLength( connection.getContentLength() );
543+
}
537544

538545
// spool the actual contents
539546
OutputStream out = response.getOutputStream();
@@ -784,7 +791,7 @@ private static final String toUrl( final String url, final String appRoot )
784791
* {@link WebConsoleConstants#ATTR_PLUGIN_ROOT} request attribute.
785792
* <p>
786793
*
787-
* @param request The request whose attribute is returned
794+
* @param request The request whose attribute is returned
788795
*
789796
* @return The {@link RequestVariableResolver} for the given request.
790797
* @since 3.5.0

webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/AbstractPluginAdapter.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,15 @@ private final boolean spoolResource(final HttpServletRequest request, final Htt
220220
}
221221

222222
// describe the contents
223-
response.setContentType( getServletContext().getMimeType( pi ) );
224-
response.setIntHeader( "Content-Length", connection.getContentLength() );
223+
final String contentType = getServletContext().getMimeType(pi);
224+
if ( contentType != null )
225+
{
226+
response.setContentType( contentType );
227+
}
228+
if (connection.getContentLength() != -1)
229+
{
230+
response.setContentLength( connection.getContentLength() );
231+
}
225232

226233
// spool the actual contents
227234
final OutputStream out = response.getOutputStream();
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.felix.webconsole.internal.servlet;
18+
19+
import java.util.Map;
20+
import java.util.HashMap;
21+
22+
public final class MimeTypes {
23+
24+
private static final Map<String, String> extMap = new HashMap<String, String>();
25+
static {
26+
extMap.put("abs", "audio/x-mpeg");
27+
extMap.put("ai", "application/postscript");
28+
extMap.put("aif", "audio/x-aiff");
29+
extMap.put("aifc", "audio/x-aiff");
30+
extMap.put("aiff", "audio/x-aiff");
31+
extMap.put("aim", "application/x-aim");
32+
extMap.put("art", "image/x-jg");
33+
extMap.put("asf", "video/x-ms-asf");
34+
extMap.put("asx", "video/x-ms-asf");
35+
extMap.put("au", "audio/basic");
36+
extMap.put("avi", "video/x-msvideo");
37+
extMap.put("avx", "video/x-rad-screenplay");
38+
extMap.put("bcpio", "application/x-bcpio");
39+
extMap.put("bin", "application/octet-stream");
40+
extMap.put("bmp", "image/bmp");
41+
extMap.put("body", "text/html");
42+
extMap.put("cdf", "application/x-cdf");
43+
extMap.put("cer", "application/x-x509-ca-cert");
44+
extMap.put("class", "application/java");
45+
extMap.put("cpio", "application/x-cpio");
46+
extMap.put("csh", "application/x-csh");
47+
extMap.put("css", "text/css");
48+
extMap.put("dib", "image/bmp");
49+
extMap.put("doc", "application/msword");
50+
extMap.put("dtd", "application/xml-dtd");
51+
extMap.put("dv", "video/x-dv");
52+
extMap.put("dvi", "application/x-dvi");
53+
extMap.put("eps", "application/postscript");
54+
extMap.put("etx", "text/x-setext");
55+
extMap.put("exe", "application/octet-stream");
56+
extMap.put("gif", "image/gif");
57+
extMap.put("gk", "application/octet-stream");
58+
extMap.put("gtar", "application/x-gtar");
59+
extMap.put("gz", "application/x-gzip");
60+
extMap.put("hdf", "application/x-hdf");
61+
extMap.put("hqx", "application/mac-binhex40");
62+
extMap.put("htc", "text/x-component");
63+
extMap.put("htm", "text/html");
64+
extMap.put("html", "text/html");
65+
extMap.put("hqx", "application/mac-binhex40");
66+
extMap.put("ief", "image/ief");
67+
extMap.put("jad", "text/vnd.sun.j2me.app-descriptor");
68+
extMap.put("jar", "application/java-archive");
69+
extMap.put("java", "text/plain");
70+
extMap.put("jnlp", "application/x-java-jnlp-file");
71+
extMap.put("jpe", "image/jpeg");
72+
extMap.put("jpeg", "image/jpeg");
73+
extMap.put("jpg", "image/jpeg");
74+
extMap.put("js", "text/javascript");
75+
extMap.put("kar", "audio/x-midi");
76+
extMap.put("latex", "application/x-latex");
77+
extMap.put("m3u", "audio/x-mpegurl");
78+
extMap.put("mac", "image/x-macpaint");
79+
extMap.put("man", "application/x-troff-man");
80+
extMap.put("mathml", "application/mathml+xml");
81+
extMap.put("me", "application/x-troff-me");
82+
extMap.put("mid", "audio/x-midi");
83+
extMap.put("midi", "audio/x-midi");
84+
extMap.put("mif", "application/x-mif");
85+
extMap.put("mov", "video/quicktime");
86+
extMap.put("movie", "video/x-sgi-movie");
87+
extMap.put("mp1", "audio/x-mpeg");
88+
extMap.put("mp2", "audio/x-mpeg");
89+
extMap.put("mp3", "audio/x-mpeg");
90+
extMap.put("mpa", "audio/x-mpeg");
91+
extMap.put("mpe", "video/mpeg");
92+
extMap.put("mpeg", "video/mpeg");
93+
extMap.put("mpega", "audio/x-mpeg");
94+
extMap.put("mpg", "video/mpeg");
95+
extMap.put("mpv2", "video/mpeg2");
96+
extMap.put("ms", "application/x-wais-source");
97+
extMap.put("nc", "application/x-netcdf");
98+
extMap.put("oda", "application/oda");
99+
extMap.put("ogg", "application/ogg");
100+
extMap.put("pbm", "image/x-portable-bitmap");
101+
extMap.put("pct", "image/pict");
102+
extMap.put("pdf", "application/pdf");
103+
extMap.put("pgm", "image/x-portable-graymap");
104+
extMap.put("pic", "image/pict");
105+
extMap.put("pict", "image/pict");
106+
extMap.put("pls", "audio/x-scpls");
107+
extMap.put("png", "image/png");
108+
extMap.put("pnm", "image/x-portable-anymap");
109+
extMap.put("pnt", "image/x-macpaint");
110+
extMap.put("ppm", "image/x-portable-pixmap");
111+
extMap.put("ppt", "application/powerpoint");
112+
extMap.put("ps", "application/postscript");
113+
extMap.put("psd", "image/x-photoshop");
114+
extMap.put("qt", "video/quicktime");
115+
extMap.put("qti", "image/x-quicktime");
116+
extMap.put("qtif", "image/x-quicktime");
117+
extMap.put("ras", "image/x-cmu-raster");
118+
extMap.put("rdf", "application/rdf+xml");
119+
extMap.put("rgb", "image/x-rgb");
120+
extMap.put("rm", "application/vnd.rn-realmedia");
121+
extMap.put("roff", "application/x-troff");
122+
extMap.put("rtf", "application/rtf");
123+
extMap.put("rtx", "text/richtext");
124+
extMap.put("sh", "application/x-sh");
125+
extMap.put("shar", "application/x-shar");
126+
extMap.put("shtml", "text/x-server-parsed-html");
127+
extMap.put("sit", "application/x-stuffit");
128+
extMap.put("smf", "audio/x-midi");
129+
extMap.put("snd", "audio/basic");
130+
extMap.put("src", "application/x-wais-source");
131+
extMap.put("sv4cpio", "application/x-sv4cpio");
132+
extMap.put("sv4crc", "application/x-sv4crc");
133+
extMap.put("svg", "image/svg+xml");
134+
extMap.put("svgz", "image/svg+xml");
135+
extMap.put("swf", "application/x-shockwave-flash");
136+
extMap.put("t", "application/x-troff");
137+
extMap.put("tar", "application/x-tar");
138+
extMap.put("tcl", "application/x-tcl");
139+
extMap.put("tex", "application/x-tex");
140+
extMap.put("texi", "application/x-texinfo");
141+
extMap.put("texinfo", "application/x-texinfo");
142+
extMap.put("tif", "image/tiff");
143+
extMap.put("tiff", "image/tiff");
144+
extMap.put("tr", "application/x-troff");
145+
extMap.put("tsv", "text/tab-separated-values");
146+
extMap.put("txt", "text/plain");
147+
extMap.put("ulw", "audio/basic");
148+
extMap.put("ustar", "application/x-ustar");
149+
extMap.put("xbm", "image/x-xbitmap");
150+
extMap.put("xml", "text/xml");
151+
extMap.put("xpm", "image/x-xpixmap");
152+
extMap.put("xsl", "application/xml");
153+
extMap.put("xslt", "application/xslt+xml");
154+
extMap.put("xwd", "image/x-xwindowdump");
155+
extMap.put("vsd", "application/x-visio");
156+
extMap.put("vxml", "application/voicexml+xml");
157+
extMap.put("wav", "audio/x-wav");
158+
extMap.put("wbmp", "image/vnd.wap.wbmp");
159+
extMap.put("wml", "text/vnd.wap.wml");
160+
extMap.put("wmlc", "application/vnd.wap.wmlc");
161+
extMap.put("wmls", "text/vnd.wap.wmls");
162+
extMap.put("wmlscriptc", "application/vnd.wap.wmlscriptc");
163+
extMap.put("wrl", "x-world/x-vrml");
164+
extMap.put("xht", "application/xhtml+xml");
165+
extMap.put("xhtml", "application/xhtml+xml");
166+
extMap.put("xls", "application/vnd.ms-excel");
167+
extMap.put("xul", "application/vnd.mozilla.xul+xml");
168+
extMap.put("Z", "application/x-compress");
169+
extMap.put("z", "application/x-compress");
170+
extMap.put("zip", "application/zip");
171+
}
172+
173+
public static String getByFile(final String file) {
174+
if (file == null) {
175+
return null;
176+
}
177+
final int dot = file.lastIndexOf(".");
178+
if (dot < 0) {
179+
return null;
180+
}
181+
final String ext = file.substring(dot + 1).toLowerCase();
182+
return getByExtension(ext);
183+
}
184+
185+
public static String getByExtension(final String ext){
186+
if (ext == null) {
187+
return null;
188+
}
189+
return extMap.get(ext);
190+
}
191+
}

webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManagerHttpContext.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ final class OsgiManagerHttpContext extends ServletContextHelper {
4545
this.webManagerRoot = webManagerRoot;
4646
}
4747

48+
@Override
4849
public URL getResource(final String name) {
4950
URL url = this.bundle.getResource( name );
5051
if ( url == null && name.endsWith( "/" ) ) {
@@ -53,6 +54,11 @@ public URL getResource(final String name) {
5354
return url;
5455
}
5556

57+
@Override
58+
public String getMimeType(final String name) {
59+
return MimeTypes.getByFile(name);
60+
}
61+
5662
@Override
5763
@SuppressWarnings("deprecation")
5864
public boolean handleSecurity( final HttpServletRequest r, final HttpServletResponse response ) {
@@ -103,7 +109,7 @@ public boolean authorize(String role) {
103109
public Object getUserObject() {
104110
return result;
105111
}
106-
112+
107113
});
108114
request.setAttribute(org.apache.felix.webconsole.User.USER_ATTRIBUTE, request.getAttribute(User.USER_ATTRIBUTE));
109115
}

webconsole/src/main/java/org/apache/felix/webconsole/servlet/AbstractServlet.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void renderContent( final HttpServletRequest request, final HttpServletRe
124124
*
125125
* @throws IOException If an error occurs accessing or spooling the resource.
126126
*/
127-
protected final void spoolResource(final HttpServletRequest request, final HttpServletResponse response)
127+
protected final void spoolResource(final HttpServletRequest request, final HttpServletResponse response)
128128
throws IOException {
129129
// check for a resource, fail if none
130130
final URL url = this.getResource(request.getPathInfo());
@@ -162,7 +162,10 @@ protected final void spoolResource(final HttpServletRequest request, final HttpS
162162
}
163163

164164
// describe the contents
165-
response.setContentType( getServletContext().getMimeType( request.getPathInfo() ) );
165+
final String contentType = getServletContext().getMimeType( request.getPathInfo());
166+
if ( contentType != null ) {
167+
response.setContentType( contentType );
168+
}
166169
if (connection.getContentLength() != -1) {
167170
response.setContentLength( connection.getContentLength() );
168171
}
@@ -184,7 +187,7 @@ protected final void spoolResource(final HttpServletRequest request, final HttpS
184187
* UTF-8 encoding.
185188
*
186189
* @param templateFile The absolute path to the template file to read.
187-
* @return The contents of the template file as a string
190+
* @return The contents of the template file as a string
188191
*
189192
* @throws NullPointerException if <code>templateFile</code> is
190193
* <code>null</code>

0 commit comments

Comments
 (0)