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.cloudfoundry.identity.uaa.integration.util.IntegrationTestUtils.java

public static Map<String, Object> getPasswordToken(String baseUrl, String clientId, String clientSecret,
        String username, String password, String scopes) throws Exception {
    RestTemplate template = new RestTemplate();
    template.getMessageConverters().add(0,
            new StringHttpMessageConverter(java.nio.charset.Charset.forName("UTF-8")));
    template.setRequestFactory(new StatelessRequestFactory());
    MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
    formData.add("grant_type", "password");
    formData.add("client_id", clientId);
    formData.add("username", username);
    formData.add("password", password);
    formData.add("response_type", "token id_token");
    if (StringUtils.hasText(scopes)) {
        formData.add("scope", scopes);
    }//from   w w w  . j av  a2s.  c o  m
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.set("Authorization",
            "Basic " + new String(Base64.encode(String.format("%s:%s", clientId, clientSecret).getBytes())));

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> response = template.exchange(baseUrl + "/oauth/token", HttpMethod.POST,
            new HttpEntity(formData, headers), Map.class);

    Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
    return response.getBody();
}

From source file:org.jboss.windup.decorator.integration.mvn.MavenCentralSHA1VersionDecorator.java

@Override
public void processMeta(ZipMetadata meta) {
    if (!active) {
        return;//ww  w  .ja  v a  2  s. co  m
    }
    if (!knownArchiveProfiler.isExclusivelyKnownArchive(meta)) {
        return;
    }

    String sha1Hash = null;
    for (AbstractDecoration result : meta.getDecorations()) {
        if (result instanceof PomVersion) {
            LOG.debug("Already has version result: " + result.toString());
            return;
        } else if (result instanceof Hash) {
            if (((Hash) result).getHashType() == HashType.SHA1) {
                sha1Hash = ((Hash) result).getHash();
            }
        }
    }

    if (sha1Hash == null) {
        LOG.debug("No SHA-1 Hash found. Returning.");
        return;
    }
    LOG.info("No Version Found: " + meta.getRelativePath() + "; trying Maven Central");
    if (LOG.isDebugEnabled()) {
        LOG.debug("SHA1: " + sha1Hash);
    }
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    try {
        MavenCentralSHA1VersionResponseWrapper result = restTemplate.getForObject(MAVEN_API_URL,
                MavenCentralSHA1VersionResponseWrapper.class, sha1Hash);

        if (result != null && result.getResponse() != null && result.getResponse().getNumFound() > 0) {
            MavenCentralSHA1VersionResponseItem rsp = result.getResponse().getDocs()[0];
            String groupId = rsp.getGroupId();
            String artifactId = rsp.getArtifactId();
            String version = rsp.getVersion();

            String url = generateUrl(groupId, artifactId, version);

            // pull the POM from the URL.
            ClientHttpRequestFactory request = new SimpleClientHttpRequestFactory();
            try {
                ClientHttpRequest pomRequest = request.createRequest(new URI(url), HttpMethod.GET);
                ClientHttpResponse resp = pomRequest.execute();

                String outputDir = meta.getArchiveOutputDirectory().getAbsolutePath() + File.separator
                        + "maven-remote";
                FileUtils.forceMkdir(new File(outputDir));

                File outputPath = new File(outputDir + File.separator + "pom.xml");
                IOUtils.copy(new InputStreamReader(resp.getBody()), new FileOutputStream(outputPath));

                XmlMetadata xmlMeta = new XmlMetadata();
                xmlMeta.setFilePointer(outputPath);
                xmlMeta.setArchiveMeta(meta);

                pomInterrogator.processMeta(xmlMeta);
                LOG.info("Fetched remote POM for: " + meta.getName());
            } catch (Exception e) {
                LOG.error("Exception fetching remote POM: " + url + "; skipping.", e);
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No Version Information Found in Maven Central for: " + sha1Hash);
            }
        }
    } catch (Exception e) {
        LOG.error("Exception creating API call to Central Repo for POM: " + meta.getName() + "; skipping.", e);
    }
}

From source file:org.springframework.data.keyvalue.riak.client.RiakRestClient.java

@SuppressWarnings("unchecked")
@Override/*from   w  w  w .ja  va2s . c  o  m*/
public <T> List<RiakResponse<T>> walkLinks(String bucket, String key, Class<T> clazz, RiakLinkPhase... phases)
        throws RiakException {
    List<String> pathParams = new ArrayList<String>(Arrays.asList(bucket, key));

    for (RiakLinkPhase phase : phases)
        pathParams.add(phase.toUrlFormat());
    RestTemplate privTemp = new RestTemplate();
    privTemp.getMessageConverters().add(new MultipartMixedHttpMessageConverter<T>(clazz));
    return (List<RiakResponse<T>>) privTemp.getForObject(
            getUrl("riak", null, (String[]) pathParams.toArray(new String[pathParams.size()])), List.class);
}

From source file:org.springframework.data.keyvalue.riak.util.RiakClassLoader.java

protected void init(RiakTemplate riakTemplate) {
    this.riakTemplate = riakTemplate;
    RestTemplate tmpl = this.riakTemplate.getRestTemplate();
    tmpl.getMessageConverters().add(0, new JavaSerializationMessageHandler());
    tmpl.setErrorHandler(new Ignore404sErrorHandler());
}