Search in sources :

Example 1 with SessionValue

use of io.micronaut.session.annotation.SessionValue in project micronaut-core by micronaut-projects.

the class HttpSessionFilter method encodeSessionId.

private Publisher<MutableHttpResponse<?>> encodeSessionId(HttpRequest<?> request, Publisher<MutableHttpResponse<?>> responsePublisher) {
    Flux<SessionAndResponse> responseFlowable = Flux.from(responsePublisher).switchMap(response -> {
        Optional<MethodExecutionHandle> routeMatch = request.getAttribute(HttpAttributes.ROUTE_MATCH, MethodExecutionHandle.class);
        Optional<?> body = response.getBody();
        String sessionAttr;
        if (body.isPresent()) {
            sessionAttr = routeMatch.flatMap(m -> {
                if (!m.hasAnnotation(SessionValue.class)) {
                    return Optional.empty();
                } else {
                    String attributeName = m.stringValue(SessionValue.class).orElse(null);
                    if (!StringUtils.isEmpty(attributeName)) {
                        return Optional.of(attributeName);
                    } else {
                        throw new InternalServerException("@SessionValue on a return type must specify an attribute name");
                    }
                }
            }).orElse(null);
        } else {
            sessionAttr = null;
        }
        Optional<Session> opt = request.getAttributes().get(SESSION_ATTRIBUTE, Session.class);
        if (opt.isPresent()) {
            Session session = opt.get();
            if (sessionAttr != null) {
                session.put(sessionAttr, body.get());
            }
            if (session.isNew() || session.isModified()) {
                return Flux.from(Publishers.fromCompletableFuture(() -> sessionStore.save(session))).map(s -> new SessionAndResponse(Optional.of(s), response));
            }
        } else if (sessionAttr != null) {
            Session newSession = sessionStore.newSession();
            newSession.put(sessionAttr, body.get());
            return Flux.from(Publishers.fromCompletableFuture(() -> sessionStore.save(newSession))).map(s -> new SessionAndResponse(Optional.of(s), response));
        }
        return Flux.just(new SessionAndResponse(opt, response));
    });
    return responseFlowable.map(sessionAndResponse -> {
        Optional<Session> session = sessionAndResponse.session;
        MutableHttpResponse<?> response = sessionAndResponse.response;
        if (session.isPresent()) {
            Session s = session.get();
            for (HttpSessionIdEncoder encoder : encoders) {
                encoder.encodeId(request, response, s);
            }
        }
        return response;
    });
}
Also used : Filter(io.micronaut.http.annotation.Filter) Publishers(io.micronaut.core.async.publisher.Publishers) ServerFilterChain(io.micronaut.http.filter.ServerFilterChain) Publisher(org.reactivestreams.Publisher) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) SessionValue(io.micronaut.session.annotation.SessionValue) MethodExecutionHandle(io.micronaut.inject.MethodExecutionHandle) StringUtils(io.micronaut.core.util.StringUtils) Flux(reactor.core.publisher.Flux) Session(io.micronaut.session.Session) List(java.util.List) CollectionUtils(io.micronaut.core.util.CollectionUtils) SessionStore(io.micronaut.session.SessionStore) HttpServerFilter(io.micronaut.http.filter.HttpServerFilter) InternalServerException(io.micronaut.http.server.exceptions.InternalServerException) Optional(java.util.Optional) HttpAttributes(io.micronaut.http.HttpAttributes) HttpRequest(io.micronaut.http.HttpRequest) ServerFilterPhase(io.micronaut.http.filter.ServerFilterPhase) InternalServerException(io.micronaut.http.server.exceptions.InternalServerException) SessionValue(io.micronaut.session.annotation.SessionValue) MethodExecutionHandle(io.micronaut.inject.MethodExecutionHandle) Session(io.micronaut.session.Session)

Aggregations

Publishers (io.micronaut.core.async.publisher.Publishers)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 StringUtils (io.micronaut.core.util.StringUtils)1 HttpAttributes (io.micronaut.http.HttpAttributes)1 HttpRequest (io.micronaut.http.HttpRequest)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 Filter (io.micronaut.http.annotation.Filter)1 HttpServerFilter (io.micronaut.http.filter.HttpServerFilter)1 ServerFilterChain (io.micronaut.http.filter.ServerFilterChain)1 ServerFilterPhase (io.micronaut.http.filter.ServerFilterPhase)1 InternalServerException (io.micronaut.http.server.exceptions.InternalServerException)1 MethodExecutionHandle (io.micronaut.inject.MethodExecutionHandle)1 Session (io.micronaut.session.Session)1 SessionStore (io.micronaut.session.SessionStore)1 SessionValue (io.micronaut.session.annotation.SessionValue)1 List (java.util.List)1 Optional (java.util.Optional)1 Publisher (org.reactivestreams.Publisher)1 Flux (reactor.core.publisher.Flux)1