From d38f813482f4e7ddf6e6a2796092226da9012271 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Sat, 22 Feb 2020 15:14:43 +0100 Subject: [PATCH] Fix NPE in OpenAccessDoi (#5994) --- .../logic/importer/fetcher/OpenAccessDoi.java | 39 ++++++++++++------- .../importer/fetcher/OpenAccessDoiTest.java | 2 - 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fetcher/OpenAccessDoi.java b/src/main/java/org/jabref/logic/importer/fetcher/OpenAccessDoi.java index 7bb7a5125e1..e06c1157308 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/OpenAccessDoi.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/OpenAccessDoi.java @@ -11,11 +11,10 @@ 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 oaDOI. @@ -23,6 +22,9 @@ * @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 @@ -47,17 +49,24 @@ public TrustLevel getTrustLevel() { return TrustLevel.META_SEARCH; } - public Optional findFullText(DOI doi) throws UnirestException, MalformedURLException { - HttpResponse jsonResponse = Unirest.get(API_URL + doi.getDOI() + "?email=developers@jabref.org") - .header("accept", "application/json") - .asJson(); - JSONObject root = jsonResponse.getBody().getObject(); - Optional 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 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(); + } + }); } } diff --git a/src/test/java/org/jabref/logic/importer/fetcher/OpenAccessDoiTest.java b/src/test/java/org/jabref/logic/importer/fetcher/OpenAccessDoiTest.java index b7e6c7533bd..936d685712c 100644 --- a/src/test/java/org/jabref/logic/importer/fetcher/OpenAccessDoiTest.java +++ b/src/test/java/org/jabref/logic/importer/fetcher/OpenAccessDoiTest.java @@ -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)); } }