Search in sources :

Example 1 with ReactiveHttpRequest

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

the class ReactiveClientMethodHandler method buildRequest.

protected ReactiveHttpRequest buildRequest(Object[] argv) {
    Map<String, ?> substitutionsMap = methodMetadata.indexToName().entrySet().stream().flatMap(e -> e.getValue().stream().map(v -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(), v))).collect(Collectors.toMap(Map.Entry::getValue, entry -> argv[entry.getKey()]));
    HttpMethod method = HttpMethod.resolve(methodMetadata.template().method());
    URI uri = defaultUriBuilderFactory.uriString(methodMetadata.template().url()).queryParams(parameters(argv)).build(substitutionsMap);
    return new ReactiveHttpRequest(method, uri, headers(argv, substitutionsMap), body(argv));
}
Also used : Target(feign.Target) Publisher(org.reactivestreams.Publisher) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HttpMethod(org.springframework.http.HttpMethod) Util.checkNotNull(feign.Util.checkNotNull) MultiValueMap(org.springframework.util.MultiValueMap) WebClient(org.springframework.web.reactive.function.client.WebClient) Mono(reactor.core.publisher.Mono) ReactiveHttpRequest(reactivefeign.client.ReactiveHttpRequest) MethodMetadata(feign.MethodMetadata) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Collectors.toList(java.util.stream.Collectors.toList) AbstractMap(java.util.AbstractMap) List(java.util.List) ReactiveClientFactory(reactivefeign.client.ReactiveClientFactory) Stream(java.util.stream.Stream) Collectors.mapping(java.util.stream.Collectors.mapping) Map(java.util.Map) ReactiveHttpClient(reactivefeign.client.ReactiveHttpClient) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory) URI(java.net.URI) Pattern(java.util.regex.Pattern) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) AbstractMap(java.util.AbstractMap) ReactiveHttpRequest(reactivefeign.client.ReactiveHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod)

Example 2 with ReactiveHttpRequest

use of reactivefeign.client.ReactiveHttpRequest 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 3 with ReactiveHttpRequest

use of reactivefeign.client.ReactiveHttpRequest 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 4 with ReactiveHttpRequest

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

the class RibbonPublisherClient method executeRequest.

@Override
public Publisher<?> executeRequest(ReactiveHttpRequest request) {
    if (loadBalancerCommand != null) {
        Observable<?> observable = loadBalancerCommand.submit(server -> {
            ReactiveHttpRequest lbRequest = loadBalanceRequest(request, server);
            Publisher<Object> publisher = (Publisher<Object>) publisherClient.executeRequest(lbRequest);
            return RxReactiveStreams.toObservable(publisher);
        });
        Publisher<?> publisher = RxReactiveStreams.toPublisher(observable);
        if (publisherType == Mono.class) {
            return Mono.from(publisher);
        } else if (publisherType == Flux.class) {
            return Flux.from(publisher);
        } else {
            throw new IllegalArgumentException("Unknown publisherType: " + publisherType);
        }
    } else {
        return publisherClient.executeRequest(request);
    }
}
Also used : ReactiveHttpRequest(reactivefeign.client.ReactiveHttpRequest) Flux(reactor.core.publisher.Flux) Publisher(org.reactivestreams.Publisher)

Example 5 with ReactiveHttpRequest

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

the class PublisherClientMethodHandler method buildRequest.

protected ReactiveHttpRequest buildRequest(Object[] argv) {
    Map<String, ?> substitutionsMap = methodMetadata.indexToName().entrySet().stream().flatMap(e -> e.getValue().stream().map(v -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(), v))).collect(Collectors.toMap(Map.Entry::getValue, entry -> argv[entry.getKey()]));
    try {
        String path = pathExpander.apply(substitutionsMap);
        Map<String, Collection<String>> queries = queries(argv, substitutionsMap);
        Map<String, List<String>> headers = headers(argv, substitutionsMap);
        URI uri = new URI(target.url() + path + queryLine(queries));
        return new ReactiveHttpRequest(methodMetadata.template().method(), uri, headers, body(argv));
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
Also used : PublisherHttpClient(reactivefeign.publisher.PublisherHttpClient) java.util(java.util) Target(feign.Target) MultiValueMapUtils(reactivefeign.utils.MultiValueMapUtils) Publisher(org.reactivestreams.Publisher) URISyntaxException(java.net.URISyntaxException) Util.checkNotNull(feign.Util.checkNotNull) Mono(reactor.core.publisher.Mono) MethodMetadata(feign.MethodMetadata) ReactiveHttpRequest(reactivefeign.client.ReactiveHttpRequest) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FeignUtils.returnPublisherType(reactivefeign.utils.FeignUtils.returnPublisherType) Matcher(java.util.regex.Matcher) Stream(java.util.stream.Stream) Type(java.lang.reflect.Type) ReactiveHttpClient(reactivefeign.client.ReactiveHttpClient) Pair(reactivefeign.utils.Pair) URI(java.net.URI) Pattern(java.util.regex.Pattern) ReactiveHttpRequest(reactivefeign.client.ReactiveHttpRequest) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

Publisher (org.reactivestreams.Publisher)5 ReactiveHttpRequest (reactivefeign.client.ReactiveHttpRequest)5 MethodMetadata (feign.MethodMetadata)4 ReactiveHttpClient (reactivefeign.client.ReactiveHttpClient)4 Mono (reactor.core.publisher.Mono)4 Type (java.lang.reflect.Type)3 Flux (reactor.core.publisher.Flux)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Target (feign.Target)2 Util.checkNotNull (feign.Util.checkNotNull)2 Util.resolveLastTypeParameter (feign.Util.resolveLastTypeParameter)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 URI (java.net.URI)2 List (java.util.List)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 HttpMethod (org.springframework.http.HttpMethod)2