-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from swisspost/develop
PR for release
- Loading branch information
Showing
7 changed files
with
840 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/org/swisspush/mirror/util/MimeTypeResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.swisspush.mirror.util; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
public class MimeTypeResolver { | ||
|
||
private static final Logger log = getLogger(MimeTypeResolver.class); | ||
private Map<String, String> mimeTypes = new HashMap<>(); | ||
|
||
private final String defaultMimeType; | ||
|
||
public MimeTypeResolver(String defaultMimeType) { | ||
this.defaultMimeType = defaultMimeType; | ||
Properties props = new Properties(); | ||
InputStream in = this.getClass().getClassLoader().getResourceAsStream("mime-types.properties"); | ||
try { | ||
props.load(in); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
try { | ||
if(in != null) { | ||
in.close(); | ||
} | ||
} catch (IOException ex) { | ||
log.debug("close() failed", ex); | ||
} | ||
} | ||
|
||
for( Map.Entry<Object, Object> entry : props.entrySet()) { | ||
mimeTypes.put(((String)entry.getKey()).toLowerCase(), (String)entry.getValue()); | ||
} | ||
} | ||
|
||
public String resolveMimeType(String path) { | ||
int lastSlash = path.lastIndexOf("/"); | ||
String part = path; | ||
if(lastSlash >= 0 && !path.endsWith("/")) { | ||
part = part.substring(lastSlash+1); | ||
} | ||
int dot = part.lastIndexOf("."); | ||
if(dot == -1 || part.endsWith(".")) { | ||
return defaultMimeType; | ||
} else { | ||
String extension = part.substring(dot+1); | ||
String type = mimeTypes.get(extension.toLowerCase()); | ||
if(type==null) { | ||
type = "text/plain"; | ||
} | ||
return type; | ||
} | ||
} | ||
} |
Oops, something went wrong.