src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/EnvelopeImpl.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.messaging.saaj.soap.impl;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.io.OutputStream;
aoqi@0 30 import java.io.OutputStreamWriter;
aoqi@0 31
aoqi@0 32 import java.util.Iterator;
aoqi@0 33 import java.util.logging.Level;
aoqi@0 34 import org.w3c.dom.Document;
aoqi@0 35
aoqi@0 36 import javax.xml.namespace.QName;
aoqi@0 37 import javax.xml.soap.*;
aoqi@0 38 import javax.xml.transform.*;
aoqi@0 39 import javax.xml.transform.dom.DOMSource;
aoqi@0 40 import javax.xml.transform.stream.StreamResult;
aoqi@0 41 import javax.xml.transform.sax.*;
aoqi@0 42
aoqi@0 43 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
aoqi@0 44 import com.sun.xml.internal.messaging.saaj.soap.Envelope;
aoqi@0 45 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
aoqi@0 46 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
aoqi@0 47 import com.sun.xml.internal.messaging.saaj.util.FastInfosetReflection;
aoqi@0 48 import com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer;
aoqi@0 49
aoqi@0 50 /**
aoqi@0 51 * Our implementation of the SOAP envelope.
aoqi@0 52 *
aoqi@0 53 * @author Anil Vijendran (anil@sun.com)
aoqi@0 54 */
aoqi@0 55 public abstract class EnvelopeImpl extends ElementImpl implements Envelope {
aoqi@0 56 protected HeaderImpl header;
aoqi@0 57 protected BodyImpl body;
aoqi@0 58 String omitXmlDecl = "yes";
aoqi@0 59 String charset = "utf-8";
aoqi@0 60 String xmlDecl = null;
aoqi@0 61
aoqi@0 62 protected EnvelopeImpl(SOAPDocumentImpl ownerDoc, Name name) {
aoqi@0 63 super(ownerDoc, name);
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 protected EnvelopeImpl(SOAPDocumentImpl ownerDoc, QName name) {
aoqi@0 67 super(ownerDoc, name);
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 protected EnvelopeImpl(
aoqi@0 71 SOAPDocumentImpl ownerDoc,
aoqi@0 72 NameImpl name,
aoqi@0 73 boolean createHeader,
aoqi@0 74 boolean createBody)
aoqi@0 75 throws SOAPException {
aoqi@0 76 this(ownerDoc, name);
aoqi@0 77
aoqi@0 78 ensureNamespaceIsDeclared(
aoqi@0 79 getElementQName().getPrefix(), getElementQName().getNamespaceURI());
aoqi@0 80
aoqi@0 81 // XXX
aoqi@0 82 if (createHeader)
aoqi@0 83 addHeader();
aoqi@0 84
aoqi@0 85 if (createBody)
aoqi@0 86 addBody();
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 protected abstract NameImpl getHeaderName(String prefix);
aoqi@0 90 protected abstract NameImpl getBodyName(String prefix);
aoqi@0 91
aoqi@0 92 public SOAPHeader addHeader() throws SOAPException {
aoqi@0 93 return addHeader(null);
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 public SOAPHeader addHeader(String prefix) throws SOAPException {
aoqi@0 97
aoqi@0 98 if (prefix == null || prefix.equals("")) {
aoqi@0 99 prefix = getPrefix();
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 NameImpl headerName = getHeaderName(prefix);
aoqi@0 103 NameImpl bodyName = getBodyName(prefix);
aoqi@0 104
aoqi@0 105 HeaderImpl header = null;
aoqi@0 106 SOAPElement firstChild = null;
aoqi@0 107
aoqi@0 108 Iterator eachChild = getChildElementNodes();
aoqi@0 109 if (eachChild.hasNext()) {
aoqi@0 110 firstChild = (SOAPElement) eachChild.next();
aoqi@0 111 if (firstChild.getElementName().equals(headerName)) {
aoqi@0 112 log.severe("SAAJ0120.impl.header.already.exists");
aoqi@0 113 throw new SOAPExceptionImpl("Can't add a header when one is already present.");
aoqi@0 114 } else if (!firstChild.getElementName().equals(bodyName)) {
aoqi@0 115 log.severe("SAAJ0121.impl.invalid.first.child.of.envelope");
aoqi@0 116 throw new SOAPExceptionImpl("First child of Envelope must be either a Header or Body");
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 header = (HeaderImpl) createElement(headerName);
aoqi@0 121 insertBefore(header, firstChild);
aoqi@0 122 header.ensureNamespaceIsDeclared(headerName.getPrefix(), headerName.getURI());
aoqi@0 123
aoqi@0 124 return header;
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 protected void lookForHeader() throws SOAPException {
aoqi@0 128 NameImpl headerName = getHeaderName(null);
aoqi@0 129
aoqi@0 130 HeaderImpl hdr = (HeaderImpl) findChild(headerName);
aoqi@0 131 header = hdr;
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 public SOAPHeader getHeader() throws SOAPException {
aoqi@0 135 lookForHeader();
aoqi@0 136 return header;
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 protected void lookForBody() throws SOAPException {
aoqi@0 140 NameImpl bodyName = getBodyName(null);
aoqi@0 141
aoqi@0 142 BodyImpl bodyChildElement = (BodyImpl) findChild(bodyName);
aoqi@0 143 body = bodyChildElement;
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public SOAPBody addBody() throws SOAPException {
aoqi@0 147 return addBody(null);
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 public SOAPBody addBody(String prefix) throws SOAPException {
aoqi@0 151 lookForBody();
aoqi@0 152
aoqi@0 153 if (prefix == null || prefix.equals("")) {
aoqi@0 154 prefix = getPrefix();
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 if (body == null) {
aoqi@0 158 NameImpl bodyName = getBodyName(prefix);
aoqi@0 159 body = (BodyImpl) createElement(bodyName);
aoqi@0 160 insertBefore(body, null);
aoqi@0 161 body.ensureNamespaceIsDeclared(bodyName.getPrefix(), bodyName.getURI());
aoqi@0 162 } else {
aoqi@0 163 log.severe("SAAJ0122.impl.body.already.exists");
aoqi@0 164 throw new SOAPExceptionImpl("Can't add a body when one is already present.");
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 return body;
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 protected SOAPElement addElement(Name name) throws SOAPException {
aoqi@0 171 if (getBodyName(null).equals(name)) {
aoqi@0 172 return addBody(name.getPrefix());
aoqi@0 173 }
aoqi@0 174 if (getHeaderName(null).equals(name)) {
aoqi@0 175 return addHeader(name.getPrefix());
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 return super.addElement(name);
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 protected SOAPElement addElement(QName name) throws SOAPException {
aoqi@0 182 if (getBodyName(null).equals(NameImpl.convertToName(name))) {
aoqi@0 183 return addBody(name.getPrefix());
aoqi@0 184 }
aoqi@0 185 if (getHeaderName(null).equals(NameImpl.convertToName(name))) {
aoqi@0 186 return addHeader(name.getPrefix());
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 return super.addElement(name);
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 public SOAPBody getBody() throws SOAPException {
aoqi@0 193 lookForBody();
aoqi@0 194 return body;
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 public Source getContent() {
aoqi@0 198 return new DOMSource(getOwnerDocument());
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 public Name createName(String localName, String prefix, String uri)
aoqi@0 202 throws SOAPException {
aoqi@0 203
aoqi@0 204 // validating parameters before passing them on
aoqi@0 205 // to make sure that the namespace specification rules are followed
aoqi@0 206
aoqi@0 207 // reserved xmlns prefix cannot be used.
aoqi@0 208 if ("xmlns".equals(prefix)) {
aoqi@0 209 log.severe("SAAJ0123.impl.no.reserved.xmlns");
aoqi@0 210 throw new SOAPExceptionImpl("Cannot declare reserved xmlns prefix");
aoqi@0 211 }
aoqi@0 212 // Qualified name cannot be xmlns.
aoqi@0 213 if ((prefix == null) && ("xmlns".equals(localName))) {
aoqi@0 214 log.severe("SAAJ0124.impl.qualified.name.cannot.be.xmlns");
aoqi@0 215 throw new SOAPExceptionImpl("Qualified name cannot be xmlns");
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 return NameImpl.create(localName, prefix, uri);
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 public Name createName(String localName, String prefix)
aoqi@0 222 throws SOAPException {
aoqi@0 223 String namespace = getNamespaceURI(prefix);
aoqi@0 224 if (namespace == null) {
aoqi@0 225 log.log(
aoqi@0 226 Level.SEVERE,
aoqi@0 227 "SAAJ0126.impl.cannot.locate.ns",
aoqi@0 228 new String[] { prefix });
aoqi@0 229 throw new SOAPExceptionImpl(
aoqi@0 230 "Unable to locate namespace for prefix " + prefix);
aoqi@0 231 }
aoqi@0 232 return NameImpl.create(localName, prefix, namespace);
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 public Name createName(String localName) throws SOAPException {
aoqi@0 236 return NameImpl.createFromUnqualifiedName(localName);
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 public void setOmitXmlDecl(String value) {
aoqi@0 240 this.omitXmlDecl = value;
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 public void setXmlDecl(String value) {
aoqi@0 244 this.xmlDecl = value;
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 private String getOmitXmlDecl() {
aoqi@0 248 return this.omitXmlDecl;
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 public void setCharsetEncoding(String value) {
aoqi@0 252 charset = value;
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 public void output(OutputStream out) throws IOException {
aoqi@0 256 try {
aoqi@0 257 Transformer transformer =
aoqi@0 258 EfficientStreamingTransformer.newTransformer();
aoqi@0 259
aoqi@0 260 transformer.setOutputProperty(
aoqi@0 261 OutputKeys.OMIT_XML_DECLARATION, "yes");
aoqi@0 262 /*omitXmlDecl);*/
aoqi@0 263 // no equivalent for "setExpandEmptyElements"
aoqi@0 264 transformer.setOutputProperty(
aoqi@0 265 OutputKeys.ENCODING,
aoqi@0 266 charset);
aoqi@0 267
aoqi@0 268 if (omitXmlDecl.equals("no") && xmlDecl == null) {
aoqi@0 269 xmlDecl = "<?xml version=\"" + getOwnerDocument().getXmlVersion() + "\" encoding=\"" +
aoqi@0 270 charset + "\" ?>";
aoqi@0 271 }
aoqi@0 272
aoqi@0 273 StreamResult result = new StreamResult(out);
aoqi@0 274 if (xmlDecl != null) {
aoqi@0 275 OutputStreamWriter writer = new OutputStreamWriter(out, charset);
aoqi@0 276 writer.write(xmlDecl);
aoqi@0 277 writer.flush();
aoqi@0 278 result = new StreamResult(writer);
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 if (log.isLoggable(Level.FINE)) {
aoqi@0 282 log.log(Level.FINE, "SAAJ0190.impl.set.xml.declaration",
aoqi@0 283 new String[] { omitXmlDecl });
aoqi@0 284 log.log(Level.FINE, "SAAJ0191.impl.set.encoding",
aoqi@0 285 new String[] { charset });
aoqi@0 286 }
aoqi@0 287
aoqi@0 288 //StreamResult result = new StreamResult(out);
aoqi@0 289 transformer.transform(getContent(), result);
aoqi@0 290 } catch (Exception ex) {
aoqi@0 291 throw new IOException(ex.getMessage());
aoqi@0 292 }
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 /**
aoqi@0 296 * Serialize to FI if boolean parameter set.
aoqi@0 297 */
aoqi@0 298 public void output(OutputStream out, boolean isFastInfoset)
aoqi@0 299 throws IOException
aoqi@0 300 {
aoqi@0 301 if (!isFastInfoset) {
aoqi@0 302 output(out);
aoqi@0 303 }
aoqi@0 304 else {
aoqi@0 305 try {
aoqi@0 306 // Run transform and generate FI output from content
aoqi@0 307 Source source = getContent();
aoqi@0 308 Transformer transformer = EfficientStreamingTransformer.newTransformer();
aoqi@0 309 transformer.transform(getContent(),
aoqi@0 310 FastInfosetReflection.FastInfosetResult_new(out));
aoqi@0 311 }
aoqi@0 312 catch (Exception ex) {
aoqi@0 313 throw new IOException(ex.getMessage());
aoqi@0 314 }
aoqi@0 315 }
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 // public void prettyPrint(OutputStream out) throws IOException {
aoqi@0 319 // if (getDocument() == null)
aoqi@0 320 // initDocument();
aoqi@0 321 //
aoqi@0 322 // OutputFormat format = OutputFormat.createPrettyPrint();
aoqi@0 323 //
aoqi@0 324 // format.setIndentSize(2);
aoqi@0 325 // format.setNewlines(true);
aoqi@0 326 // format.setTrimText(true);
aoqi@0 327 // format.setPadText(true);
aoqi@0 328 // format.setExpandEmptyElements(false);
aoqi@0 329 //
aoqi@0 330 // XMLWriter writer = new XMLWriter(out, format);
aoqi@0 331 // writer.write(getDocument());
aoqi@0 332 // }
aoqi@0 333 //
aoqi@0 334 // public void prettyPrint(Writer out) throws IOException {
aoqi@0 335 // if (getDocument() == null)
aoqi@0 336 // initDocument();
aoqi@0 337 //
aoqi@0 338 // OutputFormat format = OutputFormat.createPrettyPrint();
aoqi@0 339 //
aoqi@0 340 // format.setIndentSize(2);
aoqi@0 341 // format.setNewlines(true);
aoqi@0 342 // format.setTrimText(true);
aoqi@0 343 // format.setPadText(true);
aoqi@0 344 // format.setExpandEmptyElements(false);
aoqi@0 345 //
aoqi@0 346 // XMLWriter writer = new XMLWriter(out, format);
aoqi@0 347 // writer.write(getDocument());
aoqi@0 348 // }
aoqi@0 349
aoqi@0 350
aoqi@0 351 public SOAPElement setElementQName(QName newName) throws SOAPException {
aoqi@0 352 log.log(Level.SEVERE,
aoqi@0 353 "SAAJ0146.impl.invalid.name.change.requested",
aoqi@0 354 new Object[] {elementQName.getLocalPart(),
aoqi@0 355 newName.getLocalPart()});
aoqi@0 356 throw new SOAPException("Cannot change name for "
aoqi@0 357 + elementQName.getLocalPart() + " to "
aoqi@0 358 + newName.getLocalPart());
aoqi@0 359 }
aoqi@0 360 }

mercurial