Example usage for org.springframework.web.client RestTemplate getInterceptors

List of usage examples for org.springframework.web.client RestTemplate getInterceptors

Introduction

In this page you can find the example usage for org.springframework.web.client RestTemplate getInterceptors.

Prototype

public List<ClientHttpRequestInterceptor> getInterceptors() 

Source Link

Document

Return the request interceptors that this accessor uses.

Usage

From source file:org.springframework.cloud.netflix.eureka.http.RestTemplateTransportClientFactory.java

private RestTemplate restTemplate(String serviceUrl) {
    RestTemplate restTemplate = new RestTemplate();
    try {/*  ww w.j ava 2 s  . c  om*/
        URI serviceURI = new URI(serviceUrl);
        if (serviceURI.getUserInfo() != null) {
            String[] credentials = serviceURI.getUserInfo().split(":");
            if (credentials.length == 2) {
                restTemplate.getInterceptors()
                        .add(new BasicAuthorizationInterceptor(credentials[0], credentials[1]));
            }
        }
    } catch (URISyntaxException ignore) {

    }

    restTemplate.getMessageConverters().add(0, mappingJacksonHttpMessageConverter());

    return restTemplate;
}

From source file:com.bbva.arq.devops.ae.mirrorgate.collectors.jira.config.Config.java

@Bean(JIRA_REST_TEMPLATE)
public RestTemplate getJiraRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
    restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
    restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(jiraUserName, jiraPassword));
    return restTemplate;
}

From source file:com.bbva.arq.devops.ae.mirrorgate.collectors.jira.config.Config.java

@Bean(MIRRORGATE_REST_TEMPLATE)
public RestTemplate getRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
    restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
    if (!StringUtils.isBlank(mirrorGateUserName) && !StringUtils.isBlank(mirrorGatePassword)) {
        restTemplate.getInterceptors()
                .add(new BasicAuthenticationInterceptor(mirrorGateUserName, mirrorGatePassword));
    }/*  w  w  w. ja va2s .  c o m*/

    return restTemplate;
}

From source file:org.springframework.boot.web.client.RestTemplateBuilderTests.java

@Test
public void interceptorsShouldApply() throws Exception {
    RestTemplate template = this.builder.interceptors(this.interceptor).build();
    assertThat(template.getInterceptors()).containsOnly(this.interceptor);
}

From source file:com.feedeo.rest.client.AbstractOAuth2RestClient.java

@Override
protected RestOperations createRestOperations(ObjectMapper objectMapper,
        ClientHttpRequestFactory clientHttpRequestFactory) {
    RestTemplate restTemplate = (RestTemplate) super.createRestOperations(objectMapper,
            clientHttpRequestFactory);//from w w  w.  jav a  2s . c o m

    OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(new BaseOAuth2ProtectedResourceDetails(),
            new DefaultOAuth2ClientContext());
    oAuth2RestTemplate.setRequestFactory(restTemplate.getRequestFactory());
    oAuth2RestTemplate.setMessageConverters(restTemplate.getMessageConverters());
    oAuth2RestTemplate.setInterceptors(restTemplate.getInterceptors());

    return oAuth2RestTemplate;
}

From source file:com.pepaproch.gtswsdlclient.AddresCheckImplTest.java

/**
 * Test of checkAddres method, of class AddresCheckImpl.
 *///from ww  w  . j a va  2  s.  c o m
@Test
public void testCheckAddresAuthenticated() {

    System.out.println("checkAddres");
    AddressQuery addrQuery = new AddressQuery();

    RestTemplate restTemplate = getRestTemplate();
    RestTemplate authRestTemplate = getRestTemplate();
    AuthTokenProviderImpl authTokenProvider = new AuthTokenProviderImpl(BASE_URL, "test", "test",
            authRestTemplate, 1);
    AuthHeaderInterceptor securityTokenInterceptor = new AuthHeaderInterceptor(authTokenProvider);
    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
    if (interceptors == null) {
        interceptors = new ArrayList<>();
        restTemplate.setInterceptors(interceptors);
    }
    interceptors.add(securityTokenInterceptor);
    RestContext restContext = Mockito.mock(RestContext.class);
    when(restContext.getRestTemplate()).thenReturn(restTemplate);
    when(restContext.getBASE_URL()).thenReturn(BASE_URL);

    //first request
    AddressResponse expResult = null;
    mockServer = MockRestServiceServer.createServer(restTemplate);
    this.mockServer.expect(requestTo(BASE_URL + AddresCheckImpl.ADDR_CHECK_PATH))
            .andExpect(method(HttpMethod.GET))
            .andRespond(withSuccess(CHECK_ADDR_RESPONSE_BODY, MediaType.APPLICATION_JSON));

    //auth token is not set so it will call remote for acces token
    mockServerAuthProvider = MockRestServiceServer.createServer(authRestTemplate);
    String authResponseBody = "{\"accessToken\" : \"testtoken\" }";
    mockServerAuthProvider.expect(requestTo(BASE_URL + AUTH_PATH)).andExpect(method(HttpMethod.POST))
            .andRespond(withSuccess(authResponseBody, MediaType.APPLICATION_JSON));
    //token is set with no valid time so token should be renewed
    String authResponseBody1 = "{\"accessToken\" : \"testtoken-1\" }";
    mockServerAuthProvider.expect(requestTo(BASE_URL + AUTH_PATH)).andExpect(method(HttpMethod.POST))
            .andRespond(withSuccess(authResponseBody1, MediaType.APPLICATION_JSON));

    //second call with no valid auth token
    this.mockServer.expect(requestTo(BASE_URL + AddresCheckImpl.ADDR_CHECK_PATH))
            .andExpect(method(HttpMethod.GET))
            .andRespond(withSuccess(CHECK_ADDR_RESPONSE_BODY, MediaType.APPLICATION_JSON));

    AddresCheckImpl instance = new AddresCheckImpl(restContext);
    AddressResponse result = instance.checkAddres(addrQuery);
    String authToken1 = authTokenProvider.getAuthorisationToken().toString();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    AddressResponse result1 = instance.checkAddres(addrQuery);
    String authToken2 = authTokenProvider.getAuthorisationToken().toString();

    Integer expectedTotal = new Integer(3);
    assertEquals(result.getTotal(), expectedTotal);

    mockServer.verify();

    mockServerAuthProvider.verify();

}

From source file:org.springframework.boot.web.client.RestTemplateBuilderTests.java

@Test
public void basicAuthorizationShouldApply() throws Exception {
    RestTemplate template = this.builder.basicAuthorization("spring", "boot").build();
    ClientHttpRequestInterceptor interceptor = template.getInterceptors().get(0);
    assertThat(interceptor).isInstanceOf(BasicAuthorizationInterceptor.class);
    assertThat(interceptor).extracting("username").containsExactly("spring");
    assertThat(interceptor).extracting("password").containsExactly("boot");
}

From source file:org.springframework.boot.web.client.RestTemplateBuilderTests.java

@Test
public void additionalInterceptorsShouldAddToExisting() throws Exception {
    ClientHttpRequestInterceptor interceptor = mock(ClientHttpRequestInterceptor.class);
    RestTemplate template = this.builder.interceptors(interceptor).additionalInterceptors(this.interceptor)
            .build();//from  ww w.j  av  a 2  s.co m
    assertThat(template.getInterceptors()).containsOnly(interceptor, this.interceptor);
}

From source file:org.springframework.boot.web.client.RestTemplateBuilderTests.java

@Test
public void interceptorsShouldReplaceExisting() throws Exception {
    RestTemplate template = this.builder.interceptors(mock(ClientHttpRequestInterceptor.class))
            .interceptors(Collections.singleton(this.interceptor)).build();
    assertThat(template.getInterceptors()).containsOnly(this.interceptor);
}