Skip to content

Commit

Permalink
getAgency moved from DOI to DoiFetcher (#6683)
Browse files Browse the repository at this point in the history
  • Loading branch information
alchimos committed Jul 13, 2020
1 parent d552369 commit ba68c09
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
25 changes: 24 additions & 1 deletion src/main/java/org/jabref/logic/importer/fetcher/DoiFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -59,7 +61,7 @@ public Optional<BibEntry> performSearchById(String identifier) throws FetcherExc
Optional<BibEntry> 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);
}

Expand Down Expand Up @@ -102,4 +104,25 @@ public List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
}
}

/**
* Returns registration agency. Optional.empty() if no agency is found.
*
* @param doi the DOI to be searched
*/
public Optional<String> getAgency(DOI doi) throws IOException {
Optional<String> 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;
}

}
24 changes: 0 additions & 24 deletions src/main/java/org/jabref/model/entry/identifier/DOI.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<String> getAgency() throws IOException {
Optional<String> 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;
}
}

0 comments on commit ba68c09

Please sign in to comment.