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: ElementResultStateQuery.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import java.io.UnsupportedEncodingException;
011: import java.util.LinkedHashMap;
012: import java.util.Map;
013:
014: class ElementResultStateQuery implements ElementResultState {
015: private String mContextId;
016: private String mContinuationId;
017: private Map<String, String[]> mPreservedInputs;
018:
019: private String mStringEncodedState;
020: private String mBase64EncodedState;
021:
022: public ElementResultStateQuery(String contextId) {
023: mContextId = contextId;
024: }
025:
026: public void populate(ElementResultState other) {
027: this .mContextId = other.getContextId();
028: this .mContinuationId = other.getContinuationId();
029: this .mPreservedInputs = other.getPreservedInputs();
030: }
031:
032: public String getContextId() {
033: return mContextId;
034: }
035:
036: void setEncodedState(String stringEncodedState,
037: String base64EncodedState) {
038: mContinuationId = null;
039: mPreservedInputs = null;
040:
041: mStringEncodedState = stringEncodedState;
042: mBase64EncodedState = base64EncodedState;
043: }
044:
045: String getStringEncodedState() {
046: if (null == mStringEncodedState) {
047: if (mContinuationId != null) {
048: if (null == mPreservedInputs) {
049: mPreservedInputs = new LinkedHashMap<String, String[]>();
050: }
051:
052: mPreservedInputs.put(ReservedParameters.CONTID,
053: new String[] { mContinuationId });
054: }
055:
056: try {
057: mStringEncodedState = new String(ParameterMapEncoder
058: .encodeToBytes(mPreservedInputs, mContextId),
059: "UTF-8");
060: } catch (UnsupportedEncodingException e) {
061: // can't happen, UTF-8 is always supported
062: }
063: }
064:
065: return mStringEncodedState;
066: }
067:
068: String getBase64EncodedState() {
069: if (null == mBase64EncodedState) {
070: if (mContinuationId != null) {
071: if (null == mPreservedInputs) {
072: mPreservedInputs = new LinkedHashMap<String, String[]>();
073: }
074:
075: mPreservedInputs.put(ReservedParameters.CONTID,
076: new String[] { mContinuationId });
077: }
078:
079: mBase64EncodedState = ParameterMapEncoder
080: .encodeToBase64String(mPreservedInputs, mContextId);
081: }
082:
083: return mBase64EncodedState;
084: }
085:
086: public void setContinuationId(String continuationId) {
087: mStringEncodedState = null;
088: mBase64EncodedState = null;
089:
090: mContinuationId = continuationId;
091: }
092:
093: public String getContinuationId() {
094: if (null == mContinuationId) {
095: extractState();
096: }
097:
098: return mContinuationId;
099: }
100:
101: public void setPreservedInputs(Map<String, String[]> preservedInputs) {
102: mStringEncodedState = null;
103: mBase64EncodedState = null;
104:
105: mPreservedInputs = preservedInputs;
106: }
107:
108: public Map<String, String[]> getPreservedInputs() {
109: if (null == mPreservedInputs) {
110: extractState();
111: }
112:
113: return mPreservedInputs;
114: }
115:
116: private void extractState() {
117: if (mPreservedInputs != null) {
118: return;
119: }
120:
121: Map<String, String[]> inputs = ParameterMapEncoder
122: .decodeFromString(getStringEncodedState());
123: if (inputs.containsKey(ReservedParameters.CONTID)) {
124: String[] contid = inputs.remove(ReservedParameters.CONTID);
125: if (contid.length > 0) {
126: mContinuationId = contid[0];
127: }
128: }
129: mPreservedInputs = inputs;
130: }
131: }
|