Skip to content

Commit

Permalink
Fix NPE in OpenAccessDoi (#5994)
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Feb 22, 2020
1 parent 66f88d0 commit d38f813
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
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=developers@jabref.org")
.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=developers@jabref.org")
.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));
}
}

0 comments on commit d38f813

Please sign in to comment.