private RestTemplateBuilder(boolean detectRequestFactory, String rootUri, Set<HttpMessageConverter<?>> messageConverters, ClientHttpRequestFactory requestFactory, UriTemplateHandler uriTemplateHandler, ResponseErrorHandler errorHandler, BasicAuthorizationInterceptor basicAuthorization, Set<RestTemplateCustomizer> restTemplateCustomizers, Set<RequestFactoryCustomizer> requestFactoryCustomizers) { super(); this.detectRequestFactory = detectRequestFactory; this.rootUri = rootUri; this.messageConverters = messageConverters; this.requestFactory = requestFactory; this.uriTemplateHandler = uriTemplateHandler; this.errorHandler = errorHandler; this.basicAuthorization = basicAuthorization; this.restTemplateCustomizers = restTemplateCustomizers; this.requestFactoryCustomizers = requestFactoryCustomizers; }
private RestTemplateBuilder(boolean detectRequestFactory, String rootUri, Set<HttpMessageConverter<?>> messageConverters, ClientHttpRequestFactory requestFactory, UriTemplateHandler uriTemplateHandler, ResponseErrorHandler errorHandler, BasicAuthorizationInterceptor basicAuthorization, Set<RestTemplateCustomizer> customizers) { super(); this.detectRequestFactory = detectRequestFactory; this.rootUri = rootUri; this.messageConverters = messageConverters; this.requestFactory = requestFactory; this.uriTemplateHandler = uriTemplateHandler; this.errorHandler = errorHandler; this.basicAuthorization = basicAuthorization; this.customizers = customizers; }
@Test public void dotNotSetUriTemplateHandlerWithUnderlying() { UriTemplateHandler uriTemplateHandler = mock(UriTemplateHandler.class); wrapper.setUriTemplateHandler(uriTemplateHandler); assertThat(wrapper.getUriTemplateHandler(), is(uriTemplateHandler)); assertThat(wrapper.defaultRestTemplate.getUriTemplateHandler(), is(uriTemplateHandler)); verify(underlying, never()).setUriTemplateHandler(uriTemplateHandler); }
@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); }
/** * Create a new {@link RootUriTemplateHandler} instance. * @param rootUri the root URI to be used to prefix relative URLs * @param handler the delegate handler */ public RootUriTemplateHandler(String rootUri, UriTemplateHandler handler) { Assert.notNull(rootUri, "RootUri must not be null"); Assert.notNull(handler, "Handler must not be null"); this.rootUri = rootUri; this.handler = handler; }
@Test public void rootUriShouldApplyAfterUriTemplateHandler() throws Exception { UriTemplateHandler uriTemplateHandler = mock(UriTemplateHandler.class); RestTemplate template = this.builder.uriTemplateHandler(uriTemplateHandler) .rootUri("http://example.com").build(); UriTemplateHandler handler = template.getUriTemplateHandler(); handler.expand("/hello"); assertThat(handler).isInstanceOf(RootUriTemplateHandler.class); verify(uriTemplateHandler).expand("http://example.com/hello"); }
@Test public void uriTemplateHandlerShouldApply() throws Exception { UriTemplateHandler uriTemplateHandler = mock(UriTemplateHandler.class); RestTemplate template = this.builder.uriTemplateHandler(uriTemplateHandler) .build(); assertThat(template.getUriTemplateHandler()).isSameAs(uriTemplateHandler); }
/** * Return {@link RequestExpectationManager} to be used for binding with the specified * {@link RestTemplate}. If the {@link RestTemplate} is using a * {@link RootUriTemplateHandler} then a {@link RootUriRequestExpectationManager} is * returned, otherwise the source manager is returned unchanged. * @param restTemplate the source REST template * @param expectationManager the source {@link RequestExpectationManager} * @return a {@link RequestExpectationManager} to be bound to the template */ public static RequestExpectationManager forRestTemplate(RestTemplate restTemplate, RequestExpectationManager expectationManager) { Assert.notNull(restTemplate, "RestTemplate must not be null"); UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler(); if (templateHandler instanceof RootUriTemplateHandler) { return new RootUriRequestExpectationManager( ((RootUriTemplateHandler) templateHandler).getRootUri(), expectationManager); } return expectationManager; }
/** * Create a new {@link RootUriTemplateHandler} instance. * @param rootUri the root URI to used to prefix relative URLs * @param handler the delegate handler */ public RootUriTemplateHandler(String rootUri, UriTemplateHandler handler) { Assert.notNull(rootUri, "RootUri must not be null"); Assert.notNull(handler, "Handler must not be null"); this.rootUri = rootUri; this.handler = handler; }
/** * Return {@link RequestExpectationManager} to be used for binding with the specified * {@link RestTemplate}. If the {@link RestTemplate} is using a * {@link RootUriTemplateHandler} then a {@link RootUriRequestExpectationManager} is * returned, otherwise the source manager is returned unchanged. * @param restTemplate the source reset template * @param expectationManager the source {@link RequestExpectationManager} * @return a {@link RequestExpectationManager} to be bound to the template */ public static RequestExpectationManager forRestTemplate(RestTemplate restTemplate, RequestExpectationManager expectationManager) { Assert.notNull(restTemplate, "RestTemplate must not be null"); UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler(); if (templateHandler instanceof RootUriTemplateHandler) { return new RootUriRequestExpectationManager( ((RootUriTemplateHandler) templateHandler).getRootUri(), expectationManager); } return expectationManager; }
@Override public void setUriTemplateHandler(UriTemplateHandler handler) { super.setUriTemplateHandler(handler); defaultRestTemplate.setUriTemplateHandler(handler); }
public void setUriTemplateHandler(UriTemplateHandler handler) { restTemplate.setUriTemplateHandler(handler); }
public UriTemplateHandler getUriTemplateHandler() { return restTemplate.getUriTemplateHandler(); }
/** * Return the configured URI template handler. */ public UriTemplateHandler getUriTemplateHandler() { return this.uriTemplateHandler; }
/** * Return the configured URI template handler. */ public UriTemplateHandler getUriTemplateHandler() { return this.syncTemplate.getUriTemplateHandler(); }
protected RootUriTemplateHandler(UriTemplateHandler handler) { this.rootUri = null; this.handler = handler; }
/** * Set a custom {@link UriTemplateHandler} for expanding URI templates. * <p>By default, RestTemplate uses {@link DefaultUriTemplateHandler}. * @param handler the URI template handler to use */ public void setUriTemplateHandler(UriTemplateHandler handler) { Assert.notNull(handler, "UriTemplateHandler must not be null"); this.uriTemplateHandler = handler; }
/** * Set a custom {@link UriTemplateHandler} for expanding URI templates. * <p>By default, RestTemplate uses {@link DefaultUriTemplateHandler}. * @param handler the URI template handler to use */ public void setUriTemplateHandler(UriTemplateHandler handler) { this.syncTemplate.setUriTemplateHandler(handler); }
/** * Configure the {@link UriTemplateHandler} to use to expand URI templates. By default * the {@link DefaultUriTemplateHandler} is used which relies on Spring's URI template * support and exposes several useful properties that customize its behavior for * encoding and for prepending a common base URL. An alternative implementation may be * used to plug an external URI template library. * @param handler the URI template handler to use */ public void setUriTemplateHandler(UriTemplateHandler handler) { this.restTemplate.setUriTemplateHandler(handler); }