1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.ajax;
1819import java.io.IOException;
20import java.io.InputStream;
21import java.io.InputStreamReader;
22import java.io.Reader;
23import java.io.Writer;
24import java.io.OutputStreamWriter;
25import java.io.StringWriter;
26import java.util.HashMap;
27import java.util.Map;
2829import javax.servlet.ServletOutputStream;
30import javax.servlet.http.HttpServletResponse;
3132import org.apache.commons.logging.Log;
33import org.apache.commons.logging.LogFactory;
34import org.apache.jetspeed.layout.impl.Constants;
35import org.apache.jetspeed.request.RequestContext;
36import org.apache.velocity.VelocityContext;
37import org.apache.velocity.app.VelocityEngine;
38import org.apache.velocity.context.Context;
39import org.apache.velocity.tools.generic.EscapeTool;
4041/***42 * 43 * Provides a generic way to handle a Ajax request/response. Useful for AJAX since44 * the processing can be broken down into actions and builders45 */46publicclassAjaxRequestServiceImpl implements AjaxRequestService
47 {
4849protectedstaticfinal String CONTENT_TYPE = "text/xml";
5051protectedstaticfinal String AJAX_PROCESSOR = "AJAX processor";
5253protectedstaticfinal String DEFAULT_ERROR = "<js><status>failure</status><action>unknown</action></js>";
5455// Name of the parameter that will be used to lookup the56// command object. Default is action.57//58// Sample URL:59// http://localhost/js?pipeline=layout&action=move60//61// In this case the parameter "action" is used to find "move"62// "move" will be used as the key to lookup the object that will63// handle the command64protectedstaticfinal String URL_PARAMETER_NAME = "action";
6566/*** Logger */67protected Log log = LogFactory.getLog(AjaxRequestServiceImpl.class);
6869// Objects that are available to execution. These objects must70// implement either the Action interface or the Builder interface71// or both.72// If the Action interface is implemented, then the run method is called73// If the Build interface is implemented, then the build methods are called74protected Map objects;
7576// Used to create the response XML77protected VelocityEngine velocityEngine = null;
7879// Parameter on the URL that will be used to lookup the object80protected String urlParameterName = URL_PARAMETER_NAME;
8182// Default Action if no action specified83protected String defaultAction = "getpage";
8485// Handy Velocity Escape Tool (is threadsafe)86protected EscapeTool velocityEscTool = null;
8788// Spring can be used to inject this information89publicAjaxRequestServiceImpl(Map objects, VelocityEngine velocityEngine)
90 {
91this.objects = objects;
92this.velocityEngine = velocityEngine;
93this.velocityEscTool = new EscapeTool();
94 }
9596// Spring can be used to inject this information97publicAjaxRequestServiceImpl(Map objects, VelocityEngine velocityEngine,
98 String urlParameterName)
99 {
100this.objects = objects;
101this.velocityEngine = velocityEngine;
102this.urlParameterName = urlParameterName;
103this.velocityEscTool = new EscapeTool();
104 }
105106// This is the entry point for this service107publicvoid process(RequestContext requestContext) throws AJAXException
108 {
109// Lookup the object that is to be used110 String objectKey = requestContext.getRequestParameter(urlParameterName);
111if (objectKey == null)
112 {
113 objectKey = defaultAction;
114 }
115// Get the object associated with this key116 Object object = objects.get(objectKey);
117if (object != null)
118 {
119 Map resultMap = new HashMap();
120121boolean success = true;
122try123 {
124// Check to see if this object implements the action125// interface126if (object instanceof AjaxAction)
127 {
128 success = processAction((AjaxAction) object,
129 requestContext, resultMap);
130 }
131 } catch (Exception e)
132 {
133 success = false;
134 }
135136try137 {
138// Check to see if this object implements the builder139// interface140if (object instanceof AjaxBuilder)
141 {
142 processBuilder((AjaxBuilder) object, resultMap,
143 requestContext, success);
144 }
145 } catch (Exception e)
146 {
147// The builder failed, return an error response148 buildError(requestContext);
149 }
150 } else151 {
152// Log an informational message153 log.debug("could not find the object named:" + objectKey);
154155// Return an error response156 buildError(requestContext);
157 }
158 }
159160// Process the action if provided161protectedboolean processAction(AjaxAction action,
162 RequestContext requestContext, Map resultMap)
163 throws Exception
164 {
165return action.run(requestContext, resultMap);
166 }
167168// Process the builder if provided169protectedvoid processBuilder(AjaxBuilder builder, Map inputMap,
170 RequestContext requestContext, boolean actionSuccessFlag)
171 {
172// Response will always be text/xml173 String format = requestContext.getRequestParameter("format");
174if (format == null)
175 {
176 requestContext.getResponse().setContentType(CONTENT_TYPE);
177 }
178else179 {
180if (format.equals("json"))
181 {
182 requestContext.getResponse().setContentType("text/json");
183 }
184 }
185186try187 {
188// Ask the builder to construct the context189// Add the input map to the velocity context190191boolean result = true;
192193if (actionSuccessFlag == true)
194 {
195 result = builder.buildContext(requestContext, inputMap);
196 }
197else198 {
199 result = builder.buildErrorContext(requestContext, inputMap);
200 }
201202 Context context = new VelocityContext(inputMap);
203 context.put("esc", this.velocityEscTool);
204205// Check to see if we have a valid context206if (result)
207 {
208// Get the name of the template from the builder209 String templateName = null;
210211if (actionSuccessFlag == true)
212 {
213 templateName = builder.getTemplate();
214 }
215else216 {
217 templateName = builder.getErrorTemplate();
218 }
219220// Get a reader to the velocity template221final InputStream templateStream = this.getClass()
222 .getClassLoader().getResourceAsStream(templateName);
223224 Reader template = new InputStreamReader(templateStream);
225226// The results of the velocity template will be stored here227 StringWriter stringWriter = new StringWriter();
228229// Run the velocity template230 velocityEngine.evaluate(context, stringWriter,
231 AJAX_PROCESSOR, template);
232233// Get the results from the velocity processing234 String buffer = stringWriter.getBuffer().toString();
235236//log.debug("output from AjaxService:" + buffer);237238// Put the response XML on the response object239 HttpServletResponse response = requestContext.getResponse();
240 ServletOutputStream sos = response.getOutputStream();
241242 Writer writer = new OutputStreamWriter(sos, "UTF-8");
243 writer.write(buffer);
244 writer.flush();
245 }
246else247 {
248 log.error("could not create builder context");
249 buildError(requestContext);
250 }
251 } catch (Exception e)
252 {
253 log.error("builder failed", e);
254 inputMap.put(Constants.REASON, e.toString());
255256 buildError(requestContext);
257 }
258 }
259260// This is the last chance to handle an error to send back to the client261// Send back a generic response. Subclasses may want to override this 262// method263protectedvoid buildError(RequestContext requestContext)
264 {
265try266 {
267 requestContext.getResponse().getOutputStream().print(DEFAULT_ERROR);
268 }
269catch (IOException e)
270 {
271// Not much can be done here, an exception while handling an exception272 log.error("exception while trying to build an error message", e);
273 }
274 }
275276/***277 * @return Returns the objects.278 */279public Map getActionMap()
280 {
281return objects;
282 }
283284 }