class RequestHandler {

    public Flux<InvocationResponse> handle(AuthenticatedRequest request) {
        Optional<MailboxSession> mailboxSession = Optional.ofNullable(request.getMailboxSession());
        try (Closeable closeable =
                 MDCBuilder.create()
                     .addContext(MDCBuilder.USER, mailboxSession.map(MailboxSession::getUser).map(Username::asString))
                     .addContext(MDCBuilder.SESSION_ID, mailboxSession.map(MailboxSession::getSessionId))
                     .addContext(MDCBuilder.ACTION, request.getMethodName().getName())
                     .build()) {
            return Optional.ofNullable(methods.get(request.getMethodName()))
                .map(extractAndProcess(request))
                .map(jmapResponseWriter::formatMethodResponse)
                .orElseThrow(() -> new IllegalStateException("unknown method " + request.getMethodName()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Inject public  RequestHandler(Set<Method> methods, JmapRequestParser jmapRequestParser, JmapResponseWriter jmapResponseWriter);

}