1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package javax.faces.application;
20
21
22 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
23
24 import java.io.IOException;
25
26 import javax.faces.component.UIViewRoot;
27 import javax.faces.context.FacesContext;
28
29 /**
30 * Responsible for storing sufficient information about a component tree so that an identical tree can later be
31 * recreated.
32 * <p>
33 * It is up to the concrete implementation to decide whether to use information from the "view template" that was used
34 * to first create the view, or whether to store sufficient information to enable the view to be restored without any
35 * reference to the original template. However as JSF components have mutable fields that can be set by code, and
36 * affected by user input, at least some state does need to be kept in order to recreate a previously-existing component
37 * tree.
38 * <p>
39 * There are two different options defined by the specification: "client" and "server" state.
40 * <p>
41 * When "client" state is configured, all state information required to create the tree is embedded within the data
42 * rendered to the client. Note that because data received from a remote client must always be treated as "tainted",
43 * care must be taken when using such data. Some StateManager implementations may use encryption to ensure that clients
44 * cannot modify the data, and that the data received on postback is therefore trustworthy.
45 * <p>
46 * When "server" state is configured, the data is saved somewhere "on the back end", and (at most) a token is embedded
47 * in the data rendered to the user.
48 * <p>
49 * This class is usually invoked by a concrete implementation of ViewHandler.
50 * <p>
51 * Note that class ViewHandler isolates JSF components from the details of the request format. This class isolates JSF
52 * components from the details of the response format. Because request and response are usually tightly coupled, the
53 * StateManager and ViewHandler implementations are also usually fairly tightly coupled (ie the ViewHandler/StateManager
54 * implementations come as pairs).
55 * <p>
56 * See also the <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
57 *
58 * @author Manfred Geiler (latest modification by $Author: bommel $)
59 * @author Stan Silvert
60 * @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 Oct 2011) $
61 */
62 public abstract class StateManager
63 {
64 /**
65 * Define the state method to be used. There are two different options defined by the
66 * specification: "client" and "server" state.
67 * <p>
68 * When "client" state is configured, all state information required to create the tree is embedded within
69 * the data rendered to the client. Note that because data received from a remote client must always be
70 * treated as "tainted", care must be taken when using such data. Some StateManager implementations may
71 * use encryption to ensure that clients cannot modify the data, and that the data received on postback
72 * is therefore trustworthy.
73 * </p>
74 * <p>
75 * When "server" state is configured, the data is saved somewhere "on the back end", and (at most) a
76 * token is embedded in the data rendered to the user.
77 * </p>
78 */
79 @JSFWebConfigParam(defaultValue="server", expectedValues="server,client", since="1.1", group="state", tags="performance",
80 desc="Define the state method to be used. There are two different options defined by the specification: 'client' and 'server' state.")
81 public static final String STATE_SAVING_METHOD_PARAM_NAME = "javax.faces.STATE_SAVING_METHOD";
82 public static final String STATE_SAVING_METHOD_CLIENT = "client";
83 public static final String STATE_SAVING_METHOD_SERVER = "server";
84
85 /**
86 * Indicate the viewId(s) separated by commas that should be saved and restored fully, without use Partial State Saving (PSS).
87 */
88 @JSFWebConfigParam(since="2.0", group="state")
89 public static final String FULL_STATE_SAVING_VIEW_IDS_PARAM_NAME = "javax.faces.FULL_STATE_SAVING_VIEW_IDS";
90
91 /**
92 * Enable or disable partial state saving algorithm.
93 *
94 * <p>Partial State Saving algorithm allows to reduce the size of the state required to save a view,
95 * keeping track of the "delta" or differences between the view build by first time and the current
96 * state of the view.</p>
97 * <p>If the webapp faces-config file version is 2.0 or upper the default value is true, otherwise is false.</p>
98 */
99 @JSFWebConfigParam(expectedValues="true,false", since="2.0", defaultValue="true (false with 1.2 webapps)", tags="performance", group="state")
100 public static final String PARTIAL_STATE_SAVING_PARAM_NAME = "javax.faces.PARTIAL_STATE_SAVING";
101 private Boolean _savingStateInClient = null;
102
103 /**
104 * Invokes getTreeStructureToSave and getComponentStateToSave, then return an object that wraps the two resulting
105 * objects. This object can then be passed to method writeState.
106 * <p>
107 * Deprecated; use saveView instead.
108 *
109 * @deprecated
110 */
111 public StateManager.SerializedView saveSerializedView(FacesContext context)
112 {
113 Object savedView = saveView(context);
114 if (savedView != null && savedView instanceof Object[])
115 {
116 Object[] structureAndState = (Object[]) savedView;
117 if (structureAndState.length == 2)
118 {
119 return new StateManager.SerializedView(structureAndState[0], structureAndState[1]);
120 }
121 }
122
123 return null;
124 }
125
126 /**
127 * Returns an object that is sufficient to recreate the component tree that is the viewroot of the specified
128 * context.
129 * <p>
130 * The return value is suitable for passing to method writeState.
131 *
132 * @since 1.2
133 */
134 public Object saveView(FacesContext context)
135 {
136 StateManager.SerializedView serializedView = saveSerializedView(context);
137 if (serializedView == null)
138 return null;
139
140 Object[] structureAndState = new Object[2];
141 structureAndState[0] = serializedView.getStructure();
142 structureAndState[1] = serializedView.getState();
143
144 return structureAndState;
145 }
146
147 /**
148 * Return data that is sufficient to recreate the component tree that is the viewroot of the specified context, but
149 * without restoring the state in the components.
150 * <p>
151 * Using this data, a tree of components which has the same "shape" as the original component tree can be recreated.
152 * However the component instances themselves will have only their default values, ie their member fields will not
153 * have been set to the original values.
154 * <p>
155 * Deprecated; use saveView instead.
156 *
157 * @deprecated
158 */
159 protected Object getTreeStructureToSave(FacesContext context)
160 {
161 return null;
162 }
163
164 /**
165 * Return data that can be applied to a component tree created using the "getTreeStructureToSave" method.
166 * <p>
167 * Deprecated; use saveView instead.
168 *
169 * @deprecated
170 */
171 protected Object getComponentStateToSave(FacesContext context)
172 {
173 return null;
174 }
175
176 /**
177 * Associate the provided state object with the current response being generated.
178 * <p>
179 * When client-side state is enabled, it is expected that method writes the data contained in the state parameter to
180 * the response somehow.
181 * <p>
182 * When server-side state is enabled, at most a "token" is expected to be written.
183 * <p>
184 * Deprecated; use writeState(FacesContext, Object) instead. This method was abstract in JSF1.1, but is now an empty
185 * non-abstract method so that old classes that implement this method continue to work, while new classes can just
186 * override the new writeState method rather than this one.
187 *
188 * @throws IOException
189 * never
190 *
191 * @deprecated
192 */
193 public void writeState(FacesContext context, StateManager.SerializedView state)
194 throws IOException
195 {
196 if (state != null)
197 {
198 writeState(context, new Object[]{state.getStructure(), state.getState()});
199 }
200 }
201
202 /**
203 * Associate the provided state object with the current response being generated.
204 * <p>
205 * When client-side state is enabled, it is expected that method writes the data contained in the state parameter to
206 * the response somehow.
207 * <p>
208 * When server-side state is enabled, at most a "token" is expected to be written.
209 * <p>
210 * This method should be overridden by subclasses. It is not abstract because a default implementation is provided
211 * that forwards to the old writeState method; this allows subclasses of StateManager written using the JSF1.1 API
212 * to continue to work.
213 * <p>
214 *
215 * @since 1.2
216 */
217 public void writeState(FacesContext context, Object state) throws IOException
218 {
219 if (!(state instanceof Object[]))
220 return;
221 Object[] structureAndState = (Object[]) state;
222 if (structureAndState.length < 2)
223 return;
224
225 writeState(context, new StateManager.SerializedView(structureAndState[0], structureAndState[1]));
226 }
227
228 /**
229 * TODO: This method should be called from somewhere when ajax response is created to update the state saving param
230 * on client. The place where this method is called is an implementation detail, so there is no references about
231 * from where in the spec javadoc.
232 *
233 * @since 2.0
234 * @param context
235 * @return
236 */
237 public String getViewState(FacesContext context)
238 {
239 return context.getRenderKit().getResponseStateManager().getViewState(context, saveView(context));
240 }
241
242 public abstract UIViewRoot restoreView(FacesContext context, String viewId, String renderKitId);
243
244 /**
245 * @deprecated
246 */
247 protected UIViewRoot restoreTreeStructure(FacesContext context, String viewId, String renderKitId)
248 {
249 return null;
250 }
251
252 /**
253 * @deprecated
254 */
255 protected void restoreComponentState(FacesContext context, UIViewRoot viewRoot, String renderKitId)
256 {
257 // default impl does nothing as per JSF 1.2 javadoc
258 }
259
260 public boolean isSavingStateInClient(FacesContext context)
261 {
262 if (context == null)
263 throw new NullPointerException("context");
264 if (_savingStateInClient != null)
265 return _savingStateInClient.booleanValue();
266 String stateSavingMethod = context.getExternalContext().getInitParameter(STATE_SAVING_METHOD_PARAM_NAME);
267 if (stateSavingMethod == null)
268 {
269 _savingStateInClient = Boolean.FALSE; // Specs 10.1.3: default server saving
270 context.getExternalContext().log("No state saving method defined, assuming default server state saving");
271 }
272 else if (stateSavingMethod.equals(STATE_SAVING_METHOD_CLIENT))
273 {
274 _savingStateInClient = Boolean.TRUE;
275 }
276 else if (stateSavingMethod.equals(STATE_SAVING_METHOD_SERVER))
277 {
278 _savingStateInClient = Boolean.FALSE;
279 }
280 else
281 {
282 _savingStateInClient = Boolean.FALSE; // Specs 10.1.3: default server saving
283 context.getExternalContext().log(
284 "Illegal state saving method '" + stateSavingMethod + "', default server state saving will be used");
285 }
286 return _savingStateInClient.booleanValue();
287 }
288
289 /**
290 * @deprecated
291 */
292 public class SerializedView
293 {
294 private Object _structure;
295 private Object _state;
296
297 /**
298 * @deprecated
299 */
300 public SerializedView(Object structure, Object state)
301 {
302 _structure = structure;
303 _state = state;
304 }
305
306 /**
307 * @deprecated
308 */
309 public Object getStructure()
310 {
311 return _structure;
312 }
313
314 /**
315 * @deprecated
316 */
317 public Object getState()
318 {
319 return _state;
320 }
321 }
322 }