Skip to content

Commit f5cf12d

Browse files
authored
Merge pull request #504 from Artur-/mime-types
fix: Provide correct mime type for javascript and css
2 parents 0601ce7 + 0c0650e commit f5cf12d

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

aws-serverless-java-container-core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@
7171
<artifactId>commons-fileupload2</artifactId>
7272
<version>2.0-SNAPSHOT</version>
7373
</dependency>
74-
<dependency>
75-
<groupId>org.eclipse.angus</groupId>
76-
<artifactId>angus-mail</artifactId>
77-
<version>2.0.1</version>
78-
</dependency>
7974

8075
<dependency>
8176
<groupId>org.apache.httpcomponents</groupId>

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContext.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,25 @@
1717
import com.amazonaws.serverless.proxy.internal.SecurityUtils;
1818

1919
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20-
import jakarta.activation.spi.MimeTypeRegistryProvider;
20+
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323

2424
import jakarta.servlet.*;
2525
import jakarta.servlet.ServletContext;
2626
import jakarta.servlet.descriptor.JspConfigDescriptor;
27-
import jakarta.activation.MimetypesFileTypeMap;
2827

2928
import java.io.File;
29+
import java.io.IOException;
3030
import java.io.InputStream;
3131
import java.net.MalformedURLException;
3232
import java.net.URISyntaxException;
3333
import java.net.URL;
34+
import java.net.URLConnection;
35+
import java.nio.file.Files;
36+
import java.nio.file.InvalidPathException;
37+
import java.nio.file.Paths;
3438
import java.util.*;
35-
import java.util.stream.Collectors;
3639

3740

3841
/**
@@ -60,7 +63,6 @@ public class AwsServletContext
6063
private Map<String, String> initParameters;
6164
private AwsLambdaServletContainerHandler containerHandler;
6265
private Logger log = LoggerFactory.getLogger(AwsServletContext.class);
63-
private MimetypesFileTypeMap mimeTypes; // lazily loaded in the getMimeType method
6466

6567

6668
//-------------------------------------------------------------
@@ -160,18 +162,32 @@ public int getEffectiveMinorVersion() {
160162

161163
@Override
162164
@SuppressFBWarnings("PATH_TRAVERSAL_IN") // suppressing because we are using the getValidFilePath
163-
public String getMimeType(String s) {
164-
if (s == null || !s.contains(".")) {
165+
public String getMimeType(String file) {
166+
if (file == null || !file.contains(".")) {
165167
return null;
166168
}
167-
if (mimeTypes == null) {
168-
mimeTypes = new MimetypesFileTypeMap();
169+
170+
String mimeType = null;
171+
172+
// may not work on Lambda until mailcap package is present https://github.com/awslabs/aws-serverless-java-container/pull/504
173+
try {
174+
mimeType = Files.probeContentType(Paths.get(file));
175+
} catch (IOException | InvalidPathException e) {
176+
log("unable to probe for content type, will use fallback", e);
177+
}
178+
179+
if (mimeType == null) {
180+
try {
181+
String mimeTypeGuess = URLConnection.guessContentTypeFromName(new File(file).getName());
182+
if (mimeTypeGuess !=null) {
183+
mimeType = mimeTypeGuess;
184+
}
185+
} catch (Exception e) {
186+
log("couldn't find a better contentType than " + mimeType + " for file " + file, e);
187+
}
169188
}
170189

171-
// TODO: The getContentType method of the MimetypesFileTypeMap returns application/octet-stream
172-
// instead of null when the type cannot be found. We should replace with an implementation that
173-
// loads the System mime types ($JAVA_HOME/lib/mime.types
174-
return mimeTypes.getContentType(s);
190+
return mimeType;
175191
}
176192

177193

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContextTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,19 @@ void getMimeType_mimeTypeOfCorrectFile_expectMime() {
6565
mimeType = ctx.getMimeType("file://" + tmpFilePath);
6666
assertEquals("text/plain", mimeType);
6767
}
68+
@Test
69+
void getMimeType_mimeTypeOfJavascript_expectApplicationJavascript() {
70+
String tmpFilePath = TMP_DIR + "some.js";
71+
AwsServletContext ctx = new AwsServletContext(null);
72+
String mimeType = ctx.getMimeType(tmpFilePath);
73+
assertEquals("text/javascript", mimeType);
74+
}
6875

6976
@Test
70-
void getMimeType_unknownExtension_expectAppOctetStream() {
77+
void getMimeType_unknownExtension_expectNull() {
7178
AwsServletContext ctx = new AwsServletContext(null);
7279
String mimeType = ctx.getMimeType("myfile.unkext");
73-
assertEquals("application/octet-stream", mimeType);
80+
assertNull(mimeType);
7481
}
7582

7683

0 commit comments

Comments
 (0)