|
1 |
| package net.sf.crispy.impl.rest; |
|
2 |
| |
|
3 |
| import java.lang.reflect.Method; |
|
4 |
| import java.util.ArrayList; |
|
5 |
| import java.util.Hashtable; |
|
6 |
| import java.util.Iterator; |
|
7 |
| import java.util.List; |
|
8 |
| import java.util.Map; |
|
9 |
| import java.util.TreeMap; |
|
10 |
| import java.util.Vector; |
|
11 |
| import java.util.Map.Entry; |
|
12 |
| |
|
13 |
| import net.sf.crispy.impl.http.Constant; |
|
14 |
| import net.sf.crispy.util.Converter; |
|
15 |
| import net.sf.crispy.util.Invoker; |
|
16 |
| import net.sf.crispy.util.Util; |
|
17 |
| |
|
18 |
| public class ParameterDeserializer { |
|
19 |
| |
|
20 |
25
| public static Map convertString2Map (String pvParamStr) {
|
|
21 |
25
| String lvParams[] = pvParamStr.split("&");
|
|
22 |
25
| Map lvMap = new Hashtable(lvParams.length);
|
|
23 |
25
| for (int i = 0; i < lvParams.length; i++) {
|
|
24 |
259
| String lvKeyVal[] = lvParams[i].split("=");
|
|
25 |
259
| String lvValue[] = (String[]) lvMap.get(lvKeyVal[0]);
|
|
26 |
259
| if (lvValue == null) {
|
|
27 |
243
| lvMap.put(lvKeyVal[0], new String[] {lvKeyVal[1]});
|
|
28 |
| } else { |
|
29 |
16
| String lvNewValue [] = new String[lvValue.length + 1];
|
|
30 |
16
| for (int j = 0; j < lvValue.length; j++) {
|
|
31 |
18
| lvNewValue[j] = lvValue[j];
|
|
32 |
| } |
|
33 |
16
| lvNewValue[lvValue.length] = lvKeyVal[1];
|
|
34 |
16
| lvMap.put(lvKeyVal[0], lvNewValue);
|
|
35 |
| } |
|
36 |
| |
|
37 |
| } |
|
38 |
25
| return lvMap;
|
|
39 |
| } |
|
40 |
| |
|
41 |
| |
|
42 |
133
| public Object deserialize(final Map pvParamMap) throws Exception {
|
|
43 |
133
| Map lvMap = new Hashtable();
|
|
44 |
133
| Map lvRefMap = new TreeMap();
|
|
45 |
133
| pvParamMap.remove(Constant.PARAM_TYPES);
|
|
46 |
| |
|
47 |
133
| Iterator itMap = pvParamMap.entrySet().iterator();
|
|
48 |
133
| while (itMap.hasNext()) {
|
|
49 |
566
| Map.Entry lvMapEntry = (Entry) itMap.next();
|
|
50 |
566
| String lvKey = (String) lvMapEntry.getKey();
|
|
51 |
566
| Object o = lvMapEntry.getValue();
|
|
52 |
566
| String lvValue[] = null;
|
|
53 |
566
| if (o.getClass().isArray()) {
|
|
54 |
566
| lvValue = (String[]) pvParamMap.get(lvKey);
|
|
55 |
| } |
|
56 |
| else { |
|
57 |
0
| lvValue = new String[] { o.toString() };
|
|
58 |
| } |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
566
| String lvParam[] = lvKey.split(ParameterSerializer.DELIMITTER);
|
|
64 |
566
| Map lvClassMap = (Map) lvMap.get(lvParam[0]);
|
|
65 |
285
| if (lvClassMap == null) { lvClassMap = new Hashtable(); }
|
|
66 |
| |
|
67 |
566
| switch (lvParam.length) {
|
|
68 |
| |
|
69 |
130
| case 1:
|
|
70 |
130
| lvClassMap.put(lvParam[0], lvValue[0]);
|
|
71 |
130
| break;
|
|
72 |
340
| case 2:
|
|
73 |
| |
|
74 |
340
| lvClassMap.put(lvParam[1], lvValue[0]);
|
|
75 |
340
| break;
|
|
76 |
| |
|
77 |
| |
|
78 |
88
| case 3:
|
|
79 |
88
| lvRefMap.put(lvKey, lvValue);
|
|
80 |
88
| break;
|
|
81 |
| |
|
82 |
| |
|
83 |
8
| case 4:
|
|
84 |
8
| lvRefMap.put(lvKey, lvValue);
|
|
85 |
8
| break;
|
|
86 |
| |
|
87 |
0
| default:
|
|
88 |
| |
|
89 |
0
| System.out.println("???? ParameterDeserializer: " + lvParam.length
|
|
90 |
| + " --> " + Util.array2String(lvParam) |
|
91 |
| + " --> " + Util.array2String(lvValue)); |
|
92 |
0
| break;
|
|
93 |
| } |
|
94 |
508
| if (lvClassMap.size() > 0) { lvMap.put(lvParam[0], lvClassMap); }
|
|
95 |
| } |
|
96 |
| |
|
97 |
| |
|
98 |
133
| itMap = lvMap.entrySet().iterator();
|
|
99 |
| |
|
100 |
133
| Map lvParamObject = new TreeMap();
|
|
101 |
133
| while(itMap.hasNext()) {
|
|
102 |
227
| Map.Entry lvMapEntry = (Entry) itMap.next();
|
|
103 |
227
| String lvKey = (String) lvMapEntry.getKey();
|
|
104 |
227
| Map lvClassMap = (Map) lvMapEntry.getValue();
|
|
105 |
227
| String lvClassStr = (String) lvClassMap.get("class");
|
|
106 |
227
| if (lvClassStr != null) {
|
|
107 |
97
| Class lvClass = Class.forName(lvClassStr);
|
|
108 |
| |
|
109 |
97
| Iterator itClassMap = lvClassMap.entrySet().iterator();
|
|
110 |
97
| while (itClassMap.hasNext()) {
|
|
111 |
340
| Map.Entry lvMapEntry_Class = (Entry) itClassMap.next();
|
|
112 |
340
| String lvKeyClassMap = (String) lvMapEntry_Class.getKey();
|
|
113 |
340
| if (!(lvKeyClassMap.equals("class"))) {
|
|
114 |
243
| Object lvValue = lvMapEntry_Class.getValue();
|
|
115 |
| |
|
116 |
243
| if (lvKeyClassMap.startsWith(Converter.HASH_CODE_KEY)) {
|
|
117 |
97
| lvClassMap.put(lvKeyClassMap, lvValue);
|
|
118 |
146
| } else if (lvValue.toString().startsWith(Converter.HASH_CODE_KEY)) {
|
|
119 |
5
| lvClassMap.put(lvKeyClassMap, lvValue);
|
|
120 |
| } else { |
|
121 |
| |
|
122 |
141
| String lvProperty = "set" + lvKeyClassMap.substring(0,1).toUpperCase() + lvKeyClassMap.substring(1);
|
|
123 |
141
| Vector v = new Vector(1);
|
|
124 |
141
| v.add(lvValue);
|
|
125 |
141
| Method lvMethod = Invoker.findMethod(lvClass, lvProperty, v);
|
|
126 |
141
| lvValue = Converter.convertStringParams2MethodParams(lvMethod, v)[0];
|
|
127 |
141
| lvValue = new Converter().makeSimple(lvValue);
|
|
128 |
141
| lvClassMap.put(lvKeyClassMap, lvValue);
|
|
129 |
| } |
|
130 |
| |
|
131 |
| } |
|
132 |
| } |
|
133 |
| |
|
134 |
97
| lvParamObject.put(lvKey, lvClassMap);
|
|
135 |
| |
|
136 |
| } |
|
137 |
| |
|
138 |
130
| else if (lvClassMap.size() > 0) {
|
|
139 |
130
| String lvSimpleKey = (String) lvClassMap.keySet().iterator().next();
|
|
140 |
130
| lvParamObject.put(lvSimpleKey, lvClassMap.get(lvSimpleKey));
|
|
141 |
| } |
|
142 |
| |
|
143 |
| } |
|
144 |
| |
|
145 |
| |
|
146 |
133
| Iterator it = lvRefMap.entrySet().iterator();
|
|
147 |
133
| List lvDelList = new ArrayList();
|
|
148 |
| |
|
149 |
133
| while (it.hasNext()) {
|
|
150 |
96
| Map.Entry lvMapEntry = (Entry) it.next();
|
|
151 |
96
| String lvKey = (String) lvMapEntry.getKey();
|
|
152 |
96
| Object lvRefValue = lvMapEntry.getValue();
|
|
153 |
96
| String lvValues[] = (String[]) lvRefValue;
|
|
154 |
96
| String lvParam[] = lvKey.split(ParameterSerializer.DELIMITTER);
|
|
155 |
| |
|
156 |
96
| Vector lvParamVector = null;
|
|
157 |
| |
|
158 |
96
| for (int i = 0; i < lvValues.length; i++) {
|
|
159 |
124
| String lvValue = lvValues[i];
|
|
160 |
124
| Object o = lvParamObject.get(lvValue);
|
|
161 |
| |
|
162 |
124
| if (lvParam[1].equals(ParameterSerializer.LIST)) {
|
|
163 |
105
| Object ob = lvParamObject.get(lvParam[0]);
|
|
164 |
| |
|
165 |
| |
|
166 |
105
| Map m = findListReferences(lvMap, lvValue);
|
|
167 |
105
| if (ob == null) {
|
|
168 |
21
| if (lvParamVector == null) {lvParamVector = new Vector(); }
|
|
169 |
21
| if ((m != null) && (m.size() > 0)) {
|
|
170 |
21
| lvParamVector.add(m);
|
|
171 |
21
| lvDelList.add(lvValue);
|
|
172 |
| } else { |
|
173 |
0
| lvParamVector.add(lvValue);
|
|
174 |
| } |
|
175 |
21
| lvParamObject.put(lvParam[0], lvParamVector);
|
|
176 |
| } |
|
177 |
84
| else if (ob instanceof Vector) {
|
|
178 |
19
| lvParamVector = (Vector) ob;
|
|
179 |
| |
|
180 |
6
| if ((m != null) && (m.size() > 0)) { lvParamVector.add(m); }
|
|
181 |
| |
|
182 |
13
| else { lvParamVector.add(lvValue); }
|
|
183 |
19
| lvParamObject.put(lvParam[0], lvParamVector);
|
|
184 |
19
| lvDelList.add(lvValue);
|
|
185 |
| } |
|
186 |
| else { |
|
187 |
65
| Map lvMap2 = (Map) ob;
|
|
188 |
65
| lvParamVector = (Vector) lvMap2.get(lvParam[2]);
|
|
189 |
42
| if (lvParamVector == null) {lvParamVector = new Vector(); }
|
|
190 |
| |
|
191 |
10
| if (lvValue.startsWith(Converter.HASH_CODE_KEY)) { lvParamVector.add(lvValue); }
|
|
192 |
| |
|
193 |
| else { |
|
194 |
| |
|
195 |
52
| if ((m != null) && (m.size() > 0)) { lvParamVector.add(m); }
|
|
196 |
| |
|
197 |
3
| else { lvParamVector.add(lvValue); }
|
|
198 |
| } |
|
199 |
65
| lvMap2.put(lvParam[2], lvParamVector);
|
|
200 |
65
| lvParamObject.put(lvParam[0], lvMap2);
|
|
201 |
65
| lvDelList.add(lvValue);
|
|
202 |
| } |
|
203 |
| } |
|
204 |
| |
|
205 |
19
| else if (lvParam[1].equals(ParameterSerializer.MAPSIMPLE)) {
|
|
206 |
5
| Map lvParamMap = (Map) lvParamObject.get(lvParam[0]);
|
|
207 |
2
| if (lvParamMap == null) { lvParamMap = new Hashtable(); }
|
|
208 |
| |
|
209 |
5
| Map m = findListReferences(lvParamObject, lvValue);
|
|
210 |
1
| if ((m != null) && (m.size() > 0)) { lvParamMap.put(lvParam[2], m); }
|
|
211 |
| |
|
212 |
4
| else { lvParamMap.put(lvParam[2], lvValue); }
|
|
213 |
5
| if (lvParamMap.size() > 0) {
|
|
214 |
5
| lvParamObject.remove(lvValue);
|
|
215 |
5
| lvParamObject.put(lvParam[0], lvParamMap);
|
|
216 |
| } |
|
217 |
| } |
|
218 |
| |
|
219 |
14
| else if (lvParam[1].equals(ParameterSerializer.MAP)) {
|
|
220 |
| |
|
221 |
14
| if (lvMap.get(lvParam[0]) != null) {
|
|
222 |
6
| Map m = findMapReferences(lvRefMap, lvMap, lvValue);
|
|
223 |
6
| if ((m != null) && (m.size() > 0)){
|
|
224 |
| |
|
225 |
6
| String lvMapKey = lvKey.split(ParameterSerializer.DELIMITTER)[2];
|
|
226 |
| |
|
227 |
6
| Iterator lvIterator = m.keySet().iterator();
|
|
228 |
6
| while (lvIterator.hasNext()) {
|
|
229 |
8
| String lvDelKey = lvValue + ParameterSerializer.DELIMITTER
|
|
230 |
| + ParameterSerializer.MAP + ParameterSerializer.DELIMITTER + lvMapKey |
|
231 |
| + ParameterSerializer.DELIMITTER + lvIterator.next(); |
|
232 |
8
| Object obj = lvRefMap.get(lvDelKey);
|
|
233 |
8
| if (obj != null) {
|
|
234 |
8
| lvDelKey = ((String[]) obj)[0];
|
|
235 |
8
| lvDelList.add(lvDelKey);
|
|
236 |
| } |
|
237 |
| } |
|
238 |
6
| ((Map) lvParamObject.get(lvParam[0])).put(lvMapKey, m);
|
|
239 |
| } |
|
240 |
| } |
|
241 |
| } |
|
242 |
| |
|
243 |
0
| else if (o != null) {
|
|
244 |
0
| System.out.println("---- Invalid type in param (ParameterDeserializer): " + lvParam[1] + " -- " + o);
|
|
245 |
| } |
|
246 |
| else { |
|
247 |
0
| System.out.println("????????????? ParameterDeserializer");
|
|
248 |
| } |
|
249 |
| } |
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
133
| for (Iterator iter = lvDelList.iterator(); iter.hasNext();) {
|
|
255 |
113
| String lvDel = (String) iter.next();
|
|
256 |
113
| lvParamObject.remove(lvDel);
|
|
257 |
| } |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
133
| Vector lvRetVec = new Vector();
|
|
264 |
133
| it = lvParamObject.keySet().iterator();
|
|
265 |
133
| while (it.hasNext()) {
|
|
266 |
165
| Object lvKey = it.next();
|
|
267 |
165
| Object lvValue = lvParamObject.get(lvKey);
|
|
268 |
165
| lvValue = new Converter().makeComplex(lvValue);
|
|
269 |
165
| lvRetVec.add(lvValue);
|
|
270 |
| } |
|
271 |
| |
|
272 |
| |
|
273 |
90
| if (lvRetVec.size() == 1) { return lvRetVec.get(0); }
|
|
274 |
43
| else { return lvRetVec; }
|
|
275 |
| } |
|
276 |
| |
|
277 |
| |
|
278 |
110
| public Map findListReferences (Map pvClassMap, String pvFindString) {
|
|
279 |
110
| Map ret = new Hashtable();
|
|
280 |
110
| Map lvMap = (Map) pvClassMap.get(pvFindString);
|
|
281 |
| |
|
282 |
80
| if (lvMap != null) { return lvMap; }
|
|
283 |
| |
|
284 |
| else { |
|
285 |
30
| String lvSearchHashCode = null;
|
|
286 |
30
| if (pvFindString.indexOf(Converter.HASH_CODE_KEY) == 0) {
|
|
287 |
10
| lvSearchHashCode = pvFindString.substring(Converter.HASH_CODE_KEY.length());
|
|
288 |
10
| Iterator it2 = pvClassMap.entrySet().iterator();
|
|
289 |
28
| while (it2.hasNext()) {
|
|
290 |
28
| Map.Entry lvMapEntry = (Entry) it2.next();
|
|
291 |
28
| Map lvValue2 = (Map) lvMapEntry.getValue();
|
|
292 |
28
| Object o = lvValue2.get(Converter.HASH_CODE_KEY);
|
|
293 |
28
| if ((o != null) && (o.equals(lvSearchHashCode))) {
|
|
294 |
10
| return lvValue2;
|
|
295 |
| } |
|
296 |
| } |
|
297 |
| } |
|
298 |
| } |
|
299 |
20
| return ret;
|
|
300 |
| } |
|
301 |
| |
|
302 |
6
| public Map findMapReferences (Map pvRefMap, Map pvClassMap, String pvFindString) {
|
|
303 |
6
| Iterator it = pvRefMap.entrySet().iterator();
|
|
304 |
6
| Map ret = new Hashtable();
|
|
305 |
6
| while (it.hasNext()) {
|
|
306 |
27
| Map.Entry lvMapEntry = (Entry) it.next();
|
|
307 |
27
| String lvKey = (String) lvMapEntry.getKey();
|
|
308 |
27
| String lvValue[] = (String[]) lvMapEntry.getValue();
|
|
309 |
27
| if (lvKey.startsWith(pvFindString)) {
|
|
310 |
8
| String lvNewMapKey = lvKey.split(ParameterSerializer.DELIMITTER)[3];
|
|
311 |
8
| String lvFindRefKey[] = (String[]) pvRefMap.get(lvKey);
|
|
312 |
8
| if (lvFindRefKey[0].startsWith(Converter.HASH_CODE_KEY)) {
|
|
313 |
3
| ret.put(lvNewMapKey, lvFindRefKey[0]);
|
|
314 |
| } else { |
|
315 |
5
| ret.put(lvNewMapKey, pvClassMap.get(lvFindRefKey[0]));
|
|
316 |
| } |
|
317 |
| } |
|
318 |
| } |
|
319 |
6
| return ret;
|
|
320 |
| } |
|
321 |
| |
|
322 |
| } |