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.Nameable;
22  
23  import javax.servlet.FilterConfig;
24  
25  /**
26   * Allows a filter to be named via JavaBeans-compatible
27   * {@link #getName()}/{@link #setName(String)} methods.  If no name is specified, the name of the filter will
28   * default to the name given to it in {@code web.xml} (the {@code FilterConfig}'s
29   * {@link javax.servlet.FilterConfig#getFilterName() filterName}).
30   *
31   * @since 1.0
32   */
33  public abstract class NameableFilter extends AbstractFilter implements Nameable {
34  
35      /**
36       * The name of this filter, unique within an application.
37       */
38      private String name;
39  
40      /**
41       * Returns the filter's name.
42       * <p/>
43       * Unless overridden by calling the {@link #setName(String) setName(String)} method, this value defaults to the
44       * filter name as specified by the servlet container at start-up:
45       * <pre>
46       * this.name = {@link #getFilterConfig() getFilterConfig()}.{@link javax.servlet.FilterConfig#getFilterName() getName()};</pre>
47       *
48       * @return the filter name, or {@code null} if none available
49       * @see javax.servlet.GenericServlet#getServletName()
50       * @see javax.servlet.FilterConfig#getFilterName()
51       */
52      protected String getName() {
53          if (this.name == null) {
54              FilterConfig config = getFilterConfig();
55              if (config != null) {
56                  this.name = config.getFilterName();
57              }
58          }
59  
60          return this.name;
61      }
62  
63      /**
64       * Sets the filter's name.
65       * <p/>
66       * Unless overridden by calling this method, this value defaults to the filter name as specified by the
67       * servlet container at start-up:
68       * <pre>
69       * this.name = {@link #getFilterConfig() getFilterConfig()}.{@link javax.servlet.FilterConfig#getFilterName() getName()};</pre>
70       *
71       * @param name the name of the filter.
72       */
73      public void setName(String name) {
74          this.name = name;
75      }
76  
77      /**
78       * Returns a StringBuilder instance with the {@link #getName() name}, or if the name is {@code null}, just the
79       * {@code super.toStringBuilder()} instance.
80       *
81       * @return a StringBuilder instance to use for appending String data that will eventually be returned from a
82       *         {@code toString()} invocation.
83       */
84      protected StringBuilder toStringBuilder() {
85          String name = getName();
86          if (name == null) {
87              return super.toStringBuilder();
88          } else {
89              StringBuilder sb = new StringBuilder();
90              sb.append(name);
91              return sb;
92          }
93      }
94  
95  }