diff --git a/src/main/java/org/jabref/gui/linkedfile/DownloadLinkedFileAction.java b/src/main/java/org/jabref/gui/linkedfile/DownloadLinkedFileAction.java index 57f35ae1ad5..c81bf261ca2 100644 --- a/src/main/java/org/jabref/gui/linkedfile/DownloadLinkedFileAction.java +++ b/src/main/java/org/jabref/gui/linkedfile/DownloadLinkedFileAction.java @@ -280,14 +280,11 @@ private Optional inferFileType(URLDownload urlDownload) { } private Optional inferFileTypeFromMimeType(URLDownload urlDownload) { - String mimeType = urlDownload.getMimeType(); - - if (mimeType != null) { - LOGGER.debug("MIME Type suggested: {}", mimeType); - return ExternalFileTypes.getExternalFileTypeByMimeType(mimeType, externalApplicationsPreferences); - } else { - return Optional.empty(); - } + return urlDownload.getMimeType() + .flatMap(mimeType -> { + LOGGER.debug("MIME Type suggested: {}", mimeType); + return ExternalFileTypes.getExternalFileTypeByMimeType(mimeType, externalApplicationsPreferences); + }); } private Optional inferFileTypeFromURL(String url) { diff --git a/src/main/java/org/jabref/logic/net/URLDownload.java b/src/main/java/org/jabref/logic/net/URLDownload.java index 33af38e827e..1dfde1da1ce 100644 --- a/src/main/java/org/jabref/logic/net/URLDownload.java +++ b/src/main/java/org/jabref/logic/net/URLDownload.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; @@ -104,7 +105,7 @@ public URL getSource() { return source; } - public String getMimeType() { + public Optional getMimeType() { Unirest.config().setDefaultHeader("User-Agent", "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); String contentType; @@ -119,7 +120,7 @@ public String getMimeType() { } contentType = Unirest.head(urlToCheck).asString().getHeaders().getFirst("Content-Type"); if ((contentType != null) && !contentType.isEmpty()) { - return contentType; + return Optional.of(contentType); } } catch (Exception e) { LOGGER.debug("Error getting MIME type of URL via HEAD request", e); @@ -129,7 +130,7 @@ public String getMimeType() { try { contentType = Unirest.get(source.toString()).asString().getHeaders().get("Content-Type").getFirst(); if ((contentType != null) && !contentType.isEmpty()) { - return contentType; + return Optional.of(contentType); } } catch (Exception e) { LOGGER.debug("Error getting MIME type of URL via GET request", e); @@ -141,13 +142,13 @@ public String getMimeType() { contentType = connection.getContentType(); if ((contentType != null) && !contentType.isEmpty()) { - return contentType; + return Optional.of(contentType); } } catch (IOException e) { LOGGER.debug("Error trying to get MIME type of local URI", e); } - return ""; + return Optional.empty(); } /** @@ -168,13 +169,7 @@ public boolean canBeReached() throws UnirestException { } public boolean isMimeType(String type) { - String mime = getMimeType(); - - if (mime.isEmpty()) { - return false; - } - - return mime.startsWith(type); + return getMimeType().map(mimeType -> mimeType.startsWith(type)).orElse(false); } public boolean isPdf() { diff --git a/src/test/java/org/jabref/logic/net/URLDownloadTest.java b/src/test/java/org/jabref/logic/net/URLDownloadTest.java index f1fa4caa49a..5920950a71c 100644 --- a/src/test/java/org/jabref/logic/net/URLDownloadTest.java +++ b/src/test/java/org/jabref/logic/net/URLDownloadTest.java @@ -67,7 +67,7 @@ void fileDownload() throws Exception { void determineMimeType() throws Exception { URLDownload dl = new URLDownload(new URL("http://www.google.com")); - assertTrue(dl.getMimeType().startsWith("text/html")); + assertTrue(dl.getMimeType().get().startsWith("text/html")); } @Test