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.io.IOException;
20
21 import javax.servlet.Filter;
22 import javax.servlet.FilterChain;
23 import javax.servlet.FilterConfig;
24 import javax.servlet.ServletException;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.web.context.WebApplicationContext;
32
33 /***
34 * Simple ServletFilter for invoking AJAX services.
35 *
36 *
37 * @author <href a="mailto:weaver@apache.org">Scott T. Weaver</a>
38 *
39 */
40 public class AJAXFilter implements Filter
41 {
42 private ApplicationContext ctx;
43 private AJAXService ajaxService;
44 private FilterConfig config;
45
46 public void init(FilterConfig config) throws ServletException
47 {
48 this.config = config;
49 }
50
51 public void doFilter(ServletRequest request, ServletResponse response,
52 FilterChain arg2) throws IOException, ServletException
53 {
54 try
55 {
56 response.setContentType("text/xml");
57 if(ctx == null)
58 {
59 ctx = (ApplicationContext)config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
60 ajaxService = (AJAXService) ctx.getBean("AJAXService");
61 }
62
63 AJAXRequest ajaxRequest = new AJAXRequestImpl((HttpServletRequest) request, (HttpServletResponse) response, config.getServletContext());
64 AJAXResponse ajaxReponse = ajaxService.processRequest(ajaxRequest);
65 ajaxReponse.complete();
66 }
67 catch (AJAXException e)
68 {
69 ((HttpServletResponse) response).sendError(500, e.getMessage());
70 }
71 catch(Exception e)
72 {
73 throw new ServletException(e.getMessage(), e);
74 }
75 }
76
77 public void destroy()
78 {
79
80
81 }
82
83 }