Search in sources :

Example 1 with UsernameContext

use of de.spring.example.context.UsernameContext in project JavaForFun by gumartinm.

the class AdController method findAll.

@Override
public Flux<Ad> findAll() {
    Flux<Ad> entities = repository.findAll();
    return entities.flatMap(ad -> {
        return Mono.subscriberContext().map(context -> {
            UsernameContext usernameContext = context.get(UsernameContext.class);
            UsernameContext lol = usernameContext;
            return ad;
        });
    });
}
Also used : UsernameContext(de.spring.example.context.UsernameContext) Ad(de.spring.example.persistence.domain.Ad)

Example 2 with UsernameContext

use of de.spring.example.context.UsernameContext in project JavaForFun by gumartinm.

the class UsernameFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    ServerHttpRequest request = exchange.getRequest();
    if (!request.getHeaders().containsKey(UsernameThreadContext.USERNAME_HEADER)) {
        return chain.filter(exchange);
    }
    String username = request.getHeaders().get(UsernameThreadContext.USERNAME_HEADER).get(0);
    return chain.filter(exchange).compose(function -> function.then(Mono.subscriberContext()).doOnSubscribe(onSubscribe -> {
        MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
    }).doOnError(throwable -> {
        MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
    }).onErrorMap(throwable -> {
        MDC.put(UsernameThreadContext.USERNAME_HEADER, username);
        return throwable;
    }).doFinally(onFinally -> {
        MDC.remove(UsernameThreadContext.USERNAME_HEADER);
    }).flatMap(context -> {
        Mono<Void> continuation = Mono.empty();
        return continuation;
    }).subscriberContext(context -> {
        Context updatedContext = context;
        if (!context.hasKey(UsernameContext.class)) {
            updatedContext = context.put(UsernameContext.class, new UsernameContext(username));
        }
        return updatedContext;
    }));
}
Also used : UsernameThreadContext(de.spring.example.context.UsernameThreadContext) WebFilter(org.springframework.web.server.WebFilter) MDC(org.slf4j.MDC) Context(reactor.util.context.Context) Mono(reactor.core.publisher.Mono) UsernameContext(de.spring.example.context.UsernameContext) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilterChain(org.springframework.web.server.WebFilterChain) UsernameThreadContext(de.spring.example.context.UsernameThreadContext) Context(reactor.util.context.Context) UsernameContext(de.spring.example.context.UsernameContext) UsernameContext(de.spring.example.context.UsernameContext) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Aggregations

UsernameContext (de.spring.example.context.UsernameContext)2 UsernameThreadContext (de.spring.example.context.UsernameThreadContext)1 Ad (de.spring.example.persistence.domain.Ad)1 MDC (org.slf4j.MDC)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 WebFilter (org.springframework.web.server.WebFilter)1 WebFilterChain (org.springframework.web.server.WebFilterChain)1 Mono (reactor.core.publisher.Mono)1 Context (reactor.util.context.Context)1