private Mono<Resource> getResource(String resourcePath, List<? extends Resource> locations) { return Flux.fromIterable(locations) .concatMap(location -> getResource(resourcePath, location)) .next(); }
private Mono<WebSession> retrieveSession(ServerWebExchange exchange) { return Flux.fromIterable(getSessionIdResolver().resolveSessionIds(exchange)) .concatMap(this.sessionStore::retrieveSession) .next(); }
@Override public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) { if (!uri.isAbsolute()) { return Mono.error(new IllegalArgumentException("URI is not absolute: " + uri)); } return this.httpClient .request(io.netty.handler.codec.http.HttpMethod.valueOf(method.name())) .uri(uri.toString()) .send((request, outbound) -> requestCallback.apply(adaptRequest(method, uri, request, outbound))) .responseConnection((res, con) -> Mono.just(adaptResponse(res, con.inbound(), con.outbound().alloc()))) .next(); }
@Override public Mono<Void> handle(ServerWebExchange exchange) { if (this.handlerMappings == null) { return createNotFoundError(); } return Flux.fromIterable(this.handlerMappings) .concatMap(mapping -> mapping.getHandler(exchange)) .next() .switchIfEmpty(createNotFoundError()) .flatMap(handler -> invokeHandler(exchange, handler)) .flatMap(result -> handleResult(exchange, result)); }
@Override public Mono<GetResult> get(HttpHeaders headers, GetRequest getRequest) { return sendRequest(getRequest, RequestCreator.get(), GetResponse.class, headers) // .filter(GetResponse::isExists) // .map(DefaultReactiveElasticsearchClient::getResponseToGetResult) // .next(); }
public Mono<Integer> run(Integer id) { return template.inTransaction().execute(action -> { return lookup(id) // .flatMap(process -> start(action, process)) // .flatMap(it -> verify(it)) // .flatMap(process -> finish(action, process)); }).next().map(Process::getId); }
public Mono<Authentication> authenticate(Authentication authentication) { return Flux.fromIterable(this.delegates) .concatMap(m -> m.authenticate(authentication)) .next(); } }
@Override protected Mono<Void> writeToInternal(ServerWebExchange exchange, Context context) { MediaType contentType = exchange.getResponse().getHeaders().getContentType(); Locale locale = LocaleContextHolder.getLocale(exchange.getLocaleContext()); Stream<ViewResolver> viewResolverStream = context.viewResolvers().stream(); return Flux.fromStream(viewResolverStream) .concatMap(viewResolver -> viewResolver.resolveViewName(name(), locale)) .next() .switchIfEmpty(Mono.error(() -> new IllegalArgumentException("Could not resolve view with name '" + name() + "'"))) .flatMap(view -> { List<MediaType> mediaTypes = view.getSupportedMediaTypes(); return view.render(model(), contentType == null && !mediaTypes.isEmpty() ? mediaTypes.get(0) : contentType, exchange); }); }
@Override public Mono<MainResponse> info(HttpHeaders headers) { return sendRequest(new MainRequest(), RequestCreator.info(), MainResponse.class, headers) // .next(); }
@Override public Mono<Boolean> exists(HttpHeaders headers, GetRequest getRequest) { return sendRequest(getRequest, RequestCreator.exists(), RawActionResponse.class, headers) // .map(response -> response.statusCode().is2xxSuccessful()) // .next(); }
private Mono<WebSession> retrieveSession(ServerWebExchange exchange) { return Flux.fromIterable(getSessionIdResolver().resolveSessionIds(exchange)) .concatMap(this.sessionStore::retrieveSession) .next(); }
@Override public Mono<Boolean> ping(HttpHeaders headers) { return sendRequest(new MainRequest(), RequestCreator.ping(), RawActionResponse.class, headers) // .map(response -> response.statusCode().is2xxSuccessful()) // .onErrorResume(NoReachableHostException.class, error -> Mono.just(false)).next(); }
@Override public Mono<Void> execute(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) { return getHttpClient() .headers(nettyHeaders -> setNettyHeaders(requestHeaders, nettyHeaders)) .websocket(StringUtils.collectionToCommaDelimitedString(handler.getSubProtocols())) .uri(url.toString()) .handle((inbound, outbound) -> { HttpHeaders responseHeaders = toHttpHeaders(inbound); String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol"); HandshakeInfo info = new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol); NettyDataBufferFactory factory = new NettyDataBufferFactory(outbound.alloc()); WebSocketSession session = new ReactorNettyWebSocketSession(inbound, outbound, info, factory); if (logger.isDebugEnabled()) { logger.debug("Started session '" + session.getId() + "' for " + url); } return handler.handle(session); }) .doOnRequest(n -> { if (logger.isDebugEnabled()) { logger.debug("Connecting to " + url); } }) .next(); }
@Override public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) { if (!uri.isAbsolute()) { return Mono.error(new IllegalArgumentException("URI is not absolute: " + uri)); } return this.httpClient .request(io.netty.handler.codec.http.HttpMethod.valueOf(method.name())) .uri(uri.toString()) .send((request, outbound) -> requestCallback.apply(adaptRequest(method, uri, request, outbound))) .responseConnection((res, con) -> Mono.just(adaptResponse(res, con.inbound(), con.outbound().alloc()))) .next(); }
@Test // SPR-14992 public void writeAndAutoFlushBeforeComplete() { Mono<String> result = this.webClient.get() .uri("/write-and-never-complete") .retrieve() .bodyToFlux(String.class) .next(); StepVerifier.create(result) .expectNextMatches(s -> s.startsWith("0123456789")) .expectComplete() .verify(Duration.ofSeconds(10L)); }
@Test public void sampleMergeMonoTest() throws Exception { CountDownLatch latch = new CountDownLatch(2); Flux<Integer> p = Flux.merge(Flux.<Integer>empty().next(), Mono.just(1)) .log("mono"); awaitLatch(p, latch); }
@Test public void zipOfNull() { try { Flux<String> as = Flux.just("x"); Flux<String> bs = Flux.just((String)null); assertNull(Flux.zip(as, bs).next().block()); } catch (NullPointerException npe) { return; } assertFalse("Should have failed", true); }
@Test public void monoNext() { StepVerifier.create(Flux.just(1, 2, 3).next()) .expectNext(1) .verifyComplete(); }
@Test public void cancel() { TestPublisher<String> cancelTester = TestPublisher.create(); MonoProcessor<String> processor = cancelTester.flux() .next() .toProcessor(); processor.subscribe(); processor.cancel(); cancelTester.assertCancelled(); }