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

The following are Jave code examples for showing how to use setInterceptors() of the org.springframework.web.client.RestTemplate class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: EasyTransaction   File: RestRibbonEasyTransRpcConsumerImpl.java   Source Code and License Vote up 7 votes
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);
}
 
Example 2
Project: xm-uaa   File: TemplateUtil.java   Source Code and License Vote up 6 votes
public static RestTemplate getTemplate(ClientHttpRequestInterceptor interceptor) {
    RestTemplate restTemplate = new RestTemplate();

    List<ClientHttpRequestInterceptor> ris = new ArrayList<>();
    ris.add(interceptor);
    restTemplate.setInterceptors(ris);
    SimpleClientHttpRequestFactory httpFactory = new SimpleClientHttpRequestFactory();
    httpFactory.setOutputStreaming(false);
    restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(httpFactory));
    restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
    return restTemplate;
}
 
Example 3
Project: java-spring-web   File: TracingRestTemplateTest.java   Source Code and License Vote up 6 votes
public TracingRestTemplateTest() {
    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.setInterceptors(Collections.<ClientHttpRequestInterceptor>singletonList(
            new TracingRestTemplateInterceptor(mockTracer)));

    client = new Client<RestTemplate>() {
        @Override
        public <T> ResponseEntity<T> getForEntity(String url, Class<T> clazz) {
            return restTemplate.getForEntity(url, clazz);
        }

        @Override
        public RestTemplate template() {
            return restTemplate;
        }
    };

    mockServer = MockRestServiceServer.bindTo(client.template()).build();
}
 
Example 4
Project: loom   File: SelfRevokingKeystoneV3Provider.java   Source Code and License Vote up 6 votes
@Override
@SuppressWarnings("checkstyle:emptyblock")
public void run() {
    LOG.warn("****** SelfRevokingProvider Thread going to sleep for 15 secs");
    try {
        Thread.sleep(THREAD_SLEEP);
    } catch (InterruptedException ie) {
    }
    LOG.warn("****** SelfRevokingProvider finished sleeping, revoking all tokens");
    try {
        RestTemplate rt = restManager.getRestTemplate("keystone-revoke");
        rt.setInterceptors(Collections.singletonList(this));
        LOG.warn("****** deleting " + keystoneTokenURI.toString());
        for (TokenHolder tokenHolder : TokenManager.getInstance().getAllTokenHolders()) {
            currentToken = tokenHolder.getUnscoped();
            LOG.warn("****** deleting token " + currentToken + "[" + keystoneTokenURI.toString() + "]");
            rt.delete(keystoneTokenURI);
        }
    } catch (HttpClientErrorException ex) {
        LOG.error("****** token revocation refused for token " + currentToken);
    }
    LOG.warn("****** all tokens have been revoked - SelfRevokingProvider Thread is exiting...");
}
 
Example 5
Project: incubator-servicecomb-saga   File: RestTemplateConfig.java   Source Code and License Vote up 5 votes
@Bean
public RestTemplate restTemplate(OmegaContext context) {
  RestTemplate template = new RestTemplate();
  List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors();
  interceptors.add(new TransactionClientHttpRequestInterceptor(context));
  template.setInterceptors(interceptors);
  return template;
}
 
Example 6
Project: java-spring-web   File: RestTemplateAutoConfiguration.java   Source Code and License Vote up 5 votes
private void registerTracingInterceptor(RestTemplate restTemplate) {
    List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();

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

    log.info("Adding " + TracingRestTemplateInterceptor.class.getSimpleName() + " to rest template");
    interceptors = new ArrayList<>(interceptors);
    interceptors.add(new TracingRestTemplateInterceptor(tracer));
    restTemplate.setInterceptors(interceptors);
}
 
Example 7
Project: micrometer   File: MetricsRestTemplateCustomizer.java   Source Code and License Vote up 5 votes
@Override
public void customize(RestTemplate restTemplate) {
    UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
    templateHandler = this.interceptor.createUriTemplateHandler(templateHandler);
    restTemplate.setUriTemplateHandler(templateHandler);
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(this.interceptor);
    interceptors.addAll(restTemplate.getInterceptors());
    restTemplate.setInterceptors(interceptors);
}
 
Example 8
Project: xm-ms-entity   File: TemplateUtil.java   Source Code and License Vote up 5 votes
public static RestTemplate getTemplate(ClientHttpRequestInterceptor interceptor) {
    RestTemplate restTemplate = new RestTemplate();

    List<ClientHttpRequestInterceptor> ris = new ArrayList<>();
    ris.add(interceptor);
    restTemplate.setInterceptors(ris);
    SimpleClientHttpRequestFactory httpFactory = new SimpleClientHttpRequestFactory();
    httpFactory.setOutputStreaming(false);
    restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(httpFactory));
    restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
    return restTemplate;
}
 
Example 9
Project: twitch4j   File: RestClient.java   Source Code and License Vote up 5 votes
/**
 * Gets a Rest Template.
 *
 * @return A RestTemplate for rest requests.
 */
public RestTemplate getRestTemplate() {
	RestTemplate restTemplate = new RestTemplate();

	// Request Interceptors
	restTemplate.setInterceptors(new ArrayList<ClientHttpRequestInterceptor>());
	restTemplate.getInterceptors().addAll(getRestInterceptors());

	// Default Error Handler
	restTemplate.setErrorHandler(new RestErrorHandler());

	return restTemplate;
}
 
Example 10
Project: keti   File: PolicyEvaluationWithAttributeConnectorIT.java   Source Code and License Vote up 5 votes
private void setRestTemplateInterceptor(final RestTemplate restTemplate) {
    restTemplate.setInterceptors(Collections.singletonList(
            new TraceRestTemplateInterceptor(this.tracer, new ZipkinHttpSpanInjector(),
                    new HttpTraceKeysInjector(this.tracer, new TraceKeys()))));
}
 
Example 11
Project: spring-credhub   File: CredHubClient.java   Source Code and License Vote up 5 votes
/**
 * Configure a {@link RestTemplate} for communication with a CredHub server.
 * @param restTemplate an existing {@link RestTemplate} to configure
 * @param baseUri the base URI for the CredHub server
 * @param clientHttpRequestFactory the {@link ClientHttpRequestFactory} to use when
 * creating new connections
 */
public static void configureRestTemplate(RestTemplate restTemplate, String baseUri,
		ClientHttpRequestFactory clientHttpRequestFactory) {
	restTemplate.setRequestFactory(clientHttpRequestFactory);
	restTemplate.setUriTemplateHandler(createUriTemplateHandler(baseUri));
	restTemplate.setMessageConverters(createMessageConverters());
	restTemplate.setInterceptors(createInterceptors());
}
 
Example 12
Project: java-spring-web   File: AbstractTracingClientTest.java   Source Code and License Vote up 4 votes
@Test
public void testErrorUnknownHostException() {
    String url = "http://nonexisting.example.com";
    try {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setInterceptors(Collections.<ClientHttpRequestInterceptor>singletonList(
                new TracingRestTemplateInterceptor(mockTracer)));
        restTemplate.getForEntity(url, String.class);
    } catch (ResourceAccessException ex) {
        //ok UnknownHostException
    }

    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    Assert.assertEquals(1, mockSpans.size());

    MockSpan mockSpan = mockSpans.get(0);
    Assert.assertEquals("GET", mockSpan.operationName());
    Assert.assertEquals(5, mockSpan.tags().size());
    Assert.assertEquals(RestTemplateSpanDecorator.StandardTags.COMPONENT_NAME,
            mockSpan.tags().get(Tags.COMPONENT.getKey()));
    Assert.assertEquals(Tags.SPAN_KIND_CLIENT, mockSpan.tags().get(Tags.SPAN_KIND.getKey()));
    Assert.assertEquals("GET", mockSpan.tags().get(Tags.HTTP_METHOD.getKey()));
    Assert.assertEquals(url, mockSpan.tags().get(Tags.HTTP_URL.getKey()));
    Assert.assertEquals(Boolean.TRUE, mockSpan.tags().get(Tags.ERROR.getKey()));

    Assert.assertEquals(1, mockSpan.logEntries().size());
    Assert.assertEquals(2, mockSpan.logEntries().get(0).fields().size());
    Assert.assertEquals(Tags.ERROR.getKey(), mockSpan.logEntries().get(0).fields().get("event"));
    Assert.assertNotNull(mockSpan.logEntries().get(0).fields().get("error.object"));
}
 
Example 13
Project: loom   File: KeystoneCollector.java   Source Code and License Vote up 4 votes
public RestTemplate getRestTemplateWithToken() {
    RestTemplate rt = restManager.getRestTemplate("keystone-data");
    rt.setInterceptors(Collections.singletonList(this));
    return rt;
}
 
Example 14
Project: loom   File: OpenstackApi.java   Source Code and License Vote up 4 votes
/**
 * Get a RestTemplate with an unscoped token (via the interceptor).
 *
 * @return the rest template
 */
public RestTemplate getRestTemplateWithUnscopedToken() {
    RestTemplate rt = restManager.getRestTemplate("keystone-data");
    rt.setInterceptors(Collections.singletonList(new UnscopedClientHttpRequestInterceptor(tokenManager)));
    return rt;
}
 
Example 15
Project: loom   File: OpenstackApi.java   Source Code and License Vote up 4 votes
/**
 * Get a RestTemplate with an UnscopedAndSubjectToken token (via the interceptor).
 *
 * @return the rest template
 */
public RestTemplate getRestTemplateWithUnscopedAndSubjectToken() {
    RestTemplate rt = restManager.getRestTemplate("keystone-data");
    rt.setInterceptors(Collections.singletonList(new UnscopedAndSubjectClientHttpRequestInterceptor(tokenManager)));
    return rt;
}
 
Example 16
Project: loom   File: OpenstackApi.java   Source Code and License Vote up 2 votes
/**
 * Get a RestTemplate with an scoped token (via the interceptor).
 *
 * @param projectId the project id
 * @return the rest template
 */
public RestTemplate getRestTemplateWithScopedToken(final String projectId) {
    RestTemplate rt = restManager.getRestTemplate("scoped");
    rt.setInterceptors(Collections.singletonList(new ScopedClientHttpRequestInterceptor(projectId, tokenManager)));
    return rt;
}