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.sleuth.instrument.web.client.TraceWebClientAutoConfiguration.java

private boolean hasTraceInterceptor(RestTemplate restTemplate) {
    for (ClientHttpRequestInterceptor interceptor : restTemplate.getInterceptors()) {
        if (interceptor instanceof TracingClientHttpRequestInterceptor) {
            return true;
        }//from   ww  w. j  a v a  2s.  c o m
    }
    return false;
}

From source file:org.springframework.cloud.sleuth.instrument.web.client.TraceWebClientAutoConfiguration.java

void inject(RestTemplate restTemplate) {
    if (hasTraceInterceptor(restTemplate)) {
        return;//from   ww w .j  av  a  2  s  .  c om
    }
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
            restTemplate.getInterceptors());
    interceptors.add(0, this.interceptor);
    restTemplate.setInterceptors(interceptors);
}

From source file:org.springframework.boot.test.web.client.TestRestTemplate.java

private void addAuthentication(RestTemplate restTemplate, String username, String password) {
    if (username == null) {
        return;//w  w  w  .  java 2s  .c  om
    }
    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
    if (interceptors == null) {
        interceptors = Collections.emptyList();
    }
    interceptors = new ArrayList<>(interceptors);
    Iterator<ClientHttpRequestInterceptor> iterator = interceptors.iterator();
    while (iterator.hasNext()) {
        if (iterator.next() instanceof BasicAuthorizationInterceptor) {
            iterator.remove();
        }
    }
    interceptors.add(new BasicAuthorizationInterceptor(username, password));
    restTemplate.setInterceptors(interceptors);
}

From source file:org.bytesoft.bytejta.supports.springcloud.SpringCloudConfiguration.java

@org.springframework.context.annotation.Primary
@org.springframework.cloud.client.loadbalancer.LoadBalanced
@org.springframework.context.annotation.Bean
public RestTemplate defaultRestTemplate(
        @Autowired TransactionRequestInterceptor transactionRequestInterceptor) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getInterceptors().add(transactionRequestInterceptor);
    return restTemplate;
}

From source file:org.springframework.ide.eclipse.boot.wizard.github.auth.BasicAuthCredentials.java

@Override
public RestTemplate apply(RestTemplate rest) {
    List<ClientHttpRequestInterceptor> interceptors = rest.getInterceptors();
    interceptors.add(new ClientHttpRequestInterceptor() {
        public ClientHttpResponse intercept(HttpRequest request, byte[] body,
                ClientHttpRequestExecution execution) throws IOException {
            if (matchHost(request.getURI().getHost())) {
                HttpHeaders headers = request.getHeaders();
                if (!headers.containsKey("Authorization")) {
                    String authString = computeAuthString();
                    headers.add("Authorization", authString);
                }/*from www . j  a  va 2  s  .c  om*/
            }
            return execution.execute(request, body);
        }

    });
    return rest;
}