From 3eeb86ce4e6fbe457d589c080531af230290abff Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Sun, 15 Mar 2020 15:00:17 +0100 Subject: [PATCH] Improve SpringerLink fetcher --- .../logic/importer/fetcher/SpringerLink.java | 36 +++++++++---------- .../importer/fetcher/SpringerLinkTest.java | 10 ++++++ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fetcher/SpringerLink.java b/src/main/java/org/jabref/logic/importer/fetcher/SpringerLink.java index 98e313078dd..e9aafb45597 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/SpringerLink.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/SpringerLink.java @@ -33,32 +33,32 @@ public class SpringerLink implements FulltextFetcher { @Override public Optional findFullText(BibEntry entry) throws IOException { Objects.requireNonNull(entry); - Optional pdfLink = Optional.empty(); // Try unique DOI first Optional doi = entry.getField(StandardField.DOI).flatMap(DOI::parse); - if (doi.isPresent()) { - // Available in catalog? - try { - HttpResponse jsonResponse = Unirest.get(API_URL) - .queryString("api_key", API_KEY) - .queryString("q", String.format("doi:%s", doi.get().getDOI())) - .asJson(); - if (jsonResponse.getBody() != null) { - JSONObject json = jsonResponse.getBody().getObject(); - int results = json.getJSONArray("result").getJSONObject(0).getInt("total"); + if (!doi.isPresent()) { + return Optional.empty(); + } + // Available in catalog? + try { + HttpResponse jsonResponse = Unirest.get(API_URL) + .queryString("api_key", API_KEY) + .queryString("q", String.format("doi:%s", doi.get().getDOI())) + .asJson(); + if (jsonResponse.getBody() != null) { + JSONObject json = jsonResponse.getBody().getObject(); + int results = json.getJSONArray("result").getJSONObject(0).getInt("total"); - if (results > 0) { - LOGGER.info("Fulltext PDF found @ Springer."); - pdfLink = Optional.of(new URL("http", CONTENT_HOST, String.format("/content/pdf/%s.pdf", doi.get().getDOI()))); - } + if (results > 0) { + LOGGER.info("Fulltext PDF found @ Springer."); + return Optional.of(new URL("http", CONTENT_HOST, String.format("/content/pdf/%s.pdf", doi.get().getDOI()))); } - } catch (UnirestException e) { - LOGGER.warn("SpringerLink API request failed", e); } + } catch (UnirestException e) { + LOGGER.warn("SpringerLink API request failed", e); } - return pdfLink; + return Optional.empty(); } @Override diff --git a/src/test/java/org/jabref/logic/importer/fetcher/SpringerLinkTest.java b/src/test/java/org/jabref/logic/importer/fetcher/SpringerLinkTest.java index 275a5fb6761..1a84ae82cfa 100644 --- a/src/test/java/org/jabref/logic/importer/fetcher/SpringerLinkTest.java +++ b/src/test/java/org/jabref/logic/importer/fetcher/SpringerLinkTest.java @@ -53,4 +53,14 @@ public void notFoundByDOI() throws IOException { assertEquals(Optional.empty(), finder.findFullText(entry)); } + + @Test + void entityWithoutDoi() throws IOException { + assertEquals(Optional.empty(), finder.findFullText(entry)); + } + + @Test + void trustLevel() { + assertEquals(TrustLevel.PUBLISHER, finder.getTrustLevel()); + } }