Skip to content

Commit acb3880

Browse files
committed
refactor: Resolve static asset MIME types from the servlet container
Signed-off-by: gibson9583 <cgibson@outlook.com>
1 parent 25e79d6 commit acb3880

1 file changed

Lines changed: 21 additions & 24 deletions

File tree

‎server/src/com/mirth/connect/server/api/servlets/WebPluginServlet.java‎

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Set;
1111
import java.util.regex.Pattern;
1212

13+
import javax.servlet.ServletContext;
1314
import javax.servlet.http.HttpServletRequest;
1415
import javax.ws.rs.core.Context;
1516
import javax.ws.rs.core.Response;
@@ -35,8 +36,11 @@ public class WebPluginServlet extends MirthServlet implements WebPluginServletIn
3536
// both discovery and asset serving against traversal via the extension name.
3637
private static final Pattern SAFE_SEGMENT = Pattern.compile("[A-Za-z0-9._-]+");
3738

38-
public WebPluginServlet(@Context HttpServletRequest request, @Context SecurityContext sc) {
39+
private final ServletContext servletContext;
40+
41+
public WebPluginServlet(@Context HttpServletRequest request, @Context ServletContext servletContext, @Context SecurityContext sc) {
3942
super(request, sc);
43+
this.servletContext = servletContext;
4044
}
4145

4246
@Override
@@ -110,33 +114,26 @@ public Response getWebPluginResource(String extensionPath, String resourcePath)
110114
}
111115
}
112116

113-
// Minimal extension -> MIME map for the file types a web plugin ships. ES modules
114-
// MUST be a JavaScript type or the browser refuses to execute them; the rest keep
115-
// images/fonts/styles rendering correctly. Unknown types fall back to octet-stream.
116-
private static String contentType(String fileName) {
117+
/**
118+
* MIME type for a served asset. ES modules MUST be a JavaScript type with the right charset
119+
* or the browser refuses to execute them, so .js/.mjs (and .json/.map, which some containers
120+
* mislabel) are pinned here; everything else defers to the servlet container's own MIME
121+
* table ({@link ServletContext#getMimeType}) rather than a hand-rolled extension map.
122+
*/
123+
private String contentType(String fileName) {
117124
String lower = StringUtils.lowerCase(fileName);
125+
if (lower == null) {
126+
return "application/octet-stream";
127+
}
118128
if (lower.endsWith(".js") || lower.endsWith(".mjs")) {
119129
return "text/javascript; charset=utf-8";
120-
} else if (lower.endsWith(".css")) {
121-
return "text/css; charset=utf-8";
122-
} else if (lower.endsWith(".json") || lower.endsWith(".map")) {
130+
}
131+
if (lower.endsWith(".json") || lower.endsWith(".map")) {
123132
return "application/json; charset=utf-8";
124-
} else if (lower.endsWith(".html")) {
125-
return "text/html; charset=utf-8";
126-
} else if (lower.endsWith(".svg")) {
127-
return "image/svg+xml";
128-
} else if (lower.endsWith(".png")) {
129-
return "image/png";
130-
} else if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) {
131-
return "image/jpeg";
132-
} else if (lower.endsWith(".gif")) {
133-
return "image/gif";
134-
} else if (lower.endsWith(".woff2")) {
135-
return "font/woff2";
136-
} else if (lower.endsWith(".woff")) {
137-
return "font/woff";
138-
} else if (lower.endsWith(".ttf")) {
139-
return "font/ttf";
133+
}
134+
String mapped = servletContext == null ? null : servletContext.getMimeType(lower);
135+
if (mapped != null) {
136+
return mapped.startsWith("text/") ? mapped + "; charset=utf-8" : mapped;
140137
}
141138
return "application/octet-stream";
142139
}

0 commit comments

Comments
 (0)