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

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

Introduction

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

Prototype

public RestTemplate() 

Source Link

Document

Create a new instance of the RestTemplate using default settings.

Usage

From source file:org.apache.knox.solr.ui.SearchController.java

/**
 * Do a solr search// w w  w . j a va 2 s .c  om
 * 
 * @param solrRequest
 *            the Solr request
 * @return the HTML response
 */
@RequestMapping("/search")
public @ResponseBody String search(@RequestBody SolrRequest solrRequest) {
    logger.debug("Received Solr Request: {}", solrRequest);
    final String solrUrl = buildSolrUrl(solrRequest);
    logger.debug("Executing with URL: {}", solrUrl);
    final RestTemplate restTemplate = new RestTemplate();

    final ResponseEntity<String> solrResult = restTemplate.getForEntity(solrUrl, String.class);
    logger.debug("Solr Result: {}", solrResult);
    if (HttpStatus.OK.equals(solrResult.getStatusCode())) {
        return solrResult.getBody();
    } else {
        logger.error("Error getting Solr Result - http status: {}", solrResult.getStatusCodeValue());
        return "";
    }
}

From source file:org.zalando.riptide.OAuth2CompatibilityTest.java

@Test
public void responseIsConsumedIfOtherHandlerIsUsed() throws IOException {
    final RestTemplate template = new RestTemplate();
    final MockRestServiceServer server = MockRestServiceServer.createServer(template);

    server.expect(requestTo(url)).andRespond(withUnauthorizedRequest().body(new byte[] { 0x13, 0x37 }));

    template.setErrorHandler(new OAuth2ErrorHandler(new PassThroughResponseErrorHandler(), null));
    final Rest rest = Rest.create(template);

    final ClientHttpResponse response = rest.execute(GET, url).dispatch(status(), on(UNAUTHORIZED).capture())
            .to(ClientHttpResponse.class);

    // Since our mocked response is using a byte[] stream we check for the remaining bytes instead
    // of expecting an "already closed" IOException.
    assertThat(response.getBody().available(), is(0));
}

From source file:net.orpiske.tcs.service.rest.functional.DomainCreateTest.java

@Test
public void testAuthenticationError() {
    HttpEntity<Domain> requestEntity = new HttpEntity<Domain>(RestDataFixtures.customCsp(),
            getHeaders(USERNAME + ":" + BAD_PASSWORD));

    RestTemplate template = new RestTemplate();

    try {// w w  w  . j  a  v a 2s  .  c  o m
        ResponseEntity<Domain> responseEntity = template
                .postForEntity("http://localhost:8080/tcs/domain/terra.com.br", requestEntity, Domain.class);

        fail("Request Passed incorrectly with status " + responseEntity.getStatusCode());
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
    }
}

From source file:bg.vitkinov.edu.services.JokeService.java

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

From source file:de.swm.nis.logicaldecoding.gwc.GWCInvalidator.java

public GWCInvalidator() {
    template = new RestTemplate();
}

From source file:com.aktios.appthree.server.service.OAuthAuthenticationService.java

public String loginSSO(String code, String redirectUri) throws JSONException {
    RestOperations rest = new RestTemplate();
    String resAuth = rest.getForObject(
            MessageFormat.format(
                    "{0}/oauth/token?code={1}&client_id={2}&client_secret={3}&grant_type={4}&redirect_uri={5}",
                    oauthServerBaseURL, code, appToken, appPassword, "authorization_code", redirectUri),
            String.class);
    System.out.println(resAuth);//w w  w .j a v  a 2 s  .c  om
    JSONObject resJsA = new JSONObject(resAuth);
    return resJsA.getString("access_token");
}

From source file:com.provenance.cloudprovenance.connector.traceability.TraceabilityStoreConnector.java

public String getCurrentTraceabilityRecordId(URL serviceId) {
    // URI encoded

    RestTemplate restTemplate = new RestTemplate();

    // restTemplate.get

    ResponseEntity<String> traceabilityResponseEntity = null;

    try {//from  w  w  w . j ava  2 s .c o m
        traceabilityResponseEntity = restTemplate.getForEntity(serviceId.toExternalForm(), String.class);

        if (traceabilityResponseEntity.getStatusCode().value() == 200) {
            return traceabilityResponseEntity.getBody();
        } else {
            return null;
        }

    } catch (org.springframework.web.client.HttpClientErrorException ex) {
        logger.warn(ex.toString());
        return null;
    }
}

From source file:de.zib.gndms.gndmc.test.gorfx.ESGFGet.java

@Override
protected void run() throws Exception {

    SetupSSL setupSSL = new SetupSSL();
    setupSSL.setKeyStoreLocation(keyStoreLocation);
    setupSSL.prepareKeyStore(passwd, passwd);
    setupSSL.setupDefaultSSLContext(passwd);

    final RestTemplate rt = new RestTemplate();

    rt.execute(url, HttpMethod.GET, null, new ResponseExtractor<Object>() {
        @Override//from  w  w w . j  ava2s  .  c  om
        public Object extractData(final ClientHttpResponse response) throws IOException {

            String url = null;
            String cookieTmp = null;
            System.out.println(response.getStatusCode().toString());
            for (String s : response.getHeaders().keySet())
                for (String v : response.getHeaders().get(s)) {
                    System.out.println(s + ":" + v);
                    if ("Location".equals(s))
                        url = v;
                    else if ("Set-Cookie".equals(s))
                        cookieTmp = v;
                }
            final String cookie = cookieTmp.split(";")[0];

            if (url != null)
                rt.execute(decodeUrl(url), HttpMethod.GET, new RequestCallback() {
                    @Override
                    public void doWithRequest(final ClientHttpRequest request) throws IOException {

                        System.out.println("setting cookie: " + cookie);
                        request.getHeaders().set("Cookie", cookie);
                    }
                }, new ResponseExtractor<Object>() {
                    @Override
                    public Object extractData(final ClientHttpResponse response) throws IOException {

                        System.out.println(response.getStatusCode().toString());
                        System.out.println("Received data, copying");
                        InputStream is = response.getBody();
                        OutputStream os = new FileOutputStream(off);
                        StreamCopyNIO.copyStream(is, os);
                        System.out.println("Done");
                        return null;
                    }
                });

            return null;
        }
    });
}

From source file:org.craftercms.commerce.client.AbstractRestCRUDService.java

public AbstractRestCRUDService(String crafterCommerceServerUrl) {
    this.crafterCommerceServerUrl = crafterCommerceServerUrl;
    restTemplate = new RestTemplate();
    httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
}