Search in sources :

Example 1 with HttpRequestWrapper

use of org.springframework.http.client.support.HttpRequestWrapper in project spring-framework by spring-projects.

the class InterceptingClientHttpRequestFactoryTests method changeMethod.

@Test
public void changeMethod() throws Exception {
    final HttpMethod changedMethod = HttpMethod.POST;
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            return execution.execute(new HttpRequestWrapper(request) {

                @Override
                public HttpMethod getMethod() {
                    return changedMethod;
                }
            }, body);
        }
    };
    requestFactoryMock = new RequestFactoryMock() {

        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            assertEquals(changedMethod, httpMethod);
            return super.createRequest(uri, httpMethod);
        }
    };
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
    ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
    request.execute();
}
Also used : HttpRequest(org.springframework.http.HttpRequest) HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) IOException(java.io.IOException) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 2 with HttpRequestWrapper

use of org.springframework.http.client.support.HttpRequestWrapper in project ETSMobile-Android2 by ApplETS.

the class MonETSNotificationsRequest method loadDataFromNetwork.

@Override
public MonETSNotificationList loadDataFromNetwork() throws Exception {
    accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
    if (accounts.length > 0) {
        authToken = accountManager.peekAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE);
    }
    String url = context.getString(R.string.portail_api_base_url);
    if (onlyNewNotifs) {
        url += "/api/notification/dossier/1";
    } else {
        url += "/api/notification";
    }
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
            requestWrapper.getHeaders().set("Cookie", authToken);
            return execution.execute(requestWrapper, body);
        }
    };
    RestTemplate restTemplate = getRestTemplate();
    List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
    list.add(interceptor);
    restTemplate.setInterceptors(list);
    try {
        return restTemplate.getForObject(url, MonETSNotificationList.class);
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode().value() == 401) {
            if (accounts.length > 0) {
                accountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
                authToken = accountManager.blockingGetAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE, true);
                interceptor = new ClientHttpRequestInterceptor() {

                    @Override
                    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
                        HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
                        requestWrapper.getHeaders().set("Cookie", authToken);
                        return execution.execute(requestWrapper, body);
                    }
                };
                list.clear();
                list.add(interceptor);
                restTemplate.setInterceptors(list);
            }
        }
    } finally {
        return restTemplate.getForObject(url, MonETSNotificationList.class);
    }
}
Also used : HttpRequest(org.springframework.http.HttpRequest) Account(android.accounts.Account) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) RestTemplate(org.springframework.web.client.RestTemplate) ArrayList(java.util.ArrayList) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor)

Example 3 with HttpRequestWrapper

use of org.springframework.http.client.support.HttpRequestWrapper in project spring-boot by spring-projects.

the class RootUriRequestExpectationManagerTests method validateRequestWhenUriStartsWithRootUriShouldReplaceUri.

@Test
public void validateRequestWhenUriStartsWithRootUriShouldReplaceUri() throws Exception {
    ClientHttpRequest request = mock(ClientHttpRequest.class);
    given(request.getURI()).willReturn(new URI(this.uri + "/hello"));
    this.manager.validateRequest(request);
    verify(this.delegate).validateRequest(this.requestCaptor.capture());
    HttpRequestWrapper actual = (HttpRequestWrapper) this.requestCaptor.getValue();
    assertThat(actual.getRequest()).isSameAs(request);
    assertThat(actual.getURI()).isEqualTo(new URI("/hello"));
}
Also used : HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 4 with HttpRequestWrapper

use of org.springframework.http.client.support.HttpRequestWrapper in project spring-framework by spring-projects.

the class InterceptingClientHttpRequestFactoryTests method changeHeaders.

@Test
public void changeHeaders() throws Exception {
    final String headerName = "Foo";
    final String headerValue = "Bar";
    final String otherValue = "Baz";
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            HttpRequestWrapper wrapper = new HttpRequestWrapper(request);
            wrapper.getHeaders().add(headerName, otherValue);
            return execution.execute(wrapper, body);
        }
    };
    requestMock = new RequestMock() {

        @Override
        public ClientHttpResponse execute() throws IOException {
            List<String> headerValues = getHeaders().get(headerName);
            assertEquals(2, headerValues.size());
            assertEquals(headerValue, headerValues.get(0));
            assertEquals(otherValue, headerValues.get(1));
            return super.execute();
        }
    };
    requestMock.getHeaders().add(headerName, headerValue);
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
    ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
    request.execute();
}
Also used : HttpRequest(org.springframework.http.HttpRequest) IOException(java.io.IOException) URI(java.net.URI) HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 5 with HttpRequestWrapper

use of org.springframework.http.client.support.HttpRequestWrapper in project spring-framework by spring-projects.

the class InterceptingClientHttpRequestFactoryTests method changeURI.

@Test
public void changeURI() throws Exception {
    final URI changedUri = new URI("http://example.com/2");
    ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {

        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            return execution.execute(new HttpRequestWrapper(request) {

                @Override
                public URI getURI() {
                    return changedUri;
                }
            }, body);
        }
    };
    requestFactoryMock = new RequestFactoryMock() {

        @Override
        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            assertEquals(changedUri, uri);
            return super.createRequest(uri, httpMethod);
        }
    };
    requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
    ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
    request.execute();
}
Also used : HttpRequest(org.springframework.http.HttpRequest) HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) IOException(java.io.IOException) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Aggregations

HttpRequestWrapper (org.springframework.http.client.support.HttpRequestWrapper)5 URI (java.net.URI)4 Test (org.junit.Test)4 HttpRequest (org.springframework.http.HttpRequest)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 HttpMethod (org.springframework.http.HttpMethod)2 Account (android.accounts.Account)1 List (java.util.List)1 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)1 ClientHttpRequestExecution (org.springframework.http.client.ClientHttpRequestExecution)1 ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 RestTemplate (org.springframework.web.client.RestTemplate)1