View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.ws.security.message;
21  
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.ws.security.WSConstants;
26  import org.apache.ws.security.WSSecurityException;
27  import org.apache.ws.security.util.WSSecurityUtil;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  
31  /**
32   * This class uses a DOM-based approach to locate Elements that are referenced via an Id.
33   */
34  public class DOMCallbackLookup implements CallbackLookup {
35      
36      protected Document doc;
37      
38      public DOMCallbackLookup(Document doc) {
39          this.doc = doc;
40      }
41  
42      /**
43       * Get the DOM element that corresponds to the given id and ValueType reference. The Id can 
44       * be a wsu:Id or else an Id attribute, or a SAML Id when the ValueType refers to a SAML
45       * Assertion.
46       * @param id The id of the element to locate
47       * @param valueType The ValueType attribute of the element to locate (can be null)
48       * @param checkMultipleElements If true then go through the entire tree and return 
49       *        null if there are multiple elements with the same Id
50       * @return the located element
51       * @throws WSSecurityException
52       */
53      public Element getElement(
54          String id, String valueType, boolean checkMultipleElements
55      ) throws WSSecurityException {
56          //
57          // Try the SOAP Body first
58          //
59          Element bodyElement = WSSecurityUtil.findBodyElement(doc);
60          if (bodyElement != null) {
61              String cId = bodyElement.getAttributeNS(WSConstants.WSU_NS, "Id");
62              if (cId.equals(id)) {
63                   return bodyElement;
64              }
65          }
66          // Otherwise do a general search
67          Element foundElement = 
68              WSSecurityUtil.findElementById(doc.getDocumentElement(), id, checkMultipleElements);
69          if (foundElement != null) {
70              return foundElement;
71          }
72          
73          //
74          // Try to find a SAML Assertion Element if the ValueType corresponds to a SAML Assertion
75          // (or is empty)
76          //
77          if (WSConstants.WSS_SAML_KI_VALUE_TYPE.equals(valueType) 
78              || WSConstants.WSS_SAML2_KI_VALUE_TYPE.equals(valueType)
79              || "".equals(valueType)
80              || valueType == null) {
81              return 
82                  WSSecurityUtil.findSAMLAssertionElementById(
83                      doc.getDocumentElement(), id
84                  );
85          }
86          
87          return null;
88      }
89      
90      /**
91       * Get the DOM element(s) that correspond to the given localname/namespace. 
92       * @param localname The localname of the Element(s)
93       * @param namespace The namespace of the Element(s)
94       * @return the located element(s)
95       * @throws WSSecurityException
96       */
97      public List<Element> getElements(
98          String localname, String namespace
99      ) throws WSSecurityException {
100         //
101         // Try the SOAP Body first
102         //
103         Element bodyElement = WSSecurityUtil.findBodyElement(doc);
104         if (WSConstants.ELEM_BODY.equals(localname) &&
105             bodyElement.getNamespaceURI().equals(namespace)) {
106             return Collections.singletonList(bodyElement);
107         }
108         return WSSecurityUtil.findElements(doc.getDocumentElement(), localname, namespace);
109     }
110 }