javax.faces.application.ViewHandler Maven / Gradle / Ivy
Show all versions of jsf-api Show documentation
/*
* $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.io.UnsupportedEncodingException;
import java.util.Locale;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.component.UIViewRoot;
/**
* 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 {@link StateManager} for information on how the
* ViewHandler
interacts the {@link 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 {@link #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 {@link Locale} to use for this and
* subsequent requests for the current client.
*
* @param context {@link FacesContext} for the current request
*
* @throws 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.
*
*
*
* Examine the Content-Type
request header. If it has
* a charset
parameter, extract it and return that as the
* encoding.
*
* If no charset
parameter was found, check for the
* existence of a session by calling {@link ExternalContext#getSession(boolean)}
* passing false
as the argument. If that method returns
* true
, get the session Map by calling
* {@link ExternalContext#getSessionMap} and look for a value under the
* key given by the value of the symbolic constant
* {@link ViewHandler#CHARACTER_ENCODING_KEY}.
* If present, return the value, converted to String.
*
* Otherwise, return null
*
*
*/
public String calculateCharacterEncoding(FacesContext context) {
ExternalContext extContext = context.getExternalContext();
Map headerMap = 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 = (String) extContext.getSessionMap().get
(ViewHandler.CHARACTER_ENCODING_KEY);
}
}
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 {@link
* javax.faces.render.RenderKitFactory#HTML_BASIC_RENDER_KIT}.
*
* @param context {@link FacesContext} for the current request
*
* @throws NullPointerException if context
is
* null
*/
public abstract String calculateRenderKitId(FacesContext context);
/**
* Create and return a new {@link UIViewRoot} instance
* initialized with information from the argument
* FacesContext
and viewId
.
*
* If there is an existing ViewRoot
available on the
* {@link FacesContext}, this method must copy its
* locale
and renderKitId
to this new view
* root. If not, this method must call {@link #calculateLocale} and
* {@link #calculateRenderKitId}, and store the results as the
* values of the locale
and renderKitId
,
* proeprties, respectively, of the newly created
* UIViewRoot
.
*
* @throws NullPointerException if context
* is null
*/
public abstract UIViewRoot createView(FacesContext context, String viewId);
/**
* Return a URL suitable for rendering (after optional encoding
* performed by the encodeActionURL()
method of
* {@link ExternalContext}) that selects the specified view identifier.
*
* @param context {@link FacesContext} for this request
* @param viewId View identifier of the desired view
*
* @throws IllegalArgumentException if viewId
is not
* valid for this ViewHandler
.
* @throws NullPointerException if context
or
* viewId
is null
.
*/
public abstract String getActionURL(FacesContext context, String viewId);
/**
* Return a URL suitable for rendering (after optional encoding
* perfomed by the encodeResourceURL()
method of
* {@link 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.
*
* @param context {@link FacesContext} for the current request
* @param path Resource path to convert to a URL
*
* @throws IllegalArgumentException if viewId
is not
* valid for this ViewHandler
.
* @throws NullPointerException if context
or
* path
is null
.
*/
public abstract String getResourceURL(FacesContext context, String 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 {@link #calculateCharacterEncoding}
* and passes the result, if non-null
into the
* {@link ExternalContext#setRequestCharacterEncoding} method.
*
* @throws 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 context) throws 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 (log.isLoggable(Level.WARNING)) {
log.fine(message);
}
throw new FacesException(message, e);
}
}
}
/**
*
Perform whatever actions are required to render the response
* view to the response object associated with the
* current {@link FacesContext}.
*
* @param context {@link FacesContext} for the current request
* @param viewToRender the view to render
*
* @throws IOException if an input/output error occurs
* @throws NullPointerException if context
or
* viewToRender
is null
* @throws FacesException if a servlet error occurs
*/
public abstract void renderView(FacesContext context, UIViewRoot viewToRender)
throws IOException, FacesException;
/**
* Perform whatever actions are required to restore the view
* associated with the specified {@link FacesContext} and
* viewId
. It may delegate to the restoreView
* of the associated {@link StateManager} to do the actual work of
* restoring the view. If there is no available state for the
* specified viewId
, return null
.
*
* @param context {@link FacesContext} for the current request
* @param viewId the view identifier for the current request
*
* @throws NullPointerException if context
* is null
* @throws FacesException if a servlet error occurs
*/
public abstract UIViewRoot restoreView(FacesContext context, String viewId);
/**
* Take any appropriate action to either immediately
* write out the current state information (by calling
* {@link StateManager#writeState}, or noting where state information
* should later be written.
*
* @param context {@link FacesContext} for the current request
*
* @throws IOException if an input/output error occurs
* @throws NullPointerException if context
* is null
*/
public abstract void writeState(FacesContext context) throws IOException;
}