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.criteria.KeyNameCriteria;
21  import org.opensaml.xml.util.DatatypeHelper;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  /**
26   * Instance of evaluable credential criteria for evaluating credential key names.
27   */
28  public class EvaluableKeyNameCredentialCriteria implements EvaluableCredentialCriteria {
29      
30      /** Logger. */
31      private final Logger log = LoggerFactory.getLogger(EvaluableKeyNameCredentialCriteria.class);
32      
33      /** Base criteria. */
34      private String keyName;
35      
36      /**
37       * Constructor.
38       *
39       * @param criteria the criteria which is the basis for evaluation
40       */
41      public EvaluableKeyNameCredentialCriteria(KeyNameCriteria criteria) {
42          if (criteria == null) {
43              throw new NullPointerException("Criteria instance may not be null");
44          }
45          keyName = criteria.getKeyName();
46      }
47      
48      /**
49       * Constructor.
50       *
51       * @param newKeyName the criteria value which is the basis for evaluation
52       */
53      public EvaluableKeyNameCredentialCriteria(String newKeyName) {
54          if (DatatypeHelper.isEmpty(newKeyName)) {
55              throw new IllegalArgumentException("Key name may not be null");
56          }
57          keyName = newKeyName;
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          if (target.getKeyNames().isEmpty()) {
67              log.info("Could not evaluate criteria, credential contained no key names");
68              return null;
69          }
70          Boolean result = target.getKeyNames().contains(keyName);
71          return result;
72      }
73  
74  }