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.filter.mgt;
020    
021    import org.apache.shiro.util.ClassUtils;
022    import org.apache.shiro.web.filter.authc.*;
023    import org.apache.shiro.web.filter.authz.*;
024    import org.apache.shiro.web.filter.session.NoSessionCreationFilter;
025    
026    import javax.servlet.Filter;
027    import javax.servlet.FilterConfig;
028    import javax.servlet.ServletException;
029    import java.util.LinkedHashMap;
030    import java.util.Map;
031    
032    /**
033     * Enum representing all of the default Shiro Filter instances available to web applications.  Each filter instance is
034     * typically accessible in configuration the {@link #name() name} of the enum constant.
035     *
036     * @since 1.0
037     */
038    public enum DefaultFilter {
039    
040        anon(AnonymousFilter.class),
041        authc(FormAuthenticationFilter.class),
042        authcBasic(BasicHttpAuthenticationFilter.class),
043        logout(LogoutFilter.class),
044        noSessionCreation(NoSessionCreationFilter.class),
045        perms(PermissionsAuthorizationFilter.class),
046        port(PortFilter.class),
047        rest(HttpMethodPermissionFilter.class),
048        roles(RolesAuthorizationFilter.class),
049        ssl(SslFilter.class),
050        user(UserFilter.class);
051    
052        private final Class<? extends Filter> filterClass;
053    
054        private DefaultFilter(Class<? extends Filter> filterClass) {
055            this.filterClass = filterClass;
056        }
057    
058        public Filter newInstance() {
059            return (Filter) ClassUtils.newInstance(this.filterClass);
060        }
061    
062        public Class<? extends Filter> getFilterClass() {
063            return this.filterClass;
064        }
065    
066        public static Map<String, Filter> createInstanceMap(FilterConfig config) {
067            Map<String, Filter> filters = new LinkedHashMap<String, Filter>(values().length);
068            for (DefaultFilter defaultFilter : values()) {
069                Filter filter = defaultFilter.newInstance();
070                if (config != null) {
071                    try {
072                        filter.init(config);
073                    } catch (ServletException e) {
074                        String msg = "Unable to correctly init default filter instance of type " +
075                                filter.getClass().getName();
076                        throw new IllegalStateException(msg, e);
077                    }
078                }
079                filters.put(defaultFilter.name(), filter);
080            }
081            return filters;
082        }
083    }