1 17 package org.ajaxanywhere; 18 19 import javax.servlet.*; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 import java.io.IOException ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 import java.util.Map ; 26 import java.util.List ; 27 28 32 public class AAFilter implements Filter { 33 34 public String preSendHandlerClassName; 35 36 public void init(FilterConfig filterConfig) throws ServletException { 37 preSendHandlerClassName = filterConfig.getInitParameter("preSendHandlerClass"); 38 } 39 40 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException , ServletException { 41 42 HttpServletRequest request = (HttpServletRequest ) servletRequest; 43 HttpServletResponse response = (HttpServletResponse ) 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 refreshZones = new HashSet (); 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 e) { 77 XMLHandler.handleError(response, e); 78 } 79 80 } 81 82 private BufferResponseWrapper execHandler(HttpServletRequest request, BufferResponseWrapper bufferResponseWrapper) throws ClassNotFoundException , IllegalAccessException , InstantiationException { 83 if (preSendHandlerClassName!=null){ 84 Class hClass; 85 try { 86 hClass = Thread.currentThread().getContextClassLoader().loadClass(preSendHandlerClassName); 87 } catch (ClassNotFoundException 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 |