-
Notifications
You must be signed in to change notification settings - Fork 15
/
BodyImpl.java
487 lines (418 loc) · 16 KB
/
BodyImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.messaging.saaj.soap.impl;
import java.util.Iterator;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import com.sun.xml.messaging.saaj.util.LogDomainConstants;
import com.sun.xml.messaging.saaj.util.SAAJUtil;
import com.sun.xml.messaging.saaj.SOAPExceptionImpl;
import com.sun.xml.messaging.saaj.soap.SOAPDocument;
import com.sun.xml.messaging.saaj.soap.SOAPDocumentImpl;
import com.sun.xml.messaging.saaj.soap.StaxBridge;
import com.sun.xml.messaging.saaj.soap.name.NameImpl;
import jakarta.xml.soap.Name;
import jakarta.xml.soap.SOAPBody;
import jakarta.xml.soap.SOAPBodyElement;
import jakarta.xml.soap.SOAPElement;
import jakarta.xml.soap.SOAPEnvelope;
import jakarta.xml.soap.SOAPException;
import jakarta.xml.soap.SOAPFault;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* The implementation of SOAP-ENV:BODY or the SOAPBody abstraction.
*
* @author Anil Vijendran (anil@sun.com)
*/
public abstract class BodyImpl extends ElementImpl implements SOAPBody {
private static final Logger log = Logger.getLogger(LogDomainConstants.SOAP_IMPL_DOMAIN, "com.sun.xml.messaging.saaj.soap.impl.LocalStrings");
private SOAPFault fault;
// private XMLStreamReaderToXMLStreamWriter staxBridge;
private StaxBridge staxBridge;
private boolean payloadStreamRead = false;
protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) {
super(ownerDoc, bodyName);
}
protected BodyImpl(SOAPDocumentImpl ownerDoc, Element domElement) {
super(ownerDoc, domElement);
}
protected abstract NameImpl getFaultName(String name);
protected abstract boolean isFault(SOAPElement child);
protected abstract SOAPBodyElement createBodyElement(Name name);
protected abstract SOAPBodyElement createBodyElement(QName name);
protected abstract SOAPFault createFaultElement();
protected abstract QName getDefaultFaultCode();
@Override
public SOAPFault addFault() throws SOAPException {
if (hasFault()) {
log.severe("SAAJ0110.impl.fault.already.exists");
throw new SOAPExceptionImpl("Error: Fault already exists");
}
fault = createFaultElement();
addNode(fault);
fault.setFaultCode(getDefaultFaultCode());
fault.setFaultString("Fault string, and possibly fault code, not set");
return fault;
}
@Override
public SOAPFault addFault(
Name faultCode,
String faultString,
Locale locale)
throws SOAPException {
SOAPFault fault = addFault();
fault.setFaultCode(faultCode);
fault.setFaultString(faultString, locale);
return fault;
}
@Override
public SOAPFault addFault(
QName faultCode,
String faultString,
Locale locale)
throws SOAPException {
SOAPFault fault = addFault();
fault.setFaultCode(faultCode);
fault.setFaultString(faultString, locale);
return fault;
}
@Override
public SOAPFault addFault(Name faultCode, String faultString)
throws SOAPException {
SOAPFault fault = addFault();
fault.setFaultCode(faultCode);
fault.setFaultString(faultString);
return fault;
}
@Override
public SOAPFault addFault(QName faultCode, String faultString)
throws SOAPException {
SOAPFault fault = addFault();
fault.setFaultCode(faultCode);
fault.setFaultString(faultString);
return fault;
}
void initializeFault() {
FaultImpl flt = (FaultImpl) findFault();
fault = flt;
}
protected SOAPElement findFault() {
Iterator<Node> eachChild = getChildElementNodes();
while (eachChild.hasNext()) {
SOAPElement child = (SOAPElement) eachChild.next();
if (isFault(child)) {
return child;
}
}
return null;
}
@Override
public boolean hasFault() {
QName payloadQName = getPayloadQName();
return getFaultQName().equals(payloadQName);
}
private Object getFaultQName() {
return new QName(getNamespaceURI(), "Fault");
}
@Override
public SOAPFault getFault() {
if (hasFault()) {
if (fault == null) {
//initialize fault member
fault = (SOAPFault) getSoapDocument().find(getFirstChildElement());
}
return fault;
}
return null;
}
@Override
public SOAPBodyElement addBodyElement(Name name) throws SOAPException {
SOAPBodyElement newBodyElement =
(SOAPBodyElement) ElementFactory.createNamedElement(
((SOAPDocument) getOwnerDocument()).getDocument(),
name.getLocalName(),
name.getPrefix(),
name.getURI());
if (newBodyElement == null) {
newBodyElement = createBodyElement(name);
}
addNode(newBodyElement);
return newBodyElement;
}
@Override
public SOAPBodyElement addBodyElement(QName qname) throws SOAPException {
SOAPBodyElement newBodyElement =
(SOAPBodyElement) ElementFactory.createNamedElement(
((SOAPDocument) getOwnerDocument()).getDocument(),
qname.getLocalPart(),
qname.getPrefix(),
qname.getNamespaceURI());
if (newBodyElement == null) {
newBodyElement = createBodyElement(qname);
}
addNode(newBodyElement);
return newBodyElement;
}
@Override
public void setParentElement(SOAPElement element) throws SOAPException {
if (!(element instanceof SOAPEnvelope)) {
log.severe("SAAJ0111.impl.body.parent.must.be.envelope");
throw new SOAPException("Parent of SOAPBody has to be a SOAPEnvelope");
}
super.setParentElement(element);
}
@Override
protected SOAPElement addElement(Name name) throws SOAPException {
return addBodyElement(name);
}
@Override
protected SOAPElement addElement(QName name) throws SOAPException {
return addBodyElement(name);
}
// public Node insertBefore(Node newElement, Node ref) throws DOMException {
// if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
// newElement = new ElementWrapper((ElementImpl) newElement);
// }
// return super.insertBefore(newElement, ref);
// }
//
// public Node replaceChild(Node newElement, Node ref) throws DOMException {
// if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
// newElement = new ElementWrapper((ElementImpl) newElement);
// }
// return super.replaceChild(newElement, ref);
// }
@Override
public SOAPBodyElement addDocument(Document document)
throws SOAPException {
/*
Element rootNode =
document.getDocumentElement();
// Causes all deferred nodes to be inflated
rootNode.normalize();
adoptElement(rootNode);
SOAPBodyElement bodyElement = (SOAPBodyElement) convertToSoapElement(rootNode);
addNode(bodyElement);
return bodyElement;
*/
///*
SOAPBodyElement newBodyElement = null;
DocumentFragment docFrag = document.createDocumentFragment();
Element rootElement = document.getDocumentElement();
if(rootElement != null) {
docFrag.appendChild(rootElement);
Document ownerDoc = getOwnerDocument();
// This copies the whole tree which could be very big so it's slow.
// However, it does have the advantage of actually working.
org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
// Adding replacingNode at the last of the children list of body
addNode(replacingNode);
Iterator<jakarta.xml.soap.Node> i =
getChildElements(NameImpl.copyElementName(rootElement));
// Return the child element with the required name which is at the
// end of the list
while(i.hasNext())
newBodyElement = (SOAPBodyElement) i.next();
}
return newBodyElement;
//*/
}
@Override
protected SOAPElement convertToSoapElement(Element element) {
final Node soapNode = getSoapDocument().findIfPresent(element);
if ((soapNode instanceof SOAPBodyElement) &&
//this check is required because ElementImpl currently
// implements SOAPBodyElement
!(soapNode.getClass().equals(ElementImpl.class))) {
return (SOAPElement) soapNode;
} else {
return replaceElementWithSOAPElement(
element,
(ElementImpl) createBodyElement(NameImpl
.copyElementName(element)));
}
}
@Override
public SOAPElement setElementQName(QName newName) throws SOAPException {
log.log(Level.SEVERE,
"SAAJ0146.impl.invalid.name.change.requested",
new Object[] {elementQName.getLocalPart().replaceAll("[\r\n]",""),
newName.getLocalPart().replaceAll("[\r\n]","")});
throw new SOAPException("Cannot change name for "
+ elementQName.getLocalPart() + " to "
+ newName.getLocalPart());
}
@Override
public Document extractContentAsDocument() throws SOAPException {
Iterator<jakarta.xml.soap.Node> eachChild = getChildElements();
jakarta.xml.soap.Node firstBodyElement = null;
while (eachChild.hasNext() &&
!(firstBodyElement instanceof SOAPElement))
firstBodyElement = eachChild.next();
boolean exactlyOneChildElement = true;
if (firstBodyElement == null)
exactlyOneChildElement = false;
else {
for (org.w3c.dom.Node node = firstBodyElement.getNextSibling();
node != null;
node = node.getNextSibling()) {
if (node instanceof Element) {
exactlyOneChildElement = false;
break;
}
}
}
if(!exactlyOneChildElement) {
log.log(Level.SEVERE,
"SAAJ0250.impl.body.should.have.exactly.one.child");
throw new SOAPException("Cannot extract Document from body");
}
Document document = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", SAAJUtil.getSystemClassLoader());
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
Element rootElement = (Element) document.importNode(
firstBodyElement,
true);
document.appendChild(rootElement);
} catch(Exception e) {
log.log(Level.SEVERE,
"SAAJ0251.impl.cannot.extract.document.from.body");
throw new SOAPExceptionImpl(
"Unable to extract Document from body", e);
}
firstBodyElement.detachNode();
return document;
}
private void materializePayloadWrapException() {
try {
materializePayload();
} catch (SOAPException e) {
throw new RuntimeException(e);
}
}
private void materializePayload() throws SOAPException {
if (staxBridge != null) {
if (payloadStreamRead) {
//the payload has already been read via stream reader and the
//stream has been exhausted already. Throw an
//exception since we are now trying to materialize as DOM and
//there is no stream left to read
throw new SOAPException("SOAPBody payload stream has been fully read - cannot materialize as DOM!");
}
try {
staxBridge.bridgePayload();
staxBridge = null;
payloadStreamRead = true;
} catch (XMLStreamException e) {
throw new SOAPException(e);
}
}
}
@Override
public boolean hasChildNodes() {
boolean hasChildren = super.hasChildNodes();
//to answer this question we need to know _whether_ we have at least one child
//So no need to materialize body if we already know we have a header child
if (!hasChildren) {
materializePayloadWrapException();
}
return super.hasChildNodes();
}
@Override
public NodeList getChildNodes() {
materializePayloadWrapException();
return super.getChildNodes();
}
@Override
public Node getFirstChild() {
Node child = super.getFirstChild();
if (child == null) {
materializePayloadWrapException();
}
return super.getFirstChild();
}
public Node getFirstChildNoMaterialize() {
return super.getFirstChild();
}
@Override
public Node getLastChild() {
materializePayloadWrapException();
return super.getLastChild();
}
XMLStreamReader getPayloadReader() {
return staxBridge.getPayloadReader();
}
void setStaxBridge(StaxBridge bridge) {
this.staxBridge = bridge;
}
StaxBridge getStaxBridge() {
return staxBridge;
}
void setPayloadStreamRead() {
this.payloadStreamRead = true;
}
QName getPayloadQName() {
if (staxBridge != null) {
return staxBridge.getPayloadQName();
} else {
//not lazy - Just get first child element and return its name
Element elem = getFirstChildElement();
if (elem != null) {
String ns = elem.getNamespaceURI();
String pref = elem.getPrefix();
String local = elem.getLocalName();
if (pref != null) return new QName(ns, local, pref);
if (ns != null) return new QName(ns, local);
return new QName(local);
}
}
return null;
}
String getPayloadAttributeValue(String attName) {
if (staxBridge != null) {
return staxBridge.getPayloadAttributeValue(attName);
} else {
//not lazy -Just get first child element and return its attribute
Element elem = getFirstChildElement();
if (elem != null) {
return elem.getAttribute(getLocalName());
}
}
return null;
}
String getPayloadAttributeValue(QName attNAme) {
if (staxBridge != null) {
return staxBridge.getPayloadAttributeValue(attNAme);
} else {
//not lazy -Just get first child element and return its attribute
Element elem = getFirstChildElement();
if (elem != null) {
return elem.getAttributeNS(attNAme.getNamespaceURI(), attNAme.getLocalPart());
}
}
return null;
}
public boolean isLazy() {
return (staxBridge != null && !payloadStreamRead);
}
}