Search in sources :

Example 1 with ContentChunk

use of org.eclipse.jetty.reactive.client.ContentChunk in project feign-reactive by kptfh.

the class JettyReactiveHttpClient method toCharSequenceChunk.

protected ContentChunk toCharSequenceChunk(Object data) {
    CharBuffer charBuffer = CharBuffer.wrap((CharSequence) data);
    ByteBuffer byteBuffer = UTF_8.encode(charBuffer);
    return new ContentChunk(byteBuffer);
}
Also used : ContentChunk(org.eclipse.jetty.reactive.client.ContentChunk) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ContentChunk

use of org.eclipse.jetty.reactive.client.ContentChunk 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 3 with ContentChunk

use of org.eclipse.jetty.reactive.client.ContentChunk in project feign-reactive by kptfh.

the class JettyReactiveHttpClient method toJsonChunk.

protected ContentChunk toJsonChunk(Object data, boolean stream) {
    try {
        ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
        bodyWriter.writeValue(byteArrayBuilder, data);
        if (stream) {
            byteArrayBuilder.write(NEWLINE_SEPARATOR);
        }
        ByteBuffer buffer = ByteBuffer.wrap(byteArrayBuilder.toByteArray());
        return new ContentChunk(buffer);
    } catch (java.io.IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : ContentChunk(org.eclipse.jetty.reactive.client.ContentChunk) UncheckedIOException(java.io.UncheckedIOException) ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder) ByteBuffer(java.nio.ByteBuffer)

Example 4 with ContentChunk

use of org.eclipse.jetty.reactive.client.ContentChunk in project navajo by Dexels.

the class JettyClient method call.

public Flowable<ReactiveReply> call(String uri, UnaryOperator<Request> buildRequest, Optional<Flowable<byte[]>> requestBody, Optional<String> requestContentType) {
    // Reque
    Request req = httpClient.newRequest(uri);
    Request reqProcessed = buildRequest.apply(req);
    if (requestContentType.isPresent()) {
        reqProcessed = reqProcessed.header("Content-Type", requestContentType.get());
    }
    ReactiveRequest.Builder requestBuilder = ReactiveRequest.newBuilder(reqProcessed);
    if (requestBody.isPresent()) {
        Publisher<ContentChunk> bb = requestBody.get().doOnNext(b -> this.sent.addAndGet(b.length)).map(e -> new ContentChunk(ByteBuffer.wrap(e)));
        requestBuilder = requestBuilder.content(ReactiveRequest.Content.fromPublisher(bb, requestContentType.orElse("application/octet-stream")));
    }
    ReactiveRequest request = requestBuilder.build();
    return Flowable.fromPublisher(request.response((response, content) -> Flowable.just(new ReactiveReply(response, content, b -> this.sent.addAndGet(b.length))))).doOnComplete(() -> logger.info("HTTP Client to {}: sent: {} received: {}", uri, sent.get(), received.get()));
}
Also used : Logger(org.slf4j.Logger) ReactiveReply(com.dexels.navajo.client.stream.ReactiveReply) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Publisher(org.reactivestreams.Publisher) LoggerFactory(org.slf4j.LoggerFactory) UnaryOperator(java.util.function.UnaryOperator) Request(org.eclipse.jetty.client.api.Request) Single(io.reactivex.Single) ByteBuffer(java.nio.ByteBuffer) FlowableTransformer(io.reactivex.FlowableTransformer) HttpClient(org.eclipse.jetty.client.HttpClient) Emitter(io.reactivex.Emitter) AtomicLong(java.util.concurrent.atomic.AtomicLong) ContentChunk(org.eclipse.jetty.reactive.client.ContentChunk) Flowable(io.reactivex.Flowable) Optional(java.util.Optional) ReactiveRequest(org.eclipse.jetty.reactive.client.ReactiveRequest) ContentChunk(org.eclipse.jetty.reactive.client.ContentChunk) ReactiveReply(com.dexels.navajo.client.stream.ReactiveReply) Request(org.eclipse.jetty.client.api.Request) ReactiveRequest(org.eclipse.jetty.reactive.client.ReactiveRequest) ReactiveRequest(org.eclipse.jetty.reactive.client.ReactiveRequest)

Aggregations

ByteBuffer (java.nio.ByteBuffer)4 ContentChunk (org.eclipse.jetty.reactive.client.ContentChunk)4 ByteArrayBuilder (com.fasterxml.jackson.core.util.ByteArrayBuilder)2 UncheckedIOException (java.io.UncheckedIOException)2 CharBuffer (java.nio.CharBuffer)2 HttpClient (org.eclipse.jetty.client.HttpClient)2 Request (org.eclipse.jetty.client.api.Request)2 ReactiveRequest (org.eclipse.jetty.reactive.client.ReactiveRequest)2 Publisher (org.reactivestreams.Publisher)2 ReactiveReply (com.dexels.navajo.client.stream.ReactiveReply)1 JsonFactory (com.fasterxml.jackson.core.async_.JsonFactory)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 MethodMetadata (feign.MethodMetadata)1 Util.resolveLastTypeParameter (feign.Util.resolveLastTypeParameter)1 Emitter (io.reactivex.Emitter)1 Flowable (io.reactivex.Flowable)1 FlowableTransformer (io.reactivex.FlowableTransformer)1 Single (io.reactivex.Single)1