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.render;
20  
21  import javax.faces.application.StateManager;
22  import javax.faces.application.StateManager.SerializedView;
23  import javax.faces.context.FacesContext;
24  import java.io.IOException;
25  
26  /**
27   * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
28   * 
29   * @author Manfred Geiler (latest modification by $Author: bommel $)
30   * @author Stan Silvert
31   * @version $Revision: 1187701 $ $Date: 2011-10-22 07:21:54 -0500 (Sat, 22 Oct 2011) $
32   */
33  public abstract class ResponseStateManager
34  {
35      public static final String RENDER_KIT_ID_PARAM = "javax.faces.RenderKitId";
36      public static final String VIEW_STATE_PARAM = "javax.faces.ViewState";
37  
38      public void writeState(FacesContext context, Object state) throws IOException
39      {
40          SerializedView view;
41          if (state instanceof SerializedView)
42          {
43              view = (SerializedView)state;
44          }
45          else if (state instanceof Object[])
46          {
47              Object[] structureAndState = (Object[])state;
48  
49              if (structureAndState.length == 2)
50              {
51                  Object structureObj = structureAndState[0];
52                  Object stateObj = structureAndState[1];
53  
54                  StateManager stateManager = context.getApplication().getStateManager();
55                  view = stateManager.new SerializedView(structureObj, stateObj);
56              }
57              else
58              {
59                  throw new IOException("The state should be an array of Object[] of lenght 2");
60              }
61          }
62          else
63          {
64              throw new IOException("The state should be an array of Object[] of lenght 2, or a SerializedView instance");
65          }
66  
67          writeState(context, view);
68      }
69  
70      /**
71       * @throws IOException 
72       * @deprecated
73       */
74      public void writeState(FacesContext context, StateManager.SerializedView state) throws IOException
75      {
76          // does nothing as per JSF 1.2 javadoc
77      }
78      
79      /**
80       * 
81       * @since 2.0
82       * @param context
83       * @param state
84       * @return
85       */
86      public String getViewState(FacesContext context, Object state)
87      {
88          // TODO: IMPLEMENT HERE
89          
90          return null;
91      }
92  
93      /**
94       * @since 1.2
95       */
96      public Object getState(FacesContext context, String viewId)
97      {
98          Object[] structureAndState = new Object[2];
99          structureAndState[0] = getTreeStructureToRestore(context, viewId);
100         structureAndState[1] = getComponentStateToRestore(context);
101         return structureAndState;
102     }
103 
104     /**
105      * @deprecated
106      */
107     public Object getTreeStructureToRestore(FacesContext context, String viewId)
108     {
109         return null;
110     }
111 
112     /**
113      * @deprecated
114      */
115     public Object getComponentStateToRestore(FacesContext context)
116     {
117         return null;
118     }
119 
120     /**
121      * Checks if the current request is a postback
122      * 
123      * @since 1.2
124      */
125     public boolean isPostback(FacesContext context)
126     {
127         return context.getExternalContext().getRequestParameterMap().containsKey(ResponseStateManager.VIEW_STATE_PARAM);
128     }
129 
130 }