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

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

Introduction

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

Prototype

@Nullable
<T> T postForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException;

Source Link

Document

Create a new resource by POSTing the given object to the URL, and returns the representation found in the response.

Usage

From source file:example.helloworld.CubbyholeAuthenticationTests.java

/**
 * Write some data to Vault before Vault can be used as {@link VaultPropertySource}.
 *//*  w  ww.ja  v a  2s  .c o  m*/
@BeforeClass
public static void beforeClass() {

    VaultOperations vaultOperations = new VaultTestConfiguration().vaultTemplate();
    vaultOperations.write("secret/myapp/configuration", Collections.singletonMap("configuration.key", "value"));

    VaultResponse response = vaultOperations.doWithSession(new RestOperationsCallback<VaultResponse>() {

        @Override
        public VaultResponse doWithRestOperations(RestOperations restOperations) {

            HttpHeaders headers = new HttpHeaders();
            headers.add("X-Vault-Wrap-TTL", "10m");

            return restOperations.postForObject("auth/token/create", new HttpEntity<Object>(headers),
                    VaultResponse.class);
        }
    });

    // Response Wrapping requires Vault 0.6.0+
    Map<String, String> wrapInfo = response.getWrapInfo();
    initialToken = VaultToken.of(wrapInfo.get("token"));
}

From source file:it.infn.mw.iam.authn.oidc.DefaultOidcTokenRequestor.java

@Override
public String requestTokens(OidcProviderConfiguration conf, MultiValueMap<String, String> tokenRequestParams) {

    RestOperations restTemplate = restTemplateFactory.newRestTemplate();

    try {//from w  w  w .ja va 2  s  . c o  m

        return restTemplate.postForObject(conf.serverConfig.getTokenEndpointUri(),
                prepareTokenRequest(conf, tokenRequestParams), String.class);

    } catch (HttpClientErrorException e) {

        if (e.getStatusCode() != null && e.getStatusCode().equals(BAD_REQUEST)) {
            parseErrorResponse(e).ifPresent(er -> {

                String errorMessage = String.format("Token request error: %s '%s'", er.getError(),
                        er.getErrorDescription());
                LOG.error(errorMessage);

                throw new OidcClientError(e.getMessage(), er.getError(), er.getErrorDescription(),
                        er.getErrorUri());

            });
        }

        String errorMessage = String.format("Token request error: %s", e.getMessage());
        LOG.error(errorMessage, e);
        throw new OidcClientError(errorMessage, e);
    } catch (Exception e) {
        String errorMessage = String.format("Token request error: %s", e.getMessage());
        LOG.error(errorMessage, e);
        throw new OidcClientError(errorMessage, e);
    }

}

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

public String loginApp() throws JSONException {
    RestOperations rest = new RestTemplate();
    String resAuth = rest.postForObject(oauthServerBaseURL + "/oauth/token?client_id=" + appToken
            + "&client_secret=" + appPassword + "&grant_type=client_credentials", null, String.class);
    System.out.println(resAuth);/*from w  w  w. ja  v a  2 s . c o  m*/

    JSONObject resJsA = new JSONObject(resAuth);
    return resJsA.getString("access_token");
}

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

public String login(String username, String password) throws JSONException {
    RestOperations rest = new RestTemplate();
    String resAuth = rest.postForObject(oauthServerBaseURL + "/oauth/token?username=" + username + "&password="
            + password + "&client_id=" + appToken + "&client_secret=" + appPassword + "&grant_type=password",
            null, String.class);
    System.out.println(resAuth);//from   w  w w. j  av a2s .c om

    JSONObject resJsA = new JSONObject(resAuth);
    return resJsA.getString("access_token");
}

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

public String loginSSO(String code, String redirectUri) throws JSONException {
    RestOperations rest = new RestTemplate();
    String resAuth = rest.postForObject(
            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),
            null, String.class);
    System.out.println(resAuth);/*  w  w  w .j a  v  a  2 s.  c o  m*/
    JSONObject resJsA = new JSONObject(resAuth);
    return resJsA.getString("access_token");
}