1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
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
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
100
101
102
103
104
105 public void writeStateAsUrlParams(FacesContext facescontext,
106 StateManager.SerializedView serializedview) throws IOException {
107
108 throw new UnsupportedOperationException("long been deprecated...");
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
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
195 return null;
196 }
197
198 return savedState[STATE_PARAM];
199 }
200
201 }
202