Search in sources :

Example 1 with ConversationContext

use of org.jboss.weld.context.ConversationContext in project core by weld.

the class ConversationPropagationFilter method wrapResponse.

private ServletResponse wrapResponse(HttpServletResponse response, final String requestPath) {
    return new HttpServletResponseWrapper(response) {

        @Override
        public void sendRedirect(String path) throws IOException {
            FacesContext context = FacesContext.getCurrentInstance();
            if (context != null) {
                // this is a JSF request
                ConversationContext conversationContext = instance(contextId).select(HttpConversationContext.class).get();
                if (conversationContext.isActive()) {
                    Conversation conversation = conversationContext.getCurrentConversation();
                    if (!conversation.isTransient()) {
                        path = new FacesUrlTransformer(path, context).toRedirectViewId().toActionUrl().appendConversationIdIfNecessary(conversationContext.getParameterName(), conversation.getId()).encode();
                    }
                }
            }
            super.sendRedirect(path);
        }
    };
}
Also used : FacesContext(javax.faces.context.FacesContext) FacesUrlTransformer(org.jboss.weld.module.jsf.FacesUrlTransformer) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) Conversation(javax.enterprise.context.Conversation) HttpConversationContext(org.jboss.weld.context.http.HttpConversationContext) ConversationContext(org.jboss.weld.context.ConversationContext) HttpConversationContext(org.jboss.weld.context.http.HttpConversationContext)

Example 2 with ConversationContext

use of org.jboss.weld.context.ConversationContext in project core by weld.

the class ConversationContextActivator method deactivateConversationContext.

protected void deactivateConversationContext(HttpServletRequest request) {
    try {
        ConversationContext conversationContext = httpConversationContext();
        if (conversationContext.isActive()) {
            // Only deactivate the context if one is already active, otherwise we get Exceptions
            if (conversationContext instanceof LazyHttpConversationContextImpl) {
                LazyHttpConversationContextImpl lazyConversationContext = (LazyHttpConversationContextImpl) conversationContext;
                if (!lazyConversationContext.isInitialized()) {
                    // if this lazy conversation has not been touched yet, just deactivate it
                    lazyConversationContext.deactivate();
                    processDestructionQueue(request);
                    return;
                }
            }
            boolean isTransient = conversationContext.getCurrentConversation().isTransient();
            if (ConversationLogger.LOG.isTraceEnabled()) {
                if (isTransient) {
                    ConversationLogger.LOG.cleaningUpTransientConversation();
                } else {
                    ConversationLogger.LOG.cleaningUpConversation(conversationContext.getCurrentConversation().getId());
                }
            }
            if (isTransient) {
                conversationBeforeDestroyedEvent.fire(request);
            }
            conversationContext.invalidate();
            conversationContext.deactivate();
            if (isTransient) {
                conversationDestroyedEvent.fire(request);
            }
            processDestructionQueue(request);
        }
    } catch (Exception e) {
        ServletLogger.LOG.unableToDeactivateContext(httpConversationContext(), request);
        ServletLogger.LOG.catchingDebug(e);
    }
}
Also used : LazyHttpConversationContextImpl(org.jboss.weld.module.web.context.http.LazyHttpConversationContextImpl) HttpConversationContext(org.jboss.weld.context.http.HttpConversationContext) ConversationContext(org.jboss.weld.context.ConversationContext) AbstractConversationContext(org.jboss.weld.contexts.AbstractConversationContext)

Example 3 with ConversationContext

use of org.jboss.weld.context.ConversationContext in project core by weld.

the class ContextTest method testCallToConversationWithContextNotActive.

/*
    * description = "WELD-348"
    */
@Test
public void testCallToConversationWithContextNotActive() {
    ConversationContext conversationContext;
    try {
        conversationContext = Utils.getActiveContext(beanManager, ConversationContext.class);
    } catch (ContextNotActiveException e) {
        conversationContext = Utils.getReference(beanManager, HttpConversationContext.class);
    }
    new WorkInInactiveContext(conversationContext) {

        @Override
        protected void work() {
            try {
                Utils.getReference(beanManager, Conversation.class).getId();
                Assert.fail("Expected ContextNotActiveException, but no exception was thrown");
            } catch (ContextNotActiveException e) {
            // Expected
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail("Expected ContextNotActiveException, but another exception was thrown: " + e);
            }
            try {
                Utils.getReference(beanManager, Conversation.class).getTimeout();
                Assert.fail();
            } catch (ContextNotActiveException e) {
            // Expected
            } catch (Exception e) {
                Assert.fail();
            }
            try {
                Utils.getReference(beanManager, Conversation.class).begin();
                Assert.fail();
            } catch (ContextNotActiveException e) {
            // Expected
            } catch (Exception e) {
                Assert.fail();
            }
            try {
                Utils.getReference(beanManager, Conversation.class).begin("foo");
                Assert.fail();
            } catch (ContextNotActiveException e) {
            // Expected
            } catch (Exception e) {
                Assert.fail();
            }
            try {
                Utils.getReference(beanManager, Conversation.class).end();
                Assert.fail();
            } catch (ContextNotActiveException e) {
            // Expected
            } catch (Exception e) {
                Assert.fail();
            }
            try {
                Utils.getReference(beanManager, Conversation.class).isTransient();
                Assert.fail();
            } catch (ContextNotActiveException e) {
            // Expected
            } catch (Exception e) {
                Assert.fail();
            }
            try {
                Utils.getReference(beanManager, Conversation.class).setTimeout(0);
                assert false;
            } catch (ContextNotActiveException e) {
            // Expected
            } catch (Exception e) {
                Assert.fail();
            }
        }
    }.run();
}
Also used : ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) HttpConversationContext(org.jboss.weld.context.http.HttpConversationContext) ConversationContext(org.jboss.weld.context.ConversationContext) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) Test(org.junit.Test)

Example 4 with ConversationContext

use of org.jboss.weld.context.ConversationContext in project core by weld.

the class ConversationImpl method notifyConversationContext.

private void notifyConversationContext() {
    ConversationContext context = getActiveConversationContext();
    if (context instanceof AbstractConversationContext) {
        AbstractConversationContext<?, ?> abstractConversationContext = (AbstractConversationContext<?, ?>) context;
        abstractConversationContext.conversationPromotedToLongRunning(this);
    }
}
Also used : AbstractConversationContext(org.jboss.weld.contexts.AbstractConversationContext) ConversationContext(org.jboss.weld.context.ConversationContext) AbstractConversationContext(org.jboss.weld.contexts.AbstractConversationContext)

Example 5 with ConversationContext

use of org.jboss.weld.context.ConversationContext in project core by weld.

the class ConversationAwareViewHandler method getActionURL.

/**
 * Allow the delegate to produce the action URL. If the conversation is
 * long-running, append the conversation id request parameter to the query
 * string part of the URL, but only if the request parameter is not already
 * present.
 * <p/>
 * This covers form actions Ajax calls, and redirect URLs (which we want) and
 * link hrefs (which we don't)
 *
 * @see {@link ViewHandler#getActionURL(FacesContext, String)}
 */
@Override
public String getActionURL(FacesContext facesContext, String viewId) {
    if (contextId == null) {
        if (facesContext.getAttributes().containsKey(Container.CONTEXT_ID_KEY)) {
            contextId = (String) facesContext.getAttributes().get(Container.CONTEXT_ID_KEY);
        } else {
            contextId = RegistrySingletonProvider.STATIC_INSTANCE;
        }
    }
    String actionUrl = super.getActionURL(facesContext, viewId);
    final ConversationContext ctx = getConversationContext(contextId);
    if (ctx != null && ctx.isActive() && !getSource().equals(Source.BOOKMARKABLE) && !ctx.getCurrentConversation().isTransient()) {
        return new FacesUrlTransformer(actionUrl, facesContext).appendConversationIdIfNecessary(getConversationContext(contextId).getParameterName(), ctx.getCurrentConversation().getId()).getUrl();
    } else {
        return actionUrl;
    }
}
Also used : ConversationContext(org.jboss.weld.context.ConversationContext) HttpConversationContext(org.jboss.weld.context.http.HttpConversationContext)

Aggregations

ConversationContext (org.jboss.weld.context.ConversationContext)5 HttpConversationContext (org.jboss.weld.context.http.HttpConversationContext)4 AbstractConversationContext (org.jboss.weld.contexts.AbstractConversationContext)2 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)1 Conversation (javax.enterprise.context.Conversation)1 FacesContext (javax.faces.context.FacesContext)1 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)1 FacesUrlTransformer (org.jboss.weld.module.jsf.FacesUrlTransformer)1 LazyHttpConversationContextImpl (org.jboss.weld.module.web.context.http.LazyHttpConversationContextImpl)1 Test (org.junit.Test)1