From 086629d67ae49520bd4dc4ef5938ab03f0637ca7 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Sun, 15 Mar 2020 12:55:20 +0100 Subject: [PATCH] Improve OpenAccessDoi fetcher --- .../logic/importer/fetcher/OpenAccessDoi.java | 38 ++++++++++--------- .../logic/importer/FulltextFetchersTest.java | 2 +- .../importer/fetcher/OpenAccessDoiTest.java | 10 +++++ 3 files changed, 32 insertions(+), 18 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 e06c1157308..b5290ca8cea 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/OpenAccessDoi.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/OpenAccessDoi.java @@ -11,6 +11,8 @@ 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 org.slf4j.Logger; @@ -22,10 +24,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/"; + private static final String API_URL = "https://api.oadoi.org/v2/"; @Override public Optional findFullText(BibEntry entry) throws IOException { @@ -33,15 +34,16 @@ public Optional findFullText(BibEntry entry) throws IOException { Optional doi = entry.getField(StandardField.DOI) .flatMap(DOI::parse); - if (doi.isPresent()) { - try { - return findFullText(doi.get()); - } catch (UnirestException e) { - throw new IOException(e); - } - } else { + + if (!doi.isPresent()) { return Optional.empty(); } + + try { + return findFullText(doi.get()); + } catch (UnirestException e) { + throw new IOException(e); + } } @Override @@ -50,15 +52,17 @@ public TrustLevel getTrustLevel() { } 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) + HttpResponse request = Unirest.get(API_URL + doi.getDOI() + "?email=developers@jabref.org") + .header("accept", "application/json") + .asJson(); + + return Optional.of(request) + .map(HttpResponse::getBody) + .filter(Objects::nonNull) + .map(JsonNode::getObject) + .filter(Objects::nonNull) .map(root -> root.optJSONObject("best_oa_location")) - .filter(object -> object != null) + .filter(Objects::nonNull) .map(location -> location.optString("url")) .flatMap(url -> { try { diff --git a/src/test/java/org/jabref/logic/importer/FulltextFetchersTest.java b/src/test/java/org/jabref/logic/importer/FulltextFetchersTest.java index 3db92d048c1..724c0b52c80 100644 --- a/src/test/java/org/jabref/logic/importer/FulltextFetchersTest.java +++ b/src/test/java/org/jabref/logic/importer/FulltextFetchersTest.java @@ -59,7 +59,7 @@ public void noTrustLevel() throws MalformedURLException { } @Test - public void higherTrustLevelWins() throws MalformedURLException, IOException, FetcherException { + public void higherTrustLevelWins() throws IOException, FetcherException { final URL lowUrl = new URL("http://docs.oasis-open.org/opencsa/sca-bpel/sca-bpel-1.1-spec-cd-01.pdf"); final URL highUrl = new URL("http://docs.oasis-open.org/wsbpel/2.0/OS/wsbpel-v2.0-OS.pdf"); 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 936d685712c..9fa8d7f361e 100644 --- a/src/test/java/org/jabref/logic/importer/fetcher/OpenAccessDoiTest.java +++ b/src/test/java/org/jabref/logic/importer/fetcher/OpenAccessDoiTest.java @@ -36,4 +36,14 @@ void notFoundByDOI() throws IOException { entry.setField(StandardField.DOI, "10.1186/unknown-doi"); assertEquals(Optional.empty(), finder.findFullText(entry)); } + + @Test + void entryWithoutDoi() throws IOException { + assertEquals(Optional.empty(), finder.findFullText(entry)); + } + + @Test + void trustLevel() { + assertEquals(TrustLevel.META_SEARCH, finder.getTrustLevel()); + } }