View Javadoc

1   /*
2    * Copyright [2007] [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.xml.security.credential.criteria;
18  
19  import org.opensaml.xml.security.credential.Credential;
20  import org.opensaml.xml.security.credential.UsageType;
21  import org.opensaml.xml.security.criteria.UsageCriteria;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  /**
26   * Instance of evaluable credential criteria for evaluating whether a credential contains a particular usage specifier.
27   */
28  public class EvaluableUsageCredentialCriteria implements EvaluableCredentialCriteria {
29  
30      /** Logger. */
31      private final Logger log = LoggerFactory.getLogger(EvaluableUsageCredentialCriteria.class);
32  
33      /** Base criteria. */
34      private UsageType usage;
35  
36      /**
37       * Constructor.
38       * 
39       * @param criteria the criteria which is the basis for evaluation
40       */
41      public EvaluableUsageCredentialCriteria(UsageCriteria criteria) {
42          if (criteria == null) {
43              throw new NullPointerException("Criteria instance may not be null");
44          }
45          usage = criteria.getUsage();
46      }
47  
48      /**
49       * Constructor.
50       * 
51       * @param newUsage the criteria value which is the basis for evaluation
52       */
53      public EvaluableUsageCredentialCriteria(UsageType newUsage) {
54          if (newUsage == null) {
55              throw new IllegalArgumentException("Usage may not be null");
56          }
57          usage = newUsage;
58      }
59  
60      /** {@inheritDoc} */
61      public Boolean evaluate(Credential target) {
62          if (target == null) {
63              log.error("Credential target was null");
64              return null;
65          }
66          UsageType credUsage = target.getUsageType();
67          if (credUsage == null) {
68              log.info("Could not evaluate criteria, credential contained no usage specifier");
69              return null;
70          }
71  
72          Boolean result = matchUsage(credUsage, usage);
73          return result;
74      }
75  
76      /**
77       * Match usage enum type values from credential and criteria.
78       * 
79       * @param credentialUsage the usage value from the credential
80       * @param criteriaUsage the usage value from the criteria
81       * @return true if the two usage specifiers match for purposes of resolving credentials, false otherwise
82       */
83      protected boolean matchUsage(UsageType credentialUsage, UsageType criteriaUsage) {
84          if (credentialUsage == UsageType.UNSPECIFIED || criteriaUsage == UsageType.UNSPECIFIED) {
85              return true;
86          }
87          return credentialUsage == criteriaUsage;
88      }
89  
90  }