View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * 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 and
15   * limitations under the License.
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          // do nothing
80  
81      }
82  
83  }