private ReactiveClientMethodHandler(Target target, MethodMetadata methodMetadata, ReactiveHttpClient reactiveClient) { this.target = checkNotNull(target, "target must be not null"); this.methodMetadata = checkNotNull(methodMetadata, "methodMetadata must be not null"); this.reactiveClient = checkNotNull(reactiveClient, "client must be not null"); this.defaultUriBuilderFactory = new DefaultUriBuilderFactory(target.url()); Stream<AbstractMap.SimpleImmutableEntry<String, String>> simpleImmutableEntryStream = methodMetadata .template().headers().entrySet().stream() .flatMap(e -> e.getValue().stream() .map(v -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(), v))); this.headerExpanders = simpleImmutableEntryStream.collect(groupingBy( entry -> entry.getKey(), mapping(entry -> buildExpandHeaderFunction(entry.getValue()), toList()))); }
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); } }
@Override protected ReactiveClientFactory buildReactiveClientFactory() { ReactiveClientFactory reactiveClientFactory = super.buildReactiveClientFactory(); return methodMetadata -> { ReactiveHttpClient reactiveClient = reactiveClientFactory.apply(methodMetadata); String serviceName = extractServiceName(target.url()); return new RibbonReactiveClient(methodMetadata, loadBalancerCommandFactory.apply(serviceName), reactiveClient); }; }
@Override protected PublisherClientFactory buildReactiveClientFactory() { PublisherClientFactory publisherClientFactory = super.buildReactiveClientFactory(); return methodMetadata -> { PublisherHttpClient publisherClient = publisherClientFactory.apply(methodMetadata); String serviceName = extractServiceName(target.url()); return new RibbonPublisherClient(loadBalancerCommandFactory.apply(serviceName), publisherClient, returnPublisherType(methodMetadata)); }; }
@Override public PublisherHttpClient create(MethodMetadata methodMetadata) { PublisherHttpClient publisherClient = publisherClientFactory.create(methodMetadata); String serviceName = extractServiceName(target.url()); return new RibbonPublisherClient(loadBalancerCommandFactory.apply(serviceName), publisherClient, returnPublisherType(methodMetadata)); } };
private void upgradeToH2c(Target target){ try { httpClient.send(HttpRequest.newBuilder() .method("options", HttpRequest.BodyPublishers.noBody()) .uri(URI.create(target.url())) .build(), HttpResponse.BodyHandlers.discarding()); } catch (IOException | InterruptedException e) { throw new RuntimeException(e); } } }
@Test public void testGetters() { Target<ApiInterface1> target1 = new LambdaServiceFunctionTarget<>(ApiInterface1.class, "a"); assertEquals("a", target1.name()); assertEquals(ApiInterface1.class, target1.type()); assertEquals("", target1.url()); Target<ApiInterface2> target2 = new LambdaServiceFunctionTarget<>(ApiInterface2.class, "b"); assertEquals("b", target2.name()); assertEquals(ApiInterface2.class, target2.type()); assertEquals("", target2.url()); }