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