View Javadoc

1   /*
2    * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.ws.security.provider;
18  
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.opensaml.ws.message.MessageContext;
23  import org.opensaml.ws.security.SecurityPolicy;
24  import org.opensaml.ws.security.SecurityPolicyResolver;
25  import org.opensaml.xml.security.SecurityException;
26  import org.opensaml.xml.util.LazyList;
27  
28  /** A simple security policy resolver implementation that returns a static list of policies. */
29  public class StaticSecurityPolicyResolver implements SecurityPolicyResolver {
30  
31      /** Registered security policies. */
32      private List<SecurityPolicy> securityPolicies;
33  
34      /**
35       * Constructor.
36       * 
37       * @param policy the static policy returned by this resolver
38       */
39      public StaticSecurityPolicyResolver(SecurityPolicy policy) {
40          securityPolicies = new LazyList<SecurityPolicy>();
41          if(policy != null){
42              securityPolicies.add(policy);
43          }
44      }
45      
46      /**
47       * Constructor.
48       * 
49       * @param policies the static list of policies returned by this resolver
50       */
51      public StaticSecurityPolicyResolver(List<SecurityPolicy> policies) {
52          securityPolicies = new LazyList<SecurityPolicy>();
53          if(policies != null){
54              securityPolicies.addAll(policies);
55          }
56      }
57  
58      /** {@inheritDoc} */
59      public Iterable<SecurityPolicy> resolve(MessageContext criteria) throws SecurityException {
60          return Collections.unmodifiableList(securityPolicies);
61      }
62  
63      /**
64       * {@inheritDoc}
65       * 
66       * If more than one policy is registered with this resolver this method returns the first policy in the list.
67       */
68      public SecurityPolicy resolveSingle(MessageContext criteria) throws SecurityException {
69          if (!securityPolicies.isEmpty()) {
70              return securityPolicies.get(0);
71          } else {
72              return null;
73          }
74      }
75  }