KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > cactus > sample > SampleFilter


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

20 package org.apache.maven.cactus.sample;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 import javax.servlet.Filter JavaDoc;
26 import javax.servlet.FilterChain JavaDoc;
27 import javax.servlet.FilterConfig JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.ServletRequest JavaDoc;
30 import javax.servlet.ServletResponse JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.maven.cactus.sample.util.GenericResponseWrapper;
34
35 /**
36  * Sample filter that implements some very simple business logic. The goal is
37  * to provide some functional tests for Cactus and examples for Cactus users.
38  * This filter simply adds a header and a footer to the returned HTML.
39  *
40  * @version $Id: SampleFilter.java,v 1.3 2004/02/29 16:34:44 vmassol Exp $
41  */

42 public class SampleFilter implements Filter JavaDoc
43 {
44     /**
45      * We need to save the filter config as the Fitler API does not offer
46      * a means to get the filter config ... except in the <code>init()</code>
47      */

48     private FilterConfig JavaDoc config;
49
50     /**
51      * Filter initialisation. Called by the servlet engine during the life
52      * cycle of the filter.
53      *
54      * @param theConfig the filter config
55      *
56      * @exception ServletException on failure
57      */

58     public void init(FilterConfig JavaDoc theConfig) throws ServletException JavaDoc
59     {
60         this.config = theConfig;
61     }
62
63     /**
64      * Perform the filter function. Called by the container upon a request
65      * matching the filter pattern defined in <code>web.xml</code>.
66      *
67      * @param theRequest the incmoing HTTP request
68      * @param theResponse the returned HTTP response
69      * @param theChain the chain of filters extracted from the definition
70      * given in <code>web.xml</code> by the container.
71      *
72      * @exception ServletException on failure
73      * @exception IOException on failure
74      */

75     public void doFilter(ServletRequest JavaDoc theRequest,
76         ServletResponse JavaDoc theResponse, FilterChain JavaDoc theChain) throws IOException JavaDoc,
77         ServletException JavaDoc
78     {
79         OutputStream JavaDoc out = theResponse.getOutputStream();
80
81         addHeader(out);
82
83         // Create a wrapper of the response so that we can later write to
84
// the response (add the footer). If we did not do this, we would
85
// get an error saying that the response has already been
86
// committed.
87
GenericResponseWrapper wrapper =
88             new GenericResponseWrapper((HttpServletResponse JavaDoc) theResponse);
89
90         theChain.doFilter(theRequest, wrapper);
91
92         out.write(wrapper.getData());
93         addFooter(out);
94         out.close();
95     }
96
97     /**
98      * Write the header to the output stream. The header text is extracted
99      * from a filter initialisation parameter (defined in
100      * <code>web.xml</code>). Don't write anything if no parameter is defined.
101      *
102      * @param theOutputStream the output stream
103      *
104      * @exception IOException on failure
105      */

106     protected void addHeader(OutputStream JavaDoc theOutputStream) throws IOException JavaDoc
107     {
108         String JavaDoc header = this.config.getInitParameter("header");
109
110         if (header != null)
111         {
112             theOutputStream.write(header.getBytes());
113         }
114     }
115
116     /**
117      * Write the footer to the output stream. The footer text is extracted
118      * from a filter initialisation parameter (defined in
119      * <code>web.xml</code>). Don't write anything if no parameter is defined.
120      *
121      * @param theOutputStream the output stream
122      *
123      * @exception IOException on failure
124      */

125     protected void addFooter(OutputStream JavaDoc theOutputStream) throws IOException JavaDoc
126     {
127         String JavaDoc footer = this.config.getInitParameter("footer");
128
129         if (footer != null)
130         {
131             theOutputStream.write(footer.getBytes());
132         }
133     }
134
135     /**
136      * Filter un-initialisation. Called by the servlet engine during the life
137      * cycle of the filter.
138      */

139     public void destroy()
140     {
141     }
142 }
143
Popular Tags