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

The following examples show how to use org.springframework.web.client.RestTemplate#setUriTemplateHandler() . 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 check out the related API usage on the sidebar.
Example 1
Source File: VaultClientsUnitTests.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: AviRestUtils.java    From sdk with 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 3
Source File: PcfAuthenticationUnitTests.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 4
Source File: AppRoleAuthenticationUnitTests.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 5
Source File: AzureMsiAuthenticationUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 6
Source File: EvolveConfiguration.java    From mojito with 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 7
Source File: VaultClientsUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: KubernetesAuthenticationUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 9
Source File: AppIdAuthenticationUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {

	RestTemplate restTemplate = VaultClients.createRestTemplate();
	restTemplate.setUriTemplateHandler(new PrefixAwareUriTemplateHandler());
	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 10
Source File: AwsEc2AuthenticationUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 11
Source File: ClientCertificateAuthenticationUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 12
Source File: GcpIamAuthenticationUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 13
Source File: AwsIamAuthenticationUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 14
Source File: RestTemplatePostProcessor.java    From summerframework with Apache License 2.0 5 votes vote down vote up
@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 15
Source File: AuthenticationStepsExecutorUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 16
Source File: LoginTokenAdapterUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() throws Exception {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 17
Source File: CubbyholeAuthenticationUnitTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void before() {

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

	this.mockRest = MockRestServiceServer.createServer(restTemplate);
	this.restTemplate = restTemplate;
}
 
Example 18
Source File: CredHubRestTemplateFactory.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
/**
 * 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 19
Source File: SpringRESTClientConnector.java    From egeria with Apache License 2.0 5 votes vote down vote up
/**
 * 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 20
Source File: VaultClients.java    From spring-vault with 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;
}