Refine search
/** * Return a filter that generates an error signal when the given * {@link HttpStatus} predicate matches. * @param statusPredicate the predicate to check the HTTP status with * @param exceptionFunction the function that to create the exception * @return the filter to generate an error signal */ public static ExchangeFilterFunction statusError(Predicate<HttpStatus> statusPredicate, Function<ClientResponse, ? extends Throwable> exceptionFunction) { Assert.notNull(statusPredicate, "Predicate must not be null"); Assert.notNull(exceptionFunction, "Function must not be null"); return ExchangeFilterFunction.ofResponseProcessor( response -> (statusPredicate.test(response.statusCode()) ? Mono.error(exceptionFunction.apply(response)) : Mono.just(response))); }
/** * Get multiple values in one batch. * * @param keys must not be {@literal null}. * @return * @see <a href="http://redis.io/commands/mget">Redis Documentation: MGET</a> */ default Mono<List<ByteBuffer>> mGet(List<ByteBuffer> keys) { Assert.notNull(keys, "Keys must not be null!"); return mGet(Mono.just(keys)).next().map(MultiValueResponse::getOutput); }
/** * Register instance. * * @param registration instance to be registered. * @return the if of the registered instance. */ public Mono<InstanceId> register(Registration registration) { Assert.notNull(registration, "'registration' must not be null"); InstanceId id = generator.generateId(registration); Assert.notNull(id, "'id' must not be null"); return repository.compute(id, (key, instance) -> { if (instance == null) { instance = Instance.create(key); } return Mono.just(instance.register(registration)); }).map(Instance::getId); }
/** * Inserter to write the given {@code Resource}. * <p>If the resource can be resolved to a {@linkplain Resource#getFile() file}, it will * be copied using <a href="https://en.wikipedia.org/wiki/Zero-copy">zero-copy</a>. * @param resource the resource to write to the output message * @param <T> the type of the {@code Resource} * @return the inserter to write a {@code Publisher} */ public static <T extends Resource> BodyInserter<T, ReactiveHttpOutputMessage> fromResource(T resource) { Assert.notNull(resource, "Resource must not be null"); return (outputMessage, context) -> { ResolvableType elementType = RESOURCE_TYPE; HttpMessageWriter<Resource> writer = findWriter(context, elementType, null); return write(Mono.just(resource), elementType, null, outputMessage, context, writer); }; }
@Nullable private String formatBody(@Nullable MediaType contentType, Mono<byte[]> body) { return body .map(bytes -> { if (contentType == null) { return bytes.length + " bytes of content (unknown content-type)."; } Charset charset = contentType.getCharset(); if (charset != null) { return new String(bytes, charset); } if (PRINTABLE_MEDIA_TYPES.stream().anyMatch(contentType::isCompatibleWith)) { return new String(bytes, StandardCharsets.UTF_8); } return bytes.length + " bytes of content."; }) .defaultIfEmpty("No content") .onErrorResume(ex -> Mono.just("Failed to obtain content: " + ex.getMessage())) .block(this.timeout); }
/** * Delete {@literal key}. * * @param key must not be {@literal null}. * @return * @see <a href="http://redis.io/commands/del">Redis Documentation: DEL</a> */ default Mono<Long> del(ByteBuffer key) { Assert.notNull(key, "Key must not be null!"); return del(Mono.just(new KeyCommand(key))).next().map(NumericResponse::getOutput); }
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))); }
/** * Remove a specific instance from services * * @param id the instances id to unregister * @return the id of the unregistered instance */ public Mono<InstanceId> deregister(InstanceId id) { return repository.computeIfPresent(id, (key, instance) -> Mono.just(instance.deregister())) .map(Instance::getId); } }
/** * Increment value of {@literal key} by 1. * * @param key must not be {@literal null}. * @return * @see <a href="http://redis.io/commands/incr">Redis Documentation: INCR</a> */ default Mono<Long> incr(ByteBuffer key) { Assert.notNull(key, "Key must not be null!"); return incr(Mono.just(new KeyCommand(key))).next().map(NumericResponse::getOutput); }
@Override public Mono<Void> write(Publisher<? extends MultiValueMap<String, String>> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { mediaType = getMediaType(mediaType); message.getHeaders().setContentType(mediaType); Charset charset = mediaType.getCharset(); Assert.notNull(charset, "No charset"); // should never occur return Mono.from(inputStream).flatMap(form -> { logFormData(form, hints); String value = serializeForm(form, charset); ByteBuffer byteBuffer = charset.encode(value); DataBuffer buffer = message.bufferFactory().wrap(byteBuffer); message.getHeaders().setContentLength(byteBuffer.remaining()); return message.writeWith(Mono.just(buffer)); }); }
@Override protected Mono<String> resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) { return chain.resolveUrlPath(resourceUrlPath, locations) .flatMap(baseUrl -> { if (StringUtils.hasText(baseUrl)) { VersionStrategy strategy = getStrategyForPath(resourceUrlPath); if (strategy == null) { return Mono.just(baseUrl); } return chain.resolveResource(null, baseUrl, locations) .flatMap(resource -> strategy.getResourceVersion(resource) .map(version -> strategy.addVersion(baseUrl, version))); } return Mono.empty(); }); }
/** * Determine the type stored at {@literal key}. * * @param key must not be {@literal null}. * @return * @see <a href="http://redis.io/commands/type">Redis Documentation: TYPE</a> */ default Mono<DataType> type(ByteBuffer key) { Assert.notNull(key, "Key must not be null!"); return type(Mono.just(new KeyCommand(key))).next().map(CommandResponse::getOutput); }
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 Mono<? extends Resource> transform(String content, Resource resource, ResourceTransformerChain chain, ServerWebExchange exchange) { if (!content.startsWith(MANIFEST_HEADER)) { if (logger.isTraceEnabled()) { logger.trace(exchange.getLogPrefix() + "Skipping " + resource + ": Manifest does not start with 'CACHE MANIFEST'"); } return Mono.just(resource); } return Flux.generate(new LineInfoGenerator(content)) .concatMap(info -> processLine(info, exchange, resource, chain)) .reduce(new ByteArrayOutputStream(), (out, line) -> { writeToByteArrayOutputStream(out, line + "\n"); return out; }) .map(out -> { String hash = DigestUtils.md5DigestAsHex(out.toByteArray()); writeToByteArrayOutputStream(out, "\n" + "# Hash: " + hash); return new TransformedResource(resource, out.toByteArray()); }); }
/** * Remove the expiration from given {@code key}. * * @param key must not be {@literal null}. * @return * @see <a href="http://redis.io/commands/persist">Redis Documentation: PERSIST</a> */ default Mono<Boolean> persist(ByteBuffer key) { Assert.notNull(key, "Key must not be null!"); return persist(Mono.just(new KeyCommand(key))).next().map(BooleanResponse::getOutput); }
/** * Get key set (fields) of hash at {@literal key}. * * @param key must not be {@literal null}. * @return * @see <a href="http://redis.io/commands/hkeys">Redis Documentation: HKEYS</a> */ default Flux<ByteBuffer> hKeys(ByteBuffer key) { Assert.notNull(key, "Key must not be null!"); return hKeys(Mono.just(new KeyCommand(key))).flatMap(CommandResponse::getOutput); }
private Mono<? extends Resource> transformContent(String cssContent, Resource resource, ResourceTransformerChain chain, ServerWebExchange exchange) { List<ContentChunkInfo> contentChunkInfos = parseContent(cssContent); if (contentChunkInfos.isEmpty()) { return Mono.just(resource); } return Flux.fromIterable(contentChunkInfos) .concatMap(contentChunkInfo -> { String contentChunk = contentChunkInfo.getContent(cssContent); if (contentChunkInfo.isLink() && !hasScheme(contentChunk)) { String link = toAbsolutePath(contentChunk, exchange); return resolveUrlPath(link, exchange, resource, chain).defaultIfEmpty(contentChunk); } else { return Mono.just(contentChunk); } }) .reduce(new StringWriter(), (writer, chunk) -> { writer.write(chunk); return writer; }) .map(writer -> { byte[] newContent = writer.toString().getBytes(DEFAULT_CHARSET); return new TransformedResource(resource, newContent); }); }
/** * Get the size of the stream stored at {@literal key}. * * @param key must not be {@literal null}. * @return {@link Mono} emitting the length of the stream. * @see <a href="http://redis.io/commands/xlen">Redis Documentation: XLEN</a> */ default Mono<Long> xLen(ByteBuffer key) { Assert.notNull(key, "Key must not be null!"); return xLen(Mono.just(new KeyCommand(key))).next().map(NumericResponse::getOutput); }
@Override public Flux<CommandResponse<SUnionCommand, Flux<ByteBuffer>>> sUnion(Publisher<SUnionCommand> commands) { return getConnection().execute(cmd -> Flux.from(commands).concatMap(command -> { Assert.notNull(command.getKeys(), "Keys must not be null!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(command.getKeys())) { return super.sUnion(Mono.just(command)); } Flux<ByteBuffer> result = Flux.merge(command.getKeys().stream().map(cmd::smembers).collect(Collectors.toList())) .distinct(); return Mono.just(new CommandResponse<>(command, result)); })); }