1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.ajax; |
18 |
|
|
19 |
|
import java.util.ArrayList; |
20 |
|
import java.util.Iterator; |
21 |
|
import java.util.List; |
22 |
|
import java.util.Map; |
23 |
|
|
24 |
|
import javax.servlet.ServletContext; |
25 |
|
import javax.servlet.http.HttpServletRequest; |
26 |
|
import javax.servlet.http.HttpServletResponse; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
public class AJAXRequestImpl implements AJAXRequest |
35 |
|
{ |
36 |
|
public static final String AJAX_SERVICE = "ajax_service"; |
37 |
|
public static final String AJAX_PARAM_PREFIX = "ajax_param_"; |
38 |
|
|
39 |
|
private final HttpServletRequest request; |
40 |
|
private List ajaxParams; |
41 |
|
private final String serviceName; |
42 |
|
private final String methodName; |
43 |
|
private HttpServletResponse response; |
44 |
|
private ServletContext context; |
45 |
|
|
46 |
|
public AJAXRequestImpl(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws AJAXException |
47 |
0 |
{ |
48 |
0 |
this.request = request; |
49 |
0 |
this.response = response; |
50 |
0 |
this.context = context; |
51 |
0 |
String serviceRequest = request.getParameter(AJAX_SERVICE); |
52 |
0 |
if(serviceRequest == null ) |
53 |
|
{ |
54 |
0 |
throw new AJAXException("No '"+AJAX_SERVICE+"' parameter could be found in the request or it was not in the '{service_name}.{method_name}' format."); |
55 |
|
} |
56 |
0 |
final String split = serviceRequest.split("\\.")[0]; |
57 |
0 |
serviceName = split; |
58 |
0 |
methodName = serviceRequest.split("\\.")[1]; |
59 |
|
|
60 |
0 |
parseRequestArguments(); |
61 |
|
|
62 |
0 |
} |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public List getParameters() |
68 |
|
{ |
69 |
0 |
return ajaxParams; |
70 |
|
} |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
public String getServiceName() |
76 |
|
{ |
77 |
0 |
return serviceName; |
78 |
|
} |
79 |
|
|
80 |
|
protected List parseRequestArguments() throws AJAXException |
81 |
|
{ |
82 |
|
try |
83 |
|
{ |
84 |
0 |
ajaxParams = new ArrayList(); |
85 |
0 |
Map rawParams = request.getParameterMap(); |
86 |
0 |
Iterator entryItr = rawParams.entrySet().iterator(); |
87 |
0 |
while(entryItr.hasNext()) |
88 |
|
{ |
89 |
0 |
Map.Entry entry = (Map.Entry) entryItr.next(); |
90 |
0 |
String key = entry.getKey().toString(); |
91 |
|
|
92 |
0 |
if(key.startsWith(AJAX_PARAM_PREFIX)) |
93 |
|
{ |
94 |
0 |
String[] paramInfo = key.split("_"); |
95 |
0 |
int index = Integer.parseInt(paramInfo[2]); |
96 |
0 |
String type = paramInfo[3]; |
97 |
0 |
AJAXParameter ajaxParam = new AJAXParameter(type, (String[])entry.getValue()); |
98 |
0 |
ajaxParams.add(index, ajaxParam); |
99 |
|
} |
100 |
0 |
} |
101 |
0 |
return ajaxParams; |
102 |
|
} |
103 |
0 |
catch (Throwable e) |
104 |
|
{ |
105 |
0 |
throw new AJAXException("Errors were encountered parsing request parameters for the AJAX service "+serviceName+": "+e.getMessage(), e); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
|
public class AJAXParameter |
110 |
|
{ |
111 |
|
private Object value; |
112 |
|
|
113 |
|
public AJAXParameter(String typeName, String[] paramValues) |
114 |
|
{ |
115 |
|
if(typeName.equals("int")) |
116 |
|
{ |
117 |
|
if(paramValues.length > 1) |
118 |
|
{ |
119 |
|
int[] class="keyword">intValues = new class="keyword">int[paramValues.length]; |
120 |
|
for(int i=0; i<paramValues.length; i++) |
121 |
|
{ |
122 |
|
intValues[i] = Integer.parseInt(paramValues[i]); |
123 |
|
} |
124 |
|
} |
125 |
|
else |
126 |
|
{ |
127 |
|
value = new Integer(paramValues[0]); |
128 |
|
} |
129 |
|
} |
130 |
|
else if(typeName.equals("str")) |
131 |
|
{ |
132 |
|
if(paramValues.length > 1) |
133 |
|
{ |
134 |
|
value = paramValues; |
135 |
|
} |
136 |
|
else |
137 |
|
{ |
138 |
|
value = paramValues[0]; |
139 |
|
} |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
public Object getValue() |
144 |
|
{ |
145 |
|
return value; |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
public String getMethodName() |
154 |
|
{ |
155 |
0 |
return methodName; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
public ServletContext getContext() |
162 |
|
{ |
163 |
0 |
return context; |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
public HttpServletRequest getServletRequest() |
170 |
|
{ |
171 |
0 |
return request; |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
public HttpServletResponse getServletResponse() |
178 |
|
{ |
179 |
0 |
return response; |
180 |
|
} |
181 |
|
} |