|
17 | 17 | import com.amazonaws.serverless.proxy.internal.SecurityUtils; |
18 | 18 |
|
19 | 19 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
20 | | -import jakarta.activation.spi.MimeTypeRegistryProvider; |
| 20 | + |
21 | 21 | import org.slf4j.Logger; |
22 | 22 | import org.slf4j.LoggerFactory; |
23 | 23 |
|
24 | 24 | import jakarta.servlet.*; |
25 | 25 | import jakarta.servlet.ServletContext; |
26 | 26 | import jakarta.servlet.descriptor.JspConfigDescriptor; |
27 | | -import jakarta.activation.MimetypesFileTypeMap; |
28 | 27 |
|
29 | 28 | import java.io.File; |
| 29 | +import java.io.IOException; |
30 | 30 | import java.io.InputStream; |
31 | 31 | import java.net.MalformedURLException; |
32 | 32 | import java.net.URISyntaxException; |
33 | 33 | import java.net.URL; |
34 | 34 | import java.net.URLConnection; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.InvalidPathException; |
| 37 | +import java.nio.file.Paths; |
35 | 38 | import java.util.*; |
36 | | -import java.util.stream.Collectors; |
37 | 39 |
|
38 | 40 |
|
39 | 41 | /** |
@@ -61,7 +63,6 @@ public class AwsServletContext |
61 | 63 | private Map<String, String> initParameters; |
62 | 64 | private AwsLambdaServletContainerHandler containerHandler; |
63 | 65 | private Logger log = LoggerFactory.getLogger(AwsServletContext.class); |
64 | | - private MimetypesFileTypeMap mimeTypes; // lazily loaded in the getMimeType method |
65 | 66 |
|
66 | 67 |
|
67 | 68 | //------------------------------------------------------------- |
@@ -166,22 +167,16 @@ public String getMimeType(String file) { |
166 | 167 | return null; |
167 | 168 | } |
168 | 169 |
|
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; |
175 | 171 |
|
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); |
178 | 177 | } |
179 | | - String mimeType = mimeTypes.getContentType(file); |
180 | 178 |
|
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) { |
185 | 180 | try { |
186 | 181 | String mimeTypeGuess = URLConnection.guessContentTypeFromName(new File(file).getName()); |
187 | 182 | if (mimeTypeGuess !=null) { |
|
0 commit comments