use of com.mashape.unirest.http.exceptions.UnirestException in project jabref by JabRef.
the class DOAJFetcher method processQuery.
@Override
public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
shouldContinue = true;
try {
status.setStatus(Localization.lang("Searching..."));
HttpResponse<JsonNode> jsonResponse;
jsonResponse = Unirest.get(SEARCH_URL + query + "?pageSize=1").header("accept", "application/json").asJson();
JSONObject jo = jsonResponse.getBody().getObject();
int numberToFetch = jo.getInt("total");
if (numberToFetch > 0) {
if (numberToFetch > MAX_PER_PAGE) {
boolean numberEntered = false;
do {
String strCount = JOptionPane.showInputDialog(Localization.lang("%0 references found. Number of references to fetch?", String.valueOf(numberToFetch)));
if (strCount == null) {
status.setStatus(Localization.lang("%0 import canceled", getTitle()));
return false;
}
try {
numberToFetch = Integer.parseInt(strCount.trim());
numberEntered = true;
} catch (NumberFormatException ex) {
status.showMessage(Localization.lang("Please enter a valid number"));
}
} while (!numberEntered);
}
// Keep track of number of items fetched for the progress bar
int fetched = 0;
for (int page = 1; ((page - 1) * MAX_PER_PAGE) <= numberToFetch; page++) {
if (!shouldContinue) {
break;
}
int noToFetch = Math.min(MAX_PER_PAGE, numberToFetch - ((page - 1) * MAX_PER_PAGE));
jsonResponse = Unirest.get(SEARCH_URL + query + "?page=" + page + "&pageSize=" + noToFetch).header("accept", "application/json").asJson();
jo = jsonResponse.getBody().getObject();
if (jo.has("results")) {
JSONArray results = jo.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject bibJsonEntry = results.getJSONObject(i).getJSONObject("bibjson");
BibEntry entry = jsonConverter.parseBibJSONtoBibtex(bibJsonEntry, Globals.prefs.getKeywordDelimiter());
inspector.addEntry(entry);
fetched++;
inspector.setProgress(fetched, numberToFetch);
}
}
}
return true;
} else {
status.showMessage(Localization.lang("No entries found for the search string '%0'", query), Localization.lang("Search %0", getTitle()), JOptionPane.INFORMATION_MESSAGE);
return false;
}
} catch (UnirestException e) {
LOGGER.error("Error while fetching from " + getTitle(), e);
((ImportInspectionDialog) inspector).showErrorMessage(this.getTitle(), e.getLocalizedMessage());
return false;
}
}
use of com.mashape.unirest.http.exceptions.UnirestException in project jabref by JabRef.
the class IsbnViaChimboriFetcher method performSearchById.
@Override
public Optional<BibEntry> performSearchById(String identifier) throws FetcherException {
if (StringUtil.isBlank(identifier)) {
return Optional.empty();
}
this.ensureThatIsbnIsValid(identifier);
HttpResponse<String> postResponse;
try {
postResponse = Unirest.post("https://bibtex.chimbori.com/isbn-bibtex").field("isbn", identifier).asString();
} catch (UnirestException e) {
throw new FetcherException("Could not retrieve data from chimbori.com", e);
}
if (postResponse.getStatus() != 200) {
throw new FetcherException("Error while retrieving data from chimbori.com: " + postResponse.getBody());
}
List<BibEntry> fetchedEntries;
try {
fetchedEntries = getParser().parseEntries(postResponse.getRawBody());
} catch (ParseException e) {
throw new FetcherException("An internal parser error occurred", e);
}
if (fetchedEntries.isEmpty()) {
return Optional.empty();
} else if (fetchedEntries.size() > 1) {
LOGGER.info("Fetcher " + getName() + "found more than one result for identifier " + identifier + ". We will use the first entry.");
}
BibEntry entry = fetchedEntries.get(0);
// chimbori does not return an ISBN. Thus, we add the one searched for
entry.setField("isbn", identifier);
doPostCleanup(entry);
return Optional.of(entry);
}
use of com.mashape.unirest.http.exceptions.UnirestException in project jabref by JabRef.
the class ScienceDirect method findFullText.
@Override
public Optional<URL> findFullText(BibEntry entry) throws IOException {
Objects.requireNonNull(entry);
// Try unique DOI first
Optional<DOI> doi = entry.getField(FieldName.DOI).flatMap(DOI::parse);
if (doi.isPresent()) {
// Available in catalog?
try {
String sciLink = getUrlByDoi(doi.get().getDOI());
// scrape the web page not as mobile client!
if (!sciLink.isEmpty()) {
Document html = Jsoup.connect(sciLink).userAgent(URLDownload.USER_AGENT).referrer("http://www.google.com").ignoreHttpErrors(true).get();
// Retrieve PDF link (old page)
Element link = html.getElementById("pdfLink");
if (link != null) {
LOGGER.info("Fulltext PDF found @ ScienceDirect (old page).");
Optional<URL> pdfLink = Optional.of(new URL(link.attr("pdfurl")));
return pdfLink;
}
// Retrieve PDF link (new page)
String url = html.getElementsByClass("pdf-download-btn-link").attr("href");
if (url != null) {
LOGGER.info("Fulltext PDF found @ ScienceDirect (new page).");
Optional<URL> pdfLink = Optional.of(new URL("http://www.sciencedirect.com" + url));
return pdfLink;
}
}
} catch (UnirestException e) {
LOGGER.warn("ScienceDirect API request failed", e);
}
}
return Optional.empty();
}
use of com.mashape.unirest.http.exceptions.UnirestException in project zeppelin by apache.
the class HttpBasedClient method index.
@Override
public ActionResponse index(String index, String type, String id, String data) {
ActionResponse response = null;
try {
HttpRequestWithBody request = null;
if (StringUtils.isEmpty(id)) {
request = Unirest.post(getUrl(index, type, id, false));
} else {
request = Unirest.put(getUrl(index, type, id, false));
}
request.header("Accept", "application/json").header("Content-Type", "application/json").body(data).getHttpRequest();
if (StringUtils.isNotEmpty(username)) {
request.basicAuth(username, password);
}
final HttpResponse<JsonNode> result = request.asJson();
final boolean isSucceeded = isSucceeded(result);
if (isSucceeded) {
response = new ActionResponse().succeeded(true).hit(new HitWrapper(getFieldAsString(result, "_index"), getFieldAsString(result, "_type"), getFieldAsString(result, "_id"), null));
} else {
throw new ActionException(result.getBody().toString());
}
} catch (final UnirestException e) {
throw new ActionException(e);
}
return response;
}
use of com.mashape.unirest.http.exceptions.UnirestException in project goci by EBISPOT.
the class EnsemblRestService method fetchJson.
/**
* Fetch response from API
*
* @param url URL to query
* @return the corresponding result
*/
private RestResponseResult fetchJson(String url) throws UnirestException, InterruptedException, EnsemblRestIOException {
RestResponseResult restResponseResult = new RestResponseResult();
restResponseResult.setUrl(url);
// Parameters to monitor API call success
int maxTries = 5;
int tries = 0;
int wait = 1;
boolean success = false;
while (!success && tries < maxTries) {
tries++;
try {
getLog().trace("Querying URL: " + url);
HttpResponse<JsonNode> response = Unirest.get(url).header("Content-Type", "application/json").asJson();
String retryHeader = response.getHeaders().getFirst("Retry-After");
getLog().trace("URL response: " + response.getStatus());
if (response.getStatus() == 200) {
// Success
success = true;
//restResponseResult.setRestResult(response.getBody());
} else if (response.getStatus() == 429 && retryHeader != null) {
// Too Many Requests
Long waitSeconds = Long.valueOf(retryHeader);
Thread.sleep(waitSeconds * 1000);
} else {
if (response.getStatus() == 503) {
// Service unavailable
restResponseResult.setError("No server is available to handle this request (Error 503: service unavailable) at url: " + url);
getLog().error("No server is available to handle this request (Error 503: service unavailable) at url: " + url);
throw new EnsemblRestIOException("No server is available to handle this request (Error 503: service unavailable) at url: " + url);
} else if (response.getStatus() == 400) {
// Bad request (no result found)
JSONObject json_obj = response.getBody().getObject();
if (json_obj.has("error")) {
restResponseResult.setError(json_obj.getString("error"));
}
getLog().error(url + " is generating an invalid request. (Error 400: bad request)");
// Success is set to true here as the API call was successful
// but the gene or snp does not exist in Ensembl
success = true;
} else {
// Other issue
restResponseResult.setError("No data available at url " + url);
getLog().error("No data at " + url);
throw new EnsemblRestIOException("No data available at url " + url);
}
}
} catch (UnirestException e) {
getLog().error("Caught exception from Ensembl Rest call, this call will be retried after " + wait + "s.", e);
Thread.sleep(wait * 1000);
}
}
if (success) {
return restResponseResult;
} else {
getLog().error("Failed to obtain a result from from '" + url + "' after after " + maxTries + " attempts");
throw new EnsemblRestIOException("Failed to obtain a result from '" + url + "' after " + maxTries + " attempts");
}
}
Aggregations