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

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

Introduction

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

Prototype

@Override
    public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, @Nullable HttpEntity<?> requestEntity,
            ParameterizedTypeReference<T> responseType) throws RestClientException 

Source Link

Usage

From source file:com.companyname.plat.commons.client.HttpRestfulClient.java

public Object getObject(Map<String, String> params, Class clz) {
    RestTemplate restTemplate = new RestTemplate();

    try {/*from ww w  .jav a 2  s. co m*/
        ResponseEntity<Object> entity = restTemplate.exchange(getEndPoint(), HttpMethod.GET,
                getHttpRequest(params), clz);

        setResponseStatus(entity.getStatusCode().toString());
        return entity.getBody();
    } catch (HttpClientErrorException ex) {
        if (HttpStatus.UNAUTHORIZED == ex.getStatusCode()) {
            System.out
                    .println("Unauthorized call to " + this.getEndPoint() + "\nWrong login and/or password (\""
                            + this.getUserName() + "\" / \"" + this.getPassword() + "\")");

            System.out.println("Cause: \n" + ex.getMessage());
        }
    }

    return null;
}

From source file:org.intermine.app.net.request.GetRequest.java

protected byte[] loadBytes(String uriString, Map<String, ?> params) {
    RestTemplate rtp = getRestTemplate();
    HttpHeaders headers = getHeaders();/*from  w  w  w. j a v  a 2 s . com*/

    HttpEntity<String> req = new HttpEntity<>(headers);
    ResponseEntity<byte[]> res;

    URI uri = URI.create(expandQuery(uriString, params));
    res = rtp.exchange(uri, GET, req, byte[].class);
    return res.getBody();
}

From source file:com.google.cloud.servicebroker.awwvision.RedditScraper.java

@RequestMapping("/reddit")
String getRedditUrls(Model model, RestTemplate restTemplate) throws GeneralSecurityException {
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.USER_AGENT, redditUserAgent);
    RedditResponse response = restTemplate
            .exchange(REDDIT_URL, HttpMethod.GET, new HttpEntity<String>(headers), RedditResponse.class)
            .getBody();//from   w  w w  .jav a2s  .  c o  m

    storeAndLabel(response);

    return "reddit";
}

From source file:org.cloudfoundry.identity.statsd.integration.UaaMetricsEmitterIT.java

@Test
public void testStatsDClientEmitsMetricsCollectedFromUAA() throws InterruptedException, IOException {
    RestTemplate template = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.set(headers.ACCEPT, MediaType.TEXT_HTML_VALUE);
    ResponseEntity<String> loginResponse = template.exchange(UAA_BASE_URL + "/login", HttpMethod.GET,
            new HttpEntity<>(null, headers), String.class);

    if (loginResponse.getHeaders().containsKey("Set-Cookie")) {
        for (String cookie : loginResponse.getHeaders().get("Set-Cookie")) {
            headers.add("Cookie", cookie);
        }/* www  . j a v a  2 s  .  co m*/
    }
    String csrf = IntegrationTestUtils.extractCookieCsrf(loginResponse.getBody());

    LinkedMultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    body.add("username", TEST_USERNAME);
    body.add("password", TEST_PASSWORD);
    body.add(CookieBasedCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME, csrf);
    loginResponse = template.exchange(UAA_BASE_URL + "/login.do", HttpMethod.POST,
            new HttpEntity<>(body, headers), String.class);
    assertEquals(HttpStatus.FOUND, loginResponse.getStatusCode());
    assertNotNull(getMessage("uaa.audit_service.user_authentication_count:1", 5000));
}

From source file:com.wisemapping.test.rest.RestAccountITCase.java

private ResponseEntity<RestUser> findUser(HttpHeaders requestHeaders, RestTemplate templateRest, URI location) {
    HttpEntity<RestUser> findUserEntity = new HttpEntity<RestUser>(requestHeaders);
    final String url = HOST_PORT + location;
    return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class);
}

From source file:com.ge.predix.uaa.token.lib.FastTokenServiceTest.java

/**
 * Tests that connection error while retrieving token key issues RestClientException.
 */// w  w w.j  a  va  2  s  .co  m
@SuppressWarnings("unchecked")
@Test(expectedExceptions = RestClientException.class)
public void testLoadAuthenticationWithConnectionTimeout() throws Exception {
    String accessToken = this.testTokenUtil.mockAccessToken(60);

    FastTokenServices services = new FastTokenServices();
    ParameterizedTypeReference<Map<String, Object>> typeRef = new ParameterizedTypeReference<Map<String, Object>>() {
        // Nothing to add.
    };
    RestTemplate restTemplate = Mockito.mock(RestTemplate.class);
    Mockito.when(restTemplate.exchange(TOKEN_KEY_URL, HttpMethod.GET, null, typeRef))
            .thenThrow(RestClientException.class);
    services.setRestTemplate(restTemplate);

    services.loadAuthentication(accessToken);
}

From source file:io.fabric8.che.starter.client.CheRestClient.java

public List<Stack> listStacks(String cheServerURL) {
    String url = generateURL(cheServerURL, CheRestEndpoints.LIST_STACKS);
    RestTemplate template = new RestTemplate();
    ResponseEntity<List<Stack>> response = template.exchange(url, HttpMethod.GET, null,
            new ParameterizedTypeReference<List<Stack>>() {
            });//from  ww  w .  j ava 2s . c om
    return response.getBody();
}

From source file:com.ge.predix.uaa.token.lib.FastTokenServiceTest.java

public FastTokenServiceTest() throws Exception {

    this.body.put(Claims.CLIENT_ID, "remote");
    this.body.put(Claims.USER_NAME, "olds");
    this.body.put(Claims.EMAIL, "olds@vmware.com");
    this.body.put(Claims.ISS, TOKEN_ISSUER_ID);
    this.body.put(Claims.USER_ID, "HDGFJSHGDF");

    ParameterizedTypeReference<Map<String, Object>> typeRef = new ParameterizedTypeReference<Map<String, Object>>() {
        // Nothing to add.
    };//from w w w .ja  v a 2 s  .co  m

    RestTemplate restTemplate = Mockito.mock(RestTemplate.class);
    Mockito.when(restTemplate.exchange(TOKEN_KEY_URL, HttpMethod.GET, null, typeRef))
            .thenReturn(TestTokenUtil.mockTokenKeyResponseEntity());
    this.services.setRestTemplate(restTemplate);

    List<String> trustedIssuers = new ArrayList<>();
    trustedIssuers.add(TOKEN_ISSUER_ID);
    this.services.setTrustedIssuers(trustedIssuers);
}

From source file:org.slc.sli.dashboard.client.RESTClient.java

public HttpEntity<String> exchange(RestTemplate templateIn, String url, HttpMethod method, HttpEntity entity,
        Class cl) {//from w  ww  . j av a  2 s.  c  om
    return templateIn.exchange(url, method, entity, cl);
}

From source file:io.fabric8.che.starter.client.CheRestClient.java

public List<Workspace> listWorkspaces(String cheServerURL) {
    String url = generateURL(cheServerURL, CheRestEndpoints.LIST_WORKSPACES);
    RestTemplate template = new RestTemplate();
    ResponseEntity<List<Workspace>> response = template.exchange(url, HttpMethod.GET, null,
            new ParameterizedTypeReference<List<Workspace>>() {
            });//from   w w w.  jav  a 2 s. c  om

    List<Workspace> workspaces = response.getBody();
    for (Workspace workspace : workspaces) {
        workspace.setName(workspace.getConfig().getName());
        workspace.setDescription(workspace.getConfig().getDescription());

        for (WorkspaceLink link : workspace.getLinks()) {
            if (WORKSPACE_LINK_IDE_URL.equals(link.getRel())) {
                workspace.setWorkspaceIdeUrl(link.getHref());
                break;
            }
        }
    }
    return workspaces;
}