Skip to content

Commit 0c0650e

Browse files
committed
fix: for 2.0 we return null instead of application/octet-stream if content type cannot be resolved (as per servlet-api docs), a fix to make probeContentType work in Lambda execution environment is being worked on by the Lambda service team
1 parent 332a8c5 commit 0c0650e

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

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

-5
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

+12-17
Original file line numberDiff line numberDiff line change
@@ -17,23 +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;
3434
import java.net.URLConnection;
35+
import java.nio.file.Files;
36+
import java.nio.file.InvalidPathException;
37+
import java.nio.file.Paths;
3538
import java.util.*;
36-
import java.util.stream.Collectors;
3739

3840

3941
/**
@@ -61,7 +63,6 @@ public class AwsServletContext
6163
private Map<String, String> initParameters;
6264
private AwsLambdaServletContainerHandler containerHandler;
6365
private Logger log = LoggerFactory.getLogger(AwsServletContext.class);
64-
private MimetypesFileTypeMap mimeTypes; // lazily loaded in the getMimeType method
6566

6667

6768
//-------------------------------------------------------------
@@ -166,22 +167,16 @@ public String getMimeType(String file) {
166167
return null;
167168
}
168169

169-
// this implementation would be nice but returns null on Lambda
170-
// try {
171-
// mimeType = Files.probeContentType(Paths.get(file));
172-
// } catch (IOException | InvalidPathException e) {
173-
// log("unable to probe for content type, will use fallback", e);
174-
// }
170+
String mimeType = null;
175171

176-
if (mimeTypes == null) {
177-
mimeTypes = new MimetypesFileTypeMap();
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);
178177
}
179-
String mimeType = mimeTypes.getContentType(file);
180178

181-
// The getContentType method of the MimetypesFileTypeMap
182-
// returns MimetypesFileTypeMap.defaultType = application/octet-stream
183-
// instead of null when the type cannot be found. trying to improve the result...
184-
if (mimeType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mimeType)) {
179+
if (mimeType == null) {
185180
try {
186181
String mimeTypeGuess = URLConnection.guessContentTypeFromName(new File(file).getName());
187182
if (mimeTypeGuess !=null) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ void getMimeType_mimeTypeOfJavascript_expectApplicationJavascript() {
7474
}
7575

7676
@Test
77-
void getMimeType_unknownExtension_expectAppOctetStream() {
77+
void getMimeType_unknownExtension_expectNull() {
7878
AwsServletContext ctx = new AwsServletContext(null);
7979
String mimeType = ctx.getMimeType("myfile.unkext");
80-
assertEquals("application/octet-stream", mimeType);
80+
assertNull(mimeType);
8181
}
8282

8383

0 commit comments

Comments
 (0)