Search in sources :

Example 1 with ReactiveHttpResponse

use of reactivefeign.client.ReactiveHttpResponse in project feign-reactive by kptfh.

the class RestTemplateFakeReactiveHttpClient method executeRequest.

@Override
public Mono<ReactiveHttpResponse> executeRequest(ReactiveHttpRequest request) {
    Mono<Object> bodyMono;
    if (request.body() instanceof Mono) {
        bodyMono = ((Mono<Object>) request.body());
    } else if (request.body() instanceof Flux) {
        bodyMono = ((Flux) request.body()).collectList();
    } else {
        bodyMono = Mono.just(request.body());
    }
    bodyMono = bodyMono.switchIfEmpty(Mono.just(new byte[0]));
    return bodyMono.<ReactiveHttpResponse>flatMap(body -> {
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(request.headers());
        if (acceptGzip) {
            headers.add("Accept-Encoding", "gzip");
        }
        ResponseEntity response = restTemplate.exchange(request.uri().toString(), HttpMethod.valueOf(request.method()), new HttpEntity<>(body, headers), responseType());
        return Mono.just(new FakeReactiveHttpResponse(response, returnPublisherType));
    }).onErrorMap(ex -> ex instanceof ResourceAccessException && ex.getCause() instanceof SocketTimeoutException, ReadTimeoutException::new).onErrorResume(HttpStatusCodeException.class, ex -> Mono.just(new ErrorReactiveHttpResponse(ex)));
}
Also used : ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) MethodMetadata(feign.MethodMetadata) ReactiveHttpRequest(reactivefeign.client.ReactiveHttpRequest) ParameterizedTypeReference.forType(org.springframework.core.ParameterizedTypeReference.forType) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) SocketTimeoutException(java.net.SocketTimeoutException) Map(java.util.Map) RestTemplate(org.springframework.web.client.RestTemplate) Publisher(org.reactivestreams.Publisher) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(org.springframework.http.HttpMethod) MultiValueMap(org.springframework.util.MultiValueMap) ResourceAccessException(org.springframework.web.client.ResourceAccessException) Mono(reactor.core.publisher.Mono) Flux(reactor.core.publisher.Flux) HttpEntity(org.springframework.http.HttpEntity) List(java.util.List) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Util.resolveLastTypeParameter(feign.Util.resolveLastTypeParameter) ReactiveHttpResponse(reactivefeign.client.ReactiveHttpResponse) ReactiveHttpClient(reactivefeign.client.ReactiveHttpClient) ResponseEntity(org.springframework.http.ResponseEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ReadTimeoutException(reactivefeign.client.ReadTimeoutException) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Mono(reactor.core.publisher.Mono) Flux(reactor.core.publisher.Flux) ReactiveHttpResponse(reactivefeign.client.ReactiveHttpResponse) ResourceAccessException(org.springframework.web.client.ResourceAccessException) ResponseEntity(org.springframework.http.ResponseEntity) SocketTimeoutException(java.net.SocketTimeoutException)

Example 2 with ReactiveHttpResponse

use of reactivefeign.client.ReactiveHttpResponse in project feign-reactive by kptfh.

the class JettyReactiveHttpClient method executeRequest.

@Override
public Mono<ReactiveHttpResponse> executeRequest(ReactiveHttpRequest request) {
    Request jettyRequest = httpClient.newRequest(request.uri()).method(request.method());
    setUpHeaders(request, jettyRequest.getHeaders());
    if (requestTimeout > 0) {
        jettyRequest.timeout(requestTimeout, TimeUnit.MILLISECONDS);
    }
    ReactiveRequest.Builder requestBuilder = ReactiveRequest.newBuilder(jettyRequest);
    if (bodyActualClass != null) {
        ReactiveRequest.Content content = provideBody(request);
        requestBuilder.content(content);
        jettyRequest.getHeaders().put(CONTENT_TYPE.asString(), singletonList(content.getContentType()));
    }
    return Mono.<ReactiveHttpResponse>from(requestBuilder.build().response((response, content) -> Mono.just(new JettyReactiveHttpResponse(response.getResponse(), postProcess(content, (contentChunk, throwable) -> {
        if (throwable != null) {
            contentChunk.callback.failed(throwable);
        } else {
            contentChunk.callback.succeeded();
        }
    }), returnPublisherClass, returnActualClass, jsonFactory, responseReader)))).onErrorMap(ex -> ex instanceof java.util.concurrent.TimeoutException, ReadTimeoutException::new);
}
Also used : JsonFactory(com.fasterxml.jackson.core.async_.JsonFactory) MethodMetadata(feign.MethodMetadata) Request(org.eclipse.jetty.client.api.Request) ReactiveHttpRequest(reactivefeign.client.ReactiveHttpRequest) ByteBuffer(java.nio.ByteBuffer) ProxyPostProcessor.postProcess(reactivefeign.jetty.utils.ProxyPostProcessor.postProcess) Collections.singletonList(java.util.Collections.singletonList) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) HttpClient(org.eclipse.jetty.client.HttpClient) CONTENT_TYPE(org.eclipse.jetty.http.HttpHeader.CONTENT_TYPE) HttpFields(org.eclipse.jetty.http.HttpFields) ReactiveRequest(org.eclipse.jetty.reactive.client.ReactiveRequest) ACCEPT(org.eclipse.jetty.http.HttpHeader.ACCEPT) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) CharBuffer(java.nio.CharBuffer) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Publisher(org.reactivestreams.Publisher) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Mono(reactor.core.publisher.Mono) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) ContentChunk(org.eclipse.jetty.reactive.client.ContentChunk) Flux(reactor.core.publisher.Flux) FeignUtils.getBodyActualType(reactivefeign.utils.FeignUtils.getBodyActualType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Util.resolveLastTypeParameter(feign.Util.resolveLastTypeParameter) ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder) ReactiveHttpResponse(reactivefeign.client.ReactiveHttpResponse) ReactiveHttpClient(reactivefeign.client.ReactiveHttpClient) ReadTimeoutException(reactivefeign.client.ReadTimeoutException) ReadTimeoutException(reactivefeign.client.ReadTimeoutException) Request(org.eclipse.jetty.client.api.Request) ReactiveHttpRequest(reactivefeign.client.ReactiveHttpRequest) ReactiveRequest(org.eclipse.jetty.reactive.client.ReactiveRequest) ReactiveRequest(org.eclipse.jetty.reactive.client.ReactiveRequest)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 MethodMetadata (feign.MethodMetadata)2 Util.resolveLastTypeParameter (feign.Util.resolveLastTypeParameter)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 Publisher (org.reactivestreams.Publisher)2 ReactiveHttpClient (reactivefeign.client.ReactiveHttpClient)2 ReactiveHttpRequest (reactivefeign.client.ReactiveHttpRequest)2 ReactiveHttpResponse (reactivefeign.client.ReactiveHttpResponse)2 ReadTimeoutException (reactivefeign.client.ReadTimeoutException)2 Flux (reactor.core.publisher.Flux)2 Mono (reactor.core.publisher.Mono)2 JsonFactory (com.fasterxml.jackson.core.async_.JsonFactory)1 ByteArrayBuilder (com.fasterxml.jackson.core.util.ByteArrayBuilder)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 UncheckedIOException (java.io.UncheckedIOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ByteBuffer (java.nio.ByteBuffer)1