Skip to content

Commit

Permalink
Class-global Unirest config
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Sep 19, 2024
1 parent 057bfb5 commit ed18e08
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions src/main/java/org/jabref/logic/net/URLDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.jabref.logic.importer.FetcherServerException;
import org.jabref.logic.util.io.FileUtil;

import kong.unirest.core.HttpResponse;
import kong.unirest.core.Unirest;
import kong.unirest.core.UnirestException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -86,6 +85,10 @@ public URLDownload(String source) throws MalformedURLException {
public URLDownload(URL source) {
this.source = source;
this.addHeader("User-Agent", URLDownload.USER_AGENT);
Unirest.config().reset()
.followRedirects(true)
.enableCookieManagement(true)
.setDefaultHeader("User-Agent", USER_AGENT);
}

/**
Expand All @@ -106,18 +109,11 @@ public URL getSource() {
}

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;

// Try to use HEAD request to avoid downloading the whole file
try {
String urlToCheck = source.toString();
HttpResponse<String> response = Unirest.head(urlToCheck).asString();
// check if we have redirects, e.g. arxiv will give otherwise content type html for the original url
String locationHeader = response.getHeaders().getFirst("location");
if (locationHeader != null && !locationHeader.isEmpty()) {
urlToCheck = locationHeader;
}
contentType = Unirest.head(urlToCheck).asString().getHeaders().getFirst("Content-Type");
if ((contentType != null) && !contentType.isEmpty()) {
return Optional.of(contentType);
Expand Down Expand Up @@ -158,12 +154,6 @@ public Optional<String> getMimeType() {
* @return the status code of the response
*/
public boolean canBeReached() throws UnirestException {
// new unirest version does not support apache http client any longer
Unirest.config().reset()
.followRedirects(true)
.enableCookieManagement(true)
.setDefaultHeader("User-Agent", USER_AGENT);

int statusCode = Unirest.head(source.toString()).asString().getStatus();
return (statusCode >= 200) && (statusCode < 300);
}
Expand Down

0 comments on commit ed18e08

Please sign in to comment.