Search in sources :

Example 16 with JsonNode

use of com.mashape.unirest.http.JsonNode in project goci by EBISPOT.

the class EuropepmcPubMedSearchService method createStudyByPubmed.

public EuropePMCData createStudyByPubmed(String pubmedId) throws PubmedLookupException {
    EuropePMCData europePMCData = new EuropePMCData();
    String urlRequest;
    if (europepmcRoot != null && europepmcSearch != null) {
        urlRequest = europepmcRoot.concat(europepmcSearch);
    } else {
        throw new PubmedLookupException("Unable to search pubmed - no URL configured. " + "Set europepmc properties in your config!");
    }
    String queryUrl = urlRequest.replace("{idlist}", pubmedId);
    ResponseEntity<String> out;
    RestResponseResult result = new RestResponseResult();
    RestTemplate restTemplate = new RestTemplate();
    // restTemplate.setErrorHandler(new CustomResponseErrorHandler());
    // Add the Jackson message converter
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    List<MediaType> mediaTypes = new ArrayList<MediaType>();
    mediaTypes.add(MediaType.TEXT_HTML);
    mediaTypes.add(MediaType.APPLICATION_JSON);
    mediaTypes.add(MediaType.ALL);
    headers.setAccept(mediaTypes);
    // headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON,MediaType.TEXT_HTML));
    HttpEntity<Object> entity = new HttpEntity<Object>(headers);
    getLog().debug("Querying " + queryUrl);
    // and do I need this JSON media type for my use case?
    try {
        out = restTemplate.exchange(queryUrl, HttpMethod.GET, entity, String.class);
        result.setStatus(out.getStatusCode().value());
        result.setUrl(queryUrl);
        System.out.println(queryUrl);
        JsonNode body = new JsonNode(out.getBody().toString());
        result.setRestResult(body);
    } catch (Exception e) {
        throw new PubmedLookupException("EuropePMC : REST API Failed");
    }
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(EuropePMCData.class, new EuropePMCDeserializer());
    mapper.registerModule(module);
    try {
        europePMCData = mapper.readValue(result.getRestResult().toString(), EuropePMCData.class);
    } catch (IOException ioe) {
        System.out.println("EuropePMC : IO Exception - JSON conversion");
        throw new PubmedLookupException("EuropePMC : IO Exception - JSON conversion");
    } catch (Exception e) {
        System.out.println("EuropePMC : Generic Error conversion JSON");
        throw new PubmedLookupException("EuropePMC : Generic Error conversion JSON");
    }
    return europePMCData;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) PubmedLookupException(uk.ac.ebi.spot.goci.service.exception.PubmedLookupException) ArrayList(java.util.ArrayList) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode) IOException(java.io.IOException) EuropePMCData(uk.ac.ebi.spot.goci.utils.EuropePMCData) PubmedLookupException(uk.ac.ebi.spot.goci.service.exception.PubmedLookupException) IOException(java.io.IOException) EuropePMCDeserializer(uk.ac.ebi.spot.goci.utils.EuropePMCDeserializer) RestTemplate(org.springframework.web.client.RestTemplate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 17 with JsonNode

use of com.mashape.unirest.http.JsonNode in project zeppelin by apache.

the class HttpBasedClient method delete.

@Override
public ActionResponse delete(String index, String type, String id) {
    ActionResponse response = null;
    try {
        final HttpRequest request = Unirest.delete(getUrl(index, type, id, true));
        if (StringUtils.isNotEmpty(username)) {
            request.basicAuth(username, password);
        }
        final HttpResponse<String> result = request.asString();
        final boolean isSucceeded = isSucceeded(result);
        if (isSucceeded) {
            final JsonNode body = new JsonNode(result.getBody());
            response = new ActionResponse().succeeded(true).hit(new HitWrapper(getFieldAsString(body, "_index"), getFieldAsString(body, "_type"), getFieldAsString(body, "_id"), null));
        } else {
            throw new ActionException(result.getBody());
        }
    } catch (final UnirestException e) {
        throw new ActionException(e);
    }
    return response;
}
Also used : HttpRequest(com.mashape.unirest.request.HttpRequest) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) 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 18 with JsonNode

use of com.mashape.unirest.http.JsonNode in project zeppelin by apache.

the class HttpBasedClient method get.

@Override
public ActionResponse get(String index, String type, String id) {
    ActionResponse response = null;
    try {
        final HttpRequest request = Unirest.get(getUrl(index, type, id, true));
        if (StringUtils.isNotEmpty(username)) {
            request.basicAuth(username, password);
        }
        final HttpResponse<String> result = request.asString();
        final boolean isSucceeded = isSucceeded(result);
        if (isSucceeded) {
            final JsonNode body = new JsonNode(result.getBody());
            if (body.getObject().has("_index")) {
                response = new ActionResponse().succeeded(true).hit(new HitWrapper(getFieldAsString(body, "_index"), getFieldAsString(body, "_type"), getFieldAsString(body, "_id"), getFieldAsString(body, "_source")));
            } else {
                final JSONArray hits = getFieldAsArray(body.getObject(), "hits/hits");
                final JSONObject hit = (JSONObject) hits.iterator().next();
                response = new ActionResponse().succeeded(true).hit(new HitWrapper(hit.getString("_index"), hit.getString("_type"), hit.getString("_id"), hit.opt("_source").toString()));
            }
        } else {
            if (result.getStatus() == 404) {
                response = new ActionResponse().succeeded(false);
            } else {
                throw new ActionException(result.getBody());
            }
        }
    } catch (final UnirestException e) {
        throw new ActionException(e);
    }
    return response;
}
Also used : HttpRequest(com.mashape.unirest.request.HttpRequest) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) 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 19 with JsonNode

use of com.mashape.unirest.http.JsonNode in project zeppelin by apache.

the class HttpBasedClient method search.

@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
    ActionResponse response = null;
    if (!StringUtils.isEmpty(query)) {
        // So, try to parse as a JSON => if there is an error, consider the query a Lucene one
        try {
            gson.fromJson(query, Map.class);
        } catch (final JsonParseException e) {
            // This is not a JSON (or maybe not well formatted...)
            query = QUERY_STRING_TEMPLATE.replace("_Q_", query);
        }
    }
    try {
        final HttpRequestWithBody request = Unirest.post(getUrl(indices, types) + "/_search?size=" + size).header("Content-Type", "application/json");
        if (StringUtils.isNoneEmpty(query)) {
            request.header("Accept", "application/json").body(query);
        }
        if (StringUtils.isNotEmpty(username)) {
            request.basicAuth(username, password);
        }
        final HttpResponse<JsonNode> result = request.asJson();
        final JSONObject body = result.getBody() != null ? result.getBody().getObject() : null;
        if (isSucceeded(result)) {
            final long total = getTotal(result);
            response = new ActionResponse().succeeded(true).totalHits(total);
            if (containsAggs(result)) {
                JSONObject aggregationsMap = body.getJSONObject("aggregations");
                if (aggregationsMap == null) {
                    aggregationsMap = body.getJSONObject("aggs");
                }
                for (final String key : aggregationsMap.keySet()) {
                    final JSONObject aggResult = aggregationsMap.getJSONObject(key);
                    if (aggResult.has("buckets")) {
                        // Multi-bucket aggregations
                        final Iterator<Object> buckets = aggResult.getJSONArray("buckets").iterator();
                        while (buckets.hasNext()) {
                            response.addAggregation(new AggWrapper(AggregationType.MULTI_BUCKETS, buckets.next().toString()));
                        }
                    } else {
                        response.addAggregation(new AggWrapper(AggregationType.SIMPLE, aggregationsMap.toString()));
                    }
                    // Keep only one aggregation
                    break;
                }
            } else if (size > 0 && total > 0) {
                final JSONArray hits = getFieldAsArray(body, "hits/hits");
                final Iterator<Object> iter = hits.iterator();
                while (iter.hasNext()) {
                    final JSONObject hit = (JSONObject) iter.next();
                    final Object data = hit.opt("_source") != null ? hit.opt("_source") : hit.opt("fields");
                    response.addHit(new HitWrapper(hit.getString("_index"), hit.getString("_type"), hit.getString("_id"), data.toString()));
                }
            }
        } else {
            throw new ActionException(body.get("error").toString());
        }
    } catch (final UnirestException e) {
        throw new ActionException(e);
    }
    return response;
}
Also used : JSONArray(org.json.JSONArray) ActionException(org.apache.zeppelin.elasticsearch.action.ActionException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) AggWrapper(org.apache.zeppelin.elasticsearch.action.AggWrapper) JsonParseException(com.google.gson.JsonParseException) ActionResponse(org.apache.zeppelin.elasticsearch.action.ActionResponse) HitWrapper(org.apache.zeppelin.elasticsearch.action.HitWrapper) JSONObject(org.json.JSONObject) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) Iterator(java.util.Iterator) JSONObject(org.json.JSONObject)

Example 20 with JsonNode

use of com.mashape.unirest.http.JsonNode in project jabref by JabRef.

the class SpringerLink method findFullText.

@Override
public Optional<URL> findFullText(BibEntry entry) throws IOException {
    Objects.requireNonNull(entry);
    Optional<URL> pdfLink = Optional.empty();
    // Try unique DOI first
    Optional<DOI> doi = entry.getField(FieldName.DOI).flatMap(DOI::parse);
    if (doi.isPresent()) {
        // Available in catalog?
        try {
            HttpResponse<JsonNode> jsonResponse = Unirest.get(API_URL).queryString("api_key", API_KEY).queryString("q", String.format("doi:%s", doi.get().getDOI())).asJson();
            JSONObject json = jsonResponse.getBody().getObject();
            int results = json.getJSONArray("result").getJSONObject(0).getInt("total");
            if (results > 0) {
                LOGGER.info("Fulltext PDF found @ Springer.");
                pdfLink = Optional.of(new URL("http", CONTENT_HOST, String.format("/content/pdf/%s.pdf", doi.get().getDOI())));
            }
        } catch (UnirestException e) {
            LOGGER.warn("SpringerLink API request failed", e);
        }
    }
    return pdfLink;
}
Also used : JSONObject(org.json.JSONObject) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode) URL(java.net.URL) DOI(org.jabref.model.entry.identifier.DOI)

Aggregations

JsonNode (com.mashape.unirest.http.JsonNode)47 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)20 JSONObject (org.json.JSONObject)19 IOException (java.io.IOException)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 JSONArray (org.json.JSONArray)11 RestResponseResult (uk.ac.ebi.spot.goci.model.RestResponseResult)5 Incident (io.github.asw.i3a.operatorsWebClient.entitites.Incident)4 List (java.util.List)4 ActionException (org.apache.zeppelin.elasticsearch.action.ActionException)4 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)4 HitWrapper (org.apache.zeppelin.elasticsearch.action.HitWrapper)4 JSONException (org.json.JSONException)4 Gson (com.google.gson.Gson)3 Type (java.lang.reflect.Type)3 JsonObjectBuilder (javax.json.JsonObjectBuilder)3 Test (org.junit.Test)3 RestTemplate (org.springframework.web.client.RestTemplate)3 HttpResponse (com.mashape.unirest.http.HttpResponse)2 HttpRequest (com.mashape.unirest.request.HttpRequest)2