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 f72f1b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
19 changes: 9 additions & 10 deletions src/main/java/net/sf/jabref/logic/io/MimeTypeDetector.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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 +16,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
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Ignore;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import static org.junit.Assert.*;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class MimeTypeDetectorTest {
@Rule
Expand Down

0 comments on commit f72f1b3

Please sign in to comment.