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.env;
020    
021    import org.apache.shiro.env.DefaultEnvironment;
022    import org.apache.shiro.mgt.SecurityManager;
023    import org.apache.shiro.web.filter.mgt.FilterChainResolver;
024    import org.apache.shiro.web.mgt.WebSecurityManager;
025    
026    import javax.servlet.ServletContext;
027    import java.util.Map;
028    
029    /**
030     * Default {@link WebEnvironment} implementation based on a backing {@link Map} instance.
031     *
032     * @since 1.2
033     */
034    public class DefaultWebEnvironment extends DefaultEnvironment implements MutableWebEnvironment {
035    
036        private static final String DEFAULT_FILTER_CHAIN_RESOLVER_NAME = "filterChainResolver";
037    
038        private ServletContext servletContext;
039    
040        public DefaultWebEnvironment() {
041            super();
042        }
043    
044        public FilterChainResolver getFilterChainResolver() {
045            return getObject(DEFAULT_FILTER_CHAIN_RESOLVER_NAME, FilterChainResolver.class);
046        }
047    
048        public void setFilterChainResolver(FilterChainResolver filterChainResolver) {
049            setObject(DEFAULT_FILTER_CHAIN_RESOLVER_NAME, filterChainResolver);
050        }
051    
052        @Override
053        public SecurityManager getSecurityManager() throws IllegalStateException {
054            return getWebSecurityManager();
055        }
056    
057        @Override
058        public void setSecurityManager(SecurityManager securityManager) {
059            assertWebSecurityManager(securityManager);
060            super.setSecurityManager(securityManager);
061        }
062    
063        public WebSecurityManager getWebSecurityManager() {
064            SecurityManager sm = super.getSecurityManager();
065            assertWebSecurityManager(sm);
066            return (WebSecurityManager)sm;
067        }
068    
069        public void setWebSecurityManager(WebSecurityManager wsm) {
070            super.setSecurityManager(wsm);
071        }
072    
073        private void assertWebSecurityManager(SecurityManager sm) {
074            if (!(sm instanceof WebSecurityManager)) {
075                String msg = "SecurityManager instance must be a " + WebSecurityManager.class.getName() + " instance.";
076                throw new IllegalStateException(msg);
077            }
078        }
079    
080        public ServletContext getServletContext() {
081            return this.servletContext;
082        }
083    
084        public void setServletContext(ServletContext servletContext) {
085            this.servletContext = servletContext;
086        }
087    }