|
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 | +import java.net.URLConnection; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.InvalidPathException; |
| 37 | +import java.nio.file.Paths; |
34 | 38 | import java.util.*;
|
35 |
| -import java.util.stream.Collectors; |
36 | 39 |
|
37 | 40 |
|
38 | 41 | /**
|
@@ -60,7 +63,6 @@ public class AwsServletContext
|
60 | 63 | private Map<String, String> initParameters;
|
61 | 64 | private AwsLambdaServletContainerHandler containerHandler;
|
62 | 65 | private Logger log = LoggerFactory.getLogger(AwsServletContext.class);
|
63 |
| - private MimetypesFileTypeMap mimeTypes; // lazily loaded in the getMimeType method |
64 | 66 |
|
65 | 67 |
|
66 | 68 | //-------------------------------------------------------------
|
@@ -160,18 +162,32 @@ public int getEffectiveMinorVersion() {
|
160 | 162 |
|
161 | 163 | @Override
|
162 | 164 | @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(".")) { |
165 | 167 | return null;
|
166 | 168 | }
|
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 | + } |
169 | 188 | }
|
170 | 189 |
|
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; |
175 | 191 | }
|
176 | 192 |
|
177 | 193 |
|
|
0 commit comments