View Javadoc

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  public abstract class StateManager
59  {
60      /**
61       * Define the state method to be used. There are two different options defined by the 
62       * specification: "client" and "server" state.
63       * <p>
64       * When "client" state is configured, all state information required to create the tree is embedded within
65       * the data rendered to the client. Note that because data received from a remote client must always be
66       * treated as "tainted", care must be taken when using such data. Some StateManager implementations may
67       * use encryption to ensure that clients cannot modify the data, and that the data received on postback
68       * is therefore trustworthy.
69       * </p>
70       * <p>
71       * When "server" state is configured, the data is saved somewhere "on the back end", and (at most) a
72       * token is embedded in the data rendered to the user.
73       * </p>
74       */
75      @JSFWebConfigParam(defaultValue="server", expectedValues="server,client",
76              since="1.1", group="state", tags="performance",
77              desc="Define the state method to be used. There are two different options "
78                   + "defined by the specification: 'client' and 'server' state.")
79      public static final String STATE_SAVING_METHOD_PARAM_NAME = "javax.faces.STATE_SAVING_METHOD";
80      public static final String STATE_SAVING_METHOD_CLIENT = "client";
81      public static final String STATE_SAVING_METHOD_SERVER = "server";
82      
83      /**
84       * Indicate the viewId(s) separated by commas that should be saved and restored fully,
85       * without use Partial State Saving (PSS).
86       */
87      @JSFWebConfigParam(since="2.0", group="state")
88      public static final String FULL_STATE_SAVING_VIEW_IDS_PARAM_NAME = "javax.faces.FULL_STATE_SAVING_VIEW_IDS";
89      
90      /**
91       * Enable or disable partial state saving algorithm.
92       *  
93       * <p>Partial State Saving algorithm allows to reduce the size of the state required to save a view, 
94       * keeping track of the "delta" or differences between the view build by first time and the current 
95       * state of the view.</p>
96       * <p>If the webapp faces-config file version is 2.0 or upper the default value is true, otherwise is false.</p>   
97       */
98      @JSFWebConfigParam(expectedValues="true,false", since="2.0", defaultValue="true (false with 1.2 webapps)",
99                         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     public static final String IS_BUILDING_INITIAL_STATE = "javax.faces.IS_BUILDING_INITIAL_STATE";
104     
105     public static final String IS_SAVING_STATE = "javax.faces.IS_SAVING_STATE";
106 
107     /**
108      * Indicate if the state should be serialized before save it on the session.
109      * <p>
110      * Only applicable if state saving method is "server" (= default).
111      * If <code>true</code> (default) the state will be serialized to a byte stream before it is
112      * written to the session.
113      * If <code>false</code> the state will not be serialized to a byte stream.
114      * </p>
115      */
116     @JSFWebConfigParam(since="2.2", group="state", tags="performance", 
117             defaultValue="false", expectedValues="true,false")
118     public static final java.lang.String SERIALIZE_SERVER_STATE_PARAM_NAME = "javax.faces.SERIALIZE_SERVER_STATE";
119     
120     /**
121      * Invokes getTreeStructureToSave and getComponentStateToSave, then return an object that wraps the two resulting
122      * objects. This object can then be passed to method writeState.
123      * <p>
124      * Deprecated; use saveView instead.
125      * 
126      * @deprecated
127      */
128     public StateManager.SerializedView saveSerializedView(FacesContext context)
129     {
130         Object savedView = saveView(context);
131         if (savedView != null && savedView instanceof Object[])
132         {
133             Object[] structureAndState = (Object[]) savedView;
134             if (structureAndState.length == 2)
135             {
136                 return new StateManager.SerializedView(structureAndState[0], structureAndState[1]);
137             }
138         }
139         
140         return null;
141     }
142 
143     /**
144      * Returns an object that is sufficient to recreate the component tree that is the viewroot of the specified
145      * context.
146      * <p>
147      * The return value is suitable for passing to method writeState.
148      * 
149      * @since 1.2
150      */
151     public Object saveView(FacesContext context)
152     {
153         StateManager.SerializedView serializedView = saveSerializedView(context);
154         if (serializedView == null)
155         {
156             return null;
157         }
158 
159         Object[] structureAndState = new Object[2];
160         structureAndState[0] = serializedView.getStructure();
161         structureAndState[1] = serializedView.getState();
162 
163         return structureAndState;
164     }
165 
166     /**
167      * Return data that is sufficient to recreate the component tree that is the viewroot of the specified context, but
168      * without restoring the state in the components.
169      * <p>
170      * Using this data, a tree of components which has the same "shape" as the original component tree can be recreated.
171      * However the component instances themselves will have only their default values, ie their member fields will not
172      * have been set to the original values.
173      * <p>
174      * Deprecated; use saveView instead.
175      * 
176      * @deprecated
177      */
178     protected Object getTreeStructureToSave(FacesContext context)
179     {
180         return null;
181     }
182 
183     /**
184      * Return data that can be applied to a component tree created using the "getTreeStructureToSave" method.
185      * <p>
186      * Deprecated; use saveView instead.
187      * 
188      * @deprecated
189      */
190     protected Object getComponentStateToSave(FacesContext context)
191     {
192         return null;
193     }
194 
195     /**
196      * Associate the provided state object with the current response being generated.
197      * <p>
198      * When client-side state is enabled, it is expected that method writes the data contained in the state parameter to
199      * the response somehow.
200      * <p>
201      * When server-side state is enabled, at most a "token" is expected to be written.
202      * <p>
203      * Deprecated; use writeState(FacesContext, Object) instead. This method was abstract in JSF1.1, but is now an empty
204      * non-abstract method so that old classes that implement this method continue to work, while new classes can just
205      * override the new writeState method rather than this one.
206      * 
207      * @throws IOException
208      *             never
209      * 
210      * @deprecated
211      */
212     public void writeState(FacesContext context, StateManager.SerializedView state)
213         throws IOException
214     {
215         if (state != null)
216         {
217             writeState(context, new Object[]{state.getStructure(), state.getState()});
218         }
219     }
220 
221     /**
222      * Associate the provided state object with the current response being generated.
223      * <p>
224      * When client-side state is enabled, it is expected that method writes the data contained in the state parameter to
225      * the response somehow.
226      * <p>
227      * When server-side state is enabled, at most a "token" is expected to be written.
228      * <p>
229      * This method should be overridden by subclasses. It is not abstract because a default implementation is provided
230      * that forwards to the old writeState method; this allows subclasses of StateManager written using the JSF1.1 API
231      * to continue to work.
232      * <p>
233      * 
234      * @since 1.2
235      */
236     public void writeState(FacesContext context, Object state) throws IOException
237     {
238         if (!(state instanceof Object[]))
239         {
240             return;
241         }
242         Object[] structureAndState = (Object[]) state;
243         if (structureAndState.length < 2)
244         {
245             return;
246         }
247 
248         writeState(context, new StateManager.SerializedView(structureAndState[0], structureAndState[1]));
249     }
250     
251     /**
252      * TODO: This method should be called from somewhere when ajax response is created to update the state saving param
253      * on client. The place where this method is called is an implementation detail, so there is no references about
254      * from where in the spec javadoc. 
255      * 
256      * @since 2.0
257      * @param context
258      * @return
259      */
260     public String getViewState(FacesContext context)
261     {
262         return context.getRenderKit().getResponseStateManager().getViewState(context, saveView(context));
263     }
264 
265     public abstract UIViewRoot restoreView(FacesContext context, String viewId, String renderKitId);
266 
267     /**
268      * @deprecated
269      */
270     protected UIViewRoot restoreTreeStructure(FacesContext context, String viewId, String renderKitId)
271     {
272         return null;
273     }
274 
275     /**
276      * @deprecated
277      */
278     protected void restoreComponentState(FacesContext context, UIViewRoot viewRoot, String renderKitId)
279     {
280         // default impl does nothing as per JSF 1.2 javadoc
281     }
282 
283     public boolean isSavingStateInClient(FacesContext context)
284     {
285         if (context == null)
286         {
287             throw new NullPointerException("context");
288         }
289         if (_savingStateInClient != null)
290         {
291             return _savingStateInClient.booleanValue();
292         }
293 
294         String stateSavingMethod = context.getExternalContext().getInitParameter(STATE_SAVING_METHOD_PARAM_NAME);
295         if (stateSavingMethod == null)
296         {
297             _savingStateInClient = Boolean.FALSE; // Specs 10.1.3: default server saving
298             context.getExternalContext().log("No state saving method defined, assuming default server state saving");
299         }
300         else if (stateSavingMethod.equalsIgnoreCase(STATE_SAVING_METHOD_CLIENT))
301         {
302             _savingStateInClient = Boolean.TRUE;
303         }
304         else if (stateSavingMethod.equalsIgnoreCase(STATE_SAVING_METHOD_SERVER))
305         {
306             _savingStateInClient = Boolean.FALSE;
307         }
308         else
309         {
310             _savingStateInClient = Boolean.FALSE; // Specs 10.1.3: default server saving
311             context.getExternalContext().log(
312                 "Illegal state saving method '" + stateSavingMethod + "', default server state saving will be used");
313         }
314         return _savingStateInClient.booleanValue();
315     }
316 
317     /**
318      * @deprecated
319      */
320     public class SerializedView
321     {
322         private Object _structure;
323         private Object _state;
324 
325         /**
326          * @deprecated
327          */
328         public SerializedView(Object structure, Object state)
329         {
330             _structure = structure;
331             _state = state;
332         }
333 
334         /**
335          * @deprecated
336          */
337         public Object getStructure()
338         {
339             return _structure;
340         }
341 
342         /**
343          * @deprecated
344          */
345         public Object getState()
346         {
347             return _state;
348         }
349     }
350 }