@Override public Mono<Void> setComplete() { return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.empty()))); }
@Override public Mono<HandlerFunction<ServerResponse>> route(ServerRequest request) { return this.first.route(request) .map(RouterFunctions::cast) .switchIfEmpty(Mono.defer(() -> this.second.route(request).map(RouterFunctions::cast))); }
@Override public Mono<Void> filter(ServerWebExchange exchange) { return Mono.defer(() -> this.currentFilter != null && this.next != null ? this.currentFilter.filter(exchange, this.next) : this.handler.handle(exchange)); }
private Mono<DataBuffer> encodeText(CharSequence text, MediaType mediaType, DataBufferFactory bufferFactory) { Assert.notNull(mediaType.getCharset(), "Expected MediaType with charset"); byte[] bytes = text.toString().getBytes(mediaType.getCharset()); return Mono.defer(() -> Mono.just(bufferFactory.allocateBuffer(bytes.length).write(bytes))); }
private <R> Mono<R> createNotFoundError() { return Mono.defer(() -> { Exception ex = new ResponseStatusException(HttpStatus.NOT_FOUND, "No matching handler"); return Mono.error(ex); }); }
@Override protected Mono<Resource> resolveResourceInternal(@Nullable ServerWebExchange exchange, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) { return chain.resolveResource(exchange, requestPath, locations) .switchIfEmpty(Mono.defer(() -> resolveVersionedResource(exchange, requestPath, locations, chain))); }
@Override public Mono<HandlerFunction<T>> route(ServerRequest request) { return this.first.route(request) .switchIfEmpty(Mono.defer(() -> this.second.route(request))); }
private <T> Mono<ResponseEntity<T>> toEntityInternal(Mono<T> bodyMono) { HttpHeaders headers = headers().asHttpHeaders(); int status = rawStatusCode(); return bodyMono .map(body -> createEntity(body, headers, status)) .switchIfEmpty(Mono.defer( () -> Mono.just(createEntity(headers, status)))); }
private Mono<DataBuffer> stringBuffer(String value) { return Mono.defer(() -> { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return Mono.just(buffer); }); }
private Mono<DataBuffer> stringBuffer(String value) { return Mono.defer(() -> { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return Mono.just(buffer); }); }
private Mono<DataBuffer> stringBuffer(String value) { return Mono.defer(() -> { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return Mono.just(buffer); }); }
/** * Creates a deferred {@link DataBuffer} containing the given bytes. * @param bytes the bytes that are to be stored in the buffer * @return the deferred buffer */ protected Mono<DataBuffer> dataBuffer(byte[] bytes) { return Mono.defer(() -> { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length); dataBuffer.write(bytes); return Mono.just(dataBuffer); }); }
@Override public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body)))); }
private Mono<DataBuffer> dataBuffer(Msg msg) { return Mono.defer(() -> { byte[] bytes = msg.toByteArray(); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return Mono.just(buffer); }); }
@Override public Mono<WebSession> getSession(ServerWebExchange exchange) { return Mono.defer(() -> retrieveSession(exchange) .switchIfEmpty(this.sessionStore.createWebSession()) .doOnNext(session -> exchange.getResponse().beforeCommit(() -> save(exchange, session)))); }
Mono<Void> responseMonoVoid(ServerHttpResponse response) { return Mono.delay(Duration.ofMillis(100)) .thenEmpty(Mono.defer(() -> response.writeWith(getBody("body")))); }
Mono<Void> exchangeMonoVoid(ServerWebExchange exchange) { return Mono.delay(Duration.ofMillis(100)) .thenEmpty(Mono.defer(() -> exchange.getResponse().writeWith(getBody("body")))); }
@Test public void takeUntilByteCountError() { Flux<DataBuffer> source = Flux.concat( Mono.defer(() -> Mono.just(stringBuffer("foo"))), Mono.error(new RuntimeException()) ); Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(source, 5L); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .expectError(RuntimeException.class) .verify(Duration.ofSeconds(5)); }
@Override public void encode() throws Exception { Flux<DataBuffer> input = Flux.just(this.fooBytes, this.barBytes) .flatMap(bytes -> Mono.defer(() -> { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length); dataBuffer.write(bytes); return Mono.just(dataBuffer); })); testEncodeAll(input, DataBuffer.class, step -> step .consumeNextWith(expectBytes(this.fooBytes)) .consumeNextWith(expectBytes(this.barBytes)) .verifyComplete()); }
@Test // SPR-16231 public void responseCommitted() { Throwable ex = new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Oops"); this.exchange.getResponse().setStatusCode(HttpStatus.CREATED); Mono<Void> mono = this.exchange.getResponse().setComplete() .then(Mono.defer(() -> this.handler.handle(this.exchange, ex))); StepVerifier.create(mono).consumeErrorWith(actual -> assertSame(ex, actual)).verify(); }