001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package javax.servlet;
021
022 import java.io.IOException;
023
024 /**
025 * 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.
026 * <br><br>
027 * Filters perform filtering in the <code>doFilter</code> method. Every Filter has access to
028 * a FilterConfig object from which it can obtain its initialization parameters, a
029 * reference to the ServletContext which it can use, for example, to load resources
030 * needed for filtering tasks.
031 * <p/>
032 * Filters are configured in the deployment descriptor of a web application
033 * <p/>
034 * Examples that have been identified for this design are<br>
035 * 1) Authentication Filters <br>
036 * 2) Logging and Auditing Filters <br>
037 * 3) Image conversion Filters <br>
038 * 4) Data compression Filters <br>
039 * 5) Encryption Filters <br>
040 * 6) Tokenizing Filters <br>
041 * 7) Filters that trigger resource access events <br>
042 * 8) XSL/T filters <br>
043 * 9) Mime-type chain Filter <br>
044 *
045 * @since Servlet 2.3
046 */
047
048 public interface Filter {
049
050 /**
051 * Called by the web container to indicate to a filter that it is being placed into
052 * service. The servlet container calls the init method exactly once after instantiating the
053 * filter. The init method must complete successfully before the filter is asked to do any
054 * filtering work. <br><br>
055 * <p/>
056 * The web container cannot place the filter into service if the init method either<br>
057 * 1.Throws a ServletException <br>
058 * 2.Does not return within a time period defined by the web container
059 * @param filterConfig Configuration for this filter
060 * @throws ServletException if the filter cannot initialize itself
061 */
062 public void init(FilterConfig filterConfig) throws ServletException;
063
064
065 /**
066 * The <code>doFilter</code> method of the Filter is called by the container
067 * each time a request/response pair is passed through the chain due
068 * to a client request for a resource at the end of the chain. The FilterChain passed in to this
069 * method allows the Filter to pass on the request and response to the next entity in the
070 * chain.<p>
071 * A typical implementation of this method would follow the following pattern:- <br>
072 * 1. Examine the request<br>
073 * 2. Optionally wrap the request object with a custom implementation to
074 * filter content or headers for input filtering <br>
075 * 3. Optionally wrap the response object with a custom implementation to
076 * filter content or headers for output filtering <br>
077 * 4. a) <strong>Either</strong> invoke the next entity in the chain using the FilterChain object (<code>chain.doFilter()</code>), <br>
078 * * 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>
079 * * 5. Directly set headers on the response after invocation of the next entity in the filter chain.
080 * @param request servlet request to filter
081 * @param response servlet response to filter
082 * @param chain what to do next
083 * @throws java.io.IOException if there's an IO problem
084 * @throws ServletException if there is a non-IO problem
085 */
086 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;
087
088 /**
089 * Called by the web container to indicate to a filter that it is being taken out of service. This
090 * method is only called once all threads within the filter's doFilter method have exited or after
091 * a timeout period has passed. After the web container calls this method, it will not call the
092 * doFilter method again on this instance of the filter. <br><br>
093 * <p/>
094 * This method gives the filter an opportunity to clean up any resources that are being held (for
095 * example, memory, file handles, threads) and make sure that any persistent state is synchronized
096 * with the filter's current state in memory.
097 */
098
099 public void destroy();
100
101
102 }
103