List of usage examples for org.springframework.web.client RestTemplate postForLocation
@Override @Nullable public URI postForLocation(String url, @Nullable Object request, Map<String, ?> uriVariables) throws RestClientException
From source file:com.hatta.consumer.App.java
private static void createCustomer(User user, AuthTokenInfo tokenInfo) { Assert.notNull(tokenInfo, "Authenticate first please......"); RestTemplate restTemplate = new RestTemplate(); System.out.println("Create a customer: " + user.getName()); HttpEntity<Object> request = new HttpEntity<Object>(user, getHeaders()); restTemplate.postForLocation( REST_SERVICE_URI + "/api/user" + QPM_ACCESS_TOKEN + tokenInfo.getAccess_token(), request, User.class); }
From source file:org.springframework.web.client.interceptors.ZeroLeggedOAuthInterceptorTest.java
@Test public void testInterceptor() throws Exception { final String url = "http://www.test.com/lrs?param1=val1¶m2=val2"; final String data = "test"; final String id = "test"; final String realm = "realm"; final String consumerKey = "consumerKey"; final String secretKey = "secretKey"; PropertyResolver resolver = mock(PropertyResolver.class); when(resolver.getProperty(eq("org.jasig.rest.interceptor.oauth." + id + ".realm"))).thenReturn(realm); when(resolver.getProperty(eq("org.jasig.rest.interceptor.oauth." + id + ".consumerKey"))) .thenReturn(consumerKey);// w ww . ja v a 2s . com when(resolver.getProperty(eq("org.jasig.rest.interceptor.oauth." + id + ".secretKey"))) .thenReturn(secretKey); // holder for the headers... HttpHeaders headers = new HttpHeaders(); // Mock guts of RestTemplate so no need to actually hit the web... ClientHttpResponse resp = mock(ClientHttpResponse.class); when(resp.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); when(resp.getHeaders()).thenReturn(new HttpHeaders()); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ClientHttpRequest client = mock(ClientHttpRequest.class); when(client.getHeaders()).thenReturn(headers); when(client.getBody()).thenReturn(buffer); when(client.execute()).thenReturn(resp); ClientHttpRequestFactory factory = mock(ClientHttpRequestFactory.class); when(factory.createRequest(any(URI.class), any(HttpMethod.class))).thenReturn(client); // add the new interceptor... ZeroLeggedOAuthInterceptor interceptor = new ZeroLeggedOAuthInterceptor(); interceptor.setPropertyResolver(resolver); interceptor.setId(id); List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(); interceptors.add(interceptor); RestTemplate rest = new RestTemplate(factory); rest.setInterceptors(interceptors); rest.postForLocation(url, data, Collections.emptyMap()); // make sure auth header is correctly set... assertThat(headers, hasKey(Headers.Authorization.name())); String authHeader = headers.get(Headers.Authorization.name()).get(0); assertThat(authHeader, containsString("OAuth realm=\"" + realm + "\"")); assertThat(authHeader, containsString("oauth_consumer_key=\"" + consumerKey + "\"")); // for now, only supports HMAC-SHA1. May have to fix later... assertThat(authHeader, containsString("oauth_signature_method=\"HMAC-SHA1\"")); assertThat(authHeader, containsString("oauth_version=\"1.0\"")); assertThat(authHeader, containsString("oauth_timestamp=")); assertThat(authHeader, containsString("oauth_nonce=")); assertThat(authHeader, containsString("oauth_signature=")); // oauth lib will create 2 oauth_signature params if you call sign // multiple times. Make sure only get 1. assertThat(StringUtils.countMatches(authHeader, "oauth_signature="), is(1)); }