View Javadoc

1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.securechannel;
17  
18  import org.acegisecurity.ConfigAttribute;
19  import org.acegisecurity.ConfigAttributeDefinition;
20  import org.acegisecurity.intercept.web.FilterInvocation;
21  
22  import org.springframework.beans.factory.InitializingBean;
23  
24  import java.io.IOException;
25  
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import javax.servlet.ServletException;
30  
31  
32  /***
33   * Implementation of {@link ChannelDecisionManager}.
34   * 
35   * <p>
36   * Iterates through each configured {@link ChannelProcessor}. If a
37   * <code>ChannelProcessor</code> has any issue with the security of the
38   * request, it should cause a redirect, exception or whatever other action is
39   * appropriate for the <code>ChannelProcessor</code> implementation.
40   * </p>
41   * 
42   * <P>
43   * Once any response is committed (ie a redirect is written to the response
44   * object), the <code>ChannelDecisionManagerImpl</code> will not iterate
45   * through any further <code>ChannelProcessor</code>s.
46   * </p>
47   *
48   * @author Ben Alex
49   * @version $Id: ChannelDecisionManagerImpl.java,v 1.4 2005/11/17 00:55:50 benalex Exp $
50   */
51  public class ChannelDecisionManagerImpl implements ChannelDecisionManager,
52      InitializingBean {
53      //~ Instance fields ========================================================
54  
55      private List channelProcessors;
56  
57      //~ Methods ================================================================
58  
59      public void setChannelProcessors(List newList) {
60          checkIfValidList(newList);
61  
62          Iterator iter = newList.iterator();
63  
64          while (iter.hasNext()) {
65              Object currentObject = null;
66  
67              try {
68                  currentObject = iter.next();
69  
70                  ChannelProcessor attemptToCast = (ChannelProcessor) currentObject;
71              } catch (ClassCastException cce) {
72                  throw new IllegalArgumentException("ChannelProcessor "
73                      + currentObject.getClass().getName()
74                      + " must implement ChannelProcessor");
75              }
76          }
77  
78          this.channelProcessors = newList;
79      }
80  
81      public List getChannelProcessors() {
82          return this.channelProcessors;
83      }
84  
85      public void afterPropertiesSet() throws Exception {
86          checkIfValidList(this.channelProcessors);
87      }
88  
89      public void decide(FilterInvocation invocation,
90          ConfigAttributeDefinition config) throws IOException, ServletException {
91          Iterator iter = this.channelProcessors.iterator();
92  
93          while (iter.hasNext()) {
94              ChannelProcessor processor = (ChannelProcessor) iter.next();
95  
96              processor.decide(invocation, config);
97  
98              if (invocation.getResponse().isCommitted()) {
99                  break;
100             }
101         }
102     }
103 
104     public boolean supports(ConfigAttribute attribute) {
105         Iterator iter = this.channelProcessors.iterator();
106 
107         while (iter.hasNext()) {
108             ChannelProcessor processor = (ChannelProcessor) iter.next();
109 
110             if (processor.supports(attribute)) {
111                 return true;
112             }
113         }
114 
115         return false;
116     }
117 
118     private void checkIfValidList(List listToCheck) {
119         if ((listToCheck == null) || (listToCheck.size() == 0)) {
120             throw new IllegalArgumentException(
121                 "A list of ChannelProcessors is required");
122         }
123     }
124 }