Skip to content
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
39 changes: 24 additions & 15 deletions src/main/java/org/jabref/logic/importer/fetcher/OpenAccessDoi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.identifier.DOI;

import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import kong.unirest.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A fulltext fetcher that uses <a href="https://oadoi.org/">oaDOI</a>.
*
* @implSpec API is documented at http://unpaywall.org/api/v2
*/
public class OpenAccessDoi implements FulltextFetcher {

private static final Logger LOGGER = LoggerFactory.getLogger(FulltextFetcher.class);

private static String API_URL = "https://api.oadoi.org/v2/";

@Override
Expand All @@ -47,17 +49,24 @@ public TrustLevel getTrustLevel() {
return TrustLevel.META_SEARCH;
}

public Optional<URL> findFullText(DOI doi) throws UnirestException, MalformedURLException {
HttpResponse<JsonNode> jsonResponse = Unirest.get(API_URL + doi.getDOI() + "[email protected]")
.header("accept", "application/json")
.asJson();
JSONObject root = jsonResponse.getBody().getObject();
Optional<String> url = Optional.ofNullable(root.optJSONObject("best_oa_location"))
.map(location -> location.optString("url"));
if (url.isPresent()) {
return Optional.of(new URL(url.get()));
} else {
return Optional.empty();
}
public Optional<URL> findFullText(DOI doi) throws UnirestException {
return Optional.of(Unirest.get(API_URL + doi.getDOI() + "[email protected]")
.header("accept", "application/json")
.asJson())
.map(response -> response.getBody())
.filter(body -> body != null)
.map(body -> body.getObject())
.filter(root -> root != null)
.map(root -> root.optJSONObject("best_oa_location"))
.filter(object -> object != null)
.map(location -> location.optString("url"))
.flatMap(url -> {
try {
return Optional.of(new URL(url));
} catch (MalformedURLException e) {
LOGGER.debug("Could not determine URL to fetch full text from", e);
return Optional.empty();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ void setUp() {
@Test
void findByDOI() throws IOException {
entry.setField(StandardField.DOI, "10.1038/nature12373");

assertEquals(Optional.of(new URL("https://dash.harvard.edu/bitstream/1/12285462/1/Nanometer-Scale%20Thermometry.pdf")), finder.findFullText(entry));
}

@Test
void notFoundByDOI() throws IOException {
entry.setField(StandardField.DOI, "10.1186/unknown-doi");

assertEquals(Optional.empty(), finder.findFullText(entry));
}
}