org.springframework.web.util.DefaultUriTemplateHandler Java Examples

The following examples show how to use org.springframework.web.util.DefaultUriTemplateHandler. 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: UriTemplateHandlerTest.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
@Test
public void uriTemplateHandlerTest() {
    UriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
    Map<String, String> uriVariables = new HashMap<>();
    uriVariables.put("orderNo", "12345");
    URI expand = uriTemplateHandler.expand("https://chaojihao.com/user/order/detail?orderno={orderNo}", uriVariables);
    System.out.println(expand.toString());
    assertThat(expand.toString()).isEqualTo("https://chaojihao.com/user/order/detail?orderno=12345");
}
 
Example #2
Source File: RestTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #3
Source File: SmartlingClientConfiguration.java    From mojito with Apache License 2.0 5 votes vote down vote up
public OAuth2RestTemplate smartlingRestTemplate() {
    OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(smartling(), new DefaultOAuth2ClientContext());

    RestTemplateUtils restTemplateUtils = new RestTemplateUtils();
    restTemplateUtils.enableFeature(oAuth2RestTemplate, DeserializationFeature.UNWRAP_ROOT_VALUE);

    AccessTokenProviderChain accessTokenProviderChain = new AccessTokenProviderChain(Arrays.asList(
            new SmartlingAuthorizationCodeAccessTokenProvider())
    );
    oAuth2RestTemplate.setAccessTokenProvider(accessTokenProviderChain);

    DefaultUriTemplateHandler defaultUriTemplateHandler = new DefaultUriTemplateHandler();
    defaultUriTemplateHandler.setBaseUrl(baseUri);

    oAuth2RestTemplate.setUriTemplateHandler(defaultUriTemplateHandler);

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

    return oAuth2RestTemplate;
}
 
Example #4
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 #5
Source File: UriTemplateHandlerTest.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
@Test
public void uriTemplateHandlerWithTwoQueryStringTest() {
    UriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
    Map<String, String> uriVariables = new HashMap<>();
    uriVariables.put("orderNo", "12345");
    URI expand = uriTemplateHandler.expand("https://chaojihao.com/user/order/detail?orderno={orderNo}&logtoSensor=1", uriVariables);
    System.out.println(expand.toString());
    assertThat(expand.toString()).isEqualTo("https://chaojihao.com/user/order/detail?orderno=12345&logtoSensor=1");
}