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
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 {
82 this(ownerDoc, name);
83
84 ensureNamespaceIsDeclared(
85 getElementQName().getPrefix(), getElementQName().getNamespaceURI());
86
87 // XXX
88 if (createHeader)
89 addHeader();
90
91 if (createBody)
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);
135 header = hdr;
136 }
137
138 public SOAPHeader getHeader() throws SOAPException {
139 lookForHeader();
140 return header;
141 }
142
143 protected void lookForBody() throws SOAPException {
144 NameImpl bodyName = getBodyName(null);
145
146 BodyImpl bodyChildElement = (BodyImpl) findChild(bodyName);
147 body = bodyChildElement;
148 }
149
150 public SOAPBody addBody() throws SOAPException {
151 return addBody(null);
152 }
153
154 public SOAPBody addBody(String prefix) throws SOAPException {
155 lookForBody();
156
157 if (prefix == null || prefix.equals("")) {
158 prefix = getPrefix();
159 }
160
161 if (body == null) {
162 NameImpl bodyName = getBodyName(prefix);
163 body = (BodyImpl) createElement(bodyName);
164 insertBefore(body, null);
165 body.ensureNamespaceIsDeclared(bodyName.getPrefix(), bodyName.getURI());
166 } else {
167 log.severe("SAAJ0122.impl.body.already.exists");
168 throw new SOAPExceptionImpl("Can't add a body when one is already present.");
169 }
170
171 return body;
172 }
173
174 protected SOAPElement addElement(Name name) throws SOAPException {
175 if (getBodyName(null).equals(name)) {
176 return addBody(name.getPrefix());
177 }
178 if (getHeaderName(null).equals(name)) {
179 return addHeader(name.getPrefix());
180 }
181
182 return super.addElement(name);
183 }
184
|
1 /*
2 * Copyright (c) 1997, 2017, 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
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 import org.w3c.dom.Element;
56
57 /**
58 * Our implementation of the SOAP envelope.
59 *
60 * @author Anil Vijendran (anil@sun.com)
61 */
62 public abstract class EnvelopeImpl extends ElementImpl implements LazyEnvelope {
63 protected HeaderImpl header;
64 protected BodyImpl body;
65 String omitXmlDecl = "yes";
66 String charset = "utf-8";
67 String xmlDecl = null;
68
69 protected EnvelopeImpl(SOAPDocumentImpl ownerDoc, Name name) {
70 super(ownerDoc, name);
71 }
72
73 protected EnvelopeImpl(SOAPDocumentImpl ownerDoc, QName name) {
74 super(ownerDoc, name);
75 }
76
77 protected EnvelopeImpl(
78 SOAPDocumentImpl ownerDoc,
79 NameImpl name,
80 boolean createHeader,
81 boolean createBody)
82 throws SOAPException {
83 this(ownerDoc, name);
84
85 ensureNamespaceIsDeclared(
86 getElementQName().getPrefix(), getElementQName().getNamespaceURI());
87
88 // XXX
89 if (createHeader)
90 addHeader();
91
92 if (createBody)
93 addBody();
94 }
95
96 public EnvelopeImpl(SOAPDocumentImpl ownerDoc, Element domElement) {
97 super(ownerDoc, domElement);
98 }
99
100 protected abstract NameImpl getHeaderName(String prefix);
101 protected abstract NameImpl getBodyName(String prefix);
102
103 public SOAPHeader addHeader() throws SOAPException {
104 return addHeader(null);
105 }
106
107 public SOAPHeader addHeader(String prefix) throws SOAPException {
108
109 if (prefix == null || prefix.equals("")) {
110 prefix = getPrefix();
111 }
112
113 NameImpl headerName = getHeaderName(prefix);
114 NameImpl bodyName = getBodyName(prefix);
115
116 HeaderImpl header = null;
117 SOAPElement firstChild = (SOAPElement) getFirstChildElement();
118
119 if (firstChild != null) {
120 if (firstChild.getElementName().equals(headerName)) {
121 log.severe("SAAJ0120.impl.header.already.exists");
122 throw new SOAPExceptionImpl("Can't add a header when one is already present.");
123 } else if (!firstChild.getElementName().equals(bodyName)) {
124 log.severe("SAAJ0121.impl.invalid.first.child.of.envelope");
125 throw new SOAPExceptionImpl("First child of Envelope must be either a Header or Body");
126 }
127 }
128
129 header = (HeaderImpl) createElement(headerName);
130 insertBefore(header.getDomElement(), firstChild);
131 header.ensureNamespaceIsDeclared(headerName.getPrefix(), headerName.getURI());
132
133 return header;
134 }
135
136 protected void lookForHeader() throws SOAPException {
137 NameImpl headerName = getHeaderName(null);
138
139 HeaderImpl hdr = (HeaderImpl) findChild(headerName);
140 header = hdr;
141 }
142
143 public SOAPHeader getHeader() throws SOAPException {
144 lookForHeader();
145 return header;
146 }
147
148 protected void lookForBody() throws SOAPException {
149 NameImpl bodyName = getBodyName(null);
150
151 BodyImpl bodyChildElement = (BodyImpl) findChild(bodyName);
152 body = bodyChildElement;
153 }
154
155 public SOAPBody addBody() throws SOAPException {
156 return addBody(null);
157 }
158
159 public SOAPBody addBody(String prefix) throws SOAPException {
160 lookForBody();
161
162 if (prefix == null || prefix.equals("")) {
163 prefix = getPrefix();
164 }
165
166 if (body == null) {
167 NameImpl bodyName = getBodyName(prefix);
168 body = (BodyImpl) createElement(bodyName);
169 insertBefore(body.getDomElement(), null);
170 body.ensureNamespaceIsDeclared(bodyName.getPrefix(), bodyName.getURI());
171 } else {
172 log.severe("SAAJ0122.impl.body.already.exists");
173 throw new SOAPExceptionImpl("Can't add a body when one is already present.");
174 }
175
176 return body;
177 }
178
179 protected SOAPElement addElement(Name name) throws SOAPException {
180 if (getBodyName(null).equals(name)) {
181 return addBody(name.getPrefix());
182 }
183 if (getHeaderName(null).equals(name)) {
184 return addHeader(name.getPrefix());
185 }
186
187 return super.addElement(name);
188 }
189
|