Search in sources :

Example 1 with EuropePMCDeserializer

use of uk.ac.ebi.spot.goci.utils.EuropePMCDeserializer 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)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 JsonNode (com.mashape.unirest.http.JsonNode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 RestTemplate (org.springframework.web.client.RestTemplate)1 RestResponseResult (uk.ac.ebi.spot.goci.model.RestResponseResult)1 PubmedLookupException (uk.ac.ebi.spot.goci.service.exception.PubmedLookupException)1 EuropePMCData (uk.ac.ebi.spot.goci.utils.EuropePMCData)1 EuropePMCDeserializer (uk.ac.ebi.spot.goci.utils.EuropePMCDeserializer)1