1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.render.html; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import javax.faces.application.StateManager.SerializedView; |
22 | |
import javax.faces.context.FacesContext; |
23 | |
import javax.faces.context.ResponseWriter; |
24 | |
|
25 | |
import org.seasar.teeda.core.JsfConstants; |
26 | |
import org.seasar.teeda.core.render.AbstractResponseStateManager; |
27 | |
import org.seasar.teeda.core.util.StateManagerUtil; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class HtmlResponseStateManager extends AbstractResponseStateManager { |
34 | |
|
35 | 9 | public HtmlResponseStateManager() { |
36 | 9 | } |
37 | |
|
38 | |
public void writeState(final FacesContext context, |
39 | |
final SerializedView serializedView) throws IOException { |
40 | 2 | final ResponseWriter writer = context.getResponseWriter(); |
41 | 2 | writeSerializedView(context, serializedView, writer); |
42 | 2 | writeViewId(writer, context.getViewRoot().getViewId()); |
43 | 2 | } |
44 | |
|
45 | |
private void writeSerializedView(final FacesContext context, |
46 | |
final SerializedView serializedView, final ResponseWriter writer) |
47 | |
throws IOException { |
48 | 2 | writer.startElement(JsfConstants.INPUT_ELEM, null); |
49 | 2 | writer.writeAttribute(JsfConstants.TYPE_ATTR, |
50 | |
JsfConstants.HIDDEN_VALUE, null); |
51 | 2 | writer.writeAttribute(JsfConstants.NAME_ATTR, VIEW_STATE_PARAM, null); |
52 | 2 | writer.writeAttribute(JsfConstants.ID_ATTR, VIEW_STATE_PARAM, null); |
53 | |
|
54 | |
final Object value; |
55 | 2 | if (isSavingStateInClient(context)) { |
56 | 1 | value = getEncodeConverter().getAsEncodeString( |
57 | |
new StructureAndState(serializedView)); |
58 | |
} else { |
59 | 1 | value = serializedView.getStructure(); |
60 | |
} |
61 | 2 | writer.writeAttribute(JsfConstants.VALUE_ATTR, value, null); |
62 | 2 | writer.endElement(JsfConstants.INPUT_ELEM); |
63 | 2 | } |
64 | |
|
65 | |
boolean isSavingStateInClient(final FacesContext context) { |
66 | 7 | return StateManagerUtil.isSavingStateInClient(context); |
67 | |
} |
68 | |
|
69 | |
public Object getComponentStateToRestore(final FacesContext context) { |
70 | 3 | final Map requestMap = context.getExternalContext().getRequestMap(); |
71 | 3 | final Object state = requestMap.get(FACES_VIEW_STATE); |
72 | 3 | requestMap.remove(FACES_VIEW_STATE); |
73 | 3 | return state; |
74 | |
} |
75 | |
|
76 | |
public Object getTreeStructureToRestore(final FacesContext context, |
77 | |
final String viewId) { |
78 | 4 | final Map paramMap = context.getExternalContext() |
79 | |
.getRequestParameterMap(); |
80 | 4 | final String viewIdParam = (String) paramMap.get(VIEW_ID); |
81 | 4 | final String viewState = (String) paramMap.get(VIEW_STATE_PARAM); |
82 | 4 | if (viewState == null || viewIdParam == null) { |
83 | 1 | return null; |
84 | |
} |
85 | 3 | if (isSavingStateInClient(context)) { |
86 | 2 | if (!viewIdParam.equals(viewId)) { |
87 | 0 | return null; |
88 | |
} |
89 | 2 | StructureAndState structureAndState = (StructureAndState) getEncodeConverter() |
90 | |
.getAsDecodeObject(viewState); |
91 | 2 | context.getExternalContext().getRequestMap().put( |
92 | |
AbstractResponseStateManager.FACES_VIEW_STATE, |
93 | |
structureAndState.getState()); |
94 | 2 | return structureAndState.getStructure(); |
95 | |
} else { |
96 | 1 | return viewState; |
97 | |
} |
98 | |
} |
99 | |
|
100 | |
protected void writeViewId(final ResponseWriter writer, final String viewId) |
101 | |
throws IOException { |
102 | 3 | writer.startElement(JsfConstants.INPUT_ELEM, null); |
103 | 3 | writer.writeAttribute(JsfConstants.TYPE_ATTR, |
104 | |
JsfConstants.HIDDEN_VALUE, null); |
105 | 3 | writer.writeAttribute(JsfConstants.NAME_ATTR, VIEW_ID, null); |
106 | 3 | writer.writeAttribute(JsfConstants.ID_ATTR, VIEW_ID, null); |
107 | 3 | writer.writeAttribute(JsfConstants.VALUE_ATTR, viewId, null); |
108 | 3 | writer.endElement(JsfConstants.INPUT_ELEM); |
109 | 3 | } |
110 | |
|
111 | |
} |