Example usage for org.springframework.security.oauth2.client OAuth2RestTemplate exchange

List of usage examples for org.springframework.security.oauth2.client OAuth2RestTemplate exchange

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.client OAuth2RestTemplate exchange.

Prototype

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

Source Link

Usage

From source file:com.ge.predix.acceptance.test.zone.admin.ZoneEnforcementStepsDefinitions.java

@When("^client_one does a GET on (.*?) with (.*?) in zone 1$")
public void client_one_does_a_GET_on_api_with_identifier_in_test_zone_dev(final String api,
        final String identifier) throws Throwable {
    OAuth2RestTemplate acsTemplate = this.acsZone1Template;
    String encodedIdentifier = URLEncoder.encode(identifier, "UTF-8");
    URI uri = URI.create(this.acsUrl + ACS_VERSION + "/" + api + "/" + encodedIdentifier);

    try {/* w w w .  ja v a 2  s.  c o  m*/
        switch (api) {
        case "subject":
            this.responseEntity = acsTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(this.zone1Headers),
                    BaseSubject.class);
            this.status = this.responseEntity.getStatusCode().value();
            break;
        case "resource":
            this.responseEntityForResource = acsTemplate.exchange(uri, HttpMethod.GET,
                    new HttpEntity<>(this.zone1Headers), BaseResource.class);
            this.status = this.responseEntityForResource.getStatusCode().value();
            break;
        case "policy-set":
            this.policyset = acsTemplate.exchange(
                    this.acsUrl + PolicyHelper.ACS_POLICY_SET_API_PATH + this.testPolicyName, HttpMethod.GET,
                    new HttpEntity<>(this.zone1Headers), PolicySet.class);
            this.status = this.policyset.getStatusCode().value();
            break;
        default:
            Assert.fail("Api " + api + " does not match/is not yet implemented for this test code.");
        }
    } catch (HttpClientErrorException e) {
        e.printStackTrace();
        Assert.fail("Unable to GET identifier: " + identifier + " for api: " + api);
    }
}

From source file:com.ge.predix.acceptance.test.zone.admin.ZoneEnforcementStepsDefinitions.java

@When("^client_two does a DELETE on (.*?) with (.*?) in zone (.*?)$")
public void client_two_does_a_DELETE_on_api_with_identifier_in_test_zone_dev(final String api,
        final String identifier, final String zone) throws Throwable {

    OAuth2RestTemplate acsTemplate = this.acsZone2Template;
    String zoneName = getZoneName(zone);

    HttpHeaders zoneHeaders = new HttpHeaders();
    zoneHeaders.set(PolicyHelper.PREDIX_ZONE_ID, zoneName);

    String encodedIdentifier = URLEncoder.encode(identifier, "UTF-8");
    URI uri = URI.create(this.acsUrl + ACS_VERSION + "/" + api + "/" + encodedIdentifier);
    try {/*  ww w.  j a v a2  s.co m*/
        this.status = acsTemplate
                .exchange(uri, HttpMethod.DELETE, new HttpEntity<>(zoneHeaders), ResponseEntity.class)
                .getStatusCode().value();
    } catch (HttpClientErrorException e) {
        Assert.fail("Unable to DELETE identifier: " + identifier + " for api: " + api);
    }
}

From source file:com.ge.predix.acceptance.test.zone.admin.DefaultZoneAuthorizationIT.java

/**
 * 1. Create a token from zone issuer with scopes for accessing: a. zone specific resources, AND b.
 * acs.zones.admin//from w  ww. ja  v  a 2 s  . co m
 *
 * 2. Try to access a zone specific resource . This should work 3. Try to access /v1/zone - THIS SHOULD FAIL
 *
 * @throws Exception
 */
public void testAccessGlobalResourceWithZoneIssuer() throws Exception {
    OAuth2RestTemplate zone2AcsTemplate = this.acsRestTemplateFactory.getACSZone2RogueTemplate();

    HttpHeaders zoneTwoHeaders = new HttpHeaders();
    zoneTwoHeaders.set(PolicyHelper.PREDIX_ZONE_ID, this.zoneHelper.getZone2Name());

    // Write a resource to zone2. This should work
    ResponseEntity<Object> responseEntity = this.privilegeHelper.postResources(zone2AcsTemplate,
            zoneHelper.getAcsBaseURL(), zoneTwoHeaders, new BaseResource("/sites/sanramon"));
    Assert.assertEquals(responseEntity.getStatusCode(), HttpStatus.NO_CONTENT);

    // Try to get global resource from global/baseUrl. This should FAIL
    try {
        zone2AcsTemplate.exchange(this.zoneHelper.getAcsBaseURL() + "/v1/zone/" + this.zone2Name,
                HttpMethod.GET, null, Zone.class);
        Assert.fail("Able to access non-zone specific resource with a zone specific issuer token!");
    } catch (OAuth2AccessDeniedException e) {
        // expected
    }

    // Try to get global resource from zone2Url. This should FAIL
    try {
        zone2AcsTemplate.exchange(this.zoneHelper.getAcsBaseURL() + "/v1/zone/" + this.zone2Name,
                HttpMethod.GET, new HttpEntity<>(zoneTwoHeaders), Zone.class);
        Assert.fail("Able to access non-zone specific resource from a zone specific URL, "
                + "with a zone specific issuer token!");
    } catch (InvalidRequestException e) {
        // expected
    }

}

From source file:com.ge.predix.acceptance.test.zone.admin.ZoneEnforcementStepsDefinitions.java

@When("^client_two does a GET on (.*?) with (.*?) in zone (.*?)$")
public void client_two_does_a_GET_on_subject_with_subject_id__in_zone(final String api, final String identifier,
        final String subdomainSuffix) throws Throwable {

    OAuth2RestTemplate acsTemplate = this.acsZone2Template;
    String encodedIdentifier = URLEncoder.encode(identifier, "UTF-8");
    HttpHeaders zoneHeaders = new HttpHeaders();
    // differentiate between zone 1 and zone 2, which will have slightly different uris
    zoneHeaders.set(PolicyHelper.PREDIX_ZONE_ID, getZoneName(subdomainSuffix));

    URI uri = URI.create(this.acsUrl + ACS_VERSION + "/" + api + "/" + encodedIdentifier);
    try {//  ww w  . j  a  v  a  2s .co  m
        switch (api) {
        case "subject":
            this.responseEntity = acsTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(zoneHeaders),
                    BaseSubject.class);
            this.status = this.responseEntity.getStatusCode().value();
            break;
        case "resource":
            this.responseEntityForResource = acsTemplate.exchange(uri, HttpMethod.GET,
                    new HttpEntity<>(zoneHeaders), BaseResource.class);
            this.status = this.responseEntityForResource.getStatusCode().value();
            break;
        case "policy-set":
            this.policyset = acsTemplate.exchange(
                    this.acsUrl + PolicyHelper.ACS_POLICY_SET_API_PATH + this.testPolicyName, HttpMethod.GET,
                    new HttpEntity<>(zoneHeaders), PolicySet.class);
            this.status = this.policyset.getStatusCode().value();
            break;
        default:
            Assert.fail("Api " + api + " does not match/is not yet implemented for this test code.");
        }
    } catch (OAuth2Exception e) {
        this.status = e.getHttpErrorCode();
    }
}