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.saml2.encryption;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.opensaml.saml2.core.EncryptedElementType;
23  import org.opensaml.xml.encryption.AbstractEncryptedKeyResolver;
24  import org.opensaml.xml.encryption.EncryptedData;
25  import org.opensaml.xml.encryption.EncryptedKey;
26  import org.opensaml.xml.encryption.EncryptedKeyResolver;
27  
28  /**
29   * An implementation of {@link EncryptedKeyResolver} which resolves {@link EncryptedKey}
30   * elements which appear as immediate children of the {@link EncryptedElementType} which 
31   * is the parent of the {@link EncryptedData} context.
32   */
33  public class EncryptedElementTypeEncryptedKeyResolver extends AbstractEncryptedKeyResolver {
34  
35      /** {@inheritDoc} */
36      public Iterable<EncryptedKey> resolve(EncryptedData encryptedData) {
37          List<EncryptedKey> resolvedEncKeys = new ArrayList<EncryptedKey>();
38          
39          if (! (encryptedData.getParent() instanceof EncryptedElementType) ) {
40              return resolvedEncKeys;
41          }
42          
43          EncryptedElementType encElementType = (EncryptedElementType) encryptedData.getParent();
44          
45          for (EncryptedKey encKey : encElementType.getEncryptedKeys()) {
46              if (matchRecipient(encKey.getRecipient())) {
47                  resolvedEncKeys.add(encKey);
48              }
49          }
50          
51          return resolvedEncKeys;
52      }
53  }