Search in sources :

Example 1 with ClientHttpRequestExecution

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

the class BasicAuthorizationInterceptorTests method interceptShouldAddHeader.

@Test
public void interceptShouldAddHeader() throws Exception {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
    ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
    byte[] body = new byte[] {};
    new BasicAuthorizationInterceptor("spring", "boot").intercept(request, body, execution);
    verify(execution).execute(request, body);
    assertEquals("Basic c3ByaW5nOmJvb3Q=", request.getHeaders().getFirst("Authorization"));
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 2 with ClientHttpRequestExecution

use of org.springframework.http.client.ClientHttpRequestExecution in project spring-security-oauth by spring-projects.

the class ImplicitProviderTests method testPostForAutomaticApprovalToken.

@Test
@OAuth2ContextConfiguration(resource = AutoApproveImplicit.class, initialize = false)
public void testPostForAutomaticApprovalToken() throws Exception {
    final ImplicitAccessTokenProvider implicitProvider = new ImplicitAccessTokenProvider();
    implicitProvider.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(new ClientHttpRequestInterceptor() {

        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            ClientHttpResponse result = execution.execute(request, body);
            latestHeaders = result.getHeaders();
            return result;
        }
    }));
    context.setAccessTokenProvider(implicitProvider);
    context.getAccessTokenRequest().setCookie(cookie);
    assertNotNull(context.getAccessToken());
    assertTrue("Wrong location header: " + latestHeaders.getLocation().getFragment(), latestHeaders.getLocation().getFragment().contains("scope=read write trust"));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ImplicitAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 3 with ClientHttpRequestExecution

use of org.springframework.http.client.ClientHttpRequestExecution 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 4 with ClientHttpRequestExecution

use of org.springframework.http.client.ClientHttpRequestExecution in project sic by belluccifranco.

the class FacturacionIntegrationTest method setup.

@Before
public void setup() {
    String md5Test = "098f6bcd4621d373cade4e832627b4f6";
    usuarioRepository.save(new UsuarioBuilder().withNombre("test").withPassword(md5Test).build());
    // Interceptor de RestTemplate para JWT
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add((ClientHttpRequestInterceptor) (HttpRequest request, byte[] body, ClientHttpRequestExecution execution) -> {
        request.getHeaders().set("Authorization", "Bearer " + token);
        return execution.execute(request, body);
    });
    restTemplate.getRestTemplate().setInterceptors(interceptors);
    // ErrorHandler para RestTemplate        
    restTemplate.getRestTemplate().setErrorHandler(new ResponseErrorHandler() {

        @Override
        public boolean hasError(ClientHttpResponse response) throws IOException {
            HttpStatus.Series series = response.getStatusCode().series();
            return (HttpStatus.Series.CLIENT_ERROR.equals(series) || HttpStatus.Series.SERVER_ERROR.equals(series));
        }

        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
            String mensaje = IOUtils.toString(response.getBody());
            throw new RestClientResponseException(mensaje, response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), null, Charset.defaultCharset());
        }
    });
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) ArrayList(java.util.ArrayList) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) IOException(java.io.IOException) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) UsuarioBuilder(sic.builder.UsuarioBuilder) RestClientResponseException(org.springframework.web.client.RestClientResponseException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Before(org.junit.Before)

Aggregations

ClientHttpRequestExecution (org.springframework.http.client.ClientHttpRequestExecution)4 HttpRequest (org.springframework.http.HttpRequest)3 ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)2 Account (android.accounts.Account)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Before (org.junit.Before)1 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)1 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)1 HttpRequestWrapper (org.springframework.http.client.support.HttpRequestWrapper)1 OAuth2ContextConfiguration (org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration)1 ImplicitAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)1 RestClientResponseException (org.springframework.web.client.RestClientResponseException)1 RestTemplate (org.springframework.web.client.RestTemplate)1 UsuarioBuilder (sic.builder.UsuarioBuilder)1