Search in sources :

Example 11 with HttpRequest

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

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedIPv6.

//SPR-14761
@Test
public void fromHttpRequestWithForwardedIPv6() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("Forwarded", "host=[1abc:2abc:3abc::5ABC:6abc]");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("http://[1abc:2abc:3abc::5ABC:6abc]/mvc-showcase", result.toString());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 12 with HttpRequest

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

the class ServletUriComponentsBuilder method initFromRequest.

/**
	 * Initialize a builder with a scheme, host,and port (but not path and query).
	 */
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request) {
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    String scheme = uriComponents.getScheme();
    String host = uriComponents.getHost();
    int port = uriComponents.getPort();
    ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
    builder.scheme(scheme);
    builder.host(host);
    if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
        builder.port(port);
    }
    return builder;
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UriComponents(org.springframework.web.util.UriComponents)

Example 13 with HttpRequest

use of org.springframework.http.HttpRequest 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 14 with HttpRequest

use of org.springframework.http.HttpRequest 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 15 with HttpRequest

use of org.springframework.http.HttpRequest 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

HttpRequest (org.springframework.http.HttpRequest)30 Test (org.junit.Test)26 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)19 IOException (java.io.IOException)6 URI (java.net.URI)6 ArrayList (java.util.ArrayList)4 HttpRequestWrapper (org.springframework.http.client.support.HttpRequestWrapper)4 HttpMethod (org.springframework.http.HttpMethod)3 ClientHttpRequestExecution (org.springframework.http.client.ClientHttpRequestExecution)3 ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)3 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)2 UriComponents (org.springframework.web.util.UriComponents)2 Account (android.accounts.Account)1 List (java.util.List)1 Before (org.junit.Before)1 ResolvableType (org.springframework.core.ResolvableType)1 HttpHeaders (org.springframework.http.HttpHeaders)1 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)1 MediaType (org.springframework.http.MediaType)1