KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxanywhere > AAFilter


1 /*
2 Copyright 2005 Vitaliy Shevchuk (shevit@users.sourceforge.net)
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15
16 */

17 package org.ajaxanywhere;
18
19 import javax.servlet.*;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Date: 23 juil. 2005
30  * Time: 21:02:45
31  */

32 public class AAFilter implements Filter {
33
34     public String JavaDoc preSendHandlerClassName;
35
36     public void init(FilterConfig filterConfig) throws ServletException {
37         preSendHandlerClassName = filterConfig.getInitParameter("preSendHandlerClass");
38     }
39
40     public void doFilter(ServletRequest JavaDoc servletRequest, ServletResponse JavaDoc servletResponse, FilterChain filterChain) throws IOException JavaDoc, ServletException {
41
42         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) servletRequest;
43         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) servletResponse;
44
45         if (!AAUtils.isAjaxRequest(servletRequest)) {
46             filterChain.doFilter(servletRequest, response);
47             return;
48         }
49
50
51         request.setCharacterEncoding("UTF-8");
52         response.setContentType("text/xml;charset=utf-8");
53
54         response.setHeader("Cache-Control", "no-cache");
55         response.setDateHeader("Expires", 0);
56         response.setHeader("Pragma", "no-cache");
57
58         BufferResponseWrapper bufferResponseWrapper = new BufferResponseWrapper(response);
59
60         Set JavaDoc refreshZones = new HashSet JavaDoc();
61         AAUtils.setZonesToRefresh(request, refreshZones);
62         AAUtils.getRefreshZonesFromURL(request);
63
64         try {
65             filterChain.doFilter(request, bufferResponseWrapper);
66
67             bufferResponseWrapper = execHandler(request,bufferResponseWrapper);
68
69             if (bufferResponseWrapper.getRedirect() != null) {
70                 XMLHandler.sendRedirect(bufferResponseWrapper);
71             } else {
72                 XMLHandler.sendZones(bufferResponseWrapper, refreshZones);
73             }
74         }
75
76         catch (Throwable JavaDoc e) {
77             XMLHandler.handleError(response, e);
78         }
79
80     }
81
82     private BufferResponseWrapper execHandler(HttpServletRequest JavaDoc request, BufferResponseWrapper bufferResponseWrapper) throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
83         if (preSendHandlerClassName!=null){
84             Class JavaDoc hClass;
85             try {
86                 hClass = Thread.currentThread().getContextClassLoader().loadClass(preSendHandlerClassName);
87             } catch (ClassNotFoundException JavaDoc e) {
88                 hClass = Class.forName(preSendHandlerClassName);
89                 e.printStackTrace();
90             }
91             PreSendHandler handler = (PreSendHandler) hClass.newInstance();
92             return handler.handle(request, bufferResponseWrapper);
93         }
94         return bufferResponseWrapper;
95     }
96
97
98     public void destroy() {
99     }
100 }
101
Popular Tags