1
2
3 /*
4 * The contents of this file are subject to the terms
5 * of the Common Development and Distribution License
6 * (the "License"). You may not use this file except
7 * in compliance with the License.
8 *
9 * You can obtain a copy of the license at
10 * glassfish/bootstrap/legal/CDDLv1.0.txt or
11 * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12 * See the License for the specific language governing
13 * permissions and limitations under the License.
14 *
15 * When distributing Covered Code, include this CDDL
16 * HEADER in each file and include the License file at
17 * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18 * add the following below this CDDL HEADER, with the
19 * fields enclosed by brackets "[]" replaced with your
20 * own identifying information: Portions Copyright [yyyy]
21 * [name of copyright owner]
22 *
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 *
25 * Portions Copyright Apache Software Foundation.
26 */
27
28 package javax.servlet;
29
30 import java.io.IOException;
31
32 /**
33 * A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.
34 * <br><br>
35 * Filters perform filtering in the <code>doFilter</code> method. Every Filter has access to
36 ** a FilterConfig object from which it can obtain its initialization parameters, a
37 ** reference to the ServletContext which it can use, for example, to load resources
38 ** needed for filtering tasks.
39 ** <p>
40 ** Filters are configured in the deployment descriptor of a web application
41 ** <p>
42 ** Examples that have been identified for this design are<br>
43 ** 1) Authentication Filters <br>
44 ** 2) Logging and Auditing Filters <br>
45 ** 3) Image conversion Filters <br>
46 ** 4) Data compression Filters <br>
47 ** 5) Encryption Filters <br>
48 ** 6) Tokenizing Filters <br>
49 ** 7) Filters that trigger resource access events <br>
50 ** 8) XSL/T filters <br>
51 ** 9) Mime-type chain Filter <br>
52 * @since Servlet 2.3
53 */
54
55 public interface Filter {
56
57 /**
58 * Called by the web container to indicate to a filter that it is being placed into
59 * service. The servlet container calls the init method exactly once after instantiating the
60 * filter. The init method must complete successfully before the filter is asked to do any
61 * filtering work. <br><br>
62
63 * The web container cannot place the filter into service if the init method either<br>
64 * 1.Throws a ServletException <br>
65 * 2.Does not return within a time period defined by the web container
66 */
67 public void init(FilterConfig filterConfig) throws ServletException;
68
69
70 /**
71 * The <code>doFilter</code> method of the Filter is called by the container
72 * each time a request/response pair is passed through the chain due
73 * to a client request for a resource at the end of the chain. The FilterChain passed in to this
74 * method allows the Filter to pass on the request and response to the next entity in the
75 * chain.<p>
76 * A typical implementation of this method would follow the following pattern:- <br>
77 * 1. Examine the request<br>
78 * 2. Optionally wrap the request object with a custom implementation to
79 * filter content or headers for input filtering <br>
80 * 3. Optionally wrap the response object with a custom implementation to
81 * filter content or headers for output filtering <br>
82 * 4. a) <strong>Either</strong> invoke the next entity in the chain using the FilterChain object (<code>chain.doFilter()</code>), <br>
83 ** 4. b) <strong>or</strong> not pass on the request/response pair to the next entity in the filter chain to block the request processing<br>
84 ** 5. Directly set headers on the response after invocation of the next entity in the filter chain.
85 **/
86 public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException;
87
88 /**
89 * Called by the web container to indicate to a filter that it is being taken out of service. This
90 * method is only called once all threads within the filter's doFilter method have exited or after
91 * a timeout period has passed. After the web container calls this method, it will not call the
92 * doFilter method again on this instance of the filter. <br><br>
93 *
94 * This method gives the filter an opportunity to clean up any resources that are being held (for
95 * example, memory, file handles, threads) and make sure that any persistent state is synchronized
96 * with the filter's current state in memory.
97 */
98
99 public void destroy();
100
101
102 }
103