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 |
27 |
27 |
28 import java.io.IOException; |
28 import java.io.IOException; |
29 import java.io.OutputStream; |
29 import java.io.OutputStream; |
30 import java.io.OutputStreamWriter; |
30 import java.io.OutputStreamWriter; |
31 |
31 |
32 import java.util.Iterator; |
|
33 import java.util.logging.Level; |
32 import java.util.logging.Level; |
34 import org.w3c.dom.Document; |
|
35 |
33 |
36 import javax.xml.namespace.QName; |
34 import javax.xml.namespace.QName; |
37 import javax.xml.soap.*; |
35 import javax.xml.soap.*; |
|
36 import javax.xml.stream.XMLStreamException; |
|
37 import javax.xml.stream.XMLStreamReader; |
|
38 import javax.xml.stream.XMLStreamWriter; |
38 import javax.xml.transform.*; |
39 import javax.xml.transform.*; |
39 import javax.xml.transform.dom.DOMSource; |
40 import javax.xml.transform.dom.DOMSource; |
40 import javax.xml.transform.stream.StreamResult; |
41 import javax.xml.transform.stream.StreamResult; |
41 import javax.xml.transform.sax.*; |
|
42 |
42 |
43 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
43 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
44 import com.sun.xml.internal.messaging.saaj.soap.Envelope; |
44 import com.sun.xml.internal.messaging.saaj.soap.LazyEnvelope; |
45 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl; |
45 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl; |
|
46 import com.sun.xml.internal.messaging.saaj.soap.StaxBridge; |
|
47 import com.sun.xml.internal.messaging.saaj.soap.StaxLazySourceBridge; |
46 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl; |
48 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl; |
47 import com.sun.xml.internal.messaging.saaj.util.FastInfosetReflection; |
49 import com.sun.xml.internal.messaging.saaj.util.FastInfosetReflection; |
|
50 import com.sun.xml.internal.messaging.saaj.util.stax.LazyEnvelopeStaxReader; |
48 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer; |
51 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer; |
|
52 |
|
53 import com.sun.xml.internal.org.jvnet.staxex.util.DOMStreamReader; |
|
54 import com.sun.xml.internal.org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter; |
49 |
55 |
50 /** |
56 /** |
51 * Our implementation of the SOAP envelope. |
57 * Our implementation of the SOAP envelope. |
52 * |
58 * |
53 * @author Anil Vijendran (anil@sun.com) |
59 * @author Anil Vijendran (anil@sun.com) |
54 */ |
60 */ |
55 public abstract class EnvelopeImpl extends ElementImpl implements Envelope { |
61 public abstract class EnvelopeImpl extends ElementImpl implements LazyEnvelope { |
56 protected HeaderImpl header; |
62 protected HeaderImpl header; |
57 protected BodyImpl body; |
63 protected BodyImpl body; |
58 String omitXmlDecl = "yes"; |
64 String omitXmlDecl = "yes"; |
59 String charset = "utf-8"; |
65 String charset = "utf-8"; |
60 String xmlDecl = null; |
66 String xmlDecl = null; |
101 |
107 |
102 NameImpl headerName = getHeaderName(prefix); |
108 NameImpl headerName = getHeaderName(prefix); |
103 NameImpl bodyName = getBodyName(prefix); |
109 NameImpl bodyName = getBodyName(prefix); |
104 |
110 |
105 HeaderImpl header = null; |
111 HeaderImpl header = null; |
106 SOAPElement firstChild = null; |
112 SOAPElement firstChild = (SOAPElement) getFirstChildElement(); |
107 |
113 |
108 Iterator eachChild = getChildElementNodes(); |
114 if (firstChild != null) { |
109 if (eachChild.hasNext()) { |
|
110 firstChild = (SOAPElement) eachChild.next(); |
|
111 if (firstChild.getElementName().equals(headerName)) { |
115 if (firstChild.getElementName().equals(headerName)) { |
112 log.severe("SAAJ0120.impl.header.already.exists"); |
116 log.severe("SAAJ0120.impl.header.already.exists"); |
113 throw new SOAPExceptionImpl("Can't add a header when one is already present."); |
117 throw new SOAPExceptionImpl("Can't add a header when one is already present."); |
114 } else if (!firstChild.getElementName().equals(bodyName)) { |
118 } else if (!firstChild.getElementName().equals(bodyName)) { |
115 log.severe("SAAJ0121.impl.invalid.first.child.of.envelope"); |
119 log.severe("SAAJ0121.impl.invalid.first.child.of.envelope"); |
355 newName.getLocalPart()}); |
360 newName.getLocalPart()}); |
356 throw new SOAPException("Cannot change name for " |
361 throw new SOAPException("Cannot change name for " |
357 + elementQName.getLocalPart() + " to " |
362 + elementQName.getLocalPart() + " to " |
358 + newName.getLocalPart()); |
363 + newName.getLocalPart()); |
359 } |
364 } |
|
365 |
|
366 @Override |
|
367 public void setStaxBridge(StaxBridge bridge) throws SOAPException { |
|
368 //set it on the body |
|
369 ((BodyImpl) getBody()).setStaxBridge(bridge); |
|
370 } |
|
371 |
|
372 @Override |
|
373 public StaxBridge getStaxBridge() throws SOAPException { |
|
374 return ((BodyImpl) getBody()).getStaxBridge(); |
|
375 } |
|
376 |
|
377 @Override |
|
378 public XMLStreamReader getPayloadReader() throws SOAPException { |
|
379 return ((BodyImpl) getBody()).getPayloadReader(); |
|
380 } |
|
381 |
|
382 @Override |
|
383 public void writeTo(final XMLStreamWriter writer) throws XMLStreamException, SOAPException { |
|
384 StaxBridge readBridge = this.getStaxBridge(); |
|
385 if (readBridge != null && readBridge instanceof StaxLazySourceBridge) { |
|
386 // StaxSoapWriteBridge writingBridge = new StaxSoapWriteBridge(this); |
|
387 // writingBridge.write(writer); |
|
388 final String soapEnvNS = this.getNamespaceURI(); |
|
389 final DOMStreamReader reader = new DOMStreamReader(this); |
|
390 XMLStreamReaderToXMLStreamWriter writingBridge = new XMLStreamReaderToXMLStreamWriter(); |
|
391 writingBridge.bridge( new XMLStreamReaderToXMLStreamWriter.Breakpoint(reader, writer) { |
|
392 public boolean proceedAfterStartElement() { |
|
393 if ("Body".equals(reader.getLocalName()) && soapEnvNS.equals(reader.getNamespaceURI()) ){ |
|
394 return false; |
|
395 } else |
|
396 return true; |
|
397 } |
|
398 });//bridgeToBodyStartTag |
|
399 ((StaxLazySourceBridge)readBridge).writePayloadTo(writer); |
|
400 writer.writeEndElement();//body |
|
401 writer.writeEndElement();//env |
|
402 writer.writeEndDocument(); |
|
403 writer.flush(); |
|
404 } else { |
|
405 LazyEnvelopeStaxReader lazyEnvReader = new LazyEnvelopeStaxReader(this); |
|
406 XMLStreamReaderToXMLStreamWriter writingBridge = new XMLStreamReaderToXMLStreamWriter(); |
|
407 writingBridge.bridge(lazyEnvReader, writer); |
|
408 // writingBridge.bridge(new XMLStreamReaderToXMLStreamWriter.Breakpoint(lazyEnvReader, writer)); |
|
409 } |
|
410 //Assume the staxBridge is exhausted now since we would have read the body reader |
|
411 ((BodyImpl) getBody()).setPayloadStreamRead(); |
|
412 } |
|
413 |
|
414 @Override |
|
415 public QName getPayloadQName() throws SOAPException { |
|
416 return ((BodyImpl) getBody()).getPayloadQName(); |
|
417 } |
|
418 |
|
419 @Override |
|
420 public String getPayloadAttributeValue(String localName) throws SOAPException { |
|
421 return ((BodyImpl) getBody()).getPayloadAttributeValue(localName); |
|
422 } |
|
423 |
|
424 @Override |
|
425 public String getPayloadAttributeValue(QName qName) throws SOAPException { |
|
426 return ((BodyImpl) getBody()).getPayloadAttributeValue(qName); |
|
427 } |
|
428 |
|
429 @Override |
|
430 public boolean isLazy() { |
|
431 try { |
|
432 return ((BodyImpl) getBody()).isLazy(); |
|
433 } catch (SOAPException e) { |
|
434 return false; |
|
435 } |
|
436 } |
|
437 |
360 } |
438 } |