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.config.MyfacesConfig; |
25 | |
import org.apache.myfaces.shared_impl.renderkit.html.HTML; |
26 | |
import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils; |
27 | |
import org.apache.myfaces.shared_impl.renderkit.html.util.JavascriptUtils; |
28 | |
import org.apache.myfaces.shared_impl.util.StateUtils; |
29 | |
|
30 | |
import javax.faces.application.StateManager; |
31 | |
import javax.faces.context.ExternalContext; |
32 | |
import javax.faces.context.FacesContext; |
33 | |
import javax.faces.context.ResponseWriter; |
34 | |
import javax.faces.render.RenderKitFactory; |
35 | |
import javax.faces.render.ResponseStateManager; |
36 | |
import java.io.IOException; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class HtmlResponseStateManager |
43 | |
extends MyfacesResponseStateManager { |
44 | 0 | private static final Log log = LogFactory.getLog(HtmlResponseStateManager.class); |
45 | |
|
46 | |
private static final int TREE_PARAM = 0; |
47 | |
private static final int STATE_PARAM = 1; |
48 | |
private static final int VIEWID_PARAM = 2; |
49 | |
|
50 | |
public static final String STANDARD_STATE_SAVING_PARAM = "javax.faces.ViewState"; |
51 | |
|
52 | |
public void writeState(FacesContext facescontext, |
53 | |
StateManager.SerializedView serializedview) throws IOException { |
54 | 0 | ResponseWriter responseWriter = facescontext.getResponseWriter(); |
55 | |
|
56 | 0 | Object[] savedState = new Object[3]; |
57 | |
|
58 | 0 | if (facescontext.getApplication().getStateManager().isSavingStateInClient(facescontext)) { |
59 | 0 | if (log.isTraceEnabled()) log.trace("Writing state in client"); |
60 | 0 | Object treeStruct = serializedview.getStructure(); |
61 | 0 | Object compStates = serializedview.getState(); |
62 | |
|
63 | 0 | if(treeStruct != null) |
64 | |
{ |
65 | 0 | savedState[TREE_PARAM]=treeStruct; |
66 | |
} |
67 | |
else { |
68 | 0 | log.error("No tree structure to be saved in client response!"); |
69 | |
} |
70 | |
|
71 | 0 | if (compStates != null) { |
72 | 0 | savedState[STATE_PARAM] = compStates; |
73 | |
} |
74 | |
else { |
75 | 0 | log.error("No component states to be saved in client response!"); |
76 | |
} |
77 | 0 | } |
78 | |
else { |
79 | 0 | if (log.isTraceEnabled()) log.trace("Writing state in server"); |
80 | |
|
81 | 0 | Object treeStruct = serializedview.getStructure(); |
82 | 0 | if (treeStruct != null) { |
83 | 0 | if (treeStruct instanceof String) { |
84 | 0 | savedState[TREE_PARAM] = treeStruct; |
85 | |
} |
86 | |
} |
87 | |
} |
88 | |
|
89 | 0 | savedState[VIEWID_PARAM] = facescontext.getViewRoot().getViewId(); |
90 | |
|
91 | 0 | if (log.isTraceEnabled()) log.trace("Writing view state and renderKit fields"); |
92 | |
|
93 | |
|
94 | 0 | writeViewStateField(facescontext, responseWriter, savedState); |
95 | |
|
96 | |
|
97 | 0 | writeRenderKitIdField(facescontext, responseWriter); |
98 | 0 | } |
99 | |
|
100 | |
private void writeViewStateField(FacesContext facesContext, |
101 | |
ResponseWriter responseWriter, |
102 | |
Object savedState) throws IOException |
103 | |
{ |
104 | 0 | String serializedState = StateUtils.construct(savedState, |
105 | |
facesContext.getExternalContext()); |
106 | 0 | ExternalContext extContext = facesContext.getExternalContext(); |
107 | 0 | MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(extContext); |
108 | |
|
109 | |
|
110 | 0 | if (JavascriptUtils.isJavascriptAllowed(extContext) && myfacesConfig.isViewStateJavascript()) { |
111 | 0 | HtmlRendererUtils.renderViewStateJavascript(facesContext, STANDARD_STATE_SAVING_PARAM, serializedState); |
112 | |
} else { |
113 | 0 | responseWriter.startElement(HTML.INPUT_ELEM, null); |
114 | 0 | responseWriter.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null); |
115 | 0 | responseWriter.writeAttribute(HTML.NAME_ATTR, STANDARD_STATE_SAVING_PARAM, null); |
116 | 0 | if (myfacesConfig.isRenderViewStateId()) { |
117 | 0 | responseWriter.writeAttribute(HTML.ID_ATTR, STANDARD_STATE_SAVING_PARAM, null); |
118 | |
} |
119 | 0 | responseWriter.writeAttribute(HTML.VALUE_ATTR, serializedState, null); |
120 | 0 | responseWriter.endElement(HTML.INPUT_ELEM); |
121 | |
} |
122 | 0 | } |
123 | |
|
124 | |
private void writeRenderKitIdField(FacesContext facesContext, |
125 | |
ResponseWriter responseWriter) throws IOException { |
126 | |
|
127 | 0 | String defaultRenderKitId = facesContext.getApplication().getDefaultRenderKitId(); |
128 | 0 | if (defaultRenderKitId != null && !RenderKitFactory.HTML_BASIC_RENDER_KIT.equals(defaultRenderKitId)) { |
129 | 0 | responseWriter.startElement(HTML.INPUT_ELEM, null); |
130 | 0 | responseWriter.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null); |
131 | 0 | responseWriter.writeAttribute(HTML.NAME_ATTR, ResponseStateManager.RENDER_KIT_ID_PARAM, null); |
132 | 0 | responseWriter.writeAttribute(HTML.VALUE_ATTR, defaultRenderKitId, null); |
133 | 0 | responseWriter.endElement(HTML.INPUT_ELEM); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
@Override |
138 | |
public Object getState(FacesContext facesContext, String viewId) { |
139 | 0 | Object[] savedState = getSavedState(facesContext); |
140 | 0 | if (savedState == null) { |
141 | 0 | return null; |
142 | |
} |
143 | |
|
144 | 0 | return new Object[] { savedState[TREE_PARAM], savedState[STATE_PARAM] }; |
145 | |
} |
146 | |
|
147 | |
@Override |
148 | |
public Object getTreeStructureToRestore(FacesContext facesContext, String viewId) { |
149 | |
|
150 | |
|
151 | 0 | Object[] savedState = getSavedState(facesContext); |
152 | 0 | if (savedState == null) { |
153 | 0 | return null; |
154 | |
} |
155 | |
|
156 | 0 | return savedState[TREE_PARAM]; |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public Object getComponentStateToRestore(FacesContext facesContext) { |
161 | |
|
162 | |
|
163 | 0 | Object[] savedState = getSavedState(facesContext); |
164 | 0 | if (savedState == null) { |
165 | 0 | return null; |
166 | |
} |
167 | |
|
168 | 0 | return savedState[STATE_PARAM]; |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
private Object[] getSavedState(FacesContext facesContext) { |
180 | 0 | Object encodedState = |
181 | |
facesContext.getExternalContext(). |
182 | |
getRequestParameterMap().get(STANDARD_STATE_SAVING_PARAM); |
183 | 0 | if(encodedState==null || (((String) encodedState).length() == 0)) { |
184 | 0 | return null; |
185 | |
} |
186 | |
|
187 | 0 | Object[] savedState = (Object[]) StateUtils.reconstruct( |
188 | |
(String) encodedState, facesContext.getExternalContext()); |
189 | |
|
190 | 0 | if (savedState == null) |
191 | |
{ |
192 | 0 | if (log.isTraceEnabled()) { |
193 | 0 | log.trace("No saved state"); |
194 | |
} |
195 | 0 | return null; |
196 | |
} |
197 | |
|
198 | 0 | String restoredViewId = (String) savedState[VIEWID_PARAM]; |
199 | |
|
200 | 0 | if (restoredViewId == null) { |
201 | |
|
202 | 0 | if (log.isTraceEnabled()) { |
203 | 0 | log.trace("No saved state or state of a different viewId: " + restoredViewId); |
204 | |
} |
205 | |
|
206 | 0 | return null; |
207 | |
} |
208 | |
|
209 | 0 | return savedState; |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
@Override |
218 | |
public boolean isPostback(FacesContext context) |
219 | |
{ |
220 | 0 | return context.getExternalContext() |
221 | |
.getRequestParameterMap().containsKey(ResponseStateManager.VIEW_STATE_PARAM); |
222 | |
} |
223 | |
} |
224 | |
|
225 | |
|