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.filter.mgt;
20  
21  import org.apache.shiro.util.StringUtils;
22  import org.apache.shiro.web.servlet.ProxiedFilterChain;
23  
24  import javax.servlet.Filter;
25  import javax.servlet.FilterChain;
26  import java.util.*;
27  
28  /**
29   * Simple {@code NamedFilterList} implementation that is supported by a backing {@link List} instance and a simple
30   * {@link #getName() name} property. All {@link List} method implementations are immediately delegated to the
31   * wrapped backing list.
32   *
33   * @since 1.0
34   */
35  public class SimpleNamedFilterList implements NamedFilterList {
36  
37      private String name;
38      private List<Filter> backingList;
39  
40      /**
41       * Creates a new {@code SimpleNamedFilterList} instance with the specified {@code name}, defaulting to a new
42       * {@link ArrayList ArrayList} instance as the backing list.
43       *
44       * @param name the name to assign to this instance.
45       * @throws IllegalArgumentException if {@code name} is null or empty.
46       */
47      public SimpleNamedFilterList(String name) {
48          this(name, new ArrayList<Filter>());
49      }
50  
51      /**
52       * Creates a new {@code SimpleNamedFilterList} instance with the specified {@code name} and {@code backingList}.
53       *
54       * @param name        the name to assign to this instance.
55       * @param backingList the list instance used to back all of this class's {@link List} method implementations.
56       * @throws IllegalArgumentException if {@code name} is null or empty.
57       * @throws NullPointerException     if the backing list is {@code null}.
58       */
59      public SimpleNamedFilterList(String name, List<Filter> backingList) {
60          if (backingList == null) {
61              throw new NullPointerException("backingList constructor argument cannot be null.");
62          }
63          this.backingList = backingList;
64          setName(name);
65      }
66  
67      protected void setName(String name) {
68          if (!StringUtils.hasText(name)) {
69              throw new IllegalArgumentException("Cannot specify a null or empty name.");
70          }
71          this.name = name;
72      }
73  
74      public String getName() {
75          return name;
76      }
77  
78      public FilterChain proxy(FilterChain orig) {
79          return new ProxiedFilterChain(orig, this);
80      }
81  
82      public boolean add(Filter filter) {
83          return this.backingList.add(filter);
84      }
85  
86      public void add(int index, Filter filter) {
87          this.backingList.add(index, filter);
88      }
89  
90      public boolean addAll(Collection<? extends Filter> c) {
91          return this.backingList.addAll(c);
92      }
93  
94      public boolean addAll(int index, Collection<? extends Filter> c) {
95          return this.backingList.addAll(index, c);
96      }
97  
98      public void clear() {
99          this.backingList.clear();
100     }
101 
102     public boolean contains(Object o) {
103         return this.backingList.contains(o);
104     }
105 
106     public boolean containsAll(Collection<?> c) {
107         return this.backingList.containsAll(c);
108     }
109 
110     public Filter get(int index) {
111         return this.backingList.get(index);
112     }
113 
114     public int indexOf(Object o) {
115         return this.backingList.indexOf(o);
116     }
117 
118     public boolean isEmpty() {
119         return this.backingList.isEmpty();
120     }
121 
122     public Iterator<Filter> iterator() {
123         return this.backingList.iterator();
124     }
125 
126     public int lastIndexOf(Object o) {
127         return this.backingList.lastIndexOf(o);
128     }
129 
130     public ListIterator<Filter> listIterator() {
131         return this.backingList.listIterator();
132     }
133 
134     public ListIterator<Filter> listIterator(int index) {
135         return this.backingList.listIterator(index);
136     }
137 
138     public Filter remove(int index) {
139         return this.backingList.remove(index);
140     }
141 
142     public boolean remove(Object o) {
143         return this.backingList.remove(o);
144     }
145 
146     public boolean removeAll(Collection<?> c) {
147         return this.backingList.removeAll(c);
148     }
149 
150     public boolean retainAll(Collection<?> c) {
151         return this.backingList.retainAll(c);
152     }
153 
154     public Filter set(int index, Filter filter) {
155         return this.backingList.set(index, filter);
156     }
157 
158     public int size() {
159         return this.backingList.size();
160     }
161 
162     public List<Filter> subList(int fromIndex, int toIndex) {
163         return this.backingList.subList(fromIndex, toIndex);
164     }
165 
166     public Object[] toArray() {
167         return this.backingList.toArray();
168     }
169 
170     public <T> T[] toArray(T[] a) {
171         //noinspection SuspiciousToArrayCall
172         return this.backingList.toArray(a);
173     }
174 }