1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.messaging.saaj.soap.impl;
27
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.io.OutputStreamWriter;
31
32 import java.util.Iterator;
33 import java.util.logging.Level;
34 import org.w3c.dom.Document;
35
36 import javax.xml.namespace.QName;
37 import javax.xml.soap.*;
38 import javax.xml.transform.*;
39 import javax.xml.transform.dom.DOMSource;
40 import javax.xml.transform.stream.StreamResult;
41 import javax.xml.transform.sax.*;
42
43 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
44 import com.sun.xml.internal.messaging.saaj.soap.Envelope;
45 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
46 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
47 import com.sun.xml.internal.messaging.saaj.util.FastInfosetReflection;
48 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer;
49
50 /**
51 * Our implementation of the SOAP envelope.
52 *
53 * @author Anil Vijendran (anil@sun.com)
54 */
55 public abstract class EnvelopeImpl extends ElementImpl implements Envelope {
56 protected HeaderImpl header;
57 protected BodyImpl body;
58 String omitXmlDecl = "yes";
59 String charset = "utf-8";
60 String xmlDecl = null;
61
62 protected EnvelopeImpl(SOAPDocumentImpl ownerDoc, Name name) {
63 super(ownerDoc, name);
64 }
65
66 protected EnvelopeImpl(SOAPDocumentImpl ownerDoc, QName name) {
67 super(ownerDoc, name);
68 }
69
70 protected EnvelopeImpl(
71 SOAPDocumentImpl ownerDoc,
72 NameImpl name,
73 boolean createHeader,
74 boolean createBody)
75 throws SOAPException {
86 addBody();
87 }
88
89 protected abstract NameImpl getHeaderName(String prefix);
90 protected abstract NameImpl getBodyName(String prefix);
91
92 public SOAPHeader addHeader() throws SOAPException {
93 return addHeader(null);
94 }
95
96 public SOAPHeader addHeader(String prefix) throws SOAPException {
97
98 if (prefix == null || prefix.equals("")) {
99 prefix = getPrefix();
100 }
101
102 NameImpl headerName = getHeaderName(prefix);
103 NameImpl bodyName = getBodyName(prefix);
104
105 HeaderImpl header = null;
106 SOAPElement firstChild = null;
107
108 Iterator eachChild = getChildElementNodes();
109 if (eachChild.hasNext()) {
110 firstChild = (SOAPElement) eachChild.next();
111 if (firstChild.getElementName().equals(headerName)) {
112 log.severe("SAAJ0120.impl.header.already.exists");
113 throw new SOAPExceptionImpl("Can't add a header when one is already present.");
114 } else if (!firstChild.getElementName().equals(bodyName)) {
115 log.severe("SAAJ0121.impl.invalid.first.child.of.envelope");
116 throw new SOAPExceptionImpl("First child of Envelope must be either a Header or Body");
117 }
118 }
119
120 header = (HeaderImpl) createElement(headerName);
121 insertBefore(header, firstChild);
122 header.ensureNamespaceIsDeclared(headerName.getPrefix(), headerName.getURI());
123
124 return header;
125 }
126
127 protected void lookForHeader() throws SOAPException {
128 NameImpl headerName = getHeaderName(null);
129
130 HeaderImpl hdr = (HeaderImpl) findChild(headerName);
237 }
238
239 public void setOmitXmlDecl(String value) {
240 this.omitXmlDecl = value;
241 }
242
243 public void setXmlDecl(String value) {
244 this.xmlDecl = value;
245 }
246
247 private String getOmitXmlDecl() {
248 return this.omitXmlDecl;
249 }
250
251 public void setCharsetEncoding(String value) {
252 charset = value;
253 }
254
255 public void output(OutputStream out) throws IOException {
256 try {
257 Transformer transformer =
258 EfficientStreamingTransformer.newTransformer();
259
260 transformer.setOutputProperty(
261 OutputKeys.OMIT_XML_DECLARATION, "yes");
262 /*omitXmlDecl);*/
263 // no equivalent for "setExpandEmptyElements"
264 transformer.setOutputProperty(
265 OutputKeys.ENCODING,
266 charset);
267
268 if (omitXmlDecl.equals("no") && xmlDecl == null) {
269 xmlDecl = "<?xml version=\"" + getOwnerDocument().getXmlVersion() + "\" encoding=\"" +
270 charset + "\" ?>";
271 }
272
273 StreamResult result = new StreamResult(out);
274 if (xmlDecl != null) {
275 OutputStreamWriter writer = new OutputStreamWriter(out, charset);
276 writer.write(xmlDecl);
340 // format.setIndentSize(2);
341 // format.setNewlines(true);
342 // format.setTrimText(true);
343 // format.setPadText(true);
344 // format.setExpandEmptyElements(false);
345 //
346 // XMLWriter writer = new XMLWriter(out, format);
347 // writer.write(getDocument());
348 // }
349
350
351 public SOAPElement setElementQName(QName newName) throws SOAPException {
352 log.log(Level.SEVERE,
353 "SAAJ0146.impl.invalid.name.change.requested",
354 new Object[] {elementQName.getLocalPart(),
355 newName.getLocalPart()});
356 throw new SOAPException("Cannot change name for "
357 + elementQName.getLocalPart() + " to "
358 + newName.getLocalPart());
359 }
360 }
|
1 /*
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.
4 *
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.messaging.saaj.soap.impl;
27
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.io.OutputStreamWriter;
31
32 import java.util.logging.Level;
33
34 import javax.xml.namespace.QName;
35 import javax.xml.soap.*;
36 import javax.xml.stream.XMLStreamException;
37 import javax.xml.stream.XMLStreamReader;
38 import javax.xml.stream.XMLStreamWriter;
39 import javax.xml.transform.*;
40 import javax.xml.transform.dom.DOMSource;
41 import javax.xml.transform.stream.StreamResult;
42
43 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
44 import com.sun.xml.internal.messaging.saaj.soap.LazyEnvelope;
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;
48 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
49 import com.sun.xml.internal.messaging.saaj.util.FastInfosetReflection;
50 import com.sun.xml.internal.messaging.saaj.util.stax.LazyEnvelopeStaxReader;
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;
55
56 /**
57 * Our implementation of the SOAP envelope.
58 *
59 * @author Anil Vijendran (anil@sun.com)
60 */
61 public abstract class EnvelopeImpl extends ElementImpl implements LazyEnvelope {
62 protected HeaderImpl header;
63 protected BodyImpl body;
64 String omitXmlDecl = "yes";
65 String charset = "utf-8";
66 String xmlDecl = null;
67
68 protected EnvelopeImpl(SOAPDocumentImpl ownerDoc, Name name) {
69 super(ownerDoc, name);
70 }
71
72 protected EnvelopeImpl(SOAPDocumentImpl ownerDoc, QName name) {
73 super(ownerDoc, name);
74 }
75
76 protected EnvelopeImpl(
77 SOAPDocumentImpl ownerDoc,
78 NameImpl name,
79 boolean createHeader,
80 boolean createBody)
81 throws SOAPException {
92 addBody();
93 }
94
95 protected abstract NameImpl getHeaderName(String prefix);
96 protected abstract NameImpl getBodyName(String prefix);
97
98 public SOAPHeader addHeader() throws SOAPException {
99 return addHeader(null);
100 }
101
102 public SOAPHeader addHeader(String prefix) throws SOAPException {
103
104 if (prefix == null || prefix.equals("")) {
105 prefix = getPrefix();
106 }
107
108 NameImpl headerName = getHeaderName(prefix);
109 NameImpl bodyName = getBodyName(prefix);
110
111 HeaderImpl header = null;
112 SOAPElement firstChild = (SOAPElement) getFirstChildElement();
113
114 if (firstChild != null) {
115 if (firstChild.getElementName().equals(headerName)) {
116 log.severe("SAAJ0120.impl.header.already.exists");
117 throw new SOAPExceptionImpl("Can't add a header when one is already present.");
118 } else if (!firstChild.getElementName().equals(bodyName)) {
119 log.severe("SAAJ0121.impl.invalid.first.child.of.envelope");
120 throw new SOAPExceptionImpl("First child of Envelope must be either a Header or Body");
121 }
122 }
123
124 header = (HeaderImpl) createElement(headerName);
125 insertBefore(header, firstChild);
126 header.ensureNamespaceIsDeclared(headerName.getPrefix(), headerName.getURI());
127
128 return header;
129 }
130
131 protected void lookForHeader() throws SOAPException {
132 NameImpl headerName = getHeaderName(null);
133
134 HeaderImpl hdr = (HeaderImpl) findChild(headerName);
241 }
242
243 public void setOmitXmlDecl(String value) {
244 this.omitXmlDecl = value;
245 }
246
247 public void setXmlDecl(String value) {
248 this.xmlDecl = value;
249 }
250
251 private String getOmitXmlDecl() {
252 return this.omitXmlDecl;
253 }
254
255 public void setCharsetEncoding(String value) {
256 charset = value;
257 }
258
259 public void output(OutputStream out) throws IOException {
260 try {
261 // materializeBody();
262 Transformer transformer =
263 EfficientStreamingTransformer.newTransformer();
264
265 transformer.setOutputProperty(
266 OutputKeys.OMIT_XML_DECLARATION, "yes");
267 /*omitXmlDecl);*/
268 // no equivalent for "setExpandEmptyElements"
269 transformer.setOutputProperty(
270 OutputKeys.ENCODING,
271 charset);
272
273 if (omitXmlDecl.equals("no") && xmlDecl == null) {
274 xmlDecl = "<?xml version=\"" + getOwnerDocument().getXmlVersion() + "\" encoding=\"" +
275 charset + "\" ?>";
276 }
277
278 StreamResult result = new StreamResult(out);
279 if (xmlDecl != null) {
280 OutputStreamWriter writer = new OutputStreamWriter(out, charset);
281 writer.write(xmlDecl);
345 // format.setIndentSize(2);
346 // format.setNewlines(true);
347 // format.setTrimText(true);
348 // format.setPadText(true);
349 // format.setExpandEmptyElements(false);
350 //
351 // XMLWriter writer = new XMLWriter(out, format);
352 // writer.write(getDocument());
353 // }
354
355
356 public SOAPElement setElementQName(QName newName) throws SOAPException {
357 log.log(Level.SEVERE,
358 "SAAJ0146.impl.invalid.name.change.requested",
359 new Object[] {elementQName.getLocalPart(),
360 newName.getLocalPart()});
361 throw new SOAPException("Cannot change name for "
362 + elementQName.getLocalPart() + " to "
363 + newName.getLocalPart());
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
438 }
|