Search in sources :

Example 6 with UnirestException

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;
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode)

Example 7 with UnirestException

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);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FetcherException(org.jabref.logic.importer.FetcherException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) ParseException(org.jabref.logic.importer.ParseException)

Example 8 with UnirestException

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();
}
Also used : Element(org.jsoup.nodes.Element) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) Document(org.jsoup.nodes.Document) URL(java.net.URL) DOI(org.jabref.model.entry.identifier.DOI)

Example 9 with UnirestException

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;
}
Also used : HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse)

Example 10 with UnirestException

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");
    }
}
Also used : JSONObject(org.json.JSONObject) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException)

Aggregations

UnirestException (com.mashape.unirest.http.exceptions.UnirestException)15 JsonNode (com.mashape.unirest.http.JsonNode)8 JSONObject (org.json.JSONObject)8 ActionException (org.apache.zeppelin.elasticsearch.action.ActionException)4 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)4 HitWrapper (org.apache.zeppelin.elasticsearch.action.HitWrapper)4 JSONArray (org.json.JSONArray)4 URL (java.net.URL)3 BibEntry (org.jabref.model.entry.BibEntry)3 HttpRequest (com.mashape.unirest.request.HttpRequest)2 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)2 DOI (org.jabref.model.entry.identifier.DOI)2 Shard (tk.ardentbot.main.Shard)2 EnsemblRestIOException (uk.ac.ebi.spot.goci.exception.EnsemblRestIOException)2 RestResponseResult (uk.ac.ebi.spot.goci.model.RestResponseResult)2 JsonParseException (com.google.gson.JsonParseException)1 GetRequest (com.mashape.unirest.request.GetRequest)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 Iterator (java.util.Iterator)1