1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.security;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import javax.xml.crypto.dom.DOMCryptoContext;
39
40 import org.apache.ws.security.components.crypto.Crypto;
41 import org.apache.ws.security.message.CallbackLookup;
42 import org.apache.ws.security.util.WSSecurityUtil;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Element;
45
46 public class WSDocInfo {
47 private Document doc = null;
48 private Crypto crypto = null;
49 private List<Element> tokenList = null;
50 private List<WSSecurityEngineResult> resultsList = null;
51 private CallbackLookup callbackLookup = null;
52 private Element securityHeader = null;
53
54 public WSDocInfo(Document doc) {
55
56
57
58
59
60 if (doc != null && doc.getDocumentElement() != null) {
61 this.doc = doc.getDocumentElement().getOwnerDocument();
62 } else {
63 this.doc = doc;
64 }
65 }
66
67
68
69
70 public void clear() {
71 crypto = null;
72 if (tokenList != null && tokenList.size() > 0) {
73 tokenList.clear();
74 }
75 if (resultsList != null && resultsList.size() > 0) {
76 resultsList.clear();
77 }
78
79 tokenList = null;
80 resultsList = null;
81 }
82
83
84
85
86
87
88 public void addTokenElement(Element element) throws WSSecurityException {
89 addTokenElement(element, true);
90 }
91
92
93
94
95
96
97
98 public void addTokenElement(Element element, boolean checkMultipleElements) throws WSSecurityException {
99 if (tokenList == null) {
100 tokenList = new ArrayList<Element>();
101 }
102
103 if (checkMultipleElements) {
104 for (Element elem : tokenList) {
105 if (compareElementsById(element, elem)) {
106 throw new WSSecurityException(
107 WSSecurityException.INVALID_SECURITY_TOKEN, "duplicateError"
108 );
109 }
110 }
111 }
112 tokenList.add(element);
113 }
114
115 private boolean compareElementsById(Element firstElement, Element secondElement) {
116 if (firstElement.hasAttributeNS(WSConstants.WSU_NS, "Id")
117 && secondElement.hasAttributeNS(WSConstants.WSU_NS, "Id")) {
118 String id = firstElement.getAttributeNS(WSConstants.WSU_NS, "Id");
119 String id2 = secondElement.getAttributeNS(WSConstants.WSU_NS, "Id");
120 if (id.equals(id2)) {
121 return true;
122 }
123 }
124 if (firstElement.hasAttribute("AssertionID")
125 && secondElement.hasAttribute("AssertionID")) {
126 String id = firstElement.getAttribute("AssertionID");
127 String id2 = secondElement.getAttribute("AssertionID");
128 if (id.equals(id2)) {
129 return true;
130 }
131 }
132 if (firstElement.hasAttribute("ID") && secondElement.hasAttribute("ID")) {
133 String id = firstElement.getAttribute("ID");
134 String id2 = secondElement.getAttribute("ID");
135 if (id.equals(id2)) {
136 return true;
137 }
138 }
139 return false;
140 }
141
142
143
144
145
146
147
148 public Element getTokenElement(String uri) {
149 String id = uri;
150 if (id == null) {
151 return null;
152 } else if (id.charAt(0) == '#') {
153 id = id.substring(1);
154 }
155 if (tokenList != null) {
156 for (Element elem : tokenList) {
157 String cId = elem.getAttributeNS(WSConstants.WSU_NS, "Id");
158 String samlId = elem.getAttribute("AssertionID");
159 String samlId2 = elem.getAttribute("ID");
160 if ((elem.hasAttributeNS(WSConstants.WSU_NS, "Id") && id.equals(cId))
161 || (elem.hasAttribute("AssertionID") && id.equals(samlId))
162 || (elem.hasAttribute("ID") && id.equals(samlId2))) {
163 return elem;
164 }
165 }
166 }
167 return null;
168 }
169
170
171
172
173
174 public void setTokensOnContext(DOMCryptoContext context) {
175 if (tokenList != null) {
176 for (Element elem : tokenList) {
177 WSSecurityUtil.storeElementInContext(context, elem);
178 }
179 }
180 }
181
182
183
184
185
186
187
188 public void addProtectionElement(Element element) {
189 if (tokenList == null) {
190 tokenList = new ArrayList<Element>();
191 }
192 tokenList.add(element);
193 }
194
195
196
197
198
199
200
201 public Element getProtectionElement(String uri) {
202 return getTokenElement(uri);
203 }
204
205
206
207
208
209 public void addResult(WSSecurityEngineResult result) {
210 if (resultsList == null) {
211 resultsList = new ArrayList<WSSecurityEngineResult>();
212 }
213 resultsList.add(result);
214 }
215
216
217
218
219
220
221 public WSSecurityEngineResult getResult(String uri) {
222 String id = uri;
223 if (id == null) {
224 return null;
225 } else if (id.charAt(0) == '#') {
226 id = id.substring(1);
227 }
228 if (resultsList != null) {
229 for (WSSecurityEngineResult result : resultsList) {
230 if (result != null) {
231 String cId = (String)result.get(WSSecurityEngineResult.TAG_ID);
232 if (id.equals(cId)) {
233 return result;
234 }
235 }
236 }
237 }
238 return null;
239 }
240
241
242
243
244 public List<WSSecurityEngineResult> getResultsByTag(Integer tag) {
245 List<WSSecurityEngineResult> foundResults = new ArrayList<WSSecurityEngineResult>();
246 if (resultsList != null) {
247 for (WSSecurityEngineResult result : resultsList) {
248 if (result != null) {
249 Integer resultTag = (Integer)result.get(WSSecurityEngineResult.TAG_ACTION);
250 if (tag.intValue() == resultTag.intValue()) {
251 foundResults.add(result);
252 }
253 }
254 }
255 }
256 return foundResults;
257 }
258
259
260
261
262 public WSSecurityEngineResult getResultByTag(Integer tag, String uri) {
263 String id = uri;
264 if (id == null) {
265 return null;
266 } else if (id.charAt(0) == '#') {
267 id = id.substring(1);
268 }
269 if (resultsList != null) {
270 for (WSSecurityEngineResult result : resultsList) {
271 if (result != null) {
272 Integer resultTag = (Integer)result.get(WSSecurityEngineResult.TAG_ACTION);
273 String cId = (String)result.get(WSSecurityEngineResult.TAG_ID);
274 if ((tag.intValue() == resultTag.intValue()) && id.equals(cId)) {
275 return result;
276 }
277 }
278 }
279 }
280 return null;
281 }
282
283
284
285
286
287 public Crypto getCrypto() {
288 return crypto;
289 }
290
291
292
293
294 public Document getDocument() {
295 return doc;
296 }
297
298
299
300
301
302 public void setCrypto(Crypto crypto) {
303 this.crypto = crypto;
304 }
305
306
307
308
309 public void setCallbackLookup(CallbackLookup callbackLookup) {
310 this.callbackLookup = callbackLookup;
311 }
312
313
314
315
316 public CallbackLookup getCallbackLookup() {
317 return callbackLookup;
318 }
319
320
321
322
323 public Element getSecurityHeader() {
324 return securityHeader;
325 }
326
327
328
329
330
331
332 public void setSecurityHeader(Element securityHeader) {
333 this.securityHeader = securityHeader;
334 }
335 }