Skip to content

Commit

Permalink
Add new Mime tyoe detection
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Feb 22, 2016
1 parent f08466e commit 0e4f177
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/main/java/net/sf/jabref/logic/io/MimeTypeDetector.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.sf.jabref.logic.io;

import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class MimeTypeDetector {
private static final Log LOGGER = LogFactory.getLog(MimeTypeDetector.class);

Expand All @@ -14,15 +17,12 @@ public static boolean isPdfContentType(String url) {
return contentType != null && contentType.toLowerCase().startsWith("application/pdf");
}

public static String getMimeType(String url) {
private static String getMimeType(String url) {
try {
String contentType = Unirest.head(url).asBinary().getHeaders().getFirst("Content-Type");
// HEAD and GET headers might differ, try real GET request
if(contentType == null) {
contentType = Unirest.get(url).asBinary().getHeaders().getFirst("Content-Type");
}
return contentType;
} catch (UnirestException | RuntimeException e) {
URLConnection connection = new URL(url).openConnection();

return connection.getContentType();
} catch (IOException e) {
LOGGER.debug("Error getting MIME type of URL", e);
return null;
}
Expand Down

0 comments on commit 0e4f177

Please sign in to comment.