Search in sources :

Example 6 with RequestCallback

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);
}
Also used : WildcardType(java.lang.reflect.WildcardType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ResponseEntity(org.springframework.http.ResponseEntity) RequestCallback(org.springframework.web.client.RequestCallback) TypeVariable(java.lang.reflect.TypeVariable)

Example 7 with RequestCallback

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;
}
Also used : RequestCallback(org.springframework.web.client.RequestCallback)

Example 8 with RequestCallback

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);
}
Also used : Arrays(java.util.Arrays) QueryExp(javax.management.QueryExp) DistributedSystemMXBean(org.apache.geode.management.DistributedSystemMXBean) MBeanJMXAdapter(org.apache.geode.management.internal.MBeanJMXAdapter) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) LogService(org.apache.geode.internal.logging.LogService) Map(java.util.Map) HttpHeader(org.apache.geode.management.internal.web.http.HttpHeader) URI(java.net.URI) Path(java.nio.file.Path) RestTemplate(org.springframework.web.client.RestTemplate) ResponseExtractor(org.springframework.web.client.ResponseExtractor) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Set(java.util.Set) ObjectName(javax.management.ObjectName) Executors(java.util.concurrent.Executors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) Entry(java.util.Map.Entry) HttpMBeanProxyFactory(org.apache.geode.management.internal.web.shell.support.HttpMBeanProxyFactory) IOUtils(org.apache.geode.internal.util.IOUtils) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) QueryParameterSource(org.apache.geode.management.internal.web.domain.QueryParameterSource) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) Link(org.apache.geode.management.internal.web.domain.Link) ManagementConstants(org.apache.geode.management.internal.ManagementConstants) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) Iterator(java.util.Iterator) Files(java.nio.file.Files) UriUtils(org.apache.geode.management.internal.web.util.UriUtils) GemFireVersion(org.apache.geode.internal.GemFireVersion) FileUtils(org.apache.commons.io.FileUtils) ResourceAccessException(org.springframework.web.client.ResourceAccessException) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) RequestCallback(org.springframework.web.client.RequestCallback) SerializableObjectHttpMessageConverter(org.apache.geode.management.internal.web.http.converter.SerializableObjectHttpMessageConverter) TimeUnit(java.util.concurrent.TimeUnit) HttpStatus(org.springframework.http.HttpStatus) StringUtils(org.apache.geode.internal.lang.StringUtils) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMethod(org.apache.geode.management.internal.web.http.HttpMethod) ResponseEntity(org.springframework.http.ResponseEntity) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) Path(java.nio.file.Path) HttpHeaders(org.springframework.http.HttpHeaders) RequestCallback(org.springframework.web.client.RequestCallback) MediaType(org.springframework.http.MediaType) URI(java.net.URI)

Example 9 with RequestCallback

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) RequestCallback(org.springframework.web.client.RequestCallback) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) EmployeeList(org.springframework.integration.samples.rest.domain.EmployeeList) Test(org.junit.Test)

Example 10 with RequestCallback

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);
    }
}
Also used : IsIterableContainingInOrder.contains(org.hamcrest.collection.IsIterableContainingInOrder.contains) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) GET(org.springframework.http.HttpMethod.GET) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) HashMap(java.util.HashMap) PATCH(org.springframework.http.HttpMethod.PATCH) DELETE(org.springframework.http.HttpMethod.DELETE) Collections.singletonList(java.util.Collections.singletonList) Assert.assertThat(org.junit.Assert.assertThat) HashSet(java.util.HashSet) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) HEAD(org.springframework.http.HttpMethod.HEAD) UriTemplateHandler(org.springframework.web.util.UriTemplateHandler) URI(java.net.URI) OPTIONS(org.springframework.http.HttpMethod.OPTIONS) PUT(org.springframework.http.HttpMethod.PUT) RestTemplate(org.springframework.web.client.RestTemplate) Before(org.junit.Before) RequestEntity(org.springframework.http.RequestEntity) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) POST(org.springframework.http.HttpMethod.POST) ResponseExtractor(org.springframework.web.client.ResponseExtractor) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) DefaultUriTemplateHandler(org.springframework.web.util.DefaultUriTemplateHandler) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) RequestCallback(org.springframework.web.client.RequestCallback) Randomness.uniquify(com.seanyinx.github.unit.scaffolding.Randomness.uniquify) Mockito.never(org.mockito.Mockito.never) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) TRACE(org.springframework.http.HttpMethod.TRACE) ResponseEntity(org.springframework.http.ResponseEntity) OK(org.springframework.http.HttpStatus.OK) Mockito.reset(org.mockito.Mockito.reset) Mockito.mock(org.mockito.Mockito.mock) ResponseEntity(org.springframework.http.ResponseEntity) RequestCallback(org.springframework.web.client.RequestCallback) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Aggregations

RequestCallback (org.springframework.web.client.RequestCallback)24 Nullable (org.springframework.lang.Nullable)9 HttpHeaders (org.springframework.http.HttpHeaders)7 RestTemplate (org.springframework.web.client.RestTemplate)7 IOException (java.io.IOException)6 ResponseEntity (org.springframework.http.ResponseEntity)5 URI (java.net.URI)4 Date (java.util.Date)4 List (java.util.List)4 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)4 Map (java.util.Map)3 Set (java.util.Set)3 Test (org.junit.Test)3 MediaType (org.springframework.http.MediaType)3 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)3 ResponseExtractor (org.springframework.web.client.ResponseExtractor)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Randomness.uniquify (com.seanyinx.github.unit.scaffolding.Randomness.uniquify)2 Arrays.asList (java.util.Arrays.asList)2 Collections.singletonList (java.util.Collections.singletonList)2