Example usage for org.springframework.web.client RestTemplate getMessageConverters

List of usage examples for org.springframework.web.client RestTemplate getMessageConverters

Introduction

In this page you can find the example usage for org.springframework.web.client RestTemplate getMessageConverters.

Prototype

public List<HttpMessageConverter<?>> getMessageConverters() 

Source Link

Document

Return the list of message body converters.

Usage

From source file:org.starfishrespect.myconsumption.android.tasks.StatValuesUpdater.java

public void refreshDB() {

    AsyncTask<Void, List, Void> task = new AsyncTask<Void, List, Void>() {
        @Override//from   w w w .j ava2s.c  om
        protected Void doInBackground(Void... params) {
            DatabaseHelper db = SingleInstance.getDatabaseHelper();
            RestTemplate template = new RestTemplate();
            HttpHeaders httpHeaders = CryptoUtils.createHeadersCurrentUser();
            ResponseEntity<StatDTO[]> responseEnt;
            template.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

            try {
                for (SensorData sensor : db.getSensorDao().queryForAll()) {
                    // Stats
                    String url = String.format(SingleInstance.getServerUrl() + "stats/sensor/%s",
                            sensor.getSensorId());

                    responseEnt = template.exchange(url, HttpMethod.GET, new HttpEntity<>(httpHeaders),
                            StatDTO[].class);
                    StatDTO[] statsArray = responseEnt.getBody();
                    List<StatDTO> stats = new ArrayList<>(Arrays.asList(statsArray));

                    ObjectMapper mapper = new ObjectMapper();

                    try {
                        String json = mapper.writeValueAsString(stats);
                        String key = "stats_" + sensor.getSensorId();

                        int id = db.getIdForKey(key);
                        KeyValueData valueData = new KeyValueData(key, json);
                        valueData.setId(id);

                        LOGD(TAG, "writing stat in local db: " + json);
                        db.getKeyValueDao().createOrUpdate(valueData);

                    } catch (IOException e) {
                        LOGD(TAG, "Cannot create stats " + stats.toString(), e);
                    }

                }
            } catch (SQLException e) {
                LOGD(TAG, "Cannot create stats ", e);
            } catch (ResourceAccessException | HttpClientErrorException e) {
                LOGE(TAG, "Cannot access server ", e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (statUpdateFinishedCallback != null) {
                statUpdateFinishedCallback.onStatUpdateFinished();
            }
        }
    };

    task.execute();
}

From source file:au.org.ala.fielddata.mobile.service.FieldDataServiceClient.java

public List<Survey> downloadSurveys(FieldDataService.SurveyDownloadCallback callback) {

    String url = getServerUrl() + surveyUrl + ident;

    RestTemplate restTemplate = getRestTemplate();
    restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
    Log.d("URL", url);
    UserSurveyResponse[] result = restTemplate.getForObject(url, UserSurveyResponse[].class);

    url = getServerUrl() + surveyDetails;
    List<Survey> surveys = new ArrayList<Survey>();
    List<Integer> speciesIdList = new ArrayList<Integer>();
    //List<Species> species = new ArrayList<Species>();
    int count = 0;
    int total = result.length;
    for (UserSurveyResponse userSurvey : result) {
        String downloadedSurveys = jsonSurveyIds(surveys);

        if (callback != null) {
            callback.surveysDownloaded(++count, total);
        }//from  w w w .j av  a2  s .co m

        Log.d("URL", String.format(url, userSurvey.id, ident, downloadedSurveys));
        DownloadSurveyResponse surveyResponse = restTemplate.getForObject(
                String.format(url, userSurvey.id, ident, downloadedSurveys), DownloadSurveyResponse.class);

        Survey survey = mapSurvey(surveyResponse);
        surveys.add(survey);

        List<Integer> speciesIds = surveyResponse.details.speciesIds;
        if (speciesIds != null) {
            speciesIdList.addAll(surveyResponse.details.speciesIds);
        }
        //species.addAll(downloadSpecies(survey));
    }
    System.out.println(speciesIdList);

    return surveys;
}

From source file:com.bbva.arq.devops.ae.mirrorgate.collectors.jira.config.Config.java

@Bean(JIRA_REST_TEMPLATE)
public RestTemplate getJiraRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
    restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
    restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(jiraUserName, jiraPassword));
    return restTemplate;
}

From source file:fr.treeptik.cloudunit.cli.rest.RestUtils.java

/**
 * sendPostCommand/*from  w w  w .j a va 2 s .  c om*/
 *
 * @param url
 * @param parameters
 * @return
 * @throws ClientProtocolException
 */
public Map<String, Object> sendPostForUpload(String url, Map<String, Object> parameters) {
    RestTemplate restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> mc = restTemplate.getMessageConverters();
    mc.add(new MappingJacksonHttpMessageConverter());
    restTemplate.setMessageConverters(mc);
    MultiValueMap<String, Object> postParams = new LinkedMultiValueMap<String, Object>();
    postParams.setAll(parameters);
    Map<String, Object> response = new HashMap<String, Object>();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", "multipart/form-data");
    headers.set("Accept", "application/json");
    headers.add("Cookie", "JSESSIONID=" + localContext.getCookieStore().getCookies().get(0).getValue());
    org.springframework.http.HttpEntity<Object> request = new org.springframework.http.HttpEntity<Object>(
            postParams, headers);
    ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
    String body = result.getBody().toString();
    MediaType contentType = result.getHeaders().getContentType();
    HttpStatus statusCode = result.getStatusCode();
    response.put("content-type", contentType);
    response.put("statusCode", statusCode);
    response.put("body", body);

    return response;

}

From source file:com.bbva.arq.devops.ae.mirrorgate.collectors.jira.config.Config.java

@Bean(MIRRORGATE_REST_TEMPLATE)
public RestTemplate getRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
    restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
    if (!StringUtils.isBlank(mirrorGateUserName) && !StringUtils.isBlank(mirrorGatePassword)) {
        restTemplate.getInterceptors()/*  w  w w  .ja  v a2s . co m*/
                .add(new BasicAuthenticationInterceptor(mirrorGateUserName, mirrorGatePassword));
    }

    return restTemplate;
}

From source file:au.org.ala.fielddata.mobile.service.WebServiceClient.java

/**
 * The FieldData web services tend to accept regular HTTP parameters, some of which are JSON encoded.
 * /*w w w  . j  a v  a2  s .c om*/
 */
protected RestTemplate getRestTemplate() {

    RestTemplate restTemplate = new RestTemplate();
    FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
    GsonHttpMessageConverter gsonConverter = new GsonHttpMessageConverter(getGson());
    formConverter.addPartConverter(gsonConverter);
    restTemplate.getMessageConverters().add(formConverter);
    restTemplate.getMessageConverters().add(gsonConverter);

    return restTemplate;
}

From source file:de.tudarmstadt.ukp.clarin.webanno.crowdflower.CrowdClient.java

/**
 * Get the status of a job id. The returned JSON will look like:
 * {"all_judgments": 50, "golden_judgments": 5, "tainted_judgments": 15, "needed_judgments": 20}
 * @param jobId/*from  w  w w. j  a  v a 2s  .  c o  m*/
 * @return JsonNode containing the status as supplied by Crowdflower
 */
JsonNode getStatus(String jobId) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    JsonNode result = restTemplate.getForObject(pingURL, JsonNode.class, jobId, apiKey);

    return result;
}

From source file:de.tudarmstadt.ukp.clarin.webanno.crowdflower.CrowdClient.java

/**
 * Create a new job on Crowdflower using supplied job class. If this class had been retrieved with retrieveJob(String jobid),
 * then you copied the job without its data. Can be used to copy jobs across different accounts if API key is changed between the call to retrieveJob
 * @param job - job class that should be replicated as new job on Crowdflower
 * @return job class which represents the new job
 * @throws HttpServerErrorException/*from   ww w . j  a  v  a2s. c om*/
 */
CrowdJob createNewJob(CrowdJob job) throws HttpServerErrorException {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());

    JsonNode result = restTemplate.postForObject(newJobURL, job.getArgumentMap(), JsonNode.class, apiKey);

    return new CrowdJob(result);
}

From source file:de.tudarmstadt.ukp.clarin.webanno.crowdflower.CrowdClient.java

/**
 * Get the upload status of a job id. The resulting JSON will look like this if the query succeds:
 * {"count":550,"done":true}//from  w  w w .  j  a va2  s.c o  m
 * or if there is an error processing the request (e.g. wrong job id):
 * {"error": {"message":"We couldn't find what you were looking for."}}
 *
 * @param jobid
 * @result JsonNode containing the status as supplied by Crowdflower
 */

JsonNode getUploadStatus(String jobId) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    JsonNode result = restTemplate.getForObject(pingUnitsURL, JsonNode.class, jobId, apiKey);

    return result;
}