001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package javax.faces.application;
017:
018: import javax.faces.FacesException;
019: import javax.faces.context.ExternalContext;
020:
021: import java.io.UnsupportedEncodingException;
022: import java.util.Locale;
023:
024: /**
025: * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
026: *
027: * @author Manfred Geiler (latest modification by $Author: mbr $)
028: * @version $Revision: 515546 $ $Date: 2007-03-07 13:16:26 +0100 (Mi, 07 Mrz 2007) $
029: */
030: public abstract class ViewHandler {
031: public static final String CHARACTER_ENCODING_KEY = "javax.faces.request.charset";
032: public static final String DEFAULT_SUFFIX_PARAM_NAME = "javax.faces.DEFAULT_SUFFIX";
033: public static final String DEFAULT_SUFFIX = ".jsp";
034:
035: /**
036: * @since JSF 1.2
037: */
038: public String calculateCharacterEncoding(
039: javax.faces.context.FacesContext context) {
040: String _encoding = null;
041: ExternalContext externalContext = context.getExternalContext();
042: String _contentType = (String) externalContext
043: .getRequestHeaderMap().get("Content-Type");
044: int _indexOf = _contentType == null ? -1 : _contentType
045: .indexOf("charset");
046: if (_indexOf != -1) {
047: String _tempEnc = _contentType.substring(_indexOf); //charset=UTF-8
048: _encoding = _tempEnc.substring(_tempEnc.indexOf("=") + 1); //UTF-8
049: } else {
050: boolean _sessionAvailable = externalContext
051: .getSession(false) != null;
052: if (_sessionAvailable) {
053: Object _sessionParam = externalContext.getSessionMap()
054: .get(CHARACTER_ENCODING_KEY);
055: if (_sessionParam != null) {
056: _encoding = _sessionParam.toString();
057: }
058: }
059: }
060:
061: return _encoding;
062: }
063:
064: public abstract Locale calculateLocale(
065: javax.faces.context.FacesContext context);
066:
067: public abstract String calculateRenderKitId(
068: javax.faces.context.FacesContext context);
069:
070: public abstract javax.faces.component.UIViewRoot createView(
071: javax.faces.context.FacesContext context, String viewId);
072:
073: public abstract String getActionURL(
074: javax.faces.context.FacesContext context, String viewId);
075:
076: public abstract String getResourceURL(
077: javax.faces.context.FacesContext context, String path);
078:
079: /**
080: * Method must be called by the JSF impl at the beginning of Phase <i>Restore View</i> of the JSF
081: * lifecycle.
082: *
083: * @since JSF 1.2
084: */
085: public void initView(javax.faces.context.FacesContext context)
086: throws FacesException {
087: String _encoding = this .calculateCharacterEncoding(context);
088: if (_encoding != null) {
089: try {
090: context.getExternalContext()
091: .setRequestCharacterEncoding(_encoding);
092: } catch (UnsupportedEncodingException uee) {
093: throw new FacesException(uee);
094: }
095: }
096: }
097:
098: public abstract void renderView(
099: javax.faces.context.FacesContext context,
100: javax.faces.component.UIViewRoot viewToRender)
101: throws java.io.IOException, FacesException;
102:
103: public abstract javax.faces.component.UIViewRoot restoreView(
104: javax.faces.context.FacesContext context, String viewId);
105:
106: public abstract void writeState(
107: javax.faces.context.FacesContext context)
108: throws java.io.IOException;
109: }
|