Search in sources :

Example 1 with JsonFactory

use of com.fasterxml.jackson.core.async_.JsonFactory 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)

Example 2 with JsonFactory

use of com.fasterxml.jackson.core.async_.JsonFactory in project feign-reactive by kptfh.

the class JettyReactiveHttpResponse method body.

@Override
public Publisher<?> body() {
    ReactorObjectReader reactorObjectReader = new ReactorObjectReader(jsonFactory);
    Flux<ByteBuffer> content = directContent();
    if (returnActualClass == ByteBuffer.class) {
        return content;
    } else if (returnActualClass.isAssignableFrom(String.class) && returnPublisherType == Mono.class) {
        Charset charset = getCharset();
        return content.map(byteBuffer -> charset.decode(byteBuffer).toString());
    } else {
        if (returnPublisherType == Mono.class) {
            return reactorObjectReader.read(content, objectReader);
        } else if (returnPublisherType == Flux.class) {
            return reactorObjectReader.readElements(content, objectReader);
        } else {
            throw new IllegalArgumentException("Unknown returnPublisherType: " + returnPublisherType);
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Optional.ofNullable(java.util.Optional.ofNullable) Publisher(org.reactivestreams.Publisher) JsonFactory(com.fasterxml.jackson.core.async_.JsonFactory) ReactorObjectReader(reactivejson.ReactorObjectReader) Mono(reactor.core.publisher.Mono) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ContentChunk(org.eclipse.jetty.reactive.client.ContentChunk) Flux(reactor.core.publisher.Flux) List(java.util.List) Charset(java.nio.charset.Charset) HttpField(org.eclipse.jetty.http.HttpField) Arrays.asList(java.util.Arrays.asList) ReactiveHttpResponse(reactivefeign.client.ReactiveHttpResponse) Map(java.util.Map) CONTENT_TYPE(org.eclipse.jetty.http.HttpHeader.CONTENT_TYPE) Response(org.eclipse.jetty.client.api.Response) Mono(reactor.core.publisher.Mono) Charset(java.nio.charset.Charset) ReactorObjectReader(reactivejson.ReactorObjectReader) ByteBuffer(java.nio.ByteBuffer)

Example 3 with JsonFactory

use of com.fasterxml.jackson.core.async_.JsonFactory in project feign-reactive by kptfh.

the class JettyReactiveFeign method builder.

public static <T> Builder<T> builder() {
    try {
        HttpClient httpClient = new HttpClient();
        httpClient.start();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        return new Builder<>(httpClient, new JsonFactory(), objectMapper);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) JettyReactiveHttpClient(reactivefeign.jetty.client.JettyReactiveHttpClient) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JsonFactory(com.fasterxml.jackson.core.async_.JsonFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonFactory (com.fasterxml.jackson.core.async_.JsonFactory)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)2 ByteBuffer (java.nio.ByteBuffer)2 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)2 HttpClient (org.eclipse.jetty.client.HttpClient)2 CONTENT_TYPE (org.eclipse.jetty.http.HttpHeader.CONTENT_TYPE)2 ContentChunk (org.eclipse.jetty.reactive.client.ContentChunk)2 Publisher (org.reactivestreams.Publisher)2 ReactiveHttpResponse (reactivefeign.client.ReactiveHttpResponse)2 Flux (reactor.core.publisher.Flux)2 Mono (reactor.core.publisher.Mono)2 ByteArrayBuilder (com.fasterxml.jackson.core.util.ByteArrayBuilder)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 MethodMetadata (feign.MethodMetadata)1 Util.resolveLastTypeParameter (feign.Util.resolveLastTypeParameter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1