Java Code Examples for org.springframework.web.client.RestTemplate#getInterceptors()

The following examples show how to use org.springframework.web.client.RestTemplate#getInterceptors() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Bean
public SmartInitializingSingleton loadBalancedRestTemplateInitializer2(
        final ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) {
    return () -> {
        logger.info("RestTemplateResilienceAutoConfiguration init2");
        for (RestTemplate restTemplate : RestTemplateCircuitBreakerAutoConfiguration.this.restTemplates) {
            List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
            logger.info("RestTemplate init2 start,interceptor size:" + interceptors.size());
            //for (ClientHttpRequestInterceptor interceptor : interceptors) {
            //logger.info("RestTemplate init2 interceptor ing:"+interceptor.getClass().getCanonicalName());
            //}
            //logger.info("RestTemplate init2 Customizer end");
            ClientHttpRequestInterceptor interceptor1 = new RestTemplateCircuitBreakerInterceptor
                    (circuitBreakerCore);
            interceptors.add(0, interceptor1);
            restTemplate.setInterceptors(interceptors);
            logger.info("RestTemplate init2 end,add CircuitBreaker interceptor");
        }
    };
}
 
Example 2
@Override
public void afterPropertiesSet() throws Exception {
    if (null != restTemplateList) {
        for (RestTemplate restTemplate : restTemplateList) {
            List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
            if (interceptors == null) {
                restTemplate.setInterceptors(Arrays.asList(grayClientHttpRequestInterceptor));
            } else {
                List<ClientHttpRequestInterceptor> newInterceptors = new ArrayList<>();
                newInterceptors.addAll(interceptors);
                newInterceptors.add(grayClientHttpRequestInterceptor);
                restTemplate.setInterceptors(newInterceptors);
            }
        }
    }
}
 
Example 3
/** {@inheritDoc} */
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
	// RestTemplate et getInterceptors() existent depuis spring-web 3.1.0.RELEASE
	if (REST_TEMPLATE_INTERCEPTOR_AVAILABLE && bean instanceof RestTemplate) {
		final RestTemplate restTemplate = (RestTemplate) bean;
		final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
				restTemplate.getInterceptors());
		for (final ClientHttpRequestInterceptor interceptor : interceptors) {
			if (interceptor instanceof SpringRestTemplateInterceptor) {
				return bean;
			}
		}
		interceptors.add(SpringRestTemplateInterceptor.SINGLETON);
		restTemplate.setInterceptors(interceptors);
		LOG.debug("rest template interceptor initialized");
	}

	return bean;
}
 
Example 4
@PostConstruct
public void init() {
  LOGGER.debug("init restTemplate for dtm..");
  if (this.restTemplates != null) {
    for (RestTemplate restTemplate : restTemplates) {
      List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(
          restTemplate.getInterceptors());
      interceptors.add(dtmRestTemplateInterceptor);
      restTemplate.setInterceptors(interceptors);
    }
  }
}
 
Example 5
Source Project: txle   File: RestTemplateConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public RestTemplate restTemplate(@Autowired(required = false) OmegaContext context, @Autowired Tracing tracing) {
  RestTemplate template = new RestTemplate();
  List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors();
  interceptors.add(new TransactionClientHttpRequestInterceptor(context));
  // add interceptor for rest's request server By Gannalyo
  interceptors.add(TracingClientHttpRequestInterceptor.create(tracing));
  template.setInterceptors(interceptors);
  return template;
}
 
Example 6
@PostConstruct
public void init() {
    if (this.restTemplates != null) {
        Iterator var1 = this.restTemplates.iterator();

        while (var1.hasNext()) {
            RestTemplate restTemplate = (RestTemplate) var1.next();
            List<ClientHttpRequestInterceptor> interceptors = new ArrayList(restTemplate.getInterceptors());
            interceptors.add(this.seataRestTemplateInterceptor);
            restTemplate.setInterceptors(interceptors);
        }
    }

}
 
Example 7
Source Project: tutorials   File: RestTemplateLoggingLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenLoggingInterceptorConfiguration_whenSendGetForRequestEntity_thenRequestResponseCustomLog() {

    RestTemplate restTemplate = new RestTemplate();
    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
    if (CollectionUtils.isEmpty(interceptors)) {
        interceptors = new ArrayList<>();
    }
    interceptors.add(new LoggingInterceptor());
    restTemplate.setInterceptors(interceptors);

    final ResponseEntity<String> response = restTemplate.postForEntity(baseUrl + "/persons", "my request body", String.class);
    assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
}
 
Example 8
public ApiBootLoggingRestTemplateAutoConfiguration(RestTemplate restTemplate) {

        List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();

        LoggingRestTemplateInterceptor interceptor = new LoggingRestTemplateInterceptor();

        if (ObjectUtils.isEmpty(interceptors)) {
            restTemplate.setInterceptors(Arrays.asList(interceptor));
        } else {
            interceptors.add(interceptor);
        }
    }
 
Example 9
public void enhanceRestTemplateWithSofaTracer(RestTemplate restTemplate) {
    // check interceptor
    if (checkRestTemplateInterceptor(restTemplate)) {
        return;
    }
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(
        restTemplate.getInterceptors());
    interceptors.add(0, this.restTemplateInterceptor);
    restTemplate.setInterceptors(interceptors);
}
 
Example 10
private boolean checkRestTemplateInterceptor(RestTemplate restTemplate) {
    for (ClientHttpRequestInterceptor interceptor : restTemplate.getInterceptors()) {
        if (interceptor instanceof RestTemplateInterceptor) {
            return true;
        }
    }
    return false;
}
 
Example 11
/**
 * Inject the access token into the downstream service calls.
 * 
 * @return  The {@code RestTemplate} for sending HTTP requests.
 */
@Primary
@Bean
public RestTemplate getCustomRestTemplate() {
	RestTemplate template = new RestTemplate();
	List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors();
	if (interceptors == null) {
		template.setInterceptors(Collections.singletonList(new UserContextInterceptor()));  // UserContextInterceptor will inject Authentication header in every REST call
	} else {
		interceptors.add(new UserContextInterceptor());
		template.setInterceptors(interceptors);
	}
	return template;
}
 
Example 12
/**
 * 使用带有Ribbon 功能的Spring RestTemplate
 */
@LoadBalanced
@Bean
@SuppressWarnings("unchecked")
public RestTemplate getRestTemplate(){
    RestTemplate restTemplate = new RestTemplate();
    //加上拦截器,发出请求前加入管理id Header
    List interceptors = restTemplate.getInterceptors();
    if(interceptors==null){
        restTemplate.setInterceptors(Collections.singletonList(new UserContextInterceptor()));
    }else{
        interceptors.add(new UserContextInterceptor());
    }
    return restTemplate;
}
 
Example 13
@Override
protected void assertLoadBalanced(RestTemplate restTemplate) {
	List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
	then(interceptors).hasSize(1);
	ClientHttpRequestInterceptor interceptor = interceptors.get(0);
	then(interceptor).isInstanceOf(LoadBalancerInterceptor.class);
}
 
Example 14
@PostConstruct
public void init() {
	if (this.restTemplates != null) {
		for (RestTemplate restTemplate : restTemplates) {
			List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
					restTemplate.getInterceptors());
			interceptors.add(this.seataRestTemplateInterceptor);
			restTemplate.setInterceptors(interceptors);
		}
	}
}
 
Example 15
@Override
protected void assertLoadBalanced(RestTemplate restTemplate) {
	List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
	then(interceptors).hasSize(1);
	ClientHttpRequestInterceptor interceptor = interceptors.get(0);
	then(interceptor).isInstanceOf(RetryLoadBalancerInterceptor.class);
}
 
Example 16
@Override
public void customize(RestTemplate restTemplate) {
    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();

    for (ClientHttpRequestInterceptor interceptor : interceptors) {
        if (interceptor instanceof TracingRestTemplateInterceptor) {
            return;
        }
    }

    interceptors = new ArrayList<>(interceptors);
    interceptors.add(new TracingRestTemplateInterceptor(tracer, spanDecorators));
    restTemplate.setInterceptors(interceptors);
}
 
Example 17
void inject(RestTemplate restTemplate) {
	if (hasTraceInterceptor(restTemplate)) {
		return;
	}
	List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
			restTemplate.getInterceptors());
	interceptors.add(0, this.interceptor);
	restTemplate.setInterceptors(interceptors);
}
 
Example 18
private boolean hasTraceInterceptor(RestTemplate restTemplate) {
	for (ClientHttpRequestInterceptor interceptor : restTemplate.getInterceptors()) {
		if (interceptor instanceof TracingClientHttpRequestInterceptor
				|| interceptor instanceof LazyTracingClientHttpRequestInterceptor) {
			return true;
		}
	}
	return false;
}
 
Example 19
private List<ClientHttpRequestInterceptor> assertInterceptorsNotEmpty(
		RestTemplate restTemplate) {
	then(restTemplate).isNotNull();
	List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
	then(interceptors).isNotEmpty();
	return interceptors;
}
 
Example 20
private void processing(RestTemplate restTemplate) {
    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
    interceptors.add(new TraceClientHttpRequestInterceptor(traceContentFactory));
    restTemplate.setInterceptors(interceptors);
}