001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: StateStoreSession.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.uwyn.rife.config.RifeConfig;
011: import com.uwyn.rife.engine.exceptions.EngineException;
012: import com.uwyn.rife.tools.UniqueID;
013: import com.uwyn.rife.tools.UniqueIDGenerator;
014: import java.util.LinkedHashMap;
015: import java.util.Map;
016: import javax.servlet.http.HttpSession;
017:
018: public class StateStoreSession implements StateStore {
019: public static final String IDENTIFIER = "session";
020:
021: private static final String ATTRIBUTE_STATES_SUFFIX = "\u0000states";
022:
023: StateStoreSession() {
024: }
025:
026: public void init(Request request) throws EngineException {
027: request.getSession();
028: }
029:
030: public void exportQueryUrl(CharSequenceDeferred deferred,
031: String url, FlowState state, ElementInfo source,
032: String type, String name) throws EngineException {
033: Request request = RequestState.getActiveRequestState()
034: .getRequest();
035: Response response = RequestState.getActiveRequestState()
036: .getResponse();
037: HttpSession session = request.getSession();
038:
039: if (state.isEmpty()) {
040: deferred.setContent(response.encodeURL(url));
041: return;
042: }
043:
044: UniqueID id = UniqueIDGenerator.generate(request
045: .getRemoteAddr());
046: storeStates(id, session);
047: session.setAttribute(id.toString(), state);
048:
049: StringBuilder query_parameters = new StringBuilder("?");
050: query_parameters.append(ReservedParameters.STATEID);
051: query_parameters.append("=");
052: query_parameters.append(id.toString());
053:
054: deferred.setContent(response.encodeURL(url
055: + query_parameters.toString()));
056: }
057:
058: public void exportFormState(CharSequenceDeferred deferred,
059: FlowState state, FormStateType stateType)
060: throws EngineException {
061: Request request = RequestState.getActiveRequestState()
062: .getRequest();
063:
064: HttpSession session = request.getSession();
065:
066: if (state.isEmpty()) {
067: deferred.setContent("");
068: return;
069: }
070:
071: UniqueID id = UniqueIDGenerator.generate(request
072: .getRemoteAddr());
073: storeStates(id, session);
074: session.setAttribute(id.toString(), state);
075:
076: StringBuilder form_state = new StringBuilder();
077: if (FormStateType.PARAMS == stateType) {
078: StateStoreTools.appendHtmlHiddenParam(form_state, deferred,
079: ReservedParameters.STATEID, id.toString());
080: } else if (FormStateType.JAVASCRIPT == stateType) {
081: StateStoreTools.appendJavascriptHeader(form_state);
082: StateStoreTools.appendJavascriptHiddenParam(form_state,
083: ReservedParameters.STATEID, id.toString());
084: StateStoreTools.appendJavascriptFooter(form_state);
085: }
086:
087: deferred.setContent(form_state.toString());
088: }
089:
090: public void exportFormUrl(CharSequenceDeferred deferred, String url)
091: throws EngineException {
092: Response response = RequestState.getActiveRequestState()
093: .getResponse();
094: deferred.setContent(response.encodeURL(url));
095: }
096:
097: private FlowState getSessionFlowState(Request request) {
098: String[] value = request.getParameters().get(
099: ReservedParameters.STATEID);
100: if (null == value || 0 == value.length) {
101: return null;
102: }
103:
104: String state_id = value[0];
105: HttpSession session = request.getSession(false);
106: if (null == session) {
107: return null;
108: }
109:
110: return (FlowState) session.getAttribute(state_id);
111: }
112:
113: public Map<String, String[]> restoreParameters(Request request)
114: throws EngineException {
115: FlowState state = getSessionFlowState(request);
116: if (null == state) {
117: return null;
118: }
119:
120: Map<String, String[]> result = new LinkedHashMap<String, String[]>();
121: if (state.hasParameters()) {
122: result.putAll(state.getParameters());
123: }
124:
125: String[] values = null;
126: for (Map.Entry<String, String[]> entry : request
127: .getParameters().entrySet()) {
128: values = entry.getValue();
129:
130: // only override with non empty values
131: if (values != null
132: && (values.length > 1 || (values[0] != null && values[0]
133: .trim().length() > 0))) {
134: result.put(entry.getKey(), values);
135: }
136: }
137: return result;
138: }
139:
140: public ResultStates restoreResultStates(Request request)
141: throws EngineException {
142: ResultStates result_states = null;
143:
144: String[] value = request.getParameters().get(
145: ReservedParameters.STATEID);
146: if (value != null && value.length > 0) {
147: String state_id = value[0];
148: HttpSession session = request.getSession(false);
149: if (session != null) {
150: String states_id = (String) session
151: .getAttribute(state_id
152: + ATTRIBUTE_STATES_SUFFIX);
153: if (states_id != null
154: && states_id.startsWith(ResultStates.ID_PREFIX)) {
155: result_states = (ResultStates) session
156: .getAttribute(states_id);
157: if (RifeConfig.Engine.getSessionStateStoreCloning()) {
158: result_states = result_states
159: .cloneForStateStore(this );
160: result_states.regenerateId();
161: } else {
162: session.removeAttribute(states_id);
163: }
164: }
165: }
166: }
167:
168: if (null == result_states) {
169: result_states = new ResultStates();
170: }
171:
172: FlowState flowstate = getSessionFlowState(request);
173: if (flowstate != null) {
174: if (flowstate.hasSubmissionGlobalInputs()) {
175: ElementResultStateSession result_state = new ElementResultStateSession(
176: "");
177: result_state.setPreservedInputs(flowstate
178: .getSubmissionGlobalInputs());
179: result_states.put(result_state);
180: }
181:
182: if (flowstate.hasSubmissionElementInputs()
183: && flowstate.getSubmissionContextId() != null) {
184: ElementResultStateSession result_state = new ElementResultStateSession(
185: flowstate.getSubmissionContextId());
186: result_state.setPreservedInputs(flowstate
187: .getSubmissionElementInputs());
188: result_states.put(result_state);
189: }
190: }
191:
192: return result_states;
193: }
194:
195: public ElementResultState createNewResultState(String contextId)
196: throws EngineException {
197: return new ElementResultStateSession(contextId);
198: }
199:
200: public Class getResultStateType() throws EngineException {
201: return ElementResultStateSession.class;
202: }
203:
204: protected void storeStates(UniqueID stateId, HttpSession session) {
205: ResultStates states = RequestState.getActiveRequestState()
206: .getElementResultStatesObtained();
207: if (null == session.getAttribute(states.getId())) {
208: session.setAttribute(states.getId(), states);
209: }
210: session.setAttribute(stateId + ATTRIBUTE_STATES_SUFFIX, states
211: .getId());
212: }
213: }
|