forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix NPE in OpenAccessDoi (JabRef#5994)
- Loading branch information
Showing
2 changed files
with
24 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,18 +11,20 @@ | |
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 kong.unirest.json.JSONObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A fulltext fetcher that uses <a href="https://oadoi.org/">oaDOI</a>. | ||
* | ||
* @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/"; | ||
|
||
@Override | ||
|
@@ -47,17 +49,24 @@ public TrustLevel getTrustLevel() { | |
return TrustLevel.META_SEARCH; | ||
} | ||
|
||
public Optional<URL> findFullText(DOI doi) throws UnirestException, MalformedURLException { | ||
HttpResponse<JsonNode> jsonResponse = Unirest.get(API_URL + doi.getDOI() + "[email protected]") | ||
.header("accept", "application/json") | ||
.asJson(); | ||
JSONObject root = jsonResponse.getBody().getObject(); | ||
Optional<String> url = Optional.ofNullable(root.optJSONObject("best_oa_location")) | ||
.map(location -> location.optString("url")); | ||
if (url.isPresent()) { | ||
return Optional.of(new URL(url.get())); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
public Optional<URL> findFullText(DOI doi) throws UnirestException { | ||
return Optional.of(Unirest.get(API_URL + doi.getDOI() + "[email protected]") | ||
.header("accept", "application/json") | ||
.asJson()) | ||
.map(response -> response.getBody()) | ||
.filter(body -> body != null) | ||
.map(body -> body.getObject()) | ||
.filter(root -> root != null) | ||
.map(root -> root.optJSONObject("best_oa_location")) | ||
.filter(object -> object != null) | ||
.map(location -> location.optString("url")) | ||
.flatMap(url -> { | ||
try { | ||
return Optional.of(new URL(url)); | ||
} catch (MalformedURLException e) { | ||
LOGGER.debug("Could not determine URL to fetch full text from", e); | ||
return Optional.empty(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters