Java Code Examples for javax.enterprise.context.Conversation#getId()

The following examples show how to use javax.enterprise.context.Conversation#getId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConversationMiddleware.java    From enkan with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain) {
    ConversationToken token = parseToken(readTokenFunc.apply(request));

    if (!isGetRequest(request) && !token.isValid()) {
        return builder(HttpResponse.of("Invalid conversation."))
                .set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/plain"))
                .set(HttpResponse::setStatus, 403)
                .build();
    }

    Conversation conversation;
    if (token.id == null) {
        conversation = new DefaultConversation();
    } else {
        conversation = new DefaultConversation(token.id);
        ConversationState state = (ConversationState) store.read(token.id);
        request.setConversationState(state);
    }
    request.setConversation(conversation);

    HttpResponse response = castToHttpResponse(chain.next(request));
    if (conversation.isTransient()) {
        if (conversation.getId() != null) {
            store.delete(conversation.getId());
        }
    } else if (response.getConversationState() != null) {
        store.write(conversation.getId(), response.getConversationState());
    }
    return response;
}
 
Example 2
Source File: RenderTemplateMiddleware.java    From enkan with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain) {
    HttpResponse response = castToHttpResponse(chain.next(request));
    if (TemplatedHttpResponse.class.isInstance(response)) {
        TemplatedHttpResponse tres = TemplatedHttpResponse.class.cast(response);
        if (exports.contains(REQUEST)) {
            tres.getContext().put(exports.getExportName(REQUEST), request);
        }

        if (exports.contains(PARAMS)) {
            tres.getContext().put(exports.getExportName(PARAMS), request.getParams());
        }

        if (exports.contains(USER_PRINCIPAL)) {
            Stream.of(request)
                    .filter(Objects::nonNull)
                    .map(PrincipalAvailable.class::cast)
                    .findAny()
                    .ifPresent(principal -> tres.getContext()
                            .put(exports.getExportName(USER_PRINCIPAL), principal.getPrincipal()));
            tres.getContext().put("hasPermission", templateEngine.createFunction(HAS_PERMISSION));
            tres.getContext().put("hasAnyPermissions", templateEngine.createFunction(HAS_ANY_PERMISSIONS));
            tres.getContext().put("hasAllPermissions", templateEngine.createFunction(HAS_ALL_PERMISSIONS));
        }
        if (exports.contains(SESSION)) {
            tres.getContext().put(exports.getExportName(SESSION), request.getSession());
        }

        if (exports.contains(CONVERSATION)) {
            Conversation conversation = request.getConversation();
            if (conversation != null && !request.getConversation().isTransient()) {
                String token = conversation.getId() + "$"
                        + hmacEncoder.encodeToHex(conversation.getId() + "$" + conversation.getTimeout())
                        + "$" + conversation.getTimeout();
                tres.getContext().put("conversationToken", token);
            }
            tres.getContext().put(exports.getExportName(CONVERSATION), conversation);
        }

        if (exports.contains(CONVERSATION_STATE)) {
            ConversationState conversationState = request.getConversationState();
            tres.getContext().put(exports.getExportName(CONVERSATION_STATE), conversationState);
        }

        userFunctions.forEach((key, value) -> tres.getContext().put(key, templateEngine.createFunction(value)));
        render(tres);
    }
    return response;
}