Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: ViewHandler.java,v 1.45 2007/04/27 22:00:02 ofung Exp $
   */
  
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   * 
   * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
   * 
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
  * and Distribution License("CDDL") (collectively, the "License").  You
  * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  * 
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  * 
  * Contributor(s):
  * 
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package javax.faces.application;
 
 import java.util.Map;

ViewHandler is the pluggablity mechanism for allowing implementations of or applications using the JavaServer Faces specification to provide their own handling of the activities in the Render Response and Restore View phases of the request processing lifecycle. This allows for implementations to support different response generation technologies, as well as alternative strategies for saving and restoring the state of each view.

Please see StateManager for information on how the ViewHandler interacts the StateManager.

 
 
 public abstract class ViewHandler {
 
     private static Logger log = Logger.getLogger("javax.faces.application");
     
 
     // ------------------------------------------------------ Manifest Constants
 

    

The key, in the session's attribute set, under which the response character encoding may be stored and retrieved.

 
     public static final String CHARACTER_ENCODING_KEY = 
 	"javax.faces.request.charset";


    

Allow the web application to define an alternate suffix for pages containing JSF content. If this init parameter is not specified, the default value is taken from the value of the constant DEFAULT_SUFFIX.

 
     public static final String DEFAULT_SUFFIX_PARAM_NAME = 
 	"javax.faces.DEFAULT_SUFFIX";


    

The value to use for the default extension if the webapp is using url extension mapping.

    public static final String DEFAULT_SUFFIX = ".jsp";
    // ---------------------------------------------------------- Public Methods


    

Returns an appropriate java.util.Locale to use for this and subsequent requests for the current client.

Parameters:
context javax.faces.context.FacesContext for the current request
Throws:
java.lang.NullPointerException if context is null
     public abstract Locale calculateLocale(FacesContext context);
     
     

Returns the correct character encoding to be used for this request.

The following algorithm is employed.

 
     
     public String calculateCharacterEncoding(FacesContext context) {
         ExternalContext extContext = context.getExternalContext();
         Map<String,StringheaderMap = extContext.getRequestHeaderMap();
         String contentType = headerMap.get("Content-Type");
         String charEnc = null;
         
         // look for a charset in the Content-Type header first.
         if (null != contentType) {
             // see if this header had a charset
             String charsetStr = "charset=";
             int len = charsetStr.length();
             int idx = contentType.indexOf(charsetStr);
             
             // if we have a charset in this Content-Type header AND it
             // has a non-zero length.
             if (idx != -1 && idx + len < contentType.length()) {
                 charEnc = contentType.substring(idx + len);
             }
         }
         
         // failing that, look in the session for a previously saved one
         if (null == charEnc) {
             if (null != extContext.getSession(false)) {
                 charEnc = (StringextContext.getSessionMap().get
                         (.);
             }
         }
         
         return charEnc;
     }


    

Return an appropriate renderKitId for this and subsequent requests from the current client. It is an error for this method to return null.

The default return value is javax.faces.render.RenderKitFactory.HTML_BASIC_RENDER_KIT.

Parameters:
context javax.faces.context.FacesContext for the current request
Throws:
java.lang.NullPointerException if context is null
    public abstract String calculateRenderKitId(FacesContext context);


    

Create and return a new javax.faces.component.UIViewRoot instance initialized with information from the argument FacesContext and viewId.

If there is an existing ViewRoot available on the javax.faces.context.FacesContext, this method must copy its locale and renderKitId to this new view root. If not, this method must call calculateLocale(javax.faces.context.FacesContext) and calculateRenderKitId(javax.faces.context.FacesContext), and store the results as the values of the locale and renderKitId, proeprties, respectively, of the newly created UIViewRoot.

Throws:
java.lang.NullPointerException if context is null
    public abstract UIViewRoot createView(FacesContext contextString viewId);


    

Return a URL suitable for rendering (after optional encoding performed by the encodeActionURL() method of javax.faces.context.ExternalContext) that selects the specified view identifier.

Parameters:
context javax.faces.context.FacesContext for this request
viewId View identifier of the desired view
Throws:
java.lang.IllegalArgumentException if viewId is not valid for this ViewHandler.
java.lang.NullPointerException if context or viewId is null.
    public abstract String getActionURL(FacesContext contextString viewId);


    

Return a URL suitable for rendering (after optional encoding perfomed by the encodeResourceURL() method of javax.faces.context.ExternalContext) that selects the specifed web application resource. If the specified path starts with a slash, it must be treated as context relative; otherwise, it must be treated as relative to the action URL of the current view.

Parameters:
context javax.faces.context.FacesContext for the current request
path Resource path to convert to a URL
Throws:
java.lang.IllegalArgumentException if viewId is not valid for this ViewHandler.
java.lang.NullPointerException if context or path is null.
    public abstract String getResourceURL(FacesContext contextString path);
    
    

Initialize the view for the request processing lifecycle.

This method must be called at the beginning of the Restore View Phase of the Request Processing Lifecycle. It is responsible for performing any per-request initialization necessary to the operation of the lifycecle.

The default implementation calls calculateCharacterEncoding(javax.faces.context.FacesContext) and passes the result, if non-null into the javax.faces.context.ExternalContext.setRequestCharacterEncoding(java.lang.String) method.

Throws:
javax.faces.FacesException if a problem occurs setting the encoding, such as the UnsupportedEncodingException thrown by the underlying Servlet or Portlet technology when the encoding is not supported.
    
    public void initView(FacesContext contextthrows FacesException {
        String encoding = calculateCharacterEncoding(context);
        if (null != encoding) {
            try {
                context.getExternalContext().setRequestCharacterEncoding(encoding);
            } catch (UnsupportedEncodingException e) {
                // PENDING(edburns): I18N
                String message = "Can't set encoding to: " + encoding +
                        " Exception:" + e.getMessage();
                if (.isLoggable(.)) {
                    .fine(message);
                }
                throw new FacesException(messagee);
                
            }
        }
    }
    

    

Perform whatever actions are required to render the response view to the response object associated with the current javax.faces.context.FacesContext.

Parameters:
context javax.faces.context.FacesContext for the current request
viewToRender the view to render
Throws:
java.io.IOException if an input/output error occurs
java.lang.NullPointerException if context or viewToRender is null
javax.faces.FacesException if a servlet error occurs
    public abstract void renderView(FacesContext contextUIViewRoot viewToRender)
        throws IOExceptionFacesException;


    

Perform whatever actions are required to restore the view associated with the specified javax.faces.context.FacesContext and viewId. It may delegate to the restoreView of the associated StateManager to do the actual work of restoring the view. If there is no available state for the specified viewId, return null.

Parameters:
context javax.faces.context.FacesContext for the current request
viewId the view identifier for the current request
Throws:
java.lang.NullPointerException if context is null
javax.faces.FacesException if a servlet error occurs
    public abstract UIViewRoot restoreView(FacesContext contextString viewId);


    

Take any appropriate action to either immediately write out the current state information (by calling StateManager.writeState(javax.faces.context.FacesContext,java.lang.Object), or noting where state information should later be written.

Parameters:
context javax.faces.context.FacesContext for the current request
Throws:
java.io.IOException if an input/output error occurs
java.lang.NullPointerException if context is null
    public abstract void writeState(FacesContext contextthrows IOException;
New to GrepCode? Check out our FAQ X