use of org.springframework.web.client.RequestCallback in project spring-cloud-gateway by spring-cloud.
the class ServletOutputToInputConverter method exchange.
private ResponseEntity<T> exchange(RequestEntity<?> requestEntity) {
Type type = this.responseType;
if (type instanceof TypeVariable || type instanceof WildcardType) {
type = Object.class;
}
RequestCallback requestCallback = rest.httpEntityCallback((Object) requestEntity, type);
ResponseExtractor<ResponseEntity<T>> responseExtractor = rest.responseEntityExtractor(type);
return rest.execute(requestEntity.getUrl(), requestEntity.getMethod(), requestCallback, responseExtractor);
}
use of org.springframework.web.client.RequestCallback in project java-chassis by ServiceComb.
the class CseRestTemplate method httpEntityCallback.
@Override
public <T> RequestCallback httpEntityCallback(Object requestBody, Type responseType) {
RequestCallback callback = super.httpEntityCallback(requestBody, responseType);
CseRequestCallback cseCallback = new CseRequestCallback(requestBody, callback, responseType);
return cseCallback;
}
use of org.springframework.web.client.RequestCallback 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.RequestCallback in project spring-integration-samples by spring-projects.
the class RestHttpClientTest method testGetEmployeeAsXml.
/**
* @throws Exception
*/
@Test
public void testGetEmployeeAsXml() throws Exception {
Map<String, Object> employeeSearchMap = getEmployeeSearchMap("0");
final String fullUrl = "http://localhost:8080/rest-http/services/employee/{id}/search";
EmployeeList employeeList = restTemplate.execute(fullUrl, HttpMethod.GET, new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
HttpHeaders headers = getHttpHeadersWithUserCredentials(request);
headers.add("Accept", "application/xml");
}
}, responseExtractor, employeeSearchMap);
logger.info("The employee list size :" + employeeList.getEmployee().size());
StringWriter sw = new StringWriter();
StreamResult sr = new StreamResult(sw);
marshaller.marshal(employeeList, sr);
logger.info(sr.getWriter().toString());
assertTrue(employeeList.getEmployee().size() > 0);
}
use of org.springframework.web.client.RequestCallback 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);
}
}
Aggregations