Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getAgency moved from DOI to DoiFetcher #6683

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}