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 org.apache.myfaces.renderkit.html;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.myfaces.renderkit.MyfacesResponseStateManager;
24  import org.apache.myfaces.shared_impl.renderkit.html.HTML;
25  import org.apache.myfaces.shared_impl.util.StateUtils;
26  import org.apache.myfaces.shared_impl.config.MyfacesConfig;
27  
28  import javax.faces.application.StateManager;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  import java.io.IOException;
32  import java.util.Map;
33  
34  /**
35   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
36   * @version $Revision: 777351 $ $Date: 2009-05-21 21:11:24 -0500 (Thu, 21 May 2009) $
37   */
38  public class HtmlResponseStateManager
39      extends MyfacesResponseStateManager {
40      private static final Log log = LogFactory.getLog(HtmlResponseStateManager.class);
41  
42      private static final int TREE_PARAM = 0;
43      private static final int STATE_PARAM = 1;
44      private static final int VIEWID_PARAM = 2;
45  
46      public static final String STANDARD_STATE_SAVING_PARAM = "javax.faces.ViewState";
47  
48      public void writeState(FacesContext facescontext,
49                             StateManager.SerializedView serializedview) throws IOException {
50          ResponseWriter responseWriter = facescontext.getResponseWriter();
51  
52          Object[] savedState = new Object[3];
53  
54          if (facescontext.getApplication().getStateManager().isSavingStateInClient(facescontext)) {
55              Object treeStruct = serializedview.getStructure();
56              Object compStates = serializedview.getState();
57  
58              if(treeStruct != null)
59              {
60                  savedState[TREE_PARAM]=treeStruct;
61              }
62              else {
63                  log.error("No tree structure to be saved in client response!");
64              }
65  
66              if (compStates != null) {
67                  savedState[STATE_PARAM] = compStates;
68              }
69              else {
70                  log.error("No component states to be saved in client response!");
71              }
72          }
73          else {
74              // write viewSequence
75              Object treeStruct = serializedview.getStructure();
76              if (treeStruct != null) {
77                  if (treeStruct instanceof String) {
78                      savedState[TREE_PARAM] = treeStruct;
79                  }
80              }
81          }
82  
83          savedState[VIEWID_PARAM] = facescontext.getViewRoot().getViewId();
84  
85          responseWriter.startElement(HTML.INPUT_ELEM, null);
86          responseWriter.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
87          responseWriter.writeAttribute(HTML.NAME_ATTR, STANDARD_STATE_SAVING_PARAM, null);
88  
89          MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(facescontext.getExternalContext());
90          if (myfacesConfig.isRenderViewStateId()) {
91              responseWriter.writeAttribute(HTML.ID_ATTR, STANDARD_STATE_SAVING_PARAM, null);
92          }
93          responseWriter.writeAttribute(HTML.VALUE_ATTR, StateUtils.construct(savedState,
94                  facescontext.getExternalContext()), null);
95          responseWriter.endElement(HTML.INPUT_ELEM);
96      }
97  
98      /**
99       * MyFaces extension
100      *
101      * @param facescontext
102      * @param serializedview
103      * @throws IOException
104      */
105     public void writeStateAsUrlParams(FacesContext facescontext,
106                                       StateManager.SerializedView serializedview) throws IOException {
107 
108         throw new UnsupportedOperationException("long been deprecated...");
109 /*        ResponseWriter responseWriter = facescontext.getResponseWriter();
110         Object treeStruct = serializedview.getStructure();
111         Object compStates = serializedview.getState();
112 
113         if (treeStruct != null) {
114             if (treeStruct instanceof String) {
115                 if (StateUtils.isSecure(facescontext.getExternalContext()))
116                     treeStruct = StateUtils.construct(treeStruct, facescontext.getExternalContext());
117                 writeStateParam(responseWriter, TREE_PARAM, (String) treeStruct);
118             }
119             else {
120                 writeStateParam(responseWriter, BASE64_TREE_PARAM,
121                                 StateUtils.construct(treeStruct, facescontext.getExternalContext()));
122             }
123         }
124         else {
125             log.error("No tree structure to be saved in client response!");
126         }
127 
128         if (compStates != null) {
129             if (treeStruct != null) {
130                 responseWriter.write('&');
131             }
132             if (compStates instanceof String) {
133                 if (StateUtils.isSecure(facescontext.getExternalContext()))
134                     compStates = StateUtils.construct(compStates, facescontext.getExternalContext());
135                 writeStateParam(responseWriter, STATE_PARAM, (String) compStates);
136             }
137             else {
138                 writeStateParam(responseWriter, BASE64_STATE_PARAM, StateUtils.construct(compStates, facescontext.getExternalContext()));
139             }
140         }
141         else {
142             log.error("No component states to be saved in client response!");
143         }
144 
145         if (treeStruct != null || compStates != null) {
146             responseWriter.write('&');
147         }
148         writeStateParam(responseWriter, VIEWID_PARAM, facescontext.getViewRoot().getViewId());
149 
150 
151         private void writeStateParam(ResponseWriter writer, String name, String value)
152             throws IOException {
153             writer.write(name);
154             writer.write('=');
155             writer.write(URLEncoder.encode(value, writer.getCharacterEncoding()));
156         }
157 
158         */
159     }
160 
161     public Object getTreeStructureToRestore(FacesContext facesContext, String viewId) {
162         Map reqParamMap = facesContext.getExternalContext().getRequestParameterMap();
163 
164         Object encodedState = reqParamMap.get(STANDARD_STATE_SAVING_PARAM);
165 
166         if(encodedState==null  || (((String) encodedState).length() == 0))
167             return null;        
168 
169         Object[] savedState = (Object[]) StateUtils.reconstruct((String) encodedState, facesContext.getExternalContext());
170 
171         String restoredViewId = (String) savedState[VIEWID_PARAM];
172 
173         if (restoredViewId == null || !restoredViewId.equals(viewId)) {
174             //no saved state or state of different viewId
175             return null;
176         }
177 
178         return savedState[TREE_PARAM];
179     }
180 
181     public Object getComponentStateToRestore(FacesContext facesContext) {
182         Map reqParamMap = facesContext.getExternalContext().getRequestParameterMap();
183 
184         Object encodedState = reqParamMap.get(STANDARD_STATE_SAVING_PARAM);
185 
186         if(encodedState==null  || (((String) encodedState).length() == 0))
187             return null;
188 
189         Object[] savedState = (Object[]) StateUtils.reconstruct((String) encodedState, facesContext.getExternalContext());
190 
191         String restoredViewId = (String) savedState[VIEWID_PARAM];
192 
193         if (restoredViewId == null) {
194             //no saved state or state of different viewId
195             return null;
196         }
197 
198         return savedState[STATE_PARAM];
199     }
200 
201 }
202