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