src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/BodyImpl.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.util.Iterator;
aoqi@0 29 import java.util.Locale;
aoqi@0 30 import java.util.logging.Level;
aoqi@0 31
aoqi@0 32 import javax.xml.namespace.QName;
aoqi@0 33 import javax.xml.soap.*;
aoqi@0 34 import javax.xml.parsers.DocumentBuilder;
aoqi@0 35 import javax.xml.parsers.DocumentBuilderFactory;
aoqi@0 36
aoqi@0 37 import org.w3c.dom.*;
aoqi@0 38
aoqi@0 39 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
aoqi@0 40 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
aoqi@0 41 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
aoqi@0 42 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
aoqi@0 43
aoqi@0 44 /**
aoqi@0 45 * The implementation of SOAP-ENV:BODY or the SOAPBody abstraction.
aoqi@0 46 *
aoqi@0 47 * @author Anil Vijendran (anil@sun.com)
aoqi@0 48 */
aoqi@0 49 public abstract class BodyImpl extends ElementImpl implements SOAPBody {
aoqi@0 50 private SOAPFault fault;
aoqi@0 51
aoqi@0 52 protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) {
aoqi@0 53 super(ownerDoc, bodyName);
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 protected abstract NameImpl getFaultName(String name);
aoqi@0 57 protected abstract boolean isFault(SOAPElement child);
aoqi@0 58 protected abstract SOAPBodyElement createBodyElement(Name name);
aoqi@0 59 protected abstract SOAPBodyElement createBodyElement(QName name);
aoqi@0 60 protected abstract SOAPFault createFaultElement();
aoqi@0 61 protected abstract QName getDefaultFaultCode();
aoqi@0 62
aoqi@0 63 public SOAPFault addFault() throws SOAPException {
aoqi@0 64 if (hasFault()) {
aoqi@0 65 log.severe("SAAJ0110.impl.fault.already.exists");
aoqi@0 66 throw new SOAPExceptionImpl("Error: Fault already exists");
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 fault = createFaultElement();
aoqi@0 70
aoqi@0 71 addNode(fault);
aoqi@0 72
aoqi@0 73 fault.setFaultCode(getDefaultFaultCode());
aoqi@0 74 fault.setFaultString("Fault string, and possibly fault code, not set");
aoqi@0 75
aoqi@0 76 return fault;
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 public SOAPFault addFault(
aoqi@0 80 Name faultCode,
aoqi@0 81 String faultString,
aoqi@0 82 Locale locale)
aoqi@0 83 throws SOAPException {
aoqi@0 84
aoqi@0 85 SOAPFault fault = addFault();
aoqi@0 86 fault.setFaultCode(faultCode);
aoqi@0 87 fault.setFaultString(faultString, locale);
aoqi@0 88 return fault;
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 public SOAPFault addFault(
aoqi@0 92 QName faultCode,
aoqi@0 93 String faultString,
aoqi@0 94 Locale locale)
aoqi@0 95 throws SOAPException {
aoqi@0 96
aoqi@0 97 SOAPFault fault = addFault();
aoqi@0 98 fault.setFaultCode(faultCode);
aoqi@0 99 fault.setFaultString(faultString, locale);
aoqi@0 100 return fault;
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 public SOAPFault addFault(Name faultCode, String faultString)
aoqi@0 104 throws SOAPException {
aoqi@0 105
aoqi@0 106 SOAPFault fault = addFault();
aoqi@0 107 fault.setFaultCode(faultCode);
aoqi@0 108 fault.setFaultString(faultString);
aoqi@0 109 return fault;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 public SOAPFault addFault(QName faultCode, String faultString)
aoqi@0 113 throws SOAPException {
aoqi@0 114
aoqi@0 115 SOAPFault fault = addFault();
aoqi@0 116 fault.setFaultCode(faultCode);
aoqi@0 117 fault.setFaultString(faultString);
aoqi@0 118 return fault;
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 void initializeFault() {
aoqi@0 122 FaultImpl flt = (FaultImpl) findFault();
aoqi@0 123 fault = flt;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 protected SOAPElement findFault() {
aoqi@0 127 Iterator eachChild = getChildElementNodes();
aoqi@0 128 while (eachChild.hasNext()) {
aoqi@0 129 SOAPElement child = (SOAPElement) eachChild.next();
aoqi@0 130 if (isFault(child)) {
aoqi@0 131 return child;
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 return null;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public boolean hasFault() {
aoqi@0 139 initializeFault();
aoqi@0 140 return fault != null;
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 public SOAPFault getFault() {
aoqi@0 144 if (hasFault())
aoqi@0 145 return fault;
aoqi@0 146 return null;
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 public SOAPBodyElement addBodyElement(Name name) throws SOAPException {
aoqi@0 150 SOAPBodyElement newBodyElement =
aoqi@0 151 (SOAPBodyElement) ElementFactory.createNamedElement(
aoqi@0 152 ((SOAPDocument) getOwnerDocument()).getDocument(),
aoqi@0 153 name.getLocalName(),
aoqi@0 154 name.getPrefix(),
aoqi@0 155 name.getURI());
aoqi@0 156 if (newBodyElement == null) {
aoqi@0 157 newBodyElement = createBodyElement(name);
aoqi@0 158 }
aoqi@0 159 addNode(newBodyElement);
aoqi@0 160 return newBodyElement;
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 public SOAPBodyElement addBodyElement(QName qname) throws SOAPException {
aoqi@0 164 SOAPBodyElement newBodyElement =
aoqi@0 165 (SOAPBodyElement) ElementFactory.createNamedElement(
aoqi@0 166 ((SOAPDocument) getOwnerDocument()).getDocument(),
aoqi@0 167 qname.getLocalPart(),
aoqi@0 168 qname.getPrefix(),
aoqi@0 169 qname.getNamespaceURI());
aoqi@0 170 if (newBodyElement == null) {
aoqi@0 171 newBodyElement = createBodyElement(qname);
aoqi@0 172 }
aoqi@0 173 addNode(newBodyElement);
aoqi@0 174 return newBodyElement;
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 public void setParentElement(SOAPElement element) throws SOAPException {
aoqi@0 178
aoqi@0 179 if (!(element instanceof SOAPEnvelope)) {
aoqi@0 180 log.severe("SAAJ0111.impl.body.parent.must.be.envelope");
aoqi@0 181 throw new SOAPException("Parent of SOAPBody has to be a SOAPEnvelope");
aoqi@0 182 }
aoqi@0 183 super.setParentElement(element);
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 protected SOAPElement addElement(Name name) throws SOAPException {
aoqi@0 187 return addBodyElement(name);
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 protected SOAPElement addElement(QName name) throws SOAPException {
aoqi@0 191 return addBodyElement(name);
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 // public Node insertBefore(Node newElement, Node ref) throws DOMException {
aoqi@0 195 // if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
aoqi@0 196 // newElement = new ElementWrapper((ElementImpl) newElement);
aoqi@0 197 // }
aoqi@0 198 // return super.insertBefore(newElement, ref);
aoqi@0 199 // }
aoqi@0 200 //
aoqi@0 201 // public Node replaceChild(Node newElement, Node ref) throws DOMException {
aoqi@0 202 // if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
aoqi@0 203 // newElement = new ElementWrapper((ElementImpl) newElement);
aoqi@0 204 // }
aoqi@0 205 // return super.replaceChild(newElement, ref);
aoqi@0 206 // }
aoqi@0 207
aoqi@0 208 public SOAPBodyElement addDocument(Document document)
aoqi@0 209 throws SOAPException {
aoqi@0 210 /*
aoqi@0 211
aoqi@0 212 Element rootNode =
aoqi@0 213 document.getDocumentElement();
aoqi@0 214 // Causes all deferred nodes to be inflated
aoqi@0 215 rootNode.normalize();
aoqi@0 216 adoptElement(rootNode);
aoqi@0 217 SOAPBodyElement bodyElement = (SOAPBodyElement) convertToSoapElement(rootNode);
aoqi@0 218 addNode(bodyElement);
aoqi@0 219 return bodyElement;
aoqi@0 220 */
aoqi@0 221 ///*
aoqi@0 222 SOAPBodyElement newBodyElement = null;
aoqi@0 223 DocumentFragment docFrag = document.createDocumentFragment();
aoqi@0 224 Element rootElement = document.getDocumentElement();
aoqi@0 225 if(rootElement != null) {
aoqi@0 226 docFrag.appendChild(rootElement);
aoqi@0 227
aoqi@0 228 Document ownerDoc = getOwnerDocument();
aoqi@0 229 // This copies the whole tree which could be very big so it's slow.
aoqi@0 230 // However, it does have the advantage of actually working.
aoqi@0 231 org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
aoqi@0 232 // Adding replacingNode at the last of the children list of body
aoqi@0 233 addNode(replacingNode);
aoqi@0 234 Iterator i =
aoqi@0 235 getChildElements(NameImpl.copyElementName(rootElement));
aoqi@0 236 // Return the child element with the required name which is at the
aoqi@0 237 // end of the list
aoqi@0 238 while(i.hasNext())
aoqi@0 239 newBodyElement = (SOAPBodyElement) i.next();
aoqi@0 240 }
aoqi@0 241 return newBodyElement;
aoqi@0 242 //*/
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 protected SOAPElement convertToSoapElement(Element element) {
aoqi@0 246 if ((element instanceof SOAPBodyElement) &&
aoqi@0 247 //this check is required because ElementImpl currently
aoqi@0 248 // implements SOAPBodyElement
aoqi@0 249 !(element.getClass().equals(ElementImpl.class))) {
aoqi@0 250 return (SOAPElement) element;
aoqi@0 251 } else {
aoqi@0 252 return replaceElementWithSOAPElement(
aoqi@0 253 element,
aoqi@0 254 (ElementImpl) createBodyElement(NameImpl
aoqi@0 255 .copyElementName(element)));
aoqi@0 256 }
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 public SOAPElement setElementQName(QName newName) throws SOAPException {
aoqi@0 260 log.log(Level.SEVERE,
aoqi@0 261 "SAAJ0146.impl.invalid.name.change.requested",
aoqi@0 262 new Object[] {elementQName.getLocalPart(),
aoqi@0 263 newName.getLocalPart()});
aoqi@0 264 throw new SOAPException("Cannot change name for "
aoqi@0 265 + elementQName.getLocalPart() + " to "
aoqi@0 266 + newName.getLocalPart());
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 public Document extractContentAsDocument() throws SOAPException {
aoqi@0 270
aoqi@0 271 Iterator eachChild = getChildElements();
aoqi@0 272 javax.xml.soap.Node firstBodyElement = null;
aoqi@0 273
aoqi@0 274 while (eachChild.hasNext() &&
aoqi@0 275 !(firstBodyElement instanceof SOAPElement))
aoqi@0 276 firstBodyElement = (javax.xml.soap.Node) eachChild.next();
aoqi@0 277
aoqi@0 278 boolean exactlyOneChildElement = true;
aoqi@0 279 if (firstBodyElement == null)
aoqi@0 280 exactlyOneChildElement = false;
aoqi@0 281 else {
aoqi@0 282 for (org.w3c.dom.Node node = firstBodyElement.getNextSibling();
aoqi@0 283 node != null;
aoqi@0 284 node = node.getNextSibling()) {
aoqi@0 285
aoqi@0 286 if (node instanceof Element) {
aoqi@0 287 exactlyOneChildElement = false;
aoqi@0 288 break;
aoqi@0 289 }
aoqi@0 290 }
aoqi@0 291 }
aoqi@0 292
aoqi@0 293 if(!exactlyOneChildElement) {
aoqi@0 294 log.log(Level.SEVERE,
aoqi@0 295 "SAAJ0250.impl.body.should.have.exactly.one.child");
aoqi@0 296 throw new SOAPException("Cannot extract Document from body");
aoqi@0 297 }
aoqi@0 298
aoqi@0 299 Document document = null;
aoqi@0 300 try {
aoqi@0 301 DocumentBuilderFactory factory =
aoqi@0 302 new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl();
aoqi@0 303 factory.setNamespaceAware(true);
aoqi@0 304 DocumentBuilder builder = factory.newDocumentBuilder();
aoqi@0 305 document = builder.newDocument();
aoqi@0 306
aoqi@0 307 Element rootElement = (Element) document.importNode(
aoqi@0 308 firstBodyElement,
aoqi@0 309 true);
aoqi@0 310
aoqi@0 311 document.appendChild(rootElement);
aoqi@0 312
aoqi@0 313 } catch(Exception e) {
aoqi@0 314 log.log(Level.SEVERE,
aoqi@0 315 "SAAJ0251.impl.cannot.extract.document.from.body");
aoqi@0 316 throw new SOAPExceptionImpl(
aoqi@0 317 "Unable to extract Document from body", e);
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 firstBodyElement.detachNode();
aoqi@0 321
aoqi@0 322 return document;
aoqi@0 323 }
aoqi@0 324
aoqi@0 325 }

mercurial