@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); } } }
@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; }
@Test public void getAndInterceptError() throws Exception { RequestInterceptor interceptor = new RequestInterceptor(); template.setInterceptors(Collections.singletonList(interceptor)); template.getForEntity(baseUrl + "/status/notfound", String.class); interceptor.latch.await(5, TimeUnit.SECONDS); assertNotNull(interceptor.response); assertEquals(HttpStatus.NOT_FOUND, interceptor.response.getStatusCode()); assertNull(interceptor.exception); }
@Test public void getAndInterceptResponse() throws Exception { RequestInterceptor interceptor = new RequestInterceptor(); template.setInterceptors(Collections.singletonList(interceptor)); ListenableFuture<ResponseEntity<String>> future = template.getForEntity(baseUrl + "/get", String.class); interceptor.latch.await(5, TimeUnit.SECONDS); assertNotNull(interceptor.response); assertEquals(HttpStatus.OK, interceptor.response.getStatusCode()); assertNull(interceptor.exception); assertEquals(helloWorld, future.get().getBody()); }
@Override public void customize(AsyncRestTemplate restTemplate) { List<AsyncClientHttpRequestInterceptor> list = new ArrayList<>( restTemplate.getInterceptors()); list.add(loadBalancerInterceptor); restTemplate.setInterceptors(list); } };
@Override public void customize(AsyncRestTemplate restTemplate) { List<AsyncClientHttpRequestInterceptor> list = new ArrayList<>( restTemplate.getInterceptors()); list.add(loadBalancerInterceptor); restTemplate.setInterceptors(list); } };
@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); } } }
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; }
public void customize(final AsyncRestTemplate restTemplate) { UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler(); templateHandler = this.interceptor.createUriTemplateHandler(templateHandler); restTemplate.setUriTemplateHandler(templateHandler); List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(this.interceptor); interceptors.addAll(restTemplate.getInterceptors()); restTemplate.setInterceptors(interceptors); } }
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; }