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.guice.web;
20  
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.servlet.Filter;
27  
28  import com.google.inject.Inject;
29  import com.google.inject.Injector;
30  import com.google.inject.Key;
31  import com.google.inject.Singleton;
32  import com.google.inject.spi.Dependency;
33  import com.google.inject.spi.ProviderWithDependencies;
34  
35  import org.apache.shiro.util.AntPathMatcher;
36  import org.apache.shiro.util.PatternMatcher;
37  import org.apache.shiro.web.filter.mgt.FilterChainResolver;
38  
39  @Singleton
40  class FilterChainResolverProvider implements ProviderWithDependencies<FilterChainResolver> {
41      @Inject
42      Injector injector;
43  
44      private final Map<String, Key<? extends Filter>[]> chains;
45  
46      private final Set<Dependency<?>> dependencies;
47  
48      private PatternMatcher patternMatcher = new AntPathMatcher();
49  
50      public FilterChainResolverProvider(Map<String, Key<? extends Filter>[]> chains) {
51          this.chains = chains;
52          Set<Dependency<?>> dependenciesBuilder = new HashSet<Dependency<?>>();
53          for (String chain : chains.keySet()) {
54              for (Key<? extends Filter> filterKey : chains.get(chain)) {
55                  dependenciesBuilder.add(Dependency.get(filterKey));
56              }
57          }
58          this.dependencies = Collections.unmodifiableSet(dependenciesBuilder);
59      }
60  
61      @Inject(optional = true)
62      public void setPatternMatcher(PatternMatcher patternMatcher) {
63          this.patternMatcher = patternMatcher;
64      }
65  
66      public Set<Dependency<?>> getDependencies() {
67          return dependencies;
68      }
69  
70      public FilterChainResolver get() {
71          return new SimpleFilterChainResolver(chains, injector, patternMatcher);
72      }
73  
74  }