private String registerRestTemplate(final String id, final String factoryId, final String convertersId, @Nullable final String baseUrl) { return registry.register(id, RestTemplate.class, () -> { LOG.debug("Client [{}]: Registering RestTemplate", id); final BeanDefinitionBuilder restTemplate = genericBeanDefinition(RestTemplate.class); restTemplate.addConstructorArgReference(factoryId); final DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); handler.setBaseUrl(baseUrl); restTemplate.addPropertyValue("uriTemplateHandler", handler); final AbstractBeanDefinition converters = BeanDefinitionBuilder.genericBeanDefinition() .setFactoryMethod("getConverters") .getBeanDefinition(); converters.setFactoryBeanName(convertersId); restTemplate.addPropertyValue("messageConverters", converters); return restTemplate; }); }
private String registerAsyncRestTemplate(final String id, final String factoryId, final String convertersId, @Nullable final String baseUrl) { return registry.register(id, AsyncRestTemplate.class, () -> { LOG.debug("Client [{}]: Registering AsyncRestTemplate", id); final BeanDefinitionBuilder restTemplate = genericBeanDefinition(AsyncRestTemplate.class); restTemplate.addConstructorArgReference(factoryId); final DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); handler.setBaseUrl(baseUrl); restTemplate.addPropertyValue("uriTemplateHandler", handler); final AbstractBeanDefinition converters = BeanDefinitionBuilder.genericBeanDefinition() .setFactoryMethod("getConverters") .getBeanDefinition(); converters.setFactoryBeanName(convertersId); restTemplate.addPropertyValue("messageConverters", converters); return restTemplate; }); }
private void registerTemplate(final String id, final Class<?> type, final String factoryId, @Nullable final String baseUrl, final BeanDefinition converters, final List<BeanMetadataElement> plugins) { registry.registerIfAbsent(id, type, () -> { log.debug("Client [{}]: Registering AsyncRestTemplate", id); final DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); handler.setBaseUrl(baseUrl); final BeanDefinitionBuilder template = genericBeanDefinition(type); template.addConstructorArgReference(factoryId); template.addPropertyValue("uriTemplateHandler", handler); template.addPropertyValue("messageConverters", converters); template.addPropertyValue("interceptors", plugins.stream() .map(plugin -> genericBeanDefinition(PluginInterceptors.class) .setFactoryMethod("adapt") .addConstructorArgValue(plugin) .getBeanDefinition()) .collect(toCollection(Registry::list))); return template; }); }
@Test public void getForObjectWithCustomUriTemplateHandler() throws Exception { DefaultUriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler(); uriTemplateHandler.setParsePath(true); template.setUriTemplateHandler(uriTemplateHandler); URI expectedUri = new URI("http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150"); given(requestFactory.createRequest(expectedUri, HttpMethod.GET)).willReturn(request); given(request.getHeaders()).willReturn(new HttpHeaders()); given(request.execute()).willReturn(response); given(errorHandler.hasError(response)).willReturn(false); given(response.getStatusCode()).willReturn(HttpStatus.OK); given(response.getHeaders()).willReturn(new HttpHeaders()); given(response.getBody()).willReturn(null); Map<String, String> uriVariables = new HashMap<String, String>(2); uriVariables.put("hotel", "1"); uriVariables.put("publicpath", "pics/logo.png"); uriVariables.put("scale", "150x150"); String url = "http://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}"; template.getForObject(url, String.class, uriVariables); verify(response).close(); }
@PostConstruct public void init() { DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); handler.setBaseUrl(url + ":" + port); handler.setParsePath(true); client = new TestRestTemplate(); client.setUriTemplateHandler(handler); }
@Bean public RestTemplate exampleRestTemplate(final ClientHttpRequestFactory requestFactory, final ClientHttpMessageConverters converters) { final RestTemplate template = new RestTemplate(); final DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); handler.setBaseUrl("https://www.example.com"); template.setUriTemplateHandler(handler); template.setRequestFactory(requestFactory); template.setMessageConverters(converters.getConverters()); return template; }
@Bean public AsyncRestTemplate exampleAsyncRestTemplate(final AsyncClientHttpRequestFactory requestFactory, final ClientHttpMessageConverters converters) { final AsyncRestTemplate template = new AsyncRestTemplate(); final DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); handler.setBaseUrl("https://www.example.com"); template.setUriTemplateHandler(handler); template.setAsyncRequestFactory(requestFactory); template.setMessageConverters(converters.getConverters()); return template; }
private Map<String, ?> defaultUriVariablesOf(RestTemplate wrapper1) { return ((DefaultUriTemplateHandler) wrapper1.getUriTemplateHandler()).getDefaultUriVariables(); }
private static DefaultUriTemplateHandler createUriTemplateHandler( VaultEndpointProvider endpointProvider) { return new PrefixAwareUriTemplateHandler(endpointProvider); }
public LocalHostUriTemplateHandler(Environment environment) { super(new DefaultUriTemplateHandler()); Assert.notNull(environment, "Environment must not be null"); this.environment = environment; }
/** * Create a {@link UriTemplateHandler} that prefixes all {@link RestTemplate} calls * with the configured {@literal baseUri}. * * @param baseUri the base URI for the CredHub server * @return a configured {@link UriTemplateHandler} */ private static DefaultUriTemplateHandler createUriTemplateHandler(String baseUri) { DefaultUriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler(); uriTemplateHandler.setBaseUrl(baseUri); return uriTemplateHandler; }
/** * Create a new {@link RootUriTemplateHandler} instance. * @param rootUri the root URI to be used to prefix relative URLs */ public RootUriTemplateHandler(String rootUri) { this(rootUri, new DefaultUriTemplateHandler()); }
/** * Create a new {@link RootUriTemplateHandler} instance. * @param rootUri the root URI to used to prefix relative URLs */ public RootUriTemplateHandler(String rootUri) { this(rootUri, new DefaultUriTemplateHandler()); }