From f72f1b30527fd5ac52e578da57e57e78bac25329 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Mon, 22 Feb 2016 21:13:25 +0100 Subject: [PATCH] Add new Mime tyoe detection --- .../sf/jabref/logic/io/MimeTypeDetector.java | 19 +++++++++---------- .../jabref/logic/io/MimeTypeDetectorTest.java | 8 ++------ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/io/MimeTypeDetector.java b/src/main/java/net/sf/jabref/logic/io/MimeTypeDetector.java index 6f8554ea5ad..e71f3203e97 100644 --- a/src/main/java/net/sf/jabref/logic/io/MimeTypeDetector.java +++ b/src/main/java/net/sf/jabref/logic/io/MimeTypeDetector.java @@ -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); @@ -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; } diff --git a/src/test/java/net/sf/jabref/logic/io/MimeTypeDetectorTest.java b/src/test/java/net/sf/jabref/logic/io/MimeTypeDetectorTest.java index aad56e8f22e..3c9fe3c250e 100644 --- a/src/test/java/net/sf/jabref/logic/io/MimeTypeDetectorTest.java +++ b/src/test/java/net/sf/jabref/logic/io/MimeTypeDetectorTest.java @@ -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