Java Code Examples for org.springframework.web.client.AsyncRestTemplate#setInterceptors()

The following examples show how to use org.springframework.web.client.AsyncRestTemplate#setInterceptors() . 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: AsyncRestTemplateCircuitBreakerAutoConfiguration.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
@Bean
public SmartInitializingSingleton loadBalancedAsyncRestTemplateInitializer2(
        final List<AsyncRestTemplateCustomizer> customizers) {
    return new SmartInitializingSingleton() {
        @Override
        public void afterSingletonsInstantiated() {
            logger.info("init AsyncRestTemplateCircuitBreaker start");
            for (AsyncRestTemplate restTemplate : AsyncRestTemplateCircuitBreakerAutoConfiguration.this
                    .restTemplates) {
                AsyncRestTemplateCircuitInterceptor interceptor =
                        new AsyncRestTemplateCircuitInterceptor(circuitBreakerCore);
                logger.info("add AsyncRestTemplateCircuitInterceptor first");
                List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
                interceptors.add(0, interceptor);
                restTemplate.setInterceptors(interceptors);
            }
            logger.info("init AsyncRestTemplateCircuitBreaker end");
        }
    };
}
 
Example 2
Source File: AsyncRestTemplateRateLimiterConfiguration.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
@Bean
public SmartInitializingSingleton loadBalancedAsyncRestTemplateInitializer2(
        final List<AsyncRestTemplateCustomizer> customizers) {
    return new SmartInitializingSingleton() {
        @Override
        public void afterSingletonsInstantiated() {
            logger.info("Init AsyncRestTemplateRateLimiterConfiguration");
            for (AsyncRestTemplate restTemplate : AsyncRestTemplateRateLimiterConfiguration.this.restTemplates) {
                List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
                logger.debug("AsyncRestTemplate init start, interceptor size:" + interceptors.size());
                AsyncClientHttpRequestInterceptor interceptor =
                        new AsyncRestTemplateRateLimiterInterceptor(serviceName);
                interceptors.add(0, interceptor);
                logger.debug("AsyncRestTemplate init end, Add AsyncRestTemplateRateLimiterInterceptor");
                restTemplate.setInterceptors(interceptors);
            }
        }
    };
}
 
Example 3
Source File: DockerServiceFactory.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
private AsyncRestTemplate createNewRestTemplate(String addr) {
    // we use async client because usual client does not allow to interruption in some cases
    NettyRequestFactory factory = new NettyRequestFactory();
    if(AddressUtils.isHttps(addr)) {
        try {
            initSsl(addr, factory);
        } catch (Exception e) {
            log.error("", e);
        }
    }
    final AsyncRestTemplate restTemplate = new AsyncRestTemplate(factory);
    List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(new HttpAuthInterceptor(registryRepository));
    if(!StringUtils.isEmpty(agentPassword)) {
        interceptors.add(new BasicAuthAsyncInterceptor("admin", agentPassword));
    }
    restTemplate.setInterceptors(interceptors);
    return restTemplate;
}
 
Example 4
Source File: ClusterConfigFetcherTest.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
DockerServiceImpl dockerService() {
    ClusterConfig config = ClusterConfigImpl.builder().host("localhost:2375").build();
    AsyncRestTemplate restTemplate = new AsyncRestTemplate();
    restTemplate.setInterceptors(
            Collections.singletonList(
                    new HttpAuthInterceptor(null)));
    return DockerServiceImpl.builder()
      .config(config)
      .cluster("test")
      .restTemplate(restTemplate)
      .nodeInfoProvider(mock(NodeInfoProvider.class))
      .eventConsumer(mock(MessageBus.class))
      .objectMapper(new ObjectMapper())
      .build();
}
 
Example 5
Source File: DockerServiceImplTest.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
DockerServiceImpl dockerService() {
    ClusterConfig config = ClusterConfigImpl.builder()
            .host("172.31.0.12:2375").build();
    AsyncRestTemplate restTemplate = new AsyncRestTemplate();
    RegistryRepository registryRepository = mock(RegistryRepository.class);
    restTemplate.setInterceptors(
            Collections.singletonList(
                    new HttpAuthInterceptor(registryRepository)));
    return DockerServiceImpl.builder()
      .config(config)
      .cluster("test")
      .restTemplate(restTemplate)
      .nodeInfoProvider(mock(NodeInfoProvider.class))
      .eventConsumer(mock(MessageBus.class))
      .objectMapper(new ObjectMapper())
      .build();
}
 
Example 6
Source File: ITTracingAsyncClientHttpRequestInterceptor.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override protected void get(AsyncClientHttpRequestFactory client, String path,
  BiConsumer<Integer, Throwable> callback) {
  AsyncRestTemplate restTemplate = new AsyncRestTemplate(client);
  restTemplate.setInterceptors(Collections.singletonList(interceptor));
  restTemplate.getForEntity(url(path), String.class)
    .addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
      @Override public void onFailure(Throwable throwable) {
        if (throwable instanceof HttpStatusCodeException) {
          callback.accept(((HttpStatusCodeException) throwable).getRawStatusCode(), null);
        } else {
          callback.accept(null, throwable);
        }
      }

      @Override public void onSuccess(ResponseEntity<String> entity) {
        callback.accept(entity.getStatusCodeValue(), null);
      }
    });
}
 
Example 7
Source File: RestTemplatePostProcessor.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public void customize(final AsyncRestTemplate restTemplate) {
    if (restTemplate.getInterceptors().contains(this.metricsClientHttpRequestInterceptor)) {
        return;
    }
    UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
    templateHandler = this.metricsClientHttpRequestInterceptor.createUriTemplateHandler(templateHandler);
    restTemplate.setUriTemplateHandler(templateHandler);
    List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(this.metricsClientHttpRequestInterceptor);
    interceptors.addAll(restTemplate.getInterceptors());
    restTemplate.setInterceptors(interceptors);
}
 
Example 8
Source File: SofaTracerRestTemplateBuilder.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static AsyncRestTemplate buildAsyncRestTemplate() {
    AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
    List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<AsyncClientHttpRequestInterceptor>();
    AsyncRestTemplateRequestInterceptor asyncRestTemplateInterceptor = new AsyncRestTemplateRequestInterceptor(
        getRestTemplateTracer());
    interceptors.add(asyncRestTemplateInterceptor);
    asyncRestTemplate.setInterceptors(interceptors);
    return asyncRestTemplate;
}
 
Example 9
Source File: TraceWebAsyncClientAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
	if (this.restTemplates != null) {
		for (AsyncRestTemplate restTemplate : this.restTemplates) {
			List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>(
					restTemplate.getInterceptors());
			interceptors.add(this.clientInterceptor);
			restTemplate.setInterceptors(interceptors);
		}
	}
}
 
Example 10
Source File: AsyncRestTemplateBenchmarks.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override protected AsyncRestTemplate newClient(HttpTracing httpTracing) {
  OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(ok);
  AsyncRestTemplate result = new AsyncRestTemplate(factory);
  result.setInterceptors(Collections.singletonList(
    TracingAsyncClientHttpRequestInterceptor.create(httpTracing
    )));
  return result;
}
 
Example 11
Source File: ITTracingAsyncClientHttpRequestInterceptor.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override protected void get(AsyncClientHttpRequestFactory client, String pathIncludingQuery) {
  AsyncRestTemplate restTemplate = new AsyncRestTemplate(client);
  restTemplate.setInterceptors(Collections.singletonList(interceptor));
  restTemplate.getForEntity(url(pathIncludingQuery), String.class).completable().join();
}
 
Example 12
Source File: ITTracingAsyncClientHttpRequestInterceptor.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override protected void options(AsyncClientHttpRequestFactory client, String path) {
  AsyncRestTemplate restTemplate = new AsyncRestTemplate(client);
  restTemplate.setInterceptors(Collections.singletonList(interceptor));
  restTemplate.optionsForAllow(url(path)).completable().join();
}
 
Example 13
Source File: ITTracingAsyncClientHttpRequestInterceptor.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override protected void post(AsyncClientHttpRequestFactory client, String uri, String content) {
  AsyncRestTemplate restTemplate = new AsyncRestTemplate(client);
  restTemplate.setInterceptors(Collections.singletonList(interceptor));
  restTemplate.postForEntity(url(uri), RequestEntity.post(URI.create(url(uri))).body(content),
    String.class).completable().join();
}