From d11a57556ac1980bd56a982daed23d290aae5ce9 Mon Sep 17 00:00:00 2001 From: mind000 Date: Mon, 13 Jul 2020 10:43:10 +0200 Subject: [PATCH] getAgency moved from DOI to DoiFetcher --- .../logic/importer/fetcher/DoiFetcher.java | 25 ++++++++++++++++++- .../jabref/model/entry/identifier/DOI.java | 24 ------------------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fetcher/DoiFetcher.java b/src/main/java/org/jabref/logic/importer/fetcher/DoiFetcher.java index a59c02882f0..02d7812b638 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/DoiFetcher.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/DoiFetcher.java @@ -25,7 +25,9 @@ import org.jabref.model.util.DummyFileUpdateMonitor; import org.jabref.model.util.OptionalUtil; +import kong.unirest.json.JSONArray; import kong.unirest.json.JSONException; +import kong.unirest.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,7 +61,7 @@ public Optional performSearchById(String identifier) throws FetcherExc Optional fetchedEntry; // mEDRA does not return a parsable bibtex string - if (doi.get().getAgency().isPresent() && "medra".equalsIgnoreCase(doi.get().getAgency().get())) { + if (getAgency(doi.get()).isPresent() && "medra".equalsIgnoreCase(getAgency(doi.get()).get())) { return new Medra().performSearchById(identifier); } @@ -102,4 +104,25 @@ public List performSearch(BibEntry entry) throws FetcherException { } } + /** + * Returns registration agency. Optional.empty() if no agency is found. + * + * @param doi the DOI to be searched + */ + public Optional getAgency(DOI doi) throws IOException { + Optional agency = Optional.empty(); + try { + URLDownload download = getUrlDownload(new URL(DOI.AGENCY_RESOLVER + "/" + doi.getDOI())); + JSONObject response = new JSONArray(download.asString()).getJSONObject(0); + if (response != null) { + agency = Optional.ofNullable(response.optString("RA")); + } + } catch (JSONException e) { + LOGGER.error("Cannot parse agency fetcher repsonse to JSON"); + return Optional.empty(); + } + + return agency; + } + } diff --git a/src/main/java/org/jabref/model/entry/identifier/DOI.java b/src/main/java/org/jabref/model/entry/identifier/DOI.java index 9d687069831..3a0d9175b7e 100644 --- a/src/main/java/org/jabref/model/entry/identifier/DOI.java +++ b/src/main/java/org/jabref/model/entry/identifier/DOI.java @@ -1,22 +1,16 @@ package org.jabref.model.entry.identifier; -import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; import java.util.Locale; import java.util.Objects; import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.jabref.logic.net.URLDownload; import org.jabref.model.entry.field.Field; import org.jabref.model.entry.field.StandardField; -import kong.unirest.json.JSONArray; -import kong.unirest.json.JSONException; -import kong.unirest.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -260,22 +254,4 @@ public int hashCode() { return Objects.hash(doi.toLowerCase(Locale.ENGLISH)); } - /** - * Returns registration agency. Optional.empty() if no agency is found. - */ - public Optional getAgency() throws IOException { - Optional agency = Optional.empty(); - try { - URLDownload download = new URLDownload(new URL(DOI.AGENCY_RESOLVER + "/" + doi)); - JSONObject response = new JSONArray(download.asString()).getJSONObject(0); - if (response != null) { - agency = Optional.ofNullable(response.optString("RA")); - } - } catch (JSONException e) { - LOGGER.error("Cannot parse agency fetcher repsonse to JSON"); - return Optional.empty(); - } - - return agency; - } }