use of org.springframework.web.client.ResponseExtractor in project spring-security-oauth by spring-projects.
the class AbstractAuthorizationCodeProviderTests method verifyAuthorizationPage.
private void verifyAuthorizationPage(OAuth2RestTemplate restTemplate, String location) {
final AtomicReference<String> confirmationPage = new AtomicReference<String>();
AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider() {
@Override
protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
return new ResponseExtractor<ResponseEntity<Void>>() {
public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
confirmationPage.set(StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")));
return new ResponseEntity<Void>(response.getHeaders(), response.getStatusCode());
}
};
}
};
try {
provider.obtainAuthorizationCode(restTemplate.getResource(), restTemplate.getOAuth2ClientContext().getAccessTokenRequest());
} catch (UserApprovalRequiredException e) {
// ignore
}
String page = confirmationPage.get();
verifyAuthorizationPage(page);
}
use of org.springframework.web.client.ResponseExtractor in project geode by apache.
the class AbstractHttpOperationInvoker method downloadResponseToTempFile.
protected Object downloadResponseToTempFile(ClientHttpRequest request, Map<String, ?> uriVariables) {
final URI url = request.getURL(uriVariables);
// Optional Accept header
RequestCallback requestCallback = r -> {
r.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
HttpHeaders header = request.getHeaders();
r.getHeaders().setAll(request.getHeaders().toSingleValueMap());
};
// Streams the response instead of loading it all in memory
ResponseExtractor<Object> responseExtractor = resp -> {
MediaType mediaType = resp.getHeaders().getContentType();
if (mediaType.equals(MediaType.APPLICATION_JSON)) {
return org.apache.commons.io.IOUtils.toString(resp.getBody(), "UTF-8");
} else {
Path tempFile = Files.createTempFile("fileDownload", "");
if (tempFile.toFile().exists()) {
FileUtils.deleteQuietly(tempFile.toFile());
}
Files.copy(resp.getBody(), tempFile);
return tempFile;
}
};
return getRestTemplate().execute(url, org.springframework.http.HttpMethod.GET, requestCallback, responseExtractor);
}
use of org.springframework.web.client.ResponseExtractor in project incubator-servicecomb-java-chassis by apache.
the class TestRestTemplateWrapper method executeWithUnderlyingRestTemplate.
@Test
public void executeWithUnderlyingRestTemplate() {
RequestCallback requestCallback = clientHttpRequest -> {
};
ResponseExtractor<ResponseEntity<String>> responseExtractor = clientHttpResponse -> responseEntity;
ResponseEntity<String> actual;
for (HttpMethod method : httpMethods) {
when(underlying.execute(url, method, requestCallback, responseExtractor, param1, param2)).thenReturn(responseEntity);
actual = wrapper.execute(url, method, requestCallback, responseExtractor, param1, param2);
assertThat(actual, is(responseEntity));
verify(underlying).execute(url, method, requestCallback, responseExtractor, param1, param2);
when(underlying.execute(url, method, requestCallback, responseExtractor, paramsMap)).thenReturn(responseEntity);
actual = wrapper.execute(url, method, requestCallback, responseExtractor, paramsMap);
assertThat(actual, is(responseEntity));
verify(underlying).execute(url, method, requestCallback, responseExtractor, paramsMap);
when(underlying.execute(uri, method, requestCallback, responseExtractor)).thenReturn(responseEntity);
actual = wrapper.execute(uri, method, requestCallback, responseExtractor);
assertThat(actual, is(responseEntity));
verify(underlying).execute(uri, method, requestCallback, responseExtractor);
}
}
use of org.springframework.web.client.ResponseExtractor in project InciManager_i2a by Arquisoft.
the class AbstractSteps method executeGet.
protected void executeGet(String url) throws IOException {
final Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json");
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
if (restTemplate == null) {
restTemplate = new RestTemplate();
}
restTemplate.setErrorHandler(errorHandler);
latestResponse = restTemplate.execute(url, HttpMethod.GET, requestCallback, new ResponseExtractor<ResponseResults>() {
@Override
public ResponseResults extractData(ClientHttpResponse response) throws IOException {
if (errorHandler.hadError) {
return (errorHandler.getResults());
} else {
return (new ResponseResults(response));
}
}
});
}
use of org.springframework.web.client.ResponseExtractor in project java-chassis by ServiceComb.
the class TestRestTemplateWrapper method executeWithUnderlyingRestTemplate.
@Test
public void executeWithUnderlyingRestTemplate() {
RequestCallback requestCallback = clientHttpRequest -> {
};
ResponseExtractor<ResponseEntity<String>> responseExtractor = clientHttpResponse -> responseEntity;
ResponseEntity<String> actual;
for (HttpMethod method : httpMethods) {
when(underlying.execute(url, method, requestCallback, responseExtractor, param1, param2)).thenReturn(responseEntity);
actual = wrapper.execute(url, method, requestCallback, responseExtractor, param1, param2);
assertThat(actual, is(responseEntity));
verify(underlying).execute(url, method, requestCallback, responseExtractor, param1, param2);
when(underlying.execute(url, method, requestCallback, responseExtractor, paramsMap)).thenReturn(responseEntity);
actual = wrapper.execute(url, method, requestCallback, responseExtractor, paramsMap);
assertThat(actual, is(responseEntity));
verify(underlying).execute(url, method, requestCallback, responseExtractor, paramsMap);
when(underlying.execute(uri, method, requestCallback, responseExtractor)).thenReturn(responseEntity);
actual = wrapper.execute(uri, method, requestCallback, responseExtractor);
assertThat(actual, is(responseEntity));
verify(underlying).execute(uri, method, requestCallback, responseExtractor);
}
}
Aggregations