1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. 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, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package javax.servlet; 21 22 import java.io.IOException; 23 24 /** 25 * 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. 26 * <br><br> 27 * Filters perform filtering in the <code>doFilter</code> method. Every Filter has access to 28 ** a FilterConfig object from which it can obtain its initialization parameters, a 29 ** reference to the ServletContext which it can use, for example, to load resources 30 ** needed for filtering tasks. 31 ** <p> 32 ** Filters are configured in the deployment descriptor of a web application 33 ** <p> 34 ** Examples that have been identified for this design are<br> 35 ** 1) Authentication Filters <br> 36 ** 2) Logging and Auditing Filters <br> 37 ** 3) Image conversion Filters <br> 38 ** 4) Data compression Filters <br> 39 ** 5) Encryption Filters <br> 40 ** 6) Tokenizing Filters <br> 41 ** 7) Filters that trigger resource access events <br> 42 ** 8) XSL/T filters <br> 43 ** 9) Mime-type chain Filter <br> 44 * @since Servlet 2.3 45 */ 46 47 public interface Filter { 48 49 /** 50 * Called by the web container to indicate to a filter that it is being placed into 51 * service. The servlet container calls the init method exactly once after instantiating the 52 * filter. The init method must complete successfully before the filter is asked to do any 53 * filtering work. <br><br> 54 55 * The web container cannot place the filter into service if the init method either<br> 56 * 1.Throws a ServletException <br> 57 * 2.Does not return within a time period defined by the web container 58 */ 59 public void init(FilterConfig filterConfig) throws ServletException; 60 61 62 /** 63 * The <code>doFilter</code> method of the Filter is called by the container 64 * each time a request/response pair is passed through the chain due 65 * to a client request for a resource at the end of the chain. The FilterChain passed in to this 66 * method allows the Filter to pass on the request and response to the next entity in the 67 * chain.<p> 68 * A typical implementation of this method would follow the following pattern:- <br> 69 * 1. Examine the request<br> 70 * 2. Optionally wrap the request object with a custom implementation to 71 * filter content or headers for input filtering <br> 72 * 3. Optionally wrap the response object with a custom implementation to 73 * filter content or headers for output filtering <br> 74 * 4. a) <strong>Either</strong> invoke the next entity in the chain using the FilterChain object (<code>chain.doFilter()</code>), <br> 75 ** 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> 76 ** 5. Directly set headers on the response after invocation of the next entity in the filter chain. 77 **/ 78 public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException; 79 80 /** 81 * Called by the web container to indicate to a filter that it is being taken out of service. This 82 * method is only called once all threads within the filter's doFilter method have exited or after 83 * a timeout period has passed. After the web container calls this method, it will not call the 84 * doFilter method again on this instance of the filter. <br><br> 85 * 86 * This method gives the filter an opportunity to clean up any resources that are being held (for 87 * example, memory, file handles, threads) and make sure that any persistent state is synchronized 88 * with the filter's current state in memory. 89 */ 90 91 public void destroy(); 92 93 94 } 95