Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix downloading in case of circular redirect #9519

Merged
merged 4 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve




### Fixed

- The tab "deprecated fields" is shown in biblatex-mode only. [#7757](https://github.com/JabRef/jabref/issues/7757)
Expand All @@ -37,6 +36,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where extra curly braces in some fields would trigger an exception when selecting the entry or doing an integrity check [#9475](https://github.com/JabRef/jabref/issues/9475), [#9503](https://github.com/JabRef/jabref/issues/9503)
- We fixed an issue where entering a date in the format "YYYY/MM" in the entry editor date field caused an exception. [#9492](https://github.com/JabRef/jabref/issues/9492)
- For portable versions, the `.deb` file now works on plain debian again. [#9472](https://github.com/JabRef/jabref/issues/9472)
- We fixed an issue where the download of linked online files failed after an import of entries for certain urls [#9518](https://github.com/JabRef/jabref/issues/9518)


### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,17 @@ public boolean checkSSLHandshake(URLDownload urlDownload) {
if (ex.getCause() instanceof javax.net.ssl.SSLHandshakeException) {
if (dialogService.showConfirmationDialogAndWait(Localization.lang("Download file"),
Localization.lang("Unable to find valid certification path to requested target(%0), download anyway?",
urlDownload.getSource().toString()))) {
urlDownload.getSource().toString()))) {
return true;
} else {
dialogService.notify(Localization.lang("Download operation canceled."));
return false;
}
} else {
LOGGER.error("Error while checking if the file can be downloaded", ex);
dialogService.notify(Localization.lang("Error downloading"));
return false;
}
return false;
}
return true;
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/jabref/logic/net/URLDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@
import org.jabref.logic.importer.FetcherClientException;
import org.jabref.logic.importer.FetcherServerException;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.util.FileHelper;

import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import kong.unirest.apache.ApacheClient;
import org.apache.http.client.config.RequestConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -66,7 +67,7 @@
*/
public class URLDownload {

public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0";
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36";
private static final Logger LOGGER = LoggerFactory.getLogger(URLDownload.class);
private static final Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofSeconds(30);

Expand Down Expand Up @@ -202,7 +203,14 @@ public String getMimeType() {
* @return the status code of the response
*/
public boolean canBeReached() throws UnirestException {
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");

// Set a custom Apache Client Builder to be able to allow circular redirects, otherwise downloads from springer might not work
Unirest.config().httpClient(new ApacheClient.Builder()
.withRequestConfig((c, r) -> RequestConfig.custom()
.setCircularRedirectsAllowed(true)
.build()));

Unirest.config().setDefaultHeader("User-Agent", USER_AGENT);

int statusCode = Unirest.head(source.toString()).asString().getStatus();
return (statusCode >= 200) && (statusCode < 300);
Expand Down Expand Up @@ -333,7 +341,7 @@ public Path toTemporaryFile() throws IOException {
// Take everything after the last '/' as name + extension
String fileNameWithExtension = sourcePath.substring(sourcePath.lastIndexOf('/') + 1);
String fileName = "jabref-" + FileUtil.getBaseName(fileNameWithExtension);
String extension = "." + FileHelper.getFileExtension(fileNameWithExtension).orElse("tmp");
String extension = "." + FileUtil.getFileExtension(fileNameWithExtension).orElse("tmp");

// Create temporary file and download to it
Path file = Files.createTempFile(fileName, extension);
Expand Down