reactor.core.publisher.Mono.filter()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(831)

本文整理了Java中reactor.core.publisher.Mono.filter()方法的一些代码示例,展示了Mono.filter()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mono.filter()方法的具体详情如下:
包路径:reactor.core.publisher.Mono
类名称:Mono
方法名:filter

Mono.filter介绍

[英]If this Mono is valued, test the result and replay it if predicate returns true. Otherwise complete without value.
[中]如果这个Mono被赋值,测试结果并在谓词返回true时重播它。否则就没有价值了。

代码示例

代码示例来源:origin: spring-projects/spring-security

/**
 * Attempts to load the client registration id from the current {@link Authentication}
 * @return
 */
private Mono<String> clientRegistrationId(Mono<Authentication> authentication) {
  return authentication
      .filter(t -> this.defaultOAuth2AuthorizedClient && t instanceof OAuth2AuthenticationToken)
      .cast(OAuth2AuthenticationToken.class)
      .map(OAuth2AuthenticationToken::getAuthorizedClientRegistrationId);
}

代码示例来源:origin: spring-projects/spring-security

/**
 * Attempts to load the client registration id from the current {@link Authentication}
 * @return
 */
private Mono<String> clientRegistrationId(Mono<Authentication> authentication) {
  return authentication
      .filter(t -> this.defaultOAuth2AuthorizedClient && t instanceof OAuth2AuthenticationToken)
      .cast(OAuth2AuthenticationToken.class)
      .map(OAuth2AuthenticationToken::getAuthorizedClientRegistrationId);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected Mono<Object> resolveName(String name, MethodParameter parameter, ServerWebExchange exchange) {
  return exchange.getSession()
      .filter(session -> session.getAttribute(name) != null)
      .map(session -> session.getAttribute(name));
}

代码示例来源:origin: spring-projects/spring-security

@Override
public Mono<OAuth2AuthorizationRequest> loadAuthorizationRequest(
    ServerWebExchange exchange) {
  String state = getStateParameter(exchange);
  if (state == null) {
    return Mono.empty();
  }
  return getStateToAuthorizationRequest(exchange, false)
      .filter(stateToAuthorizationRequest -> stateToAuthorizationRequest.containsKey(state))
      .map(stateToAuthorizationRequest -> stateToAuthorizationRequest.get(state));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return a new {@code DataBuffer} composed from joining together the given
 * {@code dataBuffers} elements. Depending on the {@link DataBuffer} type,
 * the returned buffer may be a single buffer containing all data of the
 * provided buffers, or it may be a zero-copy, composite with references to
 * the given buffers.
 * <p>If {@code dataBuffers} produces an error or if there is a cancel
 * signal, then all accumulated buffers will be
 * {@linkplain #release(DataBuffer) released}.
 * <p>Note that the given data buffers do <strong>not</strong> have to be
 * released. They will be released as part of the returned composite.
 * @param dataBuffers the data buffers that are to be composed
 * @return a buffer that is composed from the {@code dataBuffers} argument
 * @since 5.0.3
 */
public static Mono<DataBuffer> join(Publisher<DataBuffer> dataBuffers) {
  Assert.notNull(dataBuffers, "'dataBuffers' must not be null");
  return Flux.from(dataBuffers)
      .collectList()
      .filter(list -> !list.isEmpty())
      .map(list -> list.get(0).factory().join(list))
      .doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}

代码示例来源:origin: spring-projects/spring-security

@Override
public Mono<OAuth2AuthorizationRequest> resolve(ServerWebExchange exchange) {
  return this.authorizationRequestMatcher.matches(exchange)
      .filter(matchResult -> matchResult.isMatch())
      .map(ServerWebExchangeMatcher.MatchResult::getVariables)
      .map(variables -> variables.get(DEFAULT_REGISTRATION_ID_URI_VARIABLE_NAME))
      .cast(String.class)
      .flatMap(clientRegistrationId -> resolve(exchange, clientRegistrationId));
}

代码示例来源:origin: codecentric/spring-boot-admin

/**
 * Get a single instance.
 *
 * @param id The application identifier.
 * @return The registered application.
 */
@GetMapping(path = "/instances/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<ResponseEntity<Instance>> instance(@PathVariable String id) {
  LOGGER.debug("Deliver registered instance with ID '{}'", id);
  return registry.getInstance(InstanceId.of(id))
          .filter(Instance::isRegistered)
          .map(ResponseEntity::ok)
          .defaultIfEmpty(ResponseEntity.notFound().build());
}

代码示例来源:origin: spring-projects/spring-security

@Override
public Mono<Authentication> authenticate(Authentication authentication) {
  return Mono.justOrEmpty(authentication)
      .filter(a -> a instanceof  BearerTokenAuthenticationToken)
      .cast(BearerTokenAuthenticationToken.class)
      .map(BearerTokenAuthenticationToken::getToken)
      .flatMap(this.jwtDecoder::decode)
      .flatMap(this.jwtAuthenticationConverter::convert)
      .cast(Authentication.class)
      .onErrorMap(JwtException.class, this::onError);
}

代码示例来源:origin: spring-projects/spring-security

@Override
public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException denied) {
  Map<String, String> parameters = new LinkedHashMap<>();
  if (this.realmName != null) {
    parameters.put("realm", this.realmName);
  }
  return exchange.getPrincipal()
      .filter(AbstractOAuth2TokenAuthenticationToken.class::isInstance)
      .cast(AbstractOAuth2TokenAuthenticationToken.class)
      .map(token -> errorMessageParameters(token, parameters))
      .switchIfEmpty(Mono.just(parameters))
      .flatMap(params -> respond(exchange, params));
}

代码示例来源:origin: spring-projects/spring-security

private Mono<ServerWebExchange> currentServerWebExchange() {
  return Mono.subscriberContext()
      .filter(c -> c.hasKey(ServerWebExchange.class))
      .map(c -> c.get(ServerWebExchange.class));
}

代码示例来源:origin: spring-projects/spring-security

private Mono<ServerWebExchange> currentServerWebExchange() {
  return Mono.subscriberContext()
      .filter(c -> c.hasKey(ServerWebExchange.class))
      .map(c -> c.get(ServerWebExchange.class));
}

代码示例来源:origin: spring-projects/spring-security

/**
 * Gets the {@code Mono<SecurityContext>} from Reactor {@link Context}
 * @return the {@code Mono<SecurityContext>}
 */
public static Mono<SecurityContext> getContext() {
  return Mono.subscriberContext()
    .filter( c -> c.hasKey(SECURITY_CONTEXT_KEY))
    .flatMap( c-> c.<Mono<SecurityContext>>get(SECURITY_CONTEXT_KEY));
}

代码示例来源:origin: spring-projects/spring-security

@Override
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T object) {
  return authentication
    .filter(a -> a.isAuthenticated())
    .flatMapIterable( a -> a.getAuthorities())
    .map(g -> g.getAuthority())
    .any(a -> this.authorities.contains(a))
    .map( hasAuthority -> new AuthorizationDecision(hasAuthority))
    .defaultIfEmpty(new AuthorizationDecision(false));
}

代码示例来源:origin: codecentric/spring-boot-admin

@GetMapping(path = "/applications/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<ResponseEntity<Application>> application(@PathVariable("name") String name) {
  return this.toApplication(name, registry.getInstances(name).filter(Instance::isRegistered))
        .filter(a -> !a.getInstances().isEmpty())
        .map(ResponseEntity::ok)
        .defaultIfEmpty(ResponseEntity.notFound().build());
}

代码示例来源:origin: codecentric/spring-boot-admin

@Override
public Mono<Void> notify(InstanceEvent event) {
  if (!enabled) {
    return Mono.empty();
  }
  return repository.find(event.getInstance())
           .filter(instance -> shouldNotify(event, instance))
           .flatMap(instance -> doNotify(event, instance))
           .doOnError(ex -> getLogger().error("Couldn't notify for event {} ", event, ex))
           .then();
}

代码示例来源:origin: spring-projects/spring-security

@Override
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T object) {
  return authentication
    .filter(this::isNotAnonymous)
    .map(a -> new AuthorizationDecision(a.isAuthenticated()))
    .defaultIfEmpty(new AuthorizationDecision(false));
}

代码示例来源:origin: spring-projects/spring-security

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  return this.requiresAuthenticationMatcher.matches(exchange)
      .filter( matchResult -> matchResult.isMatch())
      .flatMap( matchResult -> this.authenticationConverter.convert(exchange))
      .switchIfEmpty(chain.filter(exchange).then(Mono.empty()))
      .flatMap( token -> authenticate(exchange, chain, token));
}

代码示例来源:origin: lettuce-io/lettuce-core

@Override
  public Mono<TraceContext> getTraceContextLater() {
    return Mono.subscriberContext()
        .filter(it -> it.hasKey(Span.class) || it.hasKey(brave.propagation.TraceContext.class)).map(it -> {
          if (it.hasKey(Span.class)) {
            return new BraveTraceContext(it.get(Span.class).context());
          }
          return new BraveTraceContext(it.get(brave.propagation.TraceContext.class));
        });
  }
}

代码示例来源:origin: spring-projects/spring-security

@GetMapping("/**")
  Mono<String> pathWithinApplicationFromContext() {
    return Mono.subscriberContext()
      .filter(c -> c.hasKey(ServerWebExchange.class))
      .map(c -> c.get(ServerWebExchange.class))
      .map(e -> e.getRequest().getPath().pathWithinApplication().value());
  }
}

代码示例来源:origin: reactor/reactor-core

@Test
  public void contextForLibraryReactivePutNoContext() {
  Mono<String> put = doPut("www.example.com", Mono.just("Walter"))
      .filter(t -> t.getT1() < 300)
      .map(Tuple2::getT2);

    StepVerifier.create(put)
          .expectNext("PUT <Walter> sent to www.example.com")
          .verifyComplete();
  }
}

相关文章

Mono类方法