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;
});
});
}
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;
}));
}
Aggregations