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.env;
20  
21  import org.apache.shiro.env.DefaultEnvironment;
22  import org.apache.shiro.mgt.SecurityManager;
23  import org.apache.shiro.web.config.ShiroFilterConfiguration;
24  import org.apache.shiro.web.filter.mgt.FilterChainResolver;
25  import org.apache.shiro.web.mgt.WebSecurityManager;
26  
27  import javax.servlet.ServletContext;
28  import java.util.Map;
29  
30  /**
31   * Default {@link WebEnvironment} implementation based on a backing {@link Map} instance.
32   *
33   * @since 1.2
34   */
35  public class DefaultWebEnvironment extends DefaultEnvironment implements MutableWebEnvironment {
36  
37      private static final String DEFAULT_FILTER_CHAIN_RESOLVER_NAME = "filterChainResolver";
38      private static final String SHIRO_FILTER_CONFIG_NAME = "shiroFilter";
39  
40      private ServletContext servletContext;
41  
42      private ShiroFilterConfiguration filterConfiguration;
43  
44      public DefaultWebEnvironment() {
45          super();
46      }
47  
48      public FilterChainResolver getFilterChainResolver() {
49          return getObject(DEFAULT_FILTER_CHAIN_RESOLVER_NAME, FilterChainResolver.class);
50      }
51  
52      public void setFilterChainResolver(FilterChainResolver filterChainResolver) {
53          setObject(DEFAULT_FILTER_CHAIN_RESOLVER_NAME, filterChainResolver);
54      }
55  
56      @Override
57      public SecurityManager getSecurityManager() throws IllegalStateException {
58          return getWebSecurityManager();
59      }
60  
61      @Override
62      public void setSecurityManager(SecurityManager securityManager) {
63          assertWebSecurityManager(securityManager);
64          super.setSecurityManager(securityManager);
65      }
66  
67      public WebSecurityManager getWebSecurityManager() {
68          SecurityManager sm = super.getSecurityManager();
69          assertWebSecurityManager(sm);
70          return (WebSecurityManager) sm;
71      }
72  
73      public void setWebSecurityManager(WebSecurityManager wsm) {
74          super.setSecurityManager(wsm);
75      }
76  
77      private void assertWebSecurityManager(SecurityManager sm) {
78          if (!(sm instanceof WebSecurityManager)) {
79              String msg = "SecurityManager instance must be a " + WebSecurityManager.class.getName() + " instance.";
80              throw new IllegalStateException(msg);
81          }
82      }
83  
84      public ServletContext getServletContext() {
85          return this.servletContext;
86      }
87  
88      public void setServletContext(ServletContext servletContext) {
89          this.servletContext = servletContext;
90      }
91  
92  
93      @Override
94      public void setShiroFilterConfiguration(ShiroFilterConfiguration filterConfiguration) {
95          setObject(SHIRO_FILTER_CONFIG_NAME, filterConfiguration);
96      }
97  
98      @Override
99      public ShiroFilterConfiguration getShiroFilterConfiguration() {
100         ShiroFilterConfiguration config = getObject(SHIRO_FILTER_CONFIG_NAME, ShiroFilterConfiguration.class);
101         // Use the default configuration if config is null
102         if (config == null) {
103             config = MutableWebEnvironment.super.getShiroFilterConfiguration();
104             setShiroFilterConfiguration(config);
105         }
106         return config;
107     }
108 }