1 /* |
1 /* |
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
29 import java.util.Locale; |
29 import java.util.Locale; |
30 import java.util.logging.Level; |
30 import java.util.logging.Level; |
31 |
31 |
32 import javax.xml.namespace.QName; |
32 import javax.xml.namespace.QName; |
33 import javax.xml.soap.*; |
33 import javax.xml.soap.*; |
|
34 import javax.xml.stream.XMLStreamException; |
|
35 import javax.xml.stream.XMLStreamReader; |
34 import javax.xml.parsers.DocumentBuilder; |
36 import javax.xml.parsers.DocumentBuilder; |
35 import javax.xml.parsers.DocumentBuilderFactory; |
37 import javax.xml.parsers.DocumentBuilderFactory; |
36 |
38 |
37 import org.w3c.dom.*; |
39 import org.w3c.dom.*; |
|
40 import org.w3c.dom.Node; |
38 |
41 |
39 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
42 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
40 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument; |
43 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument; |
41 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl; |
44 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl; |
|
45 import com.sun.xml.internal.messaging.saaj.soap.StaxBridge; |
42 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl; |
46 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl; |
43 |
47 |
44 /** |
48 /** |
45 * The implementation of SOAP-ENV:BODY or the SOAPBody abstraction. |
49 * The implementation of SOAP-ENV:BODY or the SOAPBody abstraction. |
46 * |
50 * |
47 * @author Anil Vijendran (anil@sun.com) |
51 * @author Anil Vijendran (anil@sun.com) |
48 */ |
52 */ |
49 public abstract class BodyImpl extends ElementImpl implements SOAPBody { |
53 public abstract class BodyImpl extends ElementImpl implements SOAPBody { |
50 private SOAPFault fault; |
54 private SOAPFault fault; |
|
55 // private XMLStreamReaderToXMLStreamWriter staxBridge; |
|
56 private StaxBridge staxBridge; |
|
57 private boolean payloadStreamRead = false; |
51 |
58 |
52 protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) { |
59 protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) { |
53 super(ownerDoc, bodyName); |
60 super(ownerDoc, bodyName); |
54 } |
61 } |
55 |
62 |
134 |
141 |
135 return null; |
142 return null; |
136 } |
143 } |
137 |
144 |
138 public boolean hasFault() { |
145 public boolean hasFault() { |
139 initializeFault(); |
146 QName payloadQName = getPayloadQName(); |
140 return fault != null; |
147 return getFaultQName().equals(payloadQName); |
|
148 } |
|
149 |
|
150 private Object getFaultQName() { |
|
151 return new QName(getNamespaceURI(), "Fault"); |
141 } |
152 } |
142 |
153 |
143 public SOAPFault getFault() { |
154 public SOAPFault getFault() { |
144 if (hasFault()) |
155 if (hasFault()) { |
|
156 if (fault == null) { |
|
157 //initialize fault member |
|
158 fault = (SOAPFault) getFirstChildElement(); |
|
159 } |
145 return fault; |
160 return fault; |
|
161 } |
146 return null; |
162 return null; |
147 } |
163 } |
148 |
164 |
149 public SOAPBodyElement addBodyElement(Name name) throws SOAPException { |
165 public SOAPBodyElement addBodyElement(Name name) throws SOAPException { |
150 SOAPBodyElement newBodyElement = |
166 SOAPBodyElement newBodyElement = |
320 firstBodyElement.detachNode(); |
336 firstBodyElement.detachNode(); |
321 |
337 |
322 return document; |
338 return document; |
323 } |
339 } |
324 |
340 |
|
341 private void materializePayloadWrapException() { |
|
342 try { |
|
343 materializePayload(); |
|
344 } catch (SOAPException e) { |
|
345 throw new RuntimeException(e); |
|
346 } |
|
347 } |
|
348 private void materializePayload() throws SOAPException { |
|
349 if (staxBridge != null) { |
|
350 if (payloadStreamRead) { |
|
351 //the payload has already been read via stream reader and the |
|
352 //stream has been exhausted already. Throw an |
|
353 //exception since we are now trying to materialize as DOM and |
|
354 //there is no stream left to read |
|
355 throw new SOAPException("SOAPBody payload stream has been fully read - cannot materialize as DOM!"); |
|
356 } |
|
357 try { |
|
358 staxBridge.bridgePayload(); |
|
359 staxBridge = null; |
|
360 payloadStreamRead = true; |
|
361 } catch (XMLStreamException e) { |
|
362 throw new SOAPException(e); |
|
363 } |
|
364 } |
|
365 } |
|
366 |
|
367 @Override |
|
368 public boolean hasChildNodes() { |
|
369 boolean hasChildren = super.hasChildNodes(); |
|
370 //to answer this question we need to know _whether_ we have at least one child |
|
371 //So no need to materialize body if we already know we have a header child |
|
372 if (!hasChildren) { |
|
373 materializePayloadWrapException(); |
|
374 } |
|
375 return super.hasChildNodes(); |
|
376 } |
|
377 |
|
378 @Override |
|
379 public NodeList getChildNodes() { |
|
380 materializePayloadWrapException(); |
|
381 return super.getChildNodes(); |
|
382 } |
|
383 |
|
384 @Override |
|
385 public Node getFirstChild() { |
|
386 Node child = super.getFirstChild(); |
|
387 if (child == null) { |
|
388 materializePayloadWrapException(); |
|
389 } |
|
390 return super.getFirstChild(); |
|
391 } |
|
392 |
|
393 public Node getFirstChildNoMaterialize() { |
|
394 return super.getFirstChild(); |
|
395 } |
|
396 |
|
397 @Override |
|
398 public Node getLastChild() { |
|
399 materializePayloadWrapException(); |
|
400 return super.getLastChild(); |
|
401 } |
|
402 |
|
403 XMLStreamReader getPayloadReader() { |
|
404 return staxBridge.getPayloadReader(); |
|
405 } |
|
406 |
|
407 void setStaxBridge(StaxBridge bridge) { |
|
408 this.staxBridge = bridge; |
|
409 } |
|
410 |
|
411 StaxBridge getStaxBridge() { |
|
412 return staxBridge; |
|
413 } |
|
414 |
|
415 void setPayloadStreamRead() { |
|
416 this.payloadStreamRead = true; |
|
417 } |
|
418 |
|
419 QName getPayloadQName() { |
|
420 if (staxBridge != null) { |
|
421 return staxBridge.getPayloadQName(); |
|
422 } else { |
|
423 //not lazy - Just get first child element and return its name |
|
424 Element elem = getFirstChildElement(); |
|
425 if (elem != null) { |
|
426 String ns = elem.getNamespaceURI(); |
|
427 String pref = elem.getPrefix(); |
|
428 String local = elem.getLocalName(); |
|
429 if (pref != null) return new QName(ns, local, pref); |
|
430 if (ns != null) return new QName(ns, local); |
|
431 return new QName(local); |
|
432 } |
|
433 } |
|
434 return null; |
|
435 } |
|
436 |
|
437 String getPayloadAttributeValue(String attName) { |
|
438 if (staxBridge != null) { |
|
439 return staxBridge.getPayloadAttributeValue(attName); |
|
440 } else { |
|
441 //not lazy -Just get first child element and return its attribute |
|
442 Element elem = getFirstChildElement(); |
|
443 if (elem != null) { |
|
444 return elem.getAttribute(localName); |
|
445 } |
|
446 } |
|
447 return null; |
|
448 } |
|
449 |
|
450 String getPayloadAttributeValue(QName attNAme) { |
|
451 if (staxBridge != null) { |
|
452 return staxBridge.getPayloadAttributeValue(attNAme); |
|
453 } else { |
|
454 //not lazy -Just get first child element and return its attribute |
|
455 Element elem = getFirstChildElement(); |
|
456 if (elem != null) { |
|
457 return elem.getAttributeNS(attNAme.getNamespaceURI(), attNAme.getLocalPart()); |
|
458 } |
|
459 } |
|
460 return null; |
|
461 } |
|
462 |
|
463 public boolean isLazy() { |
|
464 return (staxBridge != null && !payloadStreamRead); |
|
465 } |
|
466 |
325 } |
467 } |