Skip to content

Commit

Permalink
fix: for 2.0 we return null instead of application/octet-stream if co…
Browse files Browse the repository at this point in the history
…ntent 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
  • Loading branch information
deki committed May 19, 2023
1 parent 332a8c5 commit 0c0650e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
5 changes: 0 additions & 5 deletions aws-serverless-java-container-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@
<artifactId>commons-fileupload2</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@
import com.amazonaws.serverless.proxy.internal.SecurityUtils;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.activation.spi.MimeTypeRegistryProvider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.*;
import jakarta.servlet.ServletContext;
import jakarta.servlet.descriptor.JspConfigDescriptor;
import jakarta.activation.MimetypesFileTypeMap;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;


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


//-------------------------------------------------------------
Expand Down Expand Up @@ -166,22 +167,16 @@ public String getMimeType(String file) {
return null;
}

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

if (mimeTypes == null) {
mimeTypes = new MimetypesFileTypeMap();
// may not work on Lambda until mailcap package is present https://github.com/awslabs/aws-serverless-java-container/pull/504
try {
mimeType = Files.probeContentType(Paths.get(file));
} catch (IOException | InvalidPathException e) {
log("unable to probe for content type, will use fallback", e);
}
String mimeType = mimeTypes.getContentType(file);

// The getContentType method of the MimetypesFileTypeMap
// returns MimetypesFileTypeMap.defaultType = application/octet-stream
// instead of null when the type cannot be found. trying to improve the result...
if (mimeType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mimeType)) {
if (mimeType == null) {
try {
String mimeTypeGuess = URLConnection.guessContentTypeFromName(new File(file).getName());
if (mimeTypeGuess !=null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ void getMimeType_mimeTypeOfJavascript_expectApplicationJavascript() {
}

@Test
void getMimeType_unknownExtension_expectAppOctetStream() {
void getMimeType_unknownExtension_expectNull() {
AwsServletContext ctx = new AwsServletContext(null);
String mimeType = ctx.getMimeType("myfile.unkext");
assertEquals("application/octet-stream", mimeType);
assertNull(mimeType);
}


Expand Down

0 comments on commit 0c0650e

Please sign in to comment.