KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openinventions > webappfilter > ServletFilter


1 package com.openinventions.webappfilter;
2
3 import org.apache.commons.logging.*;
4 import com.openinventions.metaframework.*;
5 import java.io.*;
6 import java.util.*;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
9
10 public class ServletFilter extends DefaultFilter implements javax.servlet.Filter JavaDoc {
11     private static final Log log = LogFactory.getLog(ServletFilter.class);
12     private static String JavaDoc rootDir = "";
13
14     public void init(FilterConfig filterConfig) throws ServletException {
15         try {
16         String JavaDoc file = filterConfig.getInitParameter("file");
17         this.rootDir = filterConfig.getServletContext().getRealPath("/");
18         if (this.rootDir.endsWith("/") || this.rootDir.endsWith("\\")) {
19             this.rootDir = this.rootDir.substring(0, this.rootDir.length() - 1);
20         }
21         if (log.isDebugEnabled()) {
22             log.debug("file = " + file);
23         }
24         ElementFactory factory = new JXPathElementFileFactory();
25         Element context = factory.createElement("context");
26         context.setValue("file", file);
27         context.setValue("rootDir", rootDir);
28         super.init(null, context);
29         } catch (Exception JavaDoc e) {
30             log.error(e);
31             throw new ServletException(e);
32         }
33     }
34     
35     public void destroy() {
36         try {
37         ElementFactory factory = new JXPathElementFileFactory();
38         Element context = factory.createElement("context");
39         context.setValue("rootDir", rootDir);
40         super.destroy(null, context);
41         } catch (Exception JavaDoc e) {
42             log.error(e);
43         }
44     }
45     
46     public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException {
47         try {
48         State state = new DefaultState();
49         initState(state);
50         Element req = (Element) request.getAttribute("com.openinventions.webappsecurity.Request");
51         Element res = (Element) request.getAttribute("com.openinventions.webappsecurity.Response");
52         String JavaDoc direction = (String JavaDoc) request.getAttribute("com.openinventions.webappsecurity.Direction");
53         if (log.isDebugEnabled()) {
54             log.debug("req = " + req);
55             log.debug("res = " + res);
56             log.debug("direction = " + direction);
57         }
58         state.set("nextCommand", "");
59         state.set("direction", direction);
60         state.set("req", req);
61         state.set("res", res);
62         state.set("rootDir", this.rootDir);
63         super.process(state, null);
64         request.setAttribute("com.openinventions.webappsecurity.Request", state.get("req"));
65         request.setAttribute("com.openinventions.webappsecurity.Response", state.get("res"));
66         request.setAttribute("com.openinventions.webappsecurity.Direction", state.get("direction"));
67         
68         direction = (String JavaDoc) state.get("direction");
69
70         if (log.isDebugEnabled()) {
71             log.debug("filterDirection = " + (String JavaDoc) state.get("filterDirection"));
72             log.debug("direction = " + (String JavaDoc) state.get("direction"));
73             log.debug("nextCommand = " + (String JavaDoc) state.get("nextCommand"));
74         }
75         
76         if (direction.equals("forward")) {
77             chain.doFilter(request, response);
78         }
79
80         direction = (String JavaDoc) request.getAttribute("com.openinventions.webappsecurity.Direction");
81         state.set("direction", direction);
82         
83         if (direction.equals("backward")) {
84             super.process(state, null);
85             request.setAttribute("com.openinventions.webappsecurity.Request", state.get("req"));
86             request.setAttribute("com.openinventions.webappsecurity.Response", state.get("res"));
87             request.setAttribute("com.openinventions.webappsecurity.Direction", direction);
88         }
89         } catch (Exception JavaDoc e) {
90             log.error(e);
91             throw new ServletException(e);
92         }
93     }
94 }
95 /* ====================================================================
96  * The MetaFramework License, Version 1.1
97  *
98  * Copyright (c) 2002 Ivar Chan. All rights
99  * reserved.
100  *
101  * Redistribution and use in source and binary forms, with or without
102  * modification, are permitted provided that the following conditions
103  * are met:
104  *
105  * 1. Redistributions of source code must retain the above copyright
106  * notice, this list of conditions and the following disclaimer.
107  *
108  * 2. Redistributions in binary form must reproduce the above copyright
109  * notice, this list of conditions and the following disclaimer in
110  * the documentation and/or other materials provided with the
111  * distribution.
112  *
113  * 3. The end-user documentation included with the redistribution,
114  * if any, must include the following acknowledgment:
115  * "This product includes software developed by
116  * Ivar Chan (http://www.openinventions.com/metaframework/)."
117  * Alternately, this acknowledgment may appear in the software itself,
118  * if and wherever such third-party acknowledgments normally appear.
119  *
120  * 4. The name "metaframework" must not be used to endorse or promote products
121  * derived from this software without prior written permission. For
122  * written permission, please contact ivarchan@acm.org.
123  *
124  * 5. Products derived from this software may not be called "metaframework",
125  * nor may "metaframework" appear in their name, without
126  * prior written permission of Ivar Chan.
127  *
128  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
129  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
130  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
131  * DISCLAIMED. IN NO EVENT SHALL THE IVAR CHAN BE LIABLE FOR ANY
132  * DIRECT, INDIRECT, INCIDENTAL,
133  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
134  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
135  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
136  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
137  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
138  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
139  * SUCH DAMAGE.
140  * ====================================================================
141  *
142  * This software consists of voluntary contributions made by many
143  * individuals. For more information on metaframework, please see
144  * <http://www.openinventions/metaframework/>.
145  */

146
147
148
Popular Tags