Skip to content

Commit

Permalink
Use Optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Sep 19, 2024
1 parent 742ed9f commit 057bfb5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,11 @@ private Optional<ExternalFileType> inferFileType(URLDownload urlDownload) {
}

private Optional<ExternalFileType> 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<ExternalFileType> inferFileTypeFromURL(String url) {
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/org/jabref/logic/net/URLDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,7 +105,7 @@ public URL getSource() {
return source;
}

public String getMimeType() {
public Optional<String> 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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}

/**
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jabref/logic/net/URLDownloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 057bfb5

Please sign in to comment.