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    package org.apache.shiro.web.servlet;
020    
021    import org.apache.shiro.util.Nameable;
022    
023    import javax.servlet.FilterConfig;
024    
025    /**
026     * Allows a filter to be named via JavaBeans-compatible
027     * {@link #getName()}/{@link #setName(String)} methods.  If no name is specified, the name of the filter will
028     * default to the name given to it in {@code web.xml} (the {@code FilterConfig}'s
029     * {@link javax.servlet.FilterConfig#getFilterName() filterName}).
030     *
031     * @since 1.0
032     */
033    public abstract class NameableFilter extends AbstractFilter implements Nameable {
034    
035        /**
036         * The name of this filter, unique within an application.
037         */
038        private String name;
039    
040        /**
041         * Returns the filter's name.
042         * <p/>
043         * Unless overridden by calling the {@link #setName(String) setName(String)} method, this value defaults to the
044         * filter name as specified by the servlet container at start-up:
045         * <pre>
046         * this.name = {@link #getFilterConfig() getFilterConfig()}.{@link javax.servlet.FilterConfig#getFilterName() getName()};</pre>
047         *
048         * @return the filter name, or {@code null} if none available
049         * @see javax.servlet.GenericServlet#getServletName()
050         * @see javax.servlet.FilterConfig#getFilterName()
051         */
052        protected String getName() {
053            if (this.name == null) {
054                FilterConfig config = getFilterConfig();
055                if (config != null) {
056                    this.name = config.getFilterName();
057                }
058            }
059    
060            return this.name;
061        }
062    
063        /**
064         * Sets the filter's name.
065         * <p/>
066         * Unless overridden by calling this method, this value defaults to the filter name as specified by the
067         * servlet container at start-up:
068         * <pre>
069         * this.name = {@link #getFilterConfig() getFilterConfig()}.{@link javax.servlet.FilterConfig#getFilterName() getName()};</pre>
070         *
071         * @param name the name of the filter.
072         */
073        public void setName(String name) {
074            this.name = name;
075        }
076    
077        /**
078         * Returns a StringBuilder instance with the {@link #getName() name}, or if the name is {@code null}, just the
079         * {@code super.toStringBuilder()} instance.
080         *
081         * @return a StringBuilder instance to use for appending String data that will eventually be returned from a
082         *         {@code toString()} invocation.
083         */
084        protected StringBuilder toStringBuilder() {
085            String name = getName();
086            if (name == null) {
087                return super.toStringBuilder();
088            } else {
089                StringBuilder sb = new StringBuilder();
090                sb.append(name);
091                return sb;
092            }
093        }
094    
095    }