Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.weld.jsf;
 
 import java.util.List;
 import java.util.Map;
 
 import  javax.faces.application.ViewHandler;
 import  javax.faces.application.ViewHandlerWrapper;
 import  javax.faces.context.FacesContext;
 

A forwarding JSF ViewHandler implementation that produces URLs containing the conversation id query string parameter. All methods except those which produce a URL that need to be enhanced are forwarded to the ViewHandler delegate.

A request parameter was chosen to propagate the conversation because it's the most technology agnostic approach for passing data between requests and allows for the ensuing request to use whatever means necessary (a servlet filter, phase listener, etc) to capture the conversation id and restore the long-running conversation.

Author(s):
Dan Allen
Pete Muir
Ales Justin
Marko Luksa
 
 public class ConversationAwareViewHandler extends ViewHandlerWrapper {
 
     private static enum Source {
 
         ACTION,
         BOOKMARKABLE,
         REDIRECT,
         RESOURCE
 
     }
 
     private final ViewHandler delegate;
     private volatile ConversationContext conversationContext;
     private static final ThreadLocal<Sourcesource = new ThreadLocal<Source>();
     private String contextId;
 
     public ConversationAwareViewHandler(ViewHandler delegate) {
         this. = delegate;
     }

    
Get conversation context.

Returns:
the conversation context
 
         if ( == null) {
             synchronized (this) {
                 if ( == null) {
                     Container container = Container.instance(id);
                      = container.deploymentManager().instance().select(HttpConversationContext.class).get();
                 }
             }
         }
         return ;
     }

    
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.

This covers form actions Ajax calls, and redirect URLs (which we want) and link hrefs (which we don't)

See also:
ViewHandler.getActionURL(FacesContext, String)
 
    @Override
    public String getActionURL(FacesContext facesContextString viewId) {
        if ( == null) {
            if (facesContext.getAttributes().containsKey(.)) {
                 = (StringfacesContext.getAttributes().get(.);
            } else {
                 = .;
            }
        }
        String actionUrl = super.getActionURL(facesContextviewId);
        final ConversationContext ctx = getConversationContext();
            return new FacesUrlTransformer(actionUrlfacesContext)
                .getUrl();
        } else {
            return actionUrl;
        }
    }
    private Source getSource() {
        if (.get() == null) {
            return .;
        } else {
            return .get();
        }
    }
    @Override
    public String getBookmarkableURL(FacesContext contextString viewIdMap<StringList<String>> parametersboolean includeViewParams) {
        try {
            .set(.);
            return super.getBookmarkableURL(contextviewIdparametersincludeViewParams);
        } finally {
            .remove();
        }
    }
    @Override
    public String getRedirectURL(FacesContext contextString viewIdMap<StringList<String>> parametersboolean includeViewParams) {
        try {
            .set(.);
            return super.getRedirectURL(contextviewIdparametersincludeViewParams);
        } finally {
            .remove();
        }
    }
    @Override
    public String getResourceURL(FacesContext contextString path) {
        try {
            .set(.);
            return super.getResourceURL(contextpath);
        } finally {
            .remove();
        }
    }
    @Override
    public ViewHandler getWrapped() {
        return ;
    }
New to GrepCode? Check out our FAQ X