public void setDebugging(boolean debugging) { List<ClientHttpRequestInterceptor> currentInterceptors = this.restTemplate.getInterceptors(); if(debugging) { if (currentInterceptors == null) { currentInterceptors = new ArrayList<ClientHttpRequestInterceptor>(); } ClientHttpRequestInterceptor interceptor = new ApiClientHttpRequestInterceptor(); currentInterceptors.add(interceptor); this.restTemplate.setInterceptors(currentInterceptors); } else { if (currentInterceptors != null && !currentInterceptors.isEmpty()) { Iterator<ClientHttpRequestInterceptor> iter = currentInterceptors.iterator(); while (iter.hasNext()) { ClientHttpRequestInterceptor interceptor = iter.next(); if (interceptor instanceof ApiClientHttpRequestInterceptor) { iter.remove(); } } this.restTemplate.setInterceptors(currentInterceptors); } } this.debugging = debugging; }
@PostConstruct public void init() { if (this.restTemplates != null) { for (RestTemplate restTemplate : restTemplates) { List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>( restTemplate.getInterceptors()); interceptors.add(this.fescarRestTemplateInterceptor); restTemplate.setInterceptors(interceptors); } } }
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(); interceptors.add(new LoggingRequestInterceptor()); restTemplate.setInterceptors(interceptors);
@Override public void customize(RestTemplate restTemplate) { List<ClientHttpRequestInterceptor> list = new ArrayList<>(restTemplate.getInterceptors()); if(loadBalancerInterceptor!=null) { list.add(0, loadBalancerInterceptor); } if(retryLoadBalancerInterceptor!=null){ list.add(0, retryLoadBalancerInterceptor); } restTemplate.setInterceptors(list); }
/** {@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); } return bean; }
void inject(RestTemplate restTemplate) { if (hasTraceInterceptor(restTemplate)) { return; } List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>( restTemplate.getInterceptors()); interceptors.add(0, this.interceptor); restTemplate.setInterceptors(interceptors); }
@Override protected RestTemplate newClient(HttpTracing httpTracing) { OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(ok); RestTemplate result = new RestTemplate(factory); result.setInterceptors(Collections.singletonList( TracingClientHttpRequestInterceptor.create(httpTracing ))); return result; }
RestTemplate rt = new RestTemplate(); //set interceptors/requestFactory ClientHttpRequestInterceptor ri = new LoggingRequestInterceptor(); List<ClientHttpRequestInterceptor> ris = new ArrayList<ClientHttpRequestInterceptor>(); ris.add(ri); rt.setInterceptors(ris); rt.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
private void init(ApplicationContext ctx) { loadBalancedRestTemplate = new RestTemplate(); SpringClientFactory springClientFactory = springClientFactory(); springClientFactory.setApplicationContext(ctx); loadBalancerClient = new RibbonLoadBalancerClient(springClientFactory); //custom restTemplate LoadBalancerRequestFactory requestFactory = new LoadBalancerRequestFactory(loadBalancerClient, Collections.emptyList()); LoadBalancerInterceptor interceptor = new LoadBalancerInterceptor(loadBalancerClient, requestFactory); List<ClientHttpRequestInterceptor> interceptors = loadBalancedRestTemplate.getInterceptors(); ArrayList<ClientHttpRequestInterceptor> customedInterceptors = new ArrayList<>(interceptors.size() + 1); customedInterceptors.addAll(interceptors); customedInterceptors.add(interceptor); loadBalancedRestTemplate.setInterceptors(customedInterceptors); }
private RestTemplate getSecureRestTemplate(ConfigClientProperties client) { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); if (client.getRequestReadTimeout() < 0) { throw new IllegalStateException("Invalid Value for Read Timeout set."); } requestFactory.setReadTimeout(client.getRequestReadTimeout()); RestTemplate template = new RestTemplate(requestFactory); Map<String, String> headers = new HashMap<>(client.getHeaders()); if (headers.containsKey(AUTHORIZATION)) { headers.remove(AUTHORIZATION); // To avoid redundant addition of header } if (!headers.isEmpty()) { template.setInterceptors(Arrays.<ClientHttpRequestInterceptor> asList( new GenericRequestHeaderInterceptor(headers))); } return template; }
protected RestOperations getRestTemplate() { if (restTemplate == null) { synchronized (this) { if (restTemplate == null) { RestTemplate restTemplate = new RestTemplate(); restTemplate.setErrorHandler(getResponseErrorHandler()); restTemplate.setRequestFactory(requestFactory); restTemplate.setInterceptors(interceptors); this.restTemplate = restTemplate; } } } if (messageConverters == null) { setMessageConverters(new RestTemplate().getMessageConverters()); } return restTemplate; }
@Test // SPR-15066 public void requestInterceptorCanAddExistingHeaderValueWithoutBody() throws Exception { ClientHttpRequestInterceptor interceptor = (request, body, execution) -> { request.getHeaders().add("MyHeader", "MyInterceptorValue"); return execution.execute(request, body); }; template.setInterceptors(Collections.singletonList(interceptor)); HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.add("MyHeader", "MyEntityValue"); HttpEntity<Void> entity = new HttpEntity<>(null, entityHeaders); template.exchange("http://example.com", POST, entity, Void.class); assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue")); verify(response).close(); }
@Test // SPR-15066 public void requestInterceptorCanAddExistingHeaderValueWithBody() throws Exception { ClientHttpRequestInterceptor interceptor = (request, body, execution) -> { request.getHeaders().add("MyHeader", "MyInterceptorValue"); return execution.execute(request, body); }; template.setInterceptors(Collections.singletonList(interceptor)); MediaType contentType = MediaType.TEXT_PLAIN; given(converter.canWrite(String.class, contentType)).willReturn(true); HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(contentType); entityHeaders.add("MyHeader", "MyEntityValue"); HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders); template.exchange("http://example.com", POST, entity, Void.class); assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue")); verify(response).close(); }
@Test // SPR-14694 public void repeatedAccessToResponseViaResource() { Resource resource = new ClassPathResource("ludwig.json", this.getClass()); RestTemplate restTemplate = new RestTemplate(); restTemplate.setInterceptors(Collections.singletonList(new ContentInterceptor(resource))); MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate) .ignoreExpectOrder(true) .bufferContent() // enable repeated reads of response body .build(); mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess(resource, MediaType.APPLICATION_JSON)); restTemplate.getForObject("/composers/{id}", Person.class, 42); mockServer.verify(); }
@Override public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) { super.setInterceptors(interceptors); defaultRestTemplate.setInterceptors(interceptors); }
@Bean @ConditionalOnMissingBean public RestTemplateCustomizer restTemplateCustomizer( final LoadBalancerInterceptor loadBalancerInterceptor) { return restTemplate -> { List<ClientHttpRequestInterceptor> list = new ArrayList<>( restTemplate.getInterceptors()); list.add(loadBalancerInterceptor); restTemplate.setInterceptors(list); }; } }
@Bean @ConditionalOnMissingBean public RestTemplateCustomizer restTemplateCustomizer( final RetryLoadBalancerInterceptor loadBalancerInterceptor) { return restTemplate -> { List<ClientHttpRequestInterceptor> list = new ArrayList<>( restTemplate.getInterceptors()); list.add(loadBalancerInterceptor); restTemplate.setInterceptors(list); }; } }
@Bean(name = "omegaRestTemplate") public RestTemplate omegaRestTemplate(@Autowired(required=false) OmegaContext context) { RestTemplate template = new RestTemplate(); List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors(); interceptors.add(new TransactionClientHttpRequestInterceptor(context)); template.setInterceptors(interceptors); return template; } }
@Override public void customize(RestTemplate restTemplate) { UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler(); templateHandler = this.interceptor.createUriTemplateHandler(templateHandler); restTemplate.setUriTemplateHandler(templateHandler); List<ClientHttpRequestInterceptor> existingInterceptors = restTemplate .getInterceptors(); if (!existingInterceptors.contains(this.interceptor)) { List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(this.interceptor); interceptors.addAll(existingInterceptors); restTemplate.setInterceptors(interceptors); } }
@Override public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) { super.setInterceptors(interceptors); defaultRestTemplate.setInterceptors(interceptors); }