1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.dom.callback;
21
22 import java.util.Collections;
23 import java.util.List;
24
25 import javax.xml.crypto.dom.DOMCryptoContext;
26
27 import org.apache.wss4j.dom.WSConstants;
28 import org.apache.wss4j.common.ext.WSSecurityException;
29 import org.apache.wss4j.common.util.XMLUtils;
30 import org.apache.wss4j.dom.util.WSSecurityUtil;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33
34
35
36
37 public class DOMCallbackLookup implements CallbackLookup {
38
39 private Document doc;
40
41 public DOMCallbackLookup(Document doc) {
42 this.doc = doc;
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public Element getElement(
58 String id, String valueType, boolean checkMultipleElements
59 ) throws WSSecurityException {
60 return getAndRegisterElement(id, valueType, checkMultipleElements, null);
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public Element getAndRegisterElement(
78 String id, String valueType, boolean checkMultipleElements, DOMCryptoContext context
79 ) throws WSSecurityException {
80 String idToMatch = XMLUtils.getIDFromReference(id);
81
82
83
84
85 Element bodyElement = getSOAPBody();
86 if (bodyElement != null) {
87 String cId = bodyElement.getAttributeNS(WSConstants.WSU_NS, "Id");
88 if (cId.equals(idToMatch)) {
89 if (context != null) {
90 context.setIdAttributeNS(bodyElement, WSConstants.WSU_NS, "Id");
91 }
92 return bodyElement;
93 }
94 }
95
96 Element foundElement =
97 XMLUtils.findElementById(doc.getDocumentElement(), idToMatch, checkMultipleElements);
98 if (foundElement != null) {
99 if (context != null) {
100 if (foundElement.hasAttributeNS(WSConstants.WSU_NS, "Id")
101 && idToMatch.equals(foundElement.getAttributeNS(WSConstants.WSU_NS, "Id"))) {
102 context.setIdAttributeNS(foundElement, WSConstants.WSU_NS, "Id");
103 }
104 if (foundElement.hasAttributeNS(null, "Id")
105 && idToMatch.equals(foundElement.getAttributeNS(null, "Id"))) {
106 context.setIdAttributeNS(foundElement, null, "Id");
107 }
108 }
109 return foundElement;
110 }
111
112
113
114
115
116 if (WSConstants.WSS_SAML_KI_VALUE_TYPE.equals(valueType)
117 || WSConstants.WSS_SAML2_KI_VALUE_TYPE.equals(valueType)
118 || valueType == null || valueType.length() == 0) {
119 foundElement =
120 XMLUtils.findSAMLAssertionElementById(
121 doc.getDocumentElement(), idToMatch
122 );
123 if (foundElement != null) {
124 if (context != null) {
125 if (foundElement.hasAttributeNS(null, "ID")
126 && idToMatch.equals(foundElement.getAttributeNS(null, "ID"))) {
127 context.setIdAttributeNS(foundElement, null, "ID");
128 }
129 if (foundElement.hasAttributeNS(null, "AssertionID")
130 && idToMatch.equals(foundElement.getAttributeNS(null, "AssertionID"))) {
131 context.setIdAttributeNS(foundElement, null, "AssertionID");
132 }
133 }
134 return foundElement;
135 }
136 }
137
138 return null;
139 }
140
141
142
143
144
145
146
147
148 public List<Element> getElements(
149 String localname, String namespace
150 ) throws WSSecurityException {
151
152
153
154 Element bodyElement = getSOAPBody();
155 if (WSConstants.ELEM_BODY.equals(localname) && bodyElement.getNamespaceURI().equals(namespace)) {
156 return Collections.singletonList(bodyElement);
157 }
158 return XMLUtils.findElements(doc.getDocumentElement(), localname, namespace);
159 }
160
161
162
163
164
165 public Element getSOAPBody() {
166 return WSSecurityUtil.findBodyElement(doc);
167 }
168 }