private Mono<ServerWebExchange> currentServerWebExchange() { return Mono.subscriberContext() .filter(c -> c.hasKey(ServerWebExchange.class)) .map(c -> c.get(ServerWebExchange.class)); }
@Test public void get() { assertThat((String) c.get(1)).isEqualTo("A"); assertThat((String) c.get(2)).isEqualTo("B"); assertThat((String) c.get(3)).isEqualTo("C"); assertThat((String) c.get(4)).isEqualTo("D"); assertThat((String) c.get(5)).isEqualTo("E"); assertThat((String) c.get(6)).isEqualTo("F"); }
/** * 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)); }
private Mono<ServerWebExchange> currentServerWebExchange() { return Mono.subscriberContext() .filter(c -> c.hasKey(ServerWebExchange.class)) .map(c -> c.get(ServerWebExchange.class)); }
/** * 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)); }
@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)); }); } }
@Test public void currentContextWithEmpty() throws InterruptedException { StepVerifier.create(Mono.just("foo") .flatMap(d -> Mono.subscriberContext() .map(c -> d + c.get(Integer.class)))) .verifyErrorMatches(t -> t instanceof NoSuchElementException && "Context is empty".equals(t.getMessage())); }
@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()); } }
@Test public void currentContext() throws InterruptedException { StepVerifier.create(Mono.just("foo") .flatMap(d -> Mono.subscriberContext() .map(c -> d + c.get(Integer.class))) .subscriberContext(ctx -> ctx.put(Integer.class, ctx.get(Integer.class) + 1)) .flatMapMany(Mono::just) .subscriberContext(ctx -> ctx.put(Integer.class, 0))) .expectNext("foo1") .verifyComplete(); }
@Test public void contextTest() { UnicastProcessor<Integer> p = UnicastProcessor.create(); p.subscriberContext(ctx -> ctx.put("foo", "bar")).subscribe(); assertThat(p.sink().currentContext().get("foo").toString()).isEqualTo("bar"); }
@Test public void contextSimple1() { String key = "message"; Mono<String> r = Mono.just("Hello") .flatMap( s -> Mono.subscriberContext() //<2> .map( ctx -> s + " " + ctx.get(key))) //<3> .subscriberContext(ctx -> ctx.put(key, "World")); //<1> StepVerifier.create(r) .expectNext("Hello World") //<4> .verifyComplete(); }
@Test public void contextTest() { StepVerifier.create(Flux.create(s -> IntStream.range(0, 10).forEach(i -> s.next(s .currentContext() .get(AtomicInteger.class) .incrementAndGet()))) .take(10) .subscriberContext(ctx -> ctx.put(AtomicInteger.class, new AtomicInteger()))) .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .verifyComplete(); }
@Test public void contextTestPush() { StepVerifier.create(Flux.push(s -> IntStream.range(0, 10).forEach(i -> s.next(s .currentContext() .get(AtomicInteger.class) .incrementAndGet()))) .take(10) .subscriberContext(ctx -> ctx.put(AtomicInteger.class, new AtomicInteger()))) .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .verifyComplete(); }
@Test public void getThrows() throws Exception { assertThatExceptionOfType(NoSuchElementException.class) .isThrownBy(() -> c.get(1)) .withMessage("Context is empty"); }
@Test public void contextSimple4() { String key = "message"; Mono<String> r = Mono.just("Hello") .flatMap( s -> Mono.subscriberContext() .map( ctx -> s + " " + ctx.get(key))) .subscriberContext(ctx -> ctx.put(key, "Reactor")) //<1> .subscriberContext(ctx -> ctx.put(key, "World")); //<2> StepVerifier.create(r) .expectNext("Hello Reactor") //<3> .verifyComplete(); }
@Test public void contextTest() { StepVerifier.create(Mono.create(s -> s.success(s.currentContext() .get(AtomicInteger.class) .incrementAndGet())) .subscriberContext(ctx -> ctx.put(AtomicInteger.class, new AtomicInteger()))) .expectNext(1) .verifyComplete(); }
@Test public void contextTest() { StepVerifier.create(Flux.generate(s -> s.next(s.currentContext() .get(AtomicInteger.class) .incrementAndGet())) .take(10) .subscriberContext(ctx -> ctx.put(AtomicInteger.class, new AtomicInteger()))) .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .verifyComplete(); }
@Test public void contextTest() { StepVerifier.create(Flux.just("foo") .handle((d, s) -> s.next(s.currentContext() .get(AtomicInteger.class) .incrementAndGet())) .repeat(9) .subscriberContext(ctx -> ctx.put(AtomicInteger.class, new AtomicInteger()))) .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .verifyComplete(); }
@Test public void contextGetMono() throws InterruptedException { StepVerifier.create(Mono.just(1) .log() .handle((d, c) -> c.next(c.currentContext().get("test") + "" + d)) .handle((d, c) -> c.next(c.currentContext().get("test2") + "" + d)) .subscriberContext(ctx -> ctx.put("test2", "bar")) .subscriberContext(ctx -> ctx.put("test", "foo")) .log()) .expectNext("barfoo1") .verifyComplete(); }