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

The following examples show how to use org.springframework.web.client.RestTemplate#setUriTemplateHandler() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: sdk   File: AviRestUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static RestTemplate getRestTemplate(AviCredentials creds) {
	RestTemplate restTemplate = null;
	if (creds != null) {
		try {
			restTemplate = getInitializedRestTemplate(creds);
			restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(getControllerURL(creds) + API_PREFIX));
			List<ClientHttpRequestInterceptor> interceptors = 
					Collections.<ClientHttpRequestInterceptor>singletonList(
							new AviAuthorizationInterceptor(creds));
			restTemplate.setInterceptors(interceptors);
			restTemplate.setMessageConverters(getMessageConverters(restTemplate));
			return restTemplate;
		} catch (Exception e) {
			LOGGER.severe("Exception during rest template initialization");

		}
	}
	return restTemplate;
}
 
Example 2
@Test
void shouldAllowNamespaceOverride() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.getInterceptors().add(VaultClients.createNamespaceInterceptor("foo/bar"));
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	MockRestServiceServer mockRest = MockRestServiceServer.createServer(restTemplate);

	mockRest.expect(requestTo("/auth/foo")).andExpect(method(HttpMethod.GET))
			.andExpect(header(VaultHttpHeaders.VAULT_NAMESPACE, "baz")).andRespond(withSuccess());

	HttpHeaders headers = new HttpHeaders();
	headers.add(VaultHttpHeaders.VAULT_NAMESPACE, "baz");

	restTemplate.exchange("/auth/foo", HttpMethod.GET, new HttpEntity<>(headers), String.class);
}
 
Example 3
@Override
public void customize(RestTemplate restTemplate) {
    if (restTemplate.getInterceptors().contains(this.metricsClientHttpRequestInterceptor)) {
        return;
    }
    UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
    templateHandler = this.metricsClientHttpRequestInterceptor.createUriTemplateHandler(templateHandler);
    restTemplate.setUriTemplateHandler(templateHandler);
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(this.metricsClientHttpRequestInterceptor);
    interceptors.addAll(restTemplate.getInterceptors());
    restTemplate.setInterceptors(interceptors);
}
 
Example 4
/**
 * Default constructor
 */
public SpringRESTClientConnector() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException
{
    super();

    /*
     * Rather than creating a RestTemplate directly, the RestTemplateBuilder is used so that the
     * uriTemplateHandler can be specified. The URI encoding is set to VALUES_ONLY so that the
     * '+' character, which is used in queryParameters conveying searchCriteria, which can be a
     * regex, is encoded as '+' and not converted to a space character.
     * Prior to this change a regex containing a '+' character would be split into two space
     * separated words. For example, the regex "name_0+7" (which would match name_07, name_007,
     * name_0007, etc) would be sent to the server as "name_0 7".
     */
    DefaultUriBuilderFactory builderFactory = new DefaultUriBuilderFactory();
    builderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);


    /* TODO: Disable SSL cert verification -- for now */
    HttpsURLConnection.setDefaultHostnameVerifier(bypassVerifier);
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, INSECURE_MANAGER, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    restTemplate = new RestTemplate();

    restTemplate.setUriTemplateHandler(builderFactory);

    /* Ensure that the REST template always uses UTF-8 */
    List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
    converters.removeIf(httpMessageConverter -> httpMessageConverter instanceof StringHttpMessageConverter);
    converters.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));

}
 
Example 5
/**
 * 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
 */
private static void configureRestTemplate(RestTemplate restTemplate, String baseUri,
		ClientHttpRequestFactory clientHttpRequestFactory) {
	restTemplate.setRequestFactory(clientHttpRequestFactory);
	restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(baseUri));
	restTemplate.getInterceptors().add(new CredHubRequestInterceptor());
	restTemplate.setMessageConverters(
			Arrays.asList(new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter(),
					new MappingJackson2HttpMessageConverter(JsonUtils.buildObjectMapper())));
}
 
Example 6
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 7
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 8
@BeforeEach
void before() {

	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 9
@BeforeEach
void before() throws Exception {

	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 10
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 11
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 12
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 13
@BeforeEach
void before() {

	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 14
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 15
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 16
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());
	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 17
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 18
@Test
void shouldApplyNamespace() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.getInterceptors().add(VaultClients.createNamespaceInterceptor("foo/bar"));
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());

	MockRestServiceServer mockRest = MockRestServiceServer.createServer(restTemplate);

	mockRest.expect(requestTo("/auth/foo")).andExpect(method(HttpMethod.GET))
			.andExpect(header(VaultHttpHeaders.VAULT_NAMESPACE, "foo/bar")).andRespond(withSuccess());

	restTemplate.getForEntity("/auth/foo", String.class);
}
 
Example 19
Source Project: mojito   File: EvolveConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
RestTemplate evolveRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    DefaultUriTemplateHandler defaultUriTemplateHandler = new DefaultUriTemplateHandler();
    defaultUriTemplateHandler.setBaseUrl(evolveConfigurationProperties.getUrl());
    restTemplate.setUriTemplateHandler(defaultUriTemplateHandler);

    restTemplate.getInterceptors().add(new ClientHttpRequestInterceptor() {
        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            request.getHeaders().add(HttpHeaders.AUTHORIZATION, evolveConfigurationProperties.getToken());
            return execution.execute(request, body);
        }
    });

    restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
            try {
                super.handleError(response);
            } catch (HttpServerErrorException | HttpClientErrorException e) {
                resttemplateLogger.debug(e.getResponseBodyAsString());
                throw e;
            }
        }
    });

    return restTemplate;
}
 
Example 20
Source Project: spring-vault   File: VaultClients.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Create a {@link RestTemplate} configured with {@link VaultEndpointProvider} and
 * {@link ClientHttpRequestFactory}. The template accepts relative URIs without a
 * leading slash that are expanded to use {@link VaultEndpoint}. {@link RestTemplate}
 * is configured with a {@link ClientHttpRequestInterceptor} to enforce serialization
 * to a byte array prior continuing the request. Eager serialization leads to a known
 * request body size that is required to send a
 * {@link org.springframework.http.HttpHeaders#CONTENT_LENGTH} request header.
 * Otherwise, Vault will deny body processing.
 * <p>
 * Requires Jackson 2 for Object-to-JSON mapping.
 * @param endpointProvider must not be {@literal null}.
 * @param requestFactory must not be {@literal null}.
 * @return the {@link RestTemplate}.
 * @see org.springframework.http.client.Netty4ClientHttpRequestFactory
 * @see MappingJackson2HttpMessageConverter
 * @since 1.1
 */
public static RestTemplate createRestTemplate(VaultEndpointProvider endpointProvider,
		ClientHttpRequestFactory requestFactory) {

	RestTemplate restTemplate = createRestTemplate();

	restTemplate.setRequestFactory(requestFactory);
	restTemplate.setUriTemplateHandler(createUriBuilderFactory(endpointProvider));

	return restTemplate;
}