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

The following examples show how to use org.springframework.web.client.resttemplate#getInterceptors() . 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
Source File: SpringRestTemplateBeanPostProcessor.java    From javamelody with Apache License 2.0 6 votes vote down vote up
/** {@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 2
Source File: RestTemplateCircuitBreakerAutoConfiguration.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: RestTemplateInitializingBean.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: Application.java    From spring-microservices-in-action with Apache License 2.0 5 votes vote down vote up
/**
 * 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 5
Source File: TraceWebClientAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
private List<ClientHttpRequestInterceptor> assertInterceptorsNotEmpty(
		RestTemplate restTemplate) {
	then(restTemplate).isNotNull();
	List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
	then(interceptors).isNotEmpty();
	return interceptors;
}
 
Example 6
Source File: TraceWebClientAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
private boolean hasTraceInterceptor(RestTemplate restTemplate) {
	for (ClientHttpRequestInterceptor interceptor : restTemplate.getInterceptors()) {
		if (interceptor instanceof TracingClientHttpRequestInterceptor
				|| interceptor instanceof LazyTracingClientHttpRequestInterceptor) {
			return true;
		}
	}
	return false;
}
 
Example 7
Source File: TraceWebClientAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
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 8
Source File: TracingRestTemplateCustomizer.java    From java-spring-web with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: RetryLoadBalancerAutoConfigurationTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: SeataRestTemplateAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: LoadBalancerAutoConfigurationTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: LicensingserviceApplication.java    From demo-project with MIT License 5 votes vote down vote up
/**
 * 使用带有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
Source File: DtmRestTemplateAutoConfiguration.java    From spring-cloud-huawei with Apache License 2.0 5 votes vote down vote up
@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 14
Source File: SofaTracerRestTemplateEnhance.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
private boolean checkRestTemplateInterceptor(RestTemplate restTemplate) {
    for (ClientHttpRequestInterceptor interceptor : restTemplate.getInterceptors()) {
        if (interceptor instanceof RestTemplateInterceptor) {
            return true;
        }
    }
    return false;
}
 
Example 15
Source File: SofaTracerRestTemplateEnhance.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
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 16
Source File: ApiBootLoggingRestTemplateAutoConfiguration.java    From api-boot with Apache License 2.0 5 votes vote down vote up
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 17
Source File: RestTemplateLoggingLiveTest.java    From tutorials with 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 18
Source File: SeataRestTemplateAutoConfiguration.java    From seata-samples with Apache License 2.0 5 votes vote down vote up
@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 19
Source File: RestTemplateConfig.java    From txle with 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 20
Source File: RestTemplatePostProcessor.java    From log-trace-spring-boot with Apache License 2.0 4 votes vote down vote up
private void processing(RestTemplate restTemplate) {
    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
    interceptors.add(new TraceClientHttpRequestInterceptor(traceContentFactory));
    restTemplate.setInterceptors(interceptors);
}