View Javadoc

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  package org.apache.shiro.web.servlet;
20  
21  import org.apache.shiro.util.StringUtils;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.servlet.Filter;
26  import javax.servlet.FilterConfig;
27  import javax.servlet.ServletException;
28  
29  /**
30   * Base abstract Filter simplifying Filter initialization and {@link #getInitParam(String) access} to init parameters.
31   * Subclass initialization logic should be performed by overriding the {@link #onFilterConfigSet()} template method.
32   * FilterChain execution logic (the
33   * {@link #doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)} method
34   * is left to subclasses.
35   *
36   * @since 1.0
37   */
38  public abstract class AbstractFilter extends ServletContextSupport implements Filter {
39  
40      private static transient final Logger log = LoggerFactory.getLogger(AbstractFilter.class);
41  
42      /**
43       * FilterConfig provided by the Servlet container at start-up.
44       */
45      protected FilterConfig filterConfig;
46  
47      /**
48       * Returns the servlet container specified {@code FilterConfig} instance provided at
49       * {@link #init(javax.servlet.FilterConfig) startup}.
50       *
51       * @return the servlet container specified {@code FilterConfig} instance provided at start-up.
52       */
53      public FilterConfig getFilterConfig() {
54          return filterConfig;
55      }
56  
57      /**
58       * Sets the FilterConfig <em>and</em> the {@code ServletContext} as attributes of this class for use by
59       * subclasses.  That is:
60       * <pre>
61       * this.filterConfig = filterConfig;
62       * setServletContext(filterConfig.getServletContext());</pre>
63       *
64       * @param filterConfig the FilterConfig instance provided by the Servlet container at start-up.
65       */
66      public void setFilterConfig(FilterConfig filterConfig) {
67          this.filterConfig = filterConfig;
68          setServletContext(filterConfig.getServletContext());
69      }
70  
71      /**
72       * Returns the value for the named {@code init-param}, or {@code null} if there was no {@code init-param}
73       * specified by that name.
74       *
75       * @param paramName the name of the {@code init-param}
76       * @return the value for the named {@code init-param}, or {@code null} if there was no {@code init-param}
77       *         specified by that name.
78       */
79      protected String getInitParam(String paramName) {
80          FilterConfig config = getFilterConfig();
81          if (config != null) {
82              return StringUtils.clean(config.getInitParameter(paramName));
83          }
84          return null;
85      }
86  
87      /**
88       * Sets the filter's {@link #setFilterConfig filterConfig} and then immediately calls
89       * {@link #onFilterConfigSet() onFilterConfigSet()} to trigger any processing a subclass might wish to perform.
90       *
91       * @param filterConfig the servlet container supplied FilterConfig instance.
92       * @throws javax.servlet.ServletException if {@link #onFilterConfigSet() onFilterConfigSet()} throws an Exception.
93       */
94      public final void init(FilterConfig filterConfig) throws ServletException {
95          setFilterConfig(filterConfig);
96          try {
97              onFilterConfigSet();
98          } catch (Exception e) {
99              if (e instanceof ServletException) {
100                 throw (ServletException) e;
101             } else {
102                 if (log.isErrorEnabled()) {
103                     log.error("Unable to start Filter: [" + e.getMessage() + "].", e);
104                 }
105                 throw new ServletException(e);
106             }
107         }
108     }
109 
110     /**
111      * Template method to be overridden by subclasses to perform initialization logic at start-up.  The
112      * {@code ServletContext} and {@code FilterConfig} will be accessible
113      * (and non-{@code null}) at the time this method is invoked via the
114      * {@link #getServletContext() getServletContext()} and {@link #getFilterConfig() getFilterConfig()}
115      * methods respectively.
116      * <p/>
117      * {@code init-param} values may be conveniently obtained via the {@link #getInitParam(String)} method.
118      *
119      * @throws Exception if the subclass has an error upon initialization.
120      */
121     protected void onFilterConfigSet() throws Exception {
122     }
123 
124     /**
125      * Default no-op implementation that can be overridden by subclasses for custom cleanup behavior.
126      */
127     public void destroy() {
128     }
129 
130 
131 }