Search in sources :

Example 11 with ResponseExtractor

use of org.springframework.web.client.ResponseExtractor in project spring-security-oauth by spring-projects.

the class AbstractAuthorizationCodeProviderTests method verifyAuthorizationPage.

private void verifyAuthorizationPage(OAuth2RestTemplate restTemplate, String location) {
    final AtomicReference<String> confirmationPage = new AtomicReference<String>();
    AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider() {

        @Override
        protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
            return new ResponseExtractor<ResponseEntity<Void>>() {

                public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
                    confirmationPage.set(StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")));
                    return new ResponseEntity<Void>(response.getHeaders(), response.getStatusCode());
                }
            };
        }
    };
    try {
        provider.obtainAuthorizationCode(restTemplate.getResource(), restTemplate.getOAuth2ClientContext().getAccessTokenRequest());
    } catch (UserApprovalRequiredException e) {
    // ignore
    }
    String page = confirmationPage.get();
    verifyAuthorizationPage(page);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) AuthorizationCodeAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider) UserApprovalRequiredException(org.springframework.security.oauth2.client.resource.UserApprovalRequiredException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseExtractor(org.springframework.web.client.ResponseExtractor) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 12 with ResponseExtractor

use of org.springframework.web.client.ResponseExtractor in project geode by apache.

the class AbstractHttpOperationInvoker method downloadResponseToTempFile.

protected Object downloadResponseToTempFile(ClientHttpRequest request, Map<String, ?> uriVariables) {
    final URI url = request.getURL(uriVariables);
    // Optional Accept header
    RequestCallback requestCallback = r -> {
        r.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
        HttpHeaders header = request.getHeaders();
        r.getHeaders().setAll(request.getHeaders().toSingleValueMap());
    };
    // Streams the response instead of loading it all in memory
    ResponseExtractor<Object> responseExtractor = resp -> {
        MediaType mediaType = resp.getHeaders().getContentType();
        if (mediaType.equals(MediaType.APPLICATION_JSON)) {
            return org.apache.commons.io.IOUtils.toString(resp.getBody(), "UTF-8");
        } else {
            Path tempFile = Files.createTempFile("fileDownload", "");
            if (tempFile.toFile().exists()) {
                FileUtils.deleteQuietly(tempFile.toFile());
            }
            Files.copy(resp.getBody(), tempFile);
            return tempFile;
        }
    };
    return getRestTemplate().execute(url, org.springframework.http.HttpMethod.GET, requestCallback, responseExtractor);
}
Also used : Arrays(java.util.Arrays) QueryExp(javax.management.QueryExp) DistributedSystemMXBean(org.apache.geode.management.DistributedSystemMXBean) MBeanJMXAdapter(org.apache.geode.management.internal.MBeanJMXAdapter) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) LogService(org.apache.geode.internal.logging.LogService) Map(java.util.Map) HttpHeader(org.apache.geode.management.internal.web.http.HttpHeader) URI(java.net.URI) Path(java.nio.file.Path) RestTemplate(org.springframework.web.client.RestTemplate) ResponseExtractor(org.springframework.web.client.ResponseExtractor) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Set(java.util.Set) ObjectName(javax.management.ObjectName) Executors(java.util.concurrent.Executors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) Entry(java.util.Map.Entry) HttpMBeanProxyFactory(org.apache.geode.management.internal.web.shell.support.HttpMBeanProxyFactory) IOUtils(org.apache.geode.internal.util.IOUtils) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) QueryParameterSource(org.apache.geode.management.internal.web.domain.QueryParameterSource) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) Link(org.apache.geode.management.internal.web.domain.Link) ManagementConstants(org.apache.geode.management.internal.ManagementConstants) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) Iterator(java.util.Iterator) Files(java.nio.file.Files) UriUtils(org.apache.geode.management.internal.web.util.UriUtils) GemFireVersion(org.apache.geode.internal.GemFireVersion) FileUtils(org.apache.commons.io.FileUtils) ResourceAccessException(org.springframework.web.client.ResourceAccessException) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) RequestCallback(org.springframework.web.client.RequestCallback) SerializableObjectHttpMessageConverter(org.apache.geode.management.internal.web.http.converter.SerializableObjectHttpMessageConverter) TimeUnit(java.util.concurrent.TimeUnit) HttpStatus(org.springframework.http.HttpStatus) StringUtils(org.apache.geode.internal.lang.StringUtils) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMethod(org.apache.geode.management.internal.web.http.HttpMethod) ResponseEntity(org.springframework.http.ResponseEntity) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) Path(java.nio.file.Path) HttpHeaders(org.springframework.http.HttpHeaders) RequestCallback(org.springframework.web.client.RequestCallback) MediaType(org.springframework.http.MediaType) URI(java.net.URI)

Example 13 with ResponseExtractor

use of org.springframework.web.client.ResponseExtractor in project incubator-servicecomb-java-chassis by apache.

the class TestRestTemplateWrapper method executeWithUnderlyingRestTemplate.

@Test
public void executeWithUnderlyingRestTemplate() {
    RequestCallback requestCallback = clientHttpRequest -> {
    };
    ResponseExtractor<ResponseEntity<String>> responseExtractor = clientHttpResponse -> responseEntity;
    ResponseEntity<String> actual;
    for (HttpMethod method : httpMethods) {
        when(underlying.execute(url, method, requestCallback, responseExtractor, param1, param2)).thenReturn(responseEntity);
        actual = wrapper.execute(url, method, requestCallback, responseExtractor, param1, param2);
        assertThat(actual, is(responseEntity));
        verify(underlying).execute(url, method, requestCallback, responseExtractor, param1, param2);
        when(underlying.execute(url, method, requestCallback, responseExtractor, paramsMap)).thenReturn(responseEntity);
        actual = wrapper.execute(url, method, requestCallback, responseExtractor, paramsMap);
        assertThat(actual, is(responseEntity));
        verify(underlying).execute(url, method, requestCallback, responseExtractor, paramsMap);
        when(underlying.execute(uri, method, requestCallback, responseExtractor)).thenReturn(responseEntity);
        actual = wrapper.execute(uri, method, requestCallback, responseExtractor);
        assertThat(actual, is(responseEntity));
        verify(underlying).execute(uri, method, requestCallback, responseExtractor);
    }
}
Also used : IsIterableContainingInOrder.contains(org.hamcrest.collection.IsIterableContainingInOrder.contains) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) GET(org.springframework.http.HttpMethod.GET) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) HashMap(java.util.HashMap) PATCH(org.springframework.http.HttpMethod.PATCH) DELETE(org.springframework.http.HttpMethod.DELETE) Collections.singletonList(java.util.Collections.singletonList) Assert.assertThat(org.junit.Assert.assertThat) HashSet(java.util.HashSet) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) HEAD(org.springframework.http.HttpMethod.HEAD) UriTemplateHandler(org.springframework.web.util.UriTemplateHandler) URI(java.net.URI) OPTIONS(org.springframework.http.HttpMethod.OPTIONS) PUT(org.springframework.http.HttpMethod.PUT) RestTemplate(org.springframework.web.client.RestTemplate) Before(org.junit.Before) RequestEntity(org.springframework.http.RequestEntity) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) POST(org.springframework.http.HttpMethod.POST) ResponseExtractor(org.springframework.web.client.ResponseExtractor) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) DefaultUriTemplateHandler(org.springframework.web.util.DefaultUriTemplateHandler) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) RequestCallback(org.springframework.web.client.RequestCallback) Randomness.uniquify(com.seanyinx.github.unit.scaffolding.Randomness.uniquify) Mockito.never(org.mockito.Mockito.never) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) TRACE(org.springframework.http.HttpMethod.TRACE) ResponseEntity(org.springframework.http.ResponseEntity) OK(org.springframework.http.HttpStatus.OK) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) ResponseEntity(org.springframework.http.ResponseEntity) RequestCallback(org.springframework.web.client.RequestCallback) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 14 with ResponseExtractor

use of org.springframework.web.client.ResponseExtractor in project InciManager_i2a by Arquisoft.

the class AbstractSteps method executeGet.

protected void executeGet(String url) throws IOException {
    final Map<String, String> headers = new HashMap<>();
    headers.put("Accept", "application/json");
    final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
    final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
    if (restTemplate == null) {
        restTemplate = new RestTemplate();
    }
    restTemplate.setErrorHandler(errorHandler);
    latestResponse = restTemplate.execute(url, HttpMethod.GET, requestCallback, new ResponseExtractor<ResponseResults>() {

        @Override
        public ResponseResults extractData(ClientHttpResponse response) throws IOException {
            if (errorHandler.hadError) {
                return (errorHandler.getResults());
            } else {
                return (new ResponseResults(response));
            }
        }
    });
}
Also used : HashMap(java.util.HashMap) RestTemplate(org.springframework.web.client.RestTemplate) ResponseExtractor(org.springframework.web.client.ResponseExtractor) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 15 with ResponseExtractor

use of org.springframework.web.client.ResponseExtractor in project java-chassis by ServiceComb.

the class TestRestTemplateWrapper method executeWithUnderlyingRestTemplate.

@Test
public void executeWithUnderlyingRestTemplate() {
    RequestCallback requestCallback = clientHttpRequest -> {
    };
    ResponseExtractor<ResponseEntity<String>> responseExtractor = clientHttpResponse -> responseEntity;
    ResponseEntity<String> actual;
    for (HttpMethod method : httpMethods) {
        when(underlying.execute(url, method, requestCallback, responseExtractor, param1, param2)).thenReturn(responseEntity);
        actual = wrapper.execute(url, method, requestCallback, responseExtractor, param1, param2);
        assertThat(actual, is(responseEntity));
        verify(underlying).execute(url, method, requestCallback, responseExtractor, param1, param2);
        when(underlying.execute(url, method, requestCallback, responseExtractor, paramsMap)).thenReturn(responseEntity);
        actual = wrapper.execute(url, method, requestCallback, responseExtractor, paramsMap);
        assertThat(actual, is(responseEntity));
        verify(underlying).execute(url, method, requestCallback, responseExtractor, paramsMap);
        when(underlying.execute(uri, method, requestCallback, responseExtractor)).thenReturn(responseEntity);
        actual = wrapper.execute(uri, method, requestCallback, responseExtractor);
        assertThat(actual, is(responseEntity));
        verify(underlying).execute(uri, method, requestCallback, responseExtractor);
    }
}
Also used : IsIterableContainingInOrder.contains(org.hamcrest.collection.IsIterableContainingInOrder.contains) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) GET(org.springframework.http.HttpMethod.GET) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) HashMap(java.util.HashMap) PATCH(org.springframework.http.HttpMethod.PATCH) DELETE(org.springframework.http.HttpMethod.DELETE) Collections.singletonList(java.util.Collections.singletonList) Assert.assertThat(org.junit.Assert.assertThat) HashSet(java.util.HashSet) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) HEAD(org.springframework.http.HttpMethod.HEAD) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory) UriTemplateHandler(org.springframework.web.util.UriTemplateHandler) URI(java.net.URI) OPTIONS(org.springframework.http.HttpMethod.OPTIONS) PUT(org.springframework.http.HttpMethod.PUT) RestTemplate(org.springframework.web.client.RestTemplate) Before(org.junit.Before) RequestEntity(org.springframework.http.RequestEntity) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) POST(org.springframework.http.HttpMethod.POST) ResponseExtractor(org.springframework.web.client.ResponseExtractor) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) RequestCallback(org.springframework.web.client.RequestCallback) Randomness.uniquify(com.seanyinx.github.unit.scaffolding.Randomness.uniquify) Mockito.never(org.mockito.Mockito.never) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) TRACE(org.springframework.http.HttpMethod.TRACE) ResponseEntity(org.springframework.http.ResponseEntity) OK(org.springframework.http.HttpStatus.OK) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) ResponseEntity(org.springframework.http.ResponseEntity) RequestCallback(org.springframework.web.client.RequestCallback) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Aggregations

ResponseExtractor (org.springframework.web.client.ResponseExtractor)15 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)12 ResponseEntity (org.springframework.http.ResponseEntity)10 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)9 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)7 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)6 IOException (java.io.IOException)5 BeforeOAuth2Context (org.springframework.security.oauth2.client.test.BeforeOAuth2Context)5 AuthorizationCodeAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider)5 URI (java.net.URI)4 HttpHeaders (org.springframework.http.HttpHeaders)4 RestTemplate (org.springframework.web.client.RestTemplate)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Set (java.util.Set)3 ResourceOwnerPasswordAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider)3 RequestCallback (org.springframework.web.client.RequestCallback)3 Randomness.uniquify (com.seanyinx.github.unit.scaffolding.Randomness.uniquify)2 Arrays.asList (java.util.Arrays.asList)2