1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.layout.impl; |
18 |
|
|
19 |
|
import java.io.InputStream; |
20 |
|
import java.io.InputStreamReader; |
21 |
|
import java.io.Reader; |
22 |
|
import java.io.StringWriter; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.StringTokenizer; |
28 |
|
|
29 |
|
import org.apache.jetspeed.ajax.AJAXException; |
30 |
|
import org.apache.jetspeed.ajax.AjaxAction; |
31 |
|
import org.apache.jetspeed.ajax.AjaxBuilder; |
32 |
|
import org.apache.jetspeed.ajax.AjaxRequestService; |
33 |
|
import org.apache.jetspeed.layout.PortletActionSecurityBehavior; |
34 |
|
import org.apache.jetspeed.page.PageManager; |
35 |
|
import org.apache.jetspeed.request.JetspeedRequestContext; |
36 |
|
import org.apache.jetspeed.request.RequestContext; |
37 |
|
import org.apache.velocity.VelocityContext; |
38 |
|
import org.apache.velocity.app.VelocityEngine; |
39 |
|
import org.apache.velocity.context.Context; |
40 |
|
import org.springframework.beans.BeansException; |
41 |
|
import org.springframework.beans.factory.BeanFactory; |
42 |
|
import org.springframework.beans.factory.BeanFactoryAware; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
public class MultipleAction extends BasePortletAction implements AjaxAction, |
62 |
|
AjaxBuilder, BeanFactoryAware |
63 |
|
{ |
64 |
|
|
65 |
|
protected static final String ALL_RESULTS = "results"; |
66 |
|
|
67 |
|
protected static final String BUILD_RESULTS = "buildresults"; |
68 |
|
|
69 |
|
protected static final String MULTIPLE_ACTION_PROCESSOR = "Multiple Action Processor"; |
70 |
|
|
71 |
|
protected static final String COMMANDS = "commands"; |
72 |
|
|
73 |
|
protected static final String COMMAND_TOKEN = ")"; |
74 |
|
|
75 |
|
protected static final String PARAM_TOKEN = ";"; |
76 |
|
|
77 |
|
protected static final String VALUE_TOKEN = ","; |
78 |
|
|
79 |
0 |
protected Map actionMap = null; |
80 |
|
|
81 |
0 |
protected VelocityEngine m_oVelocityEngine = null; |
82 |
|
|
83 |
|
public MultipleAction(AjaxRequestService requestService, String p_sTemplate, |
84 |
|
String p_sErrorTemplate, PageManager p_oPageManager, |
85 |
|
PortletActionSecurityBehavior p_oSecurityBehavior, |
86 |
|
VelocityEngine p_oVelocityEngine) |
87 |
|
{ |
88 |
0 |
super(p_sTemplate, p_sErrorTemplate, p_oPageManager, |
89 |
|
p_oSecurityBehavior); |
90 |
0 |
actionMap = requestService.getActionMap(); |
91 |
0 |
m_oVelocityEngine = p_oVelocityEngine; |
92 |
0 |
} |
93 |
|
|
94 |
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException |
95 |
|
{ |
96 |
|
|
97 |
0 |
Object proxy = beanFactory.getBean("AjaxMultipleAction"); |
98 |
0 |
actionMap.put("multiple", proxy); |
99 |
0 |
} |
100 |
|
|
101 |
|
public boolean run(RequestContext p_oRequestContext, Map p_oResultMap) |
102 |
|
throws AJAXException |
103 |
|
{ |
104 |
0 |
boolean a_bReturnSuccess = true; |
105 |
0 |
List a_oResultArray = new ArrayList(); |
106 |
|
|
107 |
0 |
p_oResultMap.put(ACTION, "multiple"); |
108 |
0 |
p_oResultMap.put(STATUS, "success"); |
109 |
|
|
110 |
|
|
111 |
0 |
String a_sCommands = p_oRequestContext.getRequestParameter(COMMANDS); |
112 |
0 |
if (a_sCommands == null || a_sCommands.length() <= 0) |
113 |
|
{ |
114 |
0 |
buildErrorContext(p_oRequestContext, p_oResultMap); |
115 |
0 |
p_oResultMap.put(STATUS, "failure"); |
116 |
0 |
p_oResultMap.put(REASON, "command parameters not found"); |
117 |
|
|
118 |
0 |
throw new AJAXException("command parameters not found"); |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
0 |
StringTokenizer a_oCommandTok = new StringTokenizer(a_sCommands, |
123 |
|
COMMAND_TOKEN); |
124 |
|
|
125 |
|
|
126 |
0 |
while (a_oCommandTok.hasMoreTokens()) |
127 |
|
{ |
128 |
|
|
129 |
0 |
String a_sCommand = a_oCommandTok.nextToken(); |
130 |
|
|
131 |
|
|
132 |
0 |
a_sCommand = a_sCommand.substring(1); |
133 |
|
|
134 |
|
|
135 |
0 |
StringTokenizer a_oParamTok = new StringTokenizer(a_sCommand, |
136 |
|
PARAM_TOKEN); |
137 |
0 |
if (a_oParamTok == null || a_oParamTok.hasMoreTokens() == false) |
138 |
|
{ |
139 |
0 |
buildErrorContext(p_oRequestContext, p_oResultMap); |
140 |
0 |
p_oResultMap.put(STATUS, "failure"); |
141 |
0 |
p_oResultMap.put(REASON, "incorrect url request"); |
142 |
|
|
143 |
0 |
throw new AJAXException("incorrect url request"); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
0 |
String a_sAction = a_oParamTok.nextToken(); |
148 |
|
|
149 |
|
|
150 |
0 |
Object a_oActionObject = actionMap.get(a_sAction); |
151 |
0 |
if (a_oActionObject == null |
152 |
|
&& !(a_oActionObject instanceof AjaxAction)) |
153 |
|
{ |
154 |
0 |
buildErrorContext(p_oRequestContext, p_oResultMap); |
155 |
0 |
p_oResultMap.put(REASON, "unknown action requested==>" |
156 |
|
+ a_sAction); |
157 |
|
|
158 |
0 |
throw new AJAXException("unknown action requested==>" |
159 |
|
+ a_sAction); |
160 |
|
} |
161 |
|
|
162 |
0 |
AjaxAction a_oAction = (AjaxAction) a_oActionObject; |
163 |
|
|
164 |
0 |
JetspeedRequestContext a_oJetspeedRequestContext = (JetspeedRequestContext) p_oRequestContext; |
165 |
|
|
166 |
|
|
167 |
0 |
while (a_oParamTok.hasMoreTokens()) |
168 |
|
{ |
169 |
0 |
String a_sName = a_oParamTok.nextToken(VALUE_TOKEN); |
170 |
|
|
171 |
0 |
if (a_sName.indexOf(';') >= 0) |
172 |
|
{ |
173 |
0 |
a_sName = a_sName.substring(1); |
174 |
|
} |
175 |
|
|
176 |
0 |
String a_sValue = a_oParamTok.nextToken(); |
177 |
|
|
178 |
|
|
179 |
0 |
a_oJetspeedRequestContext.setAttribute(a_sName, a_sValue); |
180 |
0 |
} |
181 |
|
|
182 |
|
|
183 |
0 |
Map a_oResultMap = new HashMap(); |
184 |
|
boolean a_bSuccess; |
185 |
|
|
186 |
|
try |
187 |
|
{ |
188 |
0 |
a_bSuccess = a_oAction.runBatch(a_oJetspeedRequestContext, |
189 |
|
a_oResultMap); |
190 |
0 |
} catch (Exception e) |
191 |
|
{ |
192 |
|
|
193 |
0 |
p_oResultMap.put(REASON, a_oResultMap.get(REASON)); |
194 |
|
|
195 |
0 |
throw new AJAXException(e); |
196 |
0 |
} |
197 |
|
|
198 |
|
|
199 |
0 |
if (a_bSuccess) |
200 |
|
{ |
201 |
|
|
202 |
0 |
if (a_oAction instanceof AjaxBuilder) |
203 |
|
{ |
204 |
0 |
processBuilder((AjaxBuilder) a_oAction, a_oResultMap, |
205 |
|
p_oRequestContext, a_bSuccess); |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
0 |
String a_sBuildResults = (String) a_oResultMap |
210 |
|
.get(BUILD_RESULTS); |
211 |
|
|
212 |
|
|
213 |
0 |
int a_iStartIndex = a_sBuildResults.indexOf("<?xml"); |
214 |
0 |
if (a_iStartIndex >= 0) |
215 |
|
{ |
216 |
|
|
217 |
0 |
int a_iEndIndex = a_sBuildResults.indexOf(">", |
218 |
|
a_iStartIndex); |
219 |
0 |
if (a_iEndIndex >= 0) |
220 |
|
{ |
221 |
0 |
String a_sStart = a_sBuildResults.substring(0, |
222 |
|
a_iStartIndex); |
223 |
0 |
String a_sEnd = a_sBuildResults.substring( |
224 |
|
a_iEndIndex + 1, a_sBuildResults.length()); |
225 |
0 |
a_sBuildResults = a_sStart + a_sEnd; |
226 |
|
} |
227 |
|
} |
228 |
|
|
229 |
0 |
if (a_sBuildResults != null) |
230 |
|
{ |
231 |
|
|
232 |
0 |
a_oResultArray.add(a_sBuildResults); |
233 |
|
} |
234 |
0 |
} else |
235 |
|
{ |
236 |
|
|
237 |
0 |
p_oResultMap.put(REASON, a_oResultMap.get(REASON)); |
238 |
|
|
239 |
|
|
240 |
0 |
a_bReturnSuccess = false; |
241 |
0 |
break; |
242 |
|
} |
243 |
0 |
} |
244 |
|
|
245 |
|
|
246 |
0 |
p_oResultMap.put(ALL_RESULTS, a_oResultArray); |
247 |
|
|
248 |
0 |
return a_bReturnSuccess; |
249 |
|
} |
250 |
|
|
251 |
|
|
252 |
|
protected void processBuilder(AjaxBuilder p_oBuilder, Map p_oInputMap, |
253 |
|
RequestContext p_oRequestContext, boolean p_oActionSuccessFlag) |
254 |
|
{ |
255 |
|
try |
256 |
|
{ |
257 |
|
|
258 |
|
|
259 |
0 |
boolean result = true; |
260 |
|
|
261 |
0 |
if (p_oActionSuccessFlag == true) |
262 |
|
{ |
263 |
0 |
result = p_oBuilder |
264 |
|
.buildContext(p_oRequestContext, p_oInputMap); |
265 |
|
} else |
266 |
|
{ |
267 |
0 |
result = p_oBuilder.buildErrorContext(p_oRequestContext, |
268 |
|
p_oInputMap); |
269 |
|
} |
270 |
|
|
271 |
0 |
Context a_oContext = new VelocityContext(p_oInputMap); |
272 |
|
|
273 |
|
|
274 |
0 |
if (result) |
275 |
|
{ |
276 |
|
|
277 |
0 |
String a_sTemplateName = null; |
278 |
|
|
279 |
0 |
if (p_oActionSuccessFlag == true) |
280 |
|
{ |
281 |
0 |
a_sTemplateName = p_oBuilder.getTemplate(); |
282 |
|
} else |
283 |
|
{ |
284 |
0 |
a_sTemplateName = p_oBuilder.getErrorTemplate(); |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
0 |
final InputStream a_oTemplateStream = this.getClass() |
289 |
|
.getClassLoader().getResourceAsStream(a_sTemplateName); |
290 |
|
|
291 |
0 |
Reader a_oTemplate = new InputStreamReader(a_oTemplateStream); |
292 |
|
|
293 |
|
|
294 |
0 |
StringWriter a_oStringWriter = new StringWriter(); |
295 |
|
|
296 |
|
|
297 |
0 |
m_oVelocityEngine.evaluate(a_oContext, a_oStringWriter, |
298 |
|
MULTIPLE_ACTION_PROCESSOR, a_oTemplate); |
299 |
|
|
300 |
|
|
301 |
0 |
String a_sResults = a_oStringWriter.getBuffer().toString(); |
302 |
|
|
303 |
|
|
304 |
0 |
p_oInputMap.put(BUILD_RESULTS, a_sResults); |
305 |
0 |
} else |
306 |
|
{ |
307 |
0 |
log.error("could not create builder context"); |
308 |
|
} |
309 |
0 |
} catch (Exception e) |
310 |
|
{ |
311 |
0 |
log.error("builder failed", e); |
312 |
0 |
p_oInputMap.put(Constants.REASON, e.toString()); |
313 |
0 |
} |
314 |
0 |
} |
315 |
|
|
316 |
|
public boolean buildContext(RequestContext p_oRequestContext, |
317 |
|
Map p_oInputMap) |
318 |
|
{ |
319 |
0 |
boolean a_bResults = true; |
320 |
|
|
321 |
0 |
a_bResults = super.buildContext(p_oRequestContext, p_oInputMap); |
322 |
|
|
323 |
0 |
return a_bResults; |
324 |
|
} |
325 |
|
|
326 |
|
} |