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: ParameterMapEncoder.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import java.util.*;
011:
012: import com.uwyn.rife.tools.ArrayUtils;
013: import com.uwyn.rife.tools.Base64;
014: import com.uwyn.rife.tools.StringUtils;
015: import java.io.UnsupportedEncodingException;
016:
017: class ParameterMapEncoder {
018: private static final String SEP_CONTEXT;
019: private static final String SEP_PARAMETER;
020: private static final String SEP_PARAMETER_NAME;
021: private static final String SEP_VALUE;
022: private static final byte[] SEP_CONTEXT_BYTES;
023: private static final byte[] SEP_PARAMETER_BYTES;
024: private static final byte[] SEP_PARAMETER_NAME_BYTES;
025: private static final byte[] SEP_VALUE_BYTES;
026:
027: static {
028: SEP_CONTEXT = "c\000";
029: SEP_PARAMETER = "p\000";
030: SEP_PARAMETER_NAME = "n\000";
031: SEP_VALUE = "v\000";
032: byte[] context_bytes = null;
033: byte[] parameter_bytes = null;
034: byte[] parameter_name_bytes = null;
035: byte[] value_bytes = null;
036: try {
037: context_bytes = SEP_CONTEXT.getBytes("UTF-8");
038: parameter_bytes = SEP_PARAMETER.getBytes("UTF-8");
039: parameter_name_bytes = SEP_PARAMETER_NAME.getBytes("UTF-8");
040: value_bytes = SEP_VALUE.getBytes("UTF-8");
041: } catch (UnsupportedEncodingException e) {
042: // should never happen
043: }
044: SEP_CONTEXT_BYTES = context_bytes;
045: SEP_PARAMETER_BYTES = parameter_bytes;
046: SEP_PARAMETER_NAME_BYTES = parameter_name_bytes;
047: SEP_VALUE_BYTES = value_bytes;
048: }
049:
050: static String encodeToBase64String(Map<String, String[]> map) {
051: return Base64.encodeToString(encodeToBytes(map, null), false);
052: }
053:
054: static String encodeToBase64String(Map<String, String[]> map,
055: String context) {
056: return Base64
057: .encodeToString(encodeToBytes(map, context), false);
058: }
059:
060: static byte[] encodeToBytes(Map<String, String[]> map) {
061: return encodeToBytes(map, null);
062: }
063:
064: static byte[] encodeToBytes(Map<String, String[]> map,
065: String context) {
066: byte[] map_bytes = new byte[0];
067:
068: if (map != null) {
069: Set<Map.Entry<String, String[]>> parameter_entries = map
070: .entrySet();
071: if (parameter_entries.size() > 0) {
072: try {
073: // handle the context
074: if (context != null) {
075: map_bytes = context.getBytes("UTF-8");
076: map_bytes = ArrayUtils.join(map_bytes,
077: SEP_CONTEXT_BYTES);
078: }
079:
080: // handle the parameters
081: Iterator<Map.Entry<String, String[]>> parameter_entries_it = parameter_entries
082: .iterator();
083: Map.Entry<String, String[]> parameter_entry = null;
084: String[] parameter_values = null;
085: while (parameter_entries_it.hasNext()) {
086: parameter_entry = parameter_entries_it.next();
087:
088: // add the parameter name
089: map_bytes = ArrayUtils.join(map_bytes,
090: parameter_entry.getKey().getBytes(
091: "UTF-8"));
092: map_bytes = ArrayUtils.join(map_bytes,
093: SEP_PARAMETER_NAME_BYTES);
094:
095: // add the values of the parameter
096: parameter_values = parameter_entry.getValue();
097: if (parameter_values != null) {
098: for (int i = 0; i < parameter_values.length; i++) {
099: map_bytes = ArrayUtils.join(map_bytes,
100: parameter_values[i]
101: .getBytes("UTF-8"));
102: if (i < parameter_values.length - 1) {
103: map_bytes = ArrayUtils.join(
104: map_bytes, SEP_VALUE_BYTES);
105: }
106: }
107: }
108:
109: // add the parameter seperator
110: if (parameter_entries_it.hasNext()) {
111: map_bytes = ArrayUtils.join(map_bytes,
112: SEP_PARAMETER_BYTES);
113: }
114: }
115: } catch (UnsupportedEncodingException e) {
116: // should never happen
117: }
118: }
119: }
120:
121: return map_bytes;
122: }
123:
124: static Map<String, String[]> decodeFromBase64String(
125: String base64Encoded) {
126: if (null == base64Encoded) {
127: return Collections.EMPTY_MAP;
128: }
129:
130: try {
131: byte[] decoded_bytes = Base64.decode(base64Encoded);
132: if (null == decoded_bytes) {
133: return Collections.EMPTY_MAP;
134: }
135:
136: String decoded = new String(decoded_bytes, "UTF-8");
137:
138: return decodeFromString(decoded);
139: } catch (UnsupportedEncodingException e) {
140: // should never happen
141: return null;
142: }
143: }
144:
145: static String[] seperateBase64ContextString(String base64Encoded) {
146: if (null == base64Encoded) {
147: return null;
148: }
149:
150: try {
151: byte[] decoded_bytes = Base64.decode(base64Encoded);
152: if (null == decoded_bytes) {
153: return null;
154: }
155:
156: String decoded = new String(decoded_bytes, "UTF-8");
157: List<String> context_parts = StringUtils.split(decoded,
158: SEP_CONTEXT);
159: if (2 == context_parts.size()) {
160: return new String[] { context_parts.get(0),
161: context_parts.get(1) };
162: } else if (1 == context_parts.size()) {
163: return new String[] { "", context_parts.get(0) };
164: }
165: } catch (UnsupportedEncodingException e) {
166: // should never happen
167: }
168:
169: return null;
170: }
171:
172: static Map<String, String[]> decodeFromString(String encoded) {
173: HashMap<String, String[]> parameters = new HashMap<String, String[]>();
174: if (encoded != null && encoded.length() > 0) {
175: if (encoded.length() > 0) {
176: if (encoded.indexOf(SEP_CONTEXT) != -1) {
177: return parameters;
178: }
179:
180: // deserialize the parameters and their values
181: ArrayList<String> parameter_entries = null;
182:
183: // get the parameters
184: parameter_entries = StringUtils.split(encoded,
185: SEP_PARAMETER);
186:
187: // iterate over the parameters
188: ArrayList<String> parameter_parts = null;
189: String[] parameter_values = null;
190: for (String parameter_entry : parameter_entries) {
191: // get the parameter name and its values
192: parameter_parts = StringUtils.split(
193: parameter_entry, SEP_PARAMETER_NAME);
194: parameter_values = StringUtils.splitToArray(
195: parameter_parts.get(1), SEP_VALUE);
196:
197: parameters.put(parameter_parts.get(0),
198: parameter_values);
199: }
200: }
201: }
202:
203: return parameters;
204: }
205: }
|