001: /*
002: * Copyright 2004-2006 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 org.apache.myfaces.renderkit.html;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.myfaces.renderkit.MyfacesResponseStateManager;
021: import org.apache.myfaces.shared_impl.renderkit.html.HTML;
022: import org.apache.myfaces.shared_impl.util.StateUtils;
023:
024: import javax.faces.application.StateManager;
025: import javax.faces.context.FacesContext;
026: import javax.faces.context.ResponseWriter;
027: import javax.faces.render.RenderKitFactory;
028: import javax.faces.render.ResponseStateManager;
029: import java.io.IOException;
030: import java.util.Map;
031:
032: /**
033: * @author Manfred Geiler (latest modification by $Author: baranda $)
034: * @version $Revision: 495909 $ $Date: 2007-01-13 16:27:05 +0100 (Sa, 13 Jan 2007) $
035: */
036: public class HtmlResponseStateManager extends
037: MyfacesResponseStateManager {
038: private static final Log log = LogFactory
039: .getLog(HtmlResponseStateManager.class);
040:
041: private static final int TREE_PARAM = 0;
042: private static final int STATE_PARAM = 1;
043: private static final int VIEWID_PARAM = 2;
044:
045: public static final String STANDARD_STATE_SAVING_PARAM = "javax.faces.ViewState";
046:
047: public void writeState(FacesContext facescontext,
048: StateManager.SerializedView serializedview)
049: throws IOException {
050: ResponseWriter responseWriter = facescontext
051: .getResponseWriter();
052:
053: Object[] savedState = new Object[3];
054:
055: if (facescontext.getApplication().getStateManager()
056: .isSavingStateInClient(facescontext)) {
057: if (log.isTraceEnabled())
058: log.trace("Writing state in client");
059: Object treeStruct = serializedview.getStructure();
060: Object compStates = serializedview.getState();
061:
062: if (treeStruct != null) {
063: savedState[TREE_PARAM] = treeStruct;
064: } else {
065: log
066: .error("No tree structure to be saved in client response!");
067: }
068:
069: if (compStates != null) {
070: savedState[STATE_PARAM] = compStates;
071: } else {
072: log
073: .error("No component states to be saved in client response!");
074: }
075: } else {
076: if (log.isTraceEnabled())
077: log.trace("Writing state in server");
078: // write viewSequence
079: Object treeStruct = serializedview.getStructure();
080: if (treeStruct != null) {
081: if (treeStruct instanceof String) {
082: savedState[TREE_PARAM] = treeStruct;
083: }
084: }
085: }
086:
087: savedState[VIEWID_PARAM] = facescontext.getViewRoot()
088: .getViewId();
089:
090: if (log.isTraceEnabled())
091: log.trace("Writing view state and renderKit fields");
092:
093: // write the view state field
094: writeViewStateField(facescontext, responseWriter, savedState);
095:
096: // renderKitId field
097: writeRenderKitIdField(facescontext, responseWriter);
098: }
099:
100: private void writeViewStateField(FacesContext facesContext,
101: ResponseWriter responseWriter, Object savedState)
102: throws IOException {
103: responseWriter.startElement(HTML.INPUT_ELEM, null);
104: responseWriter.writeAttribute(HTML.TYPE_ATTR,
105: HTML.INPUT_TYPE_HIDDEN, null);
106: responseWriter.writeAttribute(HTML.NAME_ATTR,
107: STANDARD_STATE_SAVING_PARAM, null);
108: responseWriter.writeAttribute(HTML.ID_ATTR,
109: STANDARD_STATE_SAVING_PARAM, null);
110: responseWriter.writeAttribute(HTML.VALUE_ATTR, StateUtils
111: .construct(savedState, facesContext
112: .getExternalContext()), null);
113: responseWriter.endElement(HTML.INPUT_ELEM);
114: }
115:
116: private void writeRenderKitIdField(FacesContext facesContext,
117: ResponseWriter responseWriter) throws IOException {
118:
119: String defaultRenderKitId = facesContext.getApplication()
120: .getDefaultRenderKitId();
121: if (defaultRenderKitId != null
122: && !RenderKitFactory.HTML_BASIC_RENDER_KIT
123: .equals(defaultRenderKitId)) {
124: responseWriter.startElement(HTML.INPUT_ELEM, null);
125: responseWriter.writeAttribute(HTML.TYPE_ATTR,
126: HTML.INPUT_TYPE_HIDDEN, null);
127: responseWriter.writeAttribute(HTML.NAME_ATTR,
128: ResponseStateManager.RENDER_KIT_ID_PARAM, null);
129: responseWriter.writeAttribute(HTML.VALUE_ATTR,
130: defaultRenderKitId, null);
131: responseWriter.endElement(HTML.INPUT_ELEM);
132: }
133: }
134:
135: public Object getTreeStructureToRestore(FacesContext facesContext,
136: String viewId) {
137: Map reqParamMap = facesContext.getExternalContext()
138: .getRequestParameterMap();
139:
140: Object encodedState = reqParamMap
141: .get(STANDARD_STATE_SAVING_PARAM);
142:
143: if (encodedState == null)
144: return null;
145:
146: Object[] savedState = (Object[]) StateUtils.reconstruct(
147: (String) encodedState, facesContext
148: .getExternalContext());
149:
150: String restoredViewId = (String) savedState[VIEWID_PARAM];
151:
152: if (restoredViewId == null || !restoredViewId.equals(viewId)) {
153: //no saved state or state of different viewId
154: if (log.isTraceEnabled())
155: log
156: .trace("No saved state or state of a different viewId"
157: + restoredViewId);
158: return null;
159: }
160:
161: return savedState[TREE_PARAM];
162: }
163:
164: public Object getComponentStateToRestore(FacesContext facesContext) {
165: Map reqParamMap = facesContext.getExternalContext()
166: .getRequestParameterMap();
167:
168: Object encodedState = reqParamMap
169: .get(STANDARD_STATE_SAVING_PARAM);
170:
171: if (encodedState == null)
172: return null;
173:
174: Object[] savedState = (Object[]) StateUtils.reconstruct(
175: (String) encodedState, facesContext
176: .getExternalContext());
177:
178: String restoredViewId = (String) savedState[VIEWID_PARAM];
179:
180: if (restoredViewId == null) {
181: //no saved state or state of different viewId
182: if (log.isTraceEnabled())
183: log
184: .trace("No saved state or state of a different viewId: "
185: + restoredViewId);
186: return null;
187: }
188:
189: return savedState[STATE_PARAM];
190: }
191:
192: /**
193: * Checks if the current request is a postback
194: * @since 1.2
195: */
196: @Override
197: public boolean isPostback(FacesContext context) {
198: return context.getExternalContext().getRequestParameterMap()
199: .containsKey(ResponseStateManager.VIEW_STATE_PARAM);
200: }
201: }
|