KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > www > WFilter


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.www;
15
16 import java.io.*;
17 import java.util.*;
18 import javax.servlet.*;
19 import javax.servlet.http.*;
20
21 import org.apache.log4j.Logger;
22 import org.apache.ecs.xhtml.*;
23 import org.compiere.util.*;
24
25
26 /**
27  * Filter to do timing and list parameters
28  *
29  * @version $Id: WFilter.java,v 1.3 2002/10/18 03:59:25 jjanke Exp $
30  */

31
32 public final class WFilter implements javax.servlet.Filter JavaDoc
33 {
34     /** The filter configuration object we are associated with.
35      * If this value is null, this filter instance is not currently configured */

36     private FilterConfig m_filterConfig = null;
37
38     /** Timing indicator */
39     private boolean m_timing = false;
40     /** Logging */
41     private Logger log = Logger.getLogger(getClass());
42
43     /**
44      * Place this filter into service.
45      *
46      * @param filterConfig The filter configuration object
47      * @throws ServletException
48      */

49     public void init (FilterConfig filterConfig) throws ServletException
50     {
51         m_filterConfig = filterConfig;
52         // List all Parameters
53
log.info("WFilter.init - " + filterConfig.getFilterName());
54         Enumeration en = filterConfig.getInitParameterNames();
55         while (en.hasMoreElements())
56         {
57             String JavaDoc name = en.nextElement().toString();
58             String JavaDoc value = filterConfig.getInitParameter(name);
59             log.info(" - " + name + "=" + value);
60             if (name.equals("Timing") && value.equals("Y"))
61                 m_timing = true;
62         }
63     } // init
64

65     /**
66      * Take this filter out of service.
67      */

68     public void destroy()
69     {
70         m_filterConfig = null;
71     } // destroy
72

73     /**
74      * Time the processing that is performed by all subsequent filters in the
75      * current filter stack, including the ultimately invoked servlet.
76      *
77      * @param request The servlet request we are processing
78      * @param response response
79      * @param chain The filter chain we are processing
80      *
81      * @exception IOException if an input/output error occurs
82      * @exception ServletException if a servlet error occurs
83      */

84     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
85         throws IOException, ServletException
86     {
87         // Get URI
88
String JavaDoc uri = "";
89         if (request instanceof HttpServletRequest)
90         {
91             HttpServletRequest req = (HttpServletRequest)request;
92             uri = req.getRequestURI();
93         }
94
95         // Ignore static content
96
boolean check = true;
97         if (!uri.startsWith(WEnv.DIR_BASE) // not requesting /compiere/...
98
|| uri.endsWith(".gif") || uri.endsWith(".html") || uri.endsWith(".css")
99             || uri.endsWith(".js"))
100             check = false;
101         //
102
boolean pass = true;
103
104         // We need to check
105
StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("| Parameters");
106         if (check)
107         {
108             // print parameter
109
Enumeration en = request.getParameterNames();
110             while (en.hasMoreElements())
111             {
112                 String JavaDoc name = (String JavaDoc)en.nextElement();
113                 sb.append(" - ").append(name).append("=").append(request.getParameter(name));
114             }
115             if (uri.endsWith("WWindowStatus"))
116                 pass = false;
117         }
118         if (pass && check)
119             log.info("Start " + uri + sb.toString());
120
121         // Timing
122
long myTime = 0l;
123         if (pass && check && m_timing)
124             myTime = System.currentTimeMillis();
125
126         // ** Start **
127
if (pass)
128             chain.doFilter(request, response);
129         else
130         {
131             log.warn("Rejected " + uri);
132             String JavaDoc msg = "Error: Access Rejected";
133             WDoc doc = WDoc.create (msg);
134             // Body
135
body b = doc.getBody();
136             b.addElement(new p(uri, "center"));
137             // fini
138
response.setContentType("text/html");
139             PrintWriter out = new PrintWriter (response.getOutputStream());
140             doc.output(out);
141             out.close();
142         }
143
144         // Post
145
if (check && pass)
146         {
147             if (m_timing)
148                 myTime = System.currentTimeMillis() - myTime;
149             log.info("End " + uri + "| " + (m_timing ? String.valueOf(myTime) : null));
150         }
151     } // doFilter
152

153
154     /**
155      * Return a String representation of this object.
156      * @return String info
157      */

158     public String JavaDoc toString()
159     {
160         if (m_filterConfig == null)
161             return ("WFilter()");
162         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("WFilter(");
163         sb.append(m_filterConfig);
164         sb.append(")");
165         return (sb.toString());
166     } // toString
167

168 } // Filter
169
Popular Tags