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.ge.predix.test.utils.PolicyHelper.java

public PolicySet[] listPolicySets(final RestTemplate restTemplate, final String acsUrl,
        final HttpHeaders headers) {
    URI uri = URI.create(acsUrl + ACS_POLICY_SET_API_PATH);
    ResponseEntity<PolicySet[]> response = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(headers),
            PolicySet[].class);
    return response.getBody();
}

From source file:org.obiba.mica.user.UserProfileService.java

private <T> T executeQuery(String serviceUrl, Class<T> returnType) {

    RestTemplate template = newRestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set(APPLICATION_AUTH_HEADER, getApplicationAuth());
    HttpEntity<String> entity = new HttpEntity<>(null, headers);
    ResponseEntity<T> response = template.exchange(serviceUrl, HttpMethod.GET, entity, returnType);

    return response.getBody();
}

From source file:org.echocat.marquardt.example.ServiceLoginIntegrationTest.java

private void whenAccessingProtectedResourceWithSelfSignedCertificate(final byte[] attackersCertificate) {
    final RestTemplate restTemplate = new RestTemplate();
    final HttpHeaders headers = new HttpHeaders();
    headers.add(X_CERTIFICATE.getHeaderName(), new String(attackersCertificate));
    final HttpEntity<Object> requestEntity = new HttpEntity<>(headers);

    restTemplate.exchange(baseUriOfApp() + "/exampleservice/someProtectedResource", HttpMethod.POST,
            requestEntity, Void.class);
}

From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.TestCredentials.java

@Override
protected UserInfo doInBackground(Void... params) {
    SharedPreferences credentials = getCredentials();
    String username = credentials.getString("tmp_username", null);
    String password = credentials.getString("tmp_password", null);
    String url = credentials.getString("tmp_url", null) + Values.SERVICE_USER + "login";

    setState(RUNNING, R.string.working_ws_credentials);
    HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAuthorization(authHeader);
    requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders);

    SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory();
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    try {//from  w ww.ja  v  a 2  s.  c  o  m
        // Make the network request
        Log.d(TAG, url);
        ResponseEntity<UserInfo> userInfo = restTemplate.exchange(url, HttpMethod.GET, entity, UserInfo.class);
        return (Values.user = userInfo.getBody());
    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
        setState(ERROR, e);
        return null;
    } finally {
        setState(DONE);
    }
}

From source file:org.cloudfoundry.identity.uaa.login.feature.LoginIT.java

@Test
public void testRedirectAfterFailedLogin() throws Exception {
    RestTemplate template = new RestTemplate();
    LinkedMultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    body.add("username", testAccounts.getUserName());
    body.add("password", "invalidpassword");
    ResponseEntity<Void> loginResponse = template.exchange(baseUrl + "/login.do", HttpMethod.POST,
            new HttpEntity<>(body, null), Void.class);
    assertEquals(HttpStatus.FOUND, loginResponse.getStatusCode());
}

From source file:com.cloudera.nav.plugin.client.NavApiCient.java

/**
 * Call the Navigator API and retrieve all available sources
 *
 * @return a collection of available sources
 *///  w ww. j a  va2  s. co m
public Collection<Source> getAllSources() {
    RestTemplate restTemplate = new RestTemplate();
    String url = getSourceUrl();
    HttpHeaders headers = getAuthHeaders();
    HttpEntity<String> request = new HttpEntity<String>(headers);
    ResponseEntity<SourceAttrs[]> response = restTemplate.exchange(url, HttpMethod.GET, request,
            SourceAttrs[].class);
    Collection<Source> sources = Lists.newArrayList();
    for (SourceAttrs info : response.getBody()) {
        sources.add(info.createSource());
    }
    return sources;
}

From source file:org.opendatakit.api.admin.UserAdminServiceTest.java

@Test
public void testChangePlaintextPassword() {
    TestUtils.putGetOneUser(restTemplate, server, testUser3);
    String username = testUser3.getUserId().substring(SecurityConsts.USERNAME_COLON.length());
    String password = "mypass";

    String changePasswordUrl = ConstantsUtils.url(server) + "/admin/users/" + testUser3.getUserId()
            + "/password";

    logger.info("Updating password for " + username + " using " + changePasswordUrl);

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
    headers.add("Content-Type", "application/json");
    HttpEntity<?> request = new HttpEntity<>(password, headers);
    ResponseEntity<String> postResponse = restTemplate.postForEntity(changePasswordUrl, request, String.class);
    assertThat(postResponse.getStatusCode(), is(HttpStatus.OK));

    logger.info("Retrieving data using " + username + "'s new password");

    RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
    RestTemplate userRestTemplate = restTemplateBuilder.basicAuthorization(username, password).build();
    String getUserUrl = ConstantsUtils.url(server) + "/admin/users/" + testUser3.getUserId();
    ResponseEntity<UserEntity> getResponse = userRestTemplate.exchange(getUserUrl, HttpMethod.GET, null,
            UserEntity.class);
    UserEntity body = getResponse.getBody();
    assertThat(getResponse.getStatusCode(), is(HttpStatus.OK));
    assertThat(body.getUserId(), equalTo(testUser3.getUserId()));

    logger.info("Cleanup: deleting " + username);
    TestUtils.deleteGetOneUser(restTemplate, server, testUser3);
}

From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchPeople.java

/**
 * Method, where all people are read from server.
 * All heavy lifting is made here./*  w  w w.j av a  2 s  . c  om*/
 *
 * @param params omitted here
 * @return list of fetched people
 */
@Override
protected List<Person> doInBackground(Void... params) {
    SharedPreferences credentials = getCredentials();
    String username = credentials.getString("username", null);
    String password = credentials.getString("password", null);
    String url = credentials.getString("url", null) + Values.SERVICE_USER + Values.SERVICE_QUALIFIER_ALL;

    setState(RUNNING, R.string.working_ws_people);
    HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAuthorization(authHeader);
    requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders);

    SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory();
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    try {
        // Make the network request
        Log.d(TAG, url);
        ResponseEntity<PersonList> response = restTemplate.exchange(url, HttpMethod.GET, entity,
                PersonList.class);
        PersonList body = response.getBody();

        if (body != null) {
            return body.getPeople();
        }

    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
        setState(ERROR, e);
    } finally {
        setState(DONE);
    }
    return Collections.emptyList();
}

From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchResearchGroups.java

/**
 * Method, where all research groups are read from server.
 * All heavy lifting is made here.// w w w. j  a v a  2s. co  m
 *
 * @param params omitted here
 * @return list of fetched research groups
 */
@Override
protected List<ResearchGroup> doInBackground(Void... params) {
    SharedPreferences credentials = getCredentials();
    String username = credentials.getString("username", null);
    String password = credentials.getString("password", null);
    String url = credentials.getString("url", null) + Values.SERVICE_RESEARCH_GROUPS + qualifier;

    setState(RUNNING, R.string.working_ws_groups);
    HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAuthorization(authHeader);
    requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders);

    SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory();
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    try {
        // Make the network request
        Log.d(TAG, url);
        ResponseEntity<ResearchGroupList> response = restTemplate.exchange(url, HttpMethod.GET, entity,
                ResearchGroupList.class);
        ResearchGroupList body = response.getBody();

        if (body != null) {
            return body.getGroups();
        }

    } catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage(), e);
        setState(ERROR, e);
    } finally {
        setState(DONE);
    }
    return Collections.emptyList();
}