reactor.util.context.Context.hasKey()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(232)

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

Context.hasKey介绍

[英]Return true if a particular key resolves to a value within the Context.
[中]如果特定键解析为上下文中的值,则返回true。

代码示例

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

/**
 * Resolve a value given a key within the {@link Context}.
 *
 * @param key a lookup key to resolve the value within the context
 *
 * @return an {@link Optional} of the value for that key.
 */
default <T> Optional<T> getOrEmpty(Object key){
  if(hasKey(key)) {
    return Optional.of(get(key));
  }
  return Optional.empty();
}

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

@Override
public StepVerifier.ContextExpectations<T> hasKey(Object key) {
  this.contextExpectations = this.contextExpectations.andThen(c -> {
      if (!c.hasKey(key))
        throw errorFormatter.assertionError(String.format("Key %s not found in Context %s", key, c));
  });
  return this;
}

代码示例来源: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

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: lettuce-io/lettuce-core

/**
 * Gets the {@link TraceContextProvider} from Reactor {@link Context}.
 *
 * @return the {@link TraceContextProvider}.
 */
static Mono<TraceContextProvider> getContext() {
  return Mono.subscriberContext().filter(c -> c.hasKey(TraceContextProvider.class))
      .map(c -> c.get(TraceContextProvider.class));
}

代码示例来源:origin: org.springframework.security/spring-security-core

/**
 * 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-data-mongodb

/**
 * Gets the {@code Mono<ClientSession>} from Reactor {@link reactor.util.context.Context}. The resulting {@link Mono}
 * emits the {@link ClientSession} if a session is associated with the current {@link reactor.util.context.Context
 * subscriber context}. If the context does not contain a session, the resulting {@link Mono} terminates empty (i.e.
 * without emitting a value).
 *
 * @return the {@link Mono} emitting the client session if present; otherwise the {@link Mono} terminates empty.
 */
public static Mono<ClientSession> getSession() {
  return Mono.subscriberContext().filter(ctx -> ctx.hasKey(SESSION_KEY))
      .flatMap(ctx -> ctx.<Mono<ClientSession>> get(SESSION_KEY));
}

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

@Test
public void hasKey() throws Exception {
  assertThat(c.hasKey(1)).as("hasKey(1)").isTrue();
  assertThat(c.hasKey(2)).as("hasKey(2)").isTrue();
  assertThat(c.hasKey(3)).as("hasKey(3)").isTrue();
  assertThat(c.hasKey(4)).as("hasKey(4)").isTrue();
  assertThat(c.hasKey(5)).as("hasKey(5)").isTrue();
  assertThat(c.hasKey(6)).as("hasKey(6)").isTrue();
  assertThat(c.hasKey(7)).as("hasKey(7)").isFalse();
}

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

static Condition<Context> key(Object k) {
  return new Condition<>(c -> c.hasKey(k), "key <%s>", k);
}

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

@Test
public void hasKey() throws Exception {
  assertThat(c.hasKey(1)).as("hasKey(1)").isFalse();
}

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

SpanSubscription<T> newCoreSubscriber(Tracing tracing) {
  Span root = this.context.hasKey(Span.class) ? this.context.get(Span.class)
      : tracing.tracer().currentSpan();
  return new ScopePassingSpanSubscriber<>(this.subscriber, this.context, tracing,
      root);
}

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

static <T> CoreSubscriber<? super T> scopePassingSpanSubscription(Tracing tracing,
    CoreSubscriber<? super T> sub) {
  Context context = sub.currentContext();
  Span root = context.hasKey(Span.class) ? context.get(Span.class)
      : tracing.tracer().currentSpan();
  if (root != null) {
    return new ScopePassingSpanSubscriber<>(sub, context, tracing, root);
  }
  else {
    return sub; // no need to trace
  }
}

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

@Test
public void monoSubscriberContextPutsAll() {
  StepVerifier.create(
      Mono.just("foo")
        .flatMap(v -> Mono.subscriberContext())
        .subscriberContext(Context.of("foo", "bar", 1, "baz"))
        .subscriberContext(Context.of("initial", "value"))
  )
        .expectNextMatches(c -> c.hasKey("foo") && c.hasKey(1) && c.hasKey("initial"))
        .verifyComplete();
}

代码示例来源: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 fluxSubscriberContextPutsAll() {
  StepVerifier.create(
      Flux.just("foo")
        .flatMap(v -> Mono.subscriberContext())
        .subscriberContext(Context.of("foo", "bar", 1, "baz"))
        .subscriberContext(Context.of("initial", "value"))
  )
        .expectNextMatches(c -> c.hasKey("foo") && c.hasKey(1) && c.hasKey("initial"))
        .verifyComplete();
}

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

@Test
public void monoSubscriberContextWithMergedEmpty() {
  StepVerifier.create(
      Mono.just("foo")
        .flatMap(v -> Mono.subscriberContext())
        .subscriberContext(Context.empty())
        .subscriberContext(Context.of("initial", "value"))
  )
        .expectNextMatches(c -> c.hasKey("initial"))
        .verifyComplete();
}

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

@Test
public void nextCompleteAndErrorHaveContext() {
  Context context = Context.of("foo", "bar");
  List<Signal> signals = new ArrayList<>();
  StepVerifier.create(Flux.just("hello")
              .doOnEach(signals::add),
      StepVerifierOptions.create().withInitialContext(context))
        .expectNext("hello")
        .verifyComplete();
  assertThat(signals)
       .allSatisfy(signal -> assertThat(signal.getContext().hasKey("foo"))
           .as("has Context value")
           .isTrue());
}

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

@Test
public void fluxSubscriberContextWithMergedEmpty() {
  StepVerifier.create(
      Flux.just("foo")
        .flatMap(v -> Mono.subscriberContext())
        .subscriberContext(Context.empty())
        .subscriberContext(Context.of("initial", "value"))
  )
        .expectNextMatches(c -> c.hasKey("initial"))
        .verifyComplete();
}

相关文章