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

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

Introduction

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

Prototype

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

Source Link

Document

Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity .

Usage

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

public String getAuthorizationCode(String accessTokenA, String redirectUri, String appTokenClientTwo) {
    RestOperations rest = new RestTemplate();

    HttpHeaders headersA = new HttpHeaders();
    headersA.set("Authorization", "Bearer " + accessTokenA);

    ResponseEntity<String> resAuth2 = rest.exchange(
            MessageFormat.format(
                    "{0}/oauth/authorize?"
                            + "client_id={1}&response_type=code&redirect_uri={2}&scope=read,write",
                    oauthServerBaseURL, appTokenClientTwo, redirectUri),
            HttpMethod.GET, new HttpEntity<String>(headersA), String.class);

    String sessionId = resAuth2.getHeaders().get("Set-Cookie").get(0);
    int p1 = sessionId.indexOf("=") + 1;
    int p2 = sessionId.indexOf(";");
    sessionId = sessionId.substring(p1, p2);
    headersA.add("Cookie", "JSESSIONID=" + sessionId);

    resAuth2 = rest.exchange(/*from  w w  w. j  ava  2 s  .  c  o m*/
            MessageFormat.format("{0}/oauth/authorize?" + "user_oauth_approval=true&authorize=Authorize",
                    oauthServerBaseURL),
            HttpMethod.POST, new HttpEntity<String>(headersA), String.class);

    String code = resAuth2.getHeaders().get("location").get(0);
    p1 = code.lastIndexOf("=") + 1;
    code = code.substring(p1);

    return code;
}

From source file:de.zib.gndms.gndmc.gorfx.GORFXClient.java

@SuppressWarnings("unchecked")
public final ResponseEntity<Specifier<Facets>> createTaskFlow(final String type, final Order order,
        final String dn, final String wid, MultiValueMap<String, String> context) {

    GNDMSResponseHeader requestHeaders = new GNDMSResponseHeader();
    if (dn != null) {
        requestHeaders.setDN(dn);/*from   w w w  .j  a va 2  s.c  om*/
    }
    if (wid != null) {
        requestHeaders.setWId(wid);
    }

    requestHeaders.putAll(context);
    HttpEntity<Order> requestEntity = new HttpEntity<Order>(order, requestHeaders);

    RestOperations restTemplate = getRestTemplate();
    if (null == restTemplate) {
        throw new IllegalStateException("No RestTemplate set in GORFXClient.");
    }

    return (ResponseEntity<Specifier<Facets>>) (Object) restTemplate
            .exchange(getServiceURL() + "/gorfx/_" + type, HttpMethod.POST, requestEntity, Specifier.class);
}

From source file:org.client.two.service.OAuthAuthenticationService.java

public String getAuthorizationCode(String accessTokenA, String redirectUri, String appTokenClientTwo) {
    RestOperations rest = new RestTemplate();

    HttpHeaders headersA = new HttpHeaders();
    headersA.set("Authorization", "Bearer " + accessTokenA);

    ResponseEntity<String> resAuth2 = rest
            .exchange(/* w  w  w  .  java  2s .  c  om*/
                    MessageFormat.format(
                            "{0}/oauth/authorize?"
                                    + "client_id={1}&response_type=code&redirect_uri={2}&scope=WRITE",
                            oauthServerBaseURL, appTokenClientTwo, redirectUri),
                    HttpMethod.POST, new HttpEntity<String>(headersA), String.class);

    String sessionId = resAuth2.getHeaders().get("Set-Cookie").get(0);
    int p1 = sessionId.indexOf("=") + 1;
    int p2 = sessionId.indexOf(";");
    sessionId = sessionId.substring(p1, p2);
    headersA.add("Cookie", "JSESSIONID=" + sessionId);

    resAuth2 = rest.exchange(
            MessageFormat.format("{0}/oauth/authorize?" + "user_oauth_approval=true&authorize=Authorize",
                    oauthServerBaseURL),
            HttpMethod.POST, new HttpEntity<String>(headersA), String.class);

    String code = resAuth2.getHeaders().get("location").get(0);
    p1 = code.lastIndexOf("=") + 1;
    code = code.substring(p1);

    return code;
}

From source file:org.zalando.github.spring.pagination.PaginationTest.java

@SuppressWarnings("unchecked")
@Test/* w  w w.  ja  v  a 2 s.co  m*/
public void pagination() {

    RestOperations restOperations = Mockito.mock(RestOperations.class);

    ResponseEntity<List<String>> firstResponse = first();
    ResponseEntity<List<String>> secondResponse = second();
    ResponseEntity<List<String>> thirdResponse = third();

    Mockito.when(restOperations.exchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class),
            Mockito.any(HttpEntity.class), Mockito.any(issueListTypeRef.getClass())))
            .thenReturn(firstResponse, secondResponse, thirdResponse);

    List<String> result = new ArrayList<>();
    PagingIterator<List<String>> iter = new PagingIterator(restOperations, uri, issueListTypeRef);
    while (iter.hasNext()) {
        List<String> next = iter.next();
        result.addAll(next);
    }
    Assertions.assertThat(result.isEmpty()).isFalse();
    Assertions.assertThat(result.size()).isEqualTo(3);
    Assertions.assertThat(result).contains("firstElement", "secondElement", "thirdElement");
}

From source file:org.client.one.service.OAuthAuthenticationService.java

public Profile getCurrentUserProfile(String token) throws JSONException {
    RestOperations rest = new RestTemplate();
    HttpHeaders headersA = new HttpHeaders();
    headersA.set("Authorization", "Bearer " + token);

    ResponseEntity<String> responseA = rest.exchange(oauthServerBaseURL + "/resources/profile/read",
            HttpMethod.GET, new HttpEntity<String>(headersA), String.class);

    JSONObject profile = new JSONObject(responseA.getBody());

    profile = profile.getJSONObject("profile");

    Profile u = new Profile();
    u.setFirstName(profile.getString("firstName"));
    u.setLastName(profile.getString("lastName"));
    u.setPhoneNumber(profile.getString("phoneNumber"));
    u.setUsername(profile.getString("username"));
    return u;/*from  w w w .j  ava  2s. c o  m*/
}

From source file:org.client.one.service.OAuthAuthenticationService.java

public String getAuthorizationCode(String accessTokenA, String redirectUri, String appTokenClientTwo) {
    RestOperations rest = new RestTemplate();

    HttpHeaders headersA = new HttpHeaders();
    headersA.set("Authorization", "Bearer " + accessTokenA);

    ResponseEntity<String> resAuth2 = rest
            .exchange(//  w  ww  .jav a2 s .  com
                    MessageFormat.format(
                            "{0}/oauth/authorize?"
                                    + "client_id={1}&response_type=code&scope=WRITE&redirect_uri={2}",
                            oauthServerBaseURL, appTokenClientTwo, redirectUri),
                    HttpMethod.POST, new HttpEntity<String>(headersA), String.class);

    String sessionId = resAuth2.getHeaders().get("Set-Cookie").get(0);
    int p1 = sessionId.indexOf("=") + 1;
    int p2 = sessionId.indexOf(";");
    sessionId = sessionId.substring(p1, p2);
    headersA.add("Cookie", "JSESSIONID=" + sessionId);

    resAuth2 = rest.exchange(
            MessageFormat.format("{0}/oauth/authorize?" + "user_oauth_approval=true&authorize=Authorize",
                    oauthServerBaseURL),
            HttpMethod.POST, new HttpEntity<String>(headersA), String.class);

    String code = resAuth2.getHeaders().get("location").get(0);
    p1 = code.lastIndexOf("=") + 1;
    code = code.substring(p1);

    return code;
}

From source file:com.appglu.android.AppGlu.java

private VersionValidationResult doValidateApplicationVersion() {
    RestOperations restOperations = this.getAppGluTemplate().restOperations();

    try {// w ww  .  j a va  2 s .c om
        restOperations.exchange("/v1", HttpMethod.GET, null, Void.class);
    } catch (AppGluHttpIncompatibleClientVersionException e) {
        if (!e.hasError() || !e.getError().hasDetail()) {
            throw new AppGluHttpInternalServerErrorException();
        }
        return new VersionValidationResult(e.getError().getDetail());
    }

    return new VersionValidationResult();
}

From source file:org.client.one.service.OAuthAuthenticationService.java

public boolean updateCurrentUserProfile(String token, Profile profile)
        throws JsonGenerationException, JsonMappingException, IOException, JSONException {
    RestOperations rest = new RestTemplate();

    HttpHeaders headersA = new HttpHeaders();
    headersA.set("Authorization", "Bearer " + token);
    headersA.setContentType(MediaType.TEXT_PLAIN);

    ObjectMapper mapper = new ObjectMapper();
    HttpEntity<String> request = new HttpEntity<String>(mapper.writeValueAsString(profile), headersA);

    ResponseEntity<String> responseUpdate = rest.exchange(oauthServerBaseURL + "/resources/profile/update",
            HttpMethod.POST, request, String.class);

    JSONObject responseUpdateJSON = new JSONObject(responseUpdate.getBody());

    return responseUpdateJSON.getBoolean("success");
}

From source file:org.client.one.service.OAuthAuthenticationService.java

public boolean registerUserIntranet(String token, Profile profile)
        throws JsonGenerationException, JsonMappingException, IOException, JSONException {
    RestOperations rest = new RestTemplate();

    HttpHeaders headersA = new HttpHeaders();
    headersA.set("Authorization", "Bearer " + token);
    headersA.setContentType(MediaType.TEXT_PLAIN);

    ObjectMapper mapper = new ObjectMapper();
    HttpEntity<String> request = new HttpEntity<String>(mapper.writeValueAsString(profile), headersA);

    ResponseEntity<String> responseUpdate = rest.exchange(
            oauthServerBaseURL + "/resources/profile/registerUser", HttpMethod.POST, request, String.class);

    JSONObject responseUpdateJSON = new JSONObject(responseUpdate.getBody());

    return responseUpdateJSON.getBoolean("success");
}

From source file:org.client.one.service.OAuthAuthenticationService.java

public boolean registerUserWS(String token, Profile profile)
        throws JsonGenerationException, JsonMappingException, IOException, JSONException {
    RestOperations rest = new RestTemplate();

    HttpHeaders headersA = new HttpHeaders();
    headersA.set("Authorization", "Bearer " + token);
    headersA.setContentType(MediaType.TEXT_PLAIN);

    ObjectMapper mapper = new ObjectMapper();
    HttpEntity<String> request = new HttpEntity<String>(
            mapper.writeValueAsString(new RequestProfile(token, profile)), headersA);

    ResponseEntity<String> responseUpdate = rest.exchange(
            appThreeServicesBaseURL + "/resources/profile/registerUser", HttpMethod.POST, request,
            String.class);

    JSONObject responseUpdateJSON = new JSONObject(responseUpdate.getBody());

    return responseUpdateJSON.getBoolean("success");
}