KickJava   Java API By Example, From Geeks To Geeks.

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


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 InitServletFilter implements javax.servlet.Filter JavaDoc {
11     private static final Log log = LogFactory.getLog(ServletFilter.class);
12
13     public void init(FilterConfig filterConfig) throws ServletException {
14     }
15     
16     public void destroy() {
17     }
18
19     public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException {
20         try {
21             ElementFactory factory = new JXPathElementFileFactory();
22             Element req = factory.createElement("request");
23             Element res = factory.createElement("response");
24             populateRequest((HttpServletRequest) request, req);
25             request.setAttribute("com.openinventions.webappsecurity.Request", req);
26             request.setAttribute("com.openinventions.webappsecurity.Response", res);
27             request.setAttribute("com.openinventions.webappsecurity.Direction", "forward");
28             chain.doFilter(request, response);
29             populateResponse((HttpServletResponse) response, res);
30         } catch (Exception JavaDoc e) {
31             log.error(e);
32             throw new ServletException(e);
33         }
34     }
35
36     public void populateRequest(HttpServletRequest request, Element req) throws Exception JavaDoc {
37         // Translate request to XML format
38
req.setValue("url", request.getRequestURL().toString());
39         req.setValue("path", request.getServletPath());
40         req.setValue("ip", request.getRemoteAddr());
41
42         Enumeration headers = request.getHeaderNames();
43
44         int i = 1;
45         while (headers.hasMoreElements()) {
46             String JavaDoc name = (String JavaDoc) headers.nextElement();
47             req.setValue("header[" + i + "]", "");
48             req.setValue("header[" + i + "]/name", name);
49             String JavaDoc value = request.getHeader(name);
50             req.setValue("header[" + i + "]/value", value);
51             i++;
52         }
53                 
54         Enumeration params = request.getParameterNames();
55
56         i = 1;
57         while (params.hasMoreElements()) {
58             String JavaDoc name = (String JavaDoc) params.nextElement();
59             req.setValue("param[" + i + "]", "");
60             req.setValue("param[" + i + "]/name", name);
61             
62             String JavaDoc[] values = request.getParameterValues(name);
63  
64             for (int j = 0; j < values.length; j++) {
65                 req.setValue("param[" + i + "]/value[" + (j + 1) + "]", values[j]);
66             }
67
68             i++;
69         }
70     }
71
72     public void populateResponse(HttpServletResponse response, Element res) throws Exception JavaDoc {
73         if (log.isDebugEnabled()) {
74             log.debug("res.getValue(body) = " + res.getValue("body"));
75         }
76
77         response.getWriter().write(res.getValue("body"));
78     }
79 }
80 /* ====================================================================
81  * The MetaFramework License, Version 1.1
82  *
83  * Copyright (c) 2002 Ivar Chan. All rights
84  * reserved.
85  *
86  * Redistribution and use in source and binary forms, with or without
87  * modification, are permitted provided that the following conditions
88  * are met:
89  *
90  * 1. Redistributions of source code must retain the above copyright
91  * notice, this list of conditions and the following disclaimer.
92  *
93  * 2. Redistributions in binary form must reproduce the above copyright
94  * notice, this list of conditions and the following disclaimer in
95  * the documentation and/or other materials provided with the
96  * distribution.
97  *
98  * 3. The end-user documentation included with the redistribution,
99  * if any, must include the following acknowledgment:
100  * "This product includes software developed by
101  * Ivar Chan (http://www.openinventions.com/metaframework/)."
102  * Alternately, this acknowledgment may appear in the software itself,
103  * if and wherever such third-party acknowledgments normally appear.
104  *
105  * 4. The name "metaframework" must not be used to endorse or promote products
106  * derived from this software without prior written permission. For
107  * written permission, please contact ivarchan@acm.org.
108  *
109  * 5. Products derived from this software may not be called "metaframework",
110  * nor may "metaframework" appear in their name, without
111  * prior written permission of Ivar Chan.
112  *
113  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
114  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
115  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
116  * DISCLAIMED. IN NO EVENT SHALL THE IVAR CHAN BE LIABLE FOR ANY
117  * DIRECT, INDIRECT, INCIDENTAL,
118  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
119  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
120  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
121  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
122  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
123  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
124  * SUCH DAMAGE.
125  * ====================================================================
126  *
127  * This software consists of voluntary contributions made by many
128  * individuals. For more information on metaframework, please see
129  * <http://www.openinventions/metaframework/>.
130  */

131
132
133
Popular Tags