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: ChildRequestEncoder.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.uwyn.rife.engine.RequestMethod;
011: import com.uwyn.rife.tools.ArrayUtils;
012: import com.uwyn.rife.tools.Base64;
013: import java.util.Map;
014:
015: class ChildRequestEncoder {
016: private static final String SEP_DECLARATION_NAME = "x\000";
017: private static final int SEP_DECLARATION_NAME_LENGTH = SEP_DECLARATION_NAME
018: .length();
019: private static final byte[] SEP_DECLARATION_NAME_BYTES = SEP_DECLARATION_NAME
020: .getBytes();
021: private static final String SEP_METHOD = "m\000";
022: private static final int SEP_METHOD_LENGTH = SEP_METHOD.length();
023: private static final byte[] SEP_METHOD_BYTES = SEP_METHOD
024: .getBytes();
025: private static final String SEP_PATHINFO = "i\000";
026: private static final int SEP_PATHINFO_LENGTH = SEP_PATHINFO
027: .length();
028: private static final byte[] SEP_PATHINFO_BYTES = SEP_PATHINFO
029: .getBytes();
030:
031: static String encode(ElementInfo elementInfo, RequestState state) {
032: byte[] childrequest_bytes = new byte[0];
033:
034: if (state != null) {
035: // serialize the declaration name
036: String declaration_name = elementInfo.getDeclarationName();
037: childrequest_bytes = declaration_name.getBytes();
038: childrequest_bytes = ArrayUtils.join(childrequest_bytes,
039: SEP_DECLARATION_NAME_BYTES);
040:
041: // serialize the method
042: RequestMethod method = state.getElementState().getMethod();
043: if (method != null) {
044: childrequest_bytes = ArrayUtils.join(
045: childrequest_bytes, method.toString()
046: .getBytes());
047: childrequest_bytes = ArrayUtils.join(
048: childrequest_bytes, SEP_METHOD_BYTES);
049: }
050:
051: // serialize the pathinfo
052: String path_info = state.getElementState().getPathInfo();
053: if (path_info != null && path_info.length() > 0) {
054: childrequest_bytes = ArrayUtils.join(
055: childrequest_bytes, path_info.getBytes());
056: childrequest_bytes = ArrayUtils.join(
057: childrequest_bytes, SEP_PATHINFO_BYTES);
058: }
059:
060: // serialize the parameters and their values
061: childrequest_bytes = ArrayUtils.join(childrequest_bytes,
062: ParameterMapEncoder.encodeToBytes(state
063: .getElementState().getRequestParameters()));
064: }
065:
066: return Base64.encodeToString(childrequest_bytes, false);
067: }
068:
069: static void decode(ElementInfo elementInfo, RequestState state) {
070: if (state != null) {
071: String encoded_childrequest = state.getElementState()
072: .getRequestParameter(
073: ReservedParameters.CHILDREQUEST);
074:
075: String declaration_name = null;
076: String method = "";
077: String path_info = "";
078: Map<String, String[]> parameters = null;
079: if (encoded_childrequest != null
080: & encoded_childrequest.length() > 0) {
081: String decoded_childrequest = new String(Base64
082: .decode(encoded_childrequest));
083:
084: // deserialize the declaration name
085: int declaration_name_index = decoded_childrequest
086: .indexOf(SEP_DECLARATION_NAME);
087: if (declaration_name_index != -1) {
088: declaration_name = decoded_childrequest.substring(
089: 0, declaration_name_index);
090: }
091: // online use this childrequest if it's element declaration name
092: // matches the one that was targeted when the child request was
093: // encoded
094: if (null == declaration_name
095: || !declaration_name.equals(elementInfo
096: .getDeclarationName())) {
097: return;
098: }
099: decoded_childrequest = decoded_childrequest
100: .substring(declaration_name_index
101: + SEP_DECLARATION_NAME_LENGTH);
102:
103: // deserialize the method
104: int method_index = decoded_childrequest
105: .indexOf(SEP_METHOD);
106: if (method_index != -1) {
107: method = decoded_childrequest.substring(0,
108: method_index);
109: decoded_childrequest = decoded_childrequest
110: .substring(method_index + SEP_METHOD_LENGTH);
111: }
112:
113: // deserialize the pathinfo
114: int path_info_index = decoded_childrequest
115: .indexOf(SEP_PATHINFO);
116: if (path_info_index != -1) {
117: path_info = decoded_childrequest.substring(0,
118: path_info_index);
119: decoded_childrequest = decoded_childrequest
120: .substring(path_info_index
121: + SEP_PATHINFO_LENGTH);
122: }
123:
124: // deserialize the parameters and their values
125: parameters = ParameterMapEncoder
126: .decodeFromString(decoded_childrequest);
127: }
128:
129: state.getElementState().setMethod(
130: RequestMethod.getMethod(method));
131: state.getElementState().setPathInfo(path_info);
132: state.getElementState().setRequestParameters(parameters);
133: }
134: }
135: }
|