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