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", ignoreUpperLowerCase = true,
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     @Deprecated
152     public Object saveView(FacesContext context)
153     {
154         StateManager.SerializedView serializedView = saveSerializedView(context);
155         if (serializedView == null)
156         {
157             return null;
158         }
159 
160         Object[] structureAndState = new Object[2];
161         structureAndState[0] = serializedView.getStructure();
162         structureAndState[1] = serializedView.getState();
163 
164         return structureAndState;
165     }
166 
167     /**
168      * Return data that is sufficient to recreate the component tree that is the viewroot of the specified context, but
169      * without restoring the state in the components.
170      * <p>
171      * Using this data, a tree of components which has the same "shape" as the original component tree can be recreated.
172      * However the component instances themselves will have only their default values, ie their member fields will not
173      * have been set to the original values.
174      * <p>
175      * Deprecated; use saveView instead.
176      * 
177      * @deprecated
178      */
179     protected Object getTreeStructureToSave(FacesContext context)
180     {
181         return null;
182     }
183 
184     /**
185      * Return data that can be applied to a component tree created using the "getTreeStructureToSave" method.
186      * <p>
187      * Deprecated; use saveView instead.
188      * 
189      * @deprecated
190      */
191     protected Object getComponentStateToSave(FacesContext context)
192     {
193         return null;
194     }
195 
196     /**
197      * Associate the provided state object with the current response being generated.
198      * <p>
199      * When client-side state is enabled, it is expected that method writes the data contained in the state parameter to
200      * the response somehow.
201      * <p>
202      * When server-side state is enabled, at most a "token" is expected to be written.
203      * <p>
204      * Deprecated; use writeState(FacesContext, Object) instead. This method was abstract in JSF1.1, but is now an empty
205      * non-abstract method so that old classes that implement this method continue to work, while new classes can just
206      * override the new writeState method rather than this one.
207      * 
208      * @throws IOException
209      *             never
210      * 
211      * @deprecated
212      */
213     public void writeState(FacesContext context, StateManager.SerializedView state)
214         throws IOException
215     {
216         if (state != null)
217         {
218             writeState(context, new Object[]{state.getStructure(), state.getState()});
219         }
220     }
221 
222     /**
223      * Associate the provided state object with the current response being generated.
224      * <p>
225      * When client-side state is enabled, it is expected that method writes the data contained in the state parameter to
226      * the response somehow.
227      * <p>
228      * When server-side state is enabled, at most a "token" is expected to be written.
229      * <p>
230      * This method should be overridden by subclasses. It is not abstract because a default implementation is provided
231      * that forwards to the old writeState method; this allows subclasses of StateManager written using the JSF1.1 API
232      * to continue to work.
233      * <p>
234      * 
235      * @since 1.2
236      */
237     public void writeState(FacesContext context, Object state) throws IOException
238     {
239         if (!(state instanceof Object[]))
240         {
241             return;
242         }
243         Object[] structureAndState = (Object[]) state;
244         if (structureAndState.length < 2)
245         {
246             return;
247         }
248 
249         writeState(context, new StateManager.SerializedView(structureAndState[0], structureAndState[1]));
250     }
251     
252     /**
253      * TODO: This method should be called from somewhere when ajax response is created to update the state saving param
254      * on client. The place where this method is called is an implementation detail, so there is no references about
255      * from where in the spec javadoc. 
256      * 
257      * @since 2.0
258      * @param context
259      * @return
260      */
261     public String getViewState(FacesContext context)
262     {
263         return context.getRenderKit().getResponseStateManager().getViewState(context, saveView(context));
264     }
265 
266     @Deprecated
267     public abstract UIViewRoot restoreView(FacesContext context, String viewId, String renderKitId);
268 
269     /**
270      * @deprecated
271      */
272     protected UIViewRoot restoreTreeStructure(FacesContext context, String viewId, String renderKitId)
273     {
274         return null;
275     }
276 
277     /**
278      * @deprecated
279      */
280     protected void restoreComponentState(FacesContext context, UIViewRoot viewRoot, String renderKitId)
281     {
282         // default impl does nothing as per JSF 1.2 javadoc
283     }
284 
285     public boolean isSavingStateInClient(FacesContext context)
286     {
287         if (context == null)
288         {
289             throw new NullPointerException("context");
290         }
291         if (_savingStateInClient != null)
292         {
293             return _savingStateInClient.booleanValue();
294         }
295 
296         String stateSavingMethod = context.getExternalContext().getInitParameter(STATE_SAVING_METHOD_PARAM_NAME);
297         if (stateSavingMethod == null)
298         {
299             _savingStateInClient = Boolean.FALSE; // Specs 10.1.3: default server saving
300             context.getExternalContext().log("No state saving method defined, assuming default server state saving");
301         }
302         else if (stateSavingMethod.equalsIgnoreCase(STATE_SAVING_METHOD_CLIENT))
303         {
304             _savingStateInClient = Boolean.TRUE;
305         }
306         else if (stateSavingMethod.equalsIgnoreCase(STATE_SAVING_METHOD_SERVER))
307         {
308             _savingStateInClient = Boolean.FALSE;
309         }
310         else
311         {
312             _savingStateInClient = Boolean.FALSE; // Specs 10.1.3: default server saving
313             context.getExternalContext().log(
314                 "Illegal state saving method '" + stateSavingMethod + "', default server state saving will be used");
315         }
316         return _savingStateInClient.booleanValue();
317     }
318 
319     /**
320      * @deprecated
321      */
322     public class SerializedView
323     {
324         private Object _structure;
325         private Object _state;
326 
327         /**
328          * @deprecated
329          */
330         public SerializedView(Object structure, Object state)
331         {
332             _structure = structure;
333             _state = state;
334         }
335 
336         /**
337          * @deprecated
338          */
339         public Object getStructure()
340         {
341             return _structure;
342         }
343 
344         /**
345          * @deprecated
346          */
347         public Object getState()
348         {
349             return _state;
350         }
351     }
352 }