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

Sun, 15 Dec 2013 23:35:45 +0100

author
mkos
date
Sun, 15 Dec 2013 23:35:45 +0100
changeset 494
2fcd3ddb57a6
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025152: Enhance activation set up
8028388: 9 jaxws tests failed in nightly build with java.lang.ClassCastException
Summary: fix also reviewed by Bill Shannon, Alexander Fomin
Reviewed-by: dfuchs, hawtin, mgrebac
Contributed-by: bill.shannon@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.messaging.saaj.soap.impl;
ohair@286 27
ohair@286 28 import java.util.Iterator;
ohair@286 29 import java.util.Locale;
ohair@286 30 import java.util.logging.Level;
ohair@286 31
ohair@286 32 import javax.xml.namespace.QName;
ohair@286 33 import javax.xml.soap.*;
ohair@286 34 import javax.xml.parsers.DocumentBuilder;
ohair@286 35 import javax.xml.parsers.DocumentBuilderFactory;
ohair@286 36
ohair@286 37 import org.w3c.dom.*;
ohair@286 38
ohair@286 39 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
ohair@286 40 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
ohair@286 41 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
ohair@286 42 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
ohair@286 43
ohair@286 44 /**
ohair@286 45 * The implementation of SOAP-ENV:BODY or the SOAPBody abstraction.
ohair@286 46 *
ohair@286 47 * @author Anil Vijendran (anil@sun.com)
ohair@286 48 */
ohair@286 49 public abstract class BodyImpl extends ElementImpl implements SOAPBody {
ohair@286 50 private SOAPFault fault;
ohair@286 51
ohair@286 52 protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) {
ohair@286 53 super(ownerDoc, bodyName);
ohair@286 54 }
ohair@286 55
ohair@286 56 protected abstract NameImpl getFaultName(String name);
ohair@286 57 protected abstract boolean isFault(SOAPElement child);
ohair@286 58 protected abstract SOAPBodyElement createBodyElement(Name name);
ohair@286 59 protected abstract SOAPBodyElement createBodyElement(QName name);
ohair@286 60 protected abstract SOAPFault createFaultElement();
ohair@286 61 protected abstract QName getDefaultFaultCode();
ohair@286 62
ohair@286 63 public SOAPFault addFault() throws SOAPException {
ohair@286 64 if (hasFault()) {
ohair@286 65 log.severe("SAAJ0110.impl.fault.already.exists");
ohair@286 66 throw new SOAPExceptionImpl("Error: Fault already exists");
ohair@286 67 }
ohair@286 68
ohair@286 69 fault = createFaultElement();
ohair@286 70
ohair@286 71 addNode(fault);
ohair@286 72
ohair@286 73 fault.setFaultCode(getDefaultFaultCode());
ohair@286 74 fault.setFaultString("Fault string, and possibly fault code, not set");
ohair@286 75
ohair@286 76 return fault;
ohair@286 77 }
ohair@286 78
ohair@286 79 public SOAPFault addFault(
ohair@286 80 Name faultCode,
ohair@286 81 String faultString,
ohair@286 82 Locale locale)
ohair@286 83 throws SOAPException {
ohair@286 84
ohair@286 85 SOAPFault fault = addFault();
ohair@286 86 fault.setFaultCode(faultCode);
ohair@286 87 fault.setFaultString(faultString, locale);
ohair@286 88 return fault;
ohair@286 89 }
ohair@286 90
ohair@286 91 public SOAPFault addFault(
ohair@286 92 QName faultCode,
ohair@286 93 String faultString,
ohair@286 94 Locale locale)
ohair@286 95 throws SOAPException {
ohair@286 96
ohair@286 97 SOAPFault fault = addFault();
ohair@286 98 fault.setFaultCode(faultCode);
ohair@286 99 fault.setFaultString(faultString, locale);
ohair@286 100 return fault;
ohair@286 101 }
ohair@286 102
ohair@286 103 public SOAPFault addFault(Name faultCode, String faultString)
ohair@286 104 throws SOAPException {
ohair@286 105
ohair@286 106 SOAPFault fault = addFault();
ohair@286 107 fault.setFaultCode(faultCode);
ohair@286 108 fault.setFaultString(faultString);
ohair@286 109 return fault;
ohair@286 110 }
ohair@286 111
ohair@286 112 public SOAPFault addFault(QName faultCode, String faultString)
ohair@286 113 throws SOAPException {
ohair@286 114
ohair@286 115 SOAPFault fault = addFault();
ohair@286 116 fault.setFaultCode(faultCode);
ohair@286 117 fault.setFaultString(faultString);
ohair@286 118 return fault;
ohair@286 119 }
ohair@286 120
ohair@286 121 void initializeFault() {
ohair@286 122 FaultImpl flt = (FaultImpl) findFault();
ohair@286 123 fault = flt;
ohair@286 124 }
ohair@286 125
ohair@286 126 protected SOAPElement findFault() {
ohair@286 127 Iterator eachChild = getChildElementNodes();
ohair@286 128 while (eachChild.hasNext()) {
ohair@286 129 SOAPElement child = (SOAPElement) eachChild.next();
ohair@286 130 if (isFault(child)) {
ohair@286 131 return child;
ohair@286 132 }
ohair@286 133 }
ohair@286 134
ohair@286 135 return null;
ohair@286 136 }
ohair@286 137
ohair@286 138 public boolean hasFault() {
ohair@286 139 initializeFault();
ohair@286 140 return fault != null;
ohair@286 141 }
ohair@286 142
ohair@286 143 public SOAPFault getFault() {
ohair@286 144 if (hasFault())
ohair@286 145 return fault;
ohair@286 146 return null;
ohair@286 147 }
ohair@286 148
ohair@286 149 public SOAPBodyElement addBodyElement(Name name) throws SOAPException {
ohair@286 150 SOAPBodyElement newBodyElement =
ohair@286 151 (SOAPBodyElement) ElementFactory.createNamedElement(
ohair@286 152 ((SOAPDocument) getOwnerDocument()).getDocument(),
ohair@286 153 name.getLocalName(),
ohair@286 154 name.getPrefix(),
ohair@286 155 name.getURI());
ohair@286 156 if (newBodyElement == null) {
ohair@286 157 newBodyElement = createBodyElement(name);
ohair@286 158 }
ohair@286 159 addNode(newBodyElement);
ohair@286 160 return newBodyElement;
ohair@286 161 }
ohair@286 162
ohair@286 163 public SOAPBodyElement addBodyElement(QName qname) throws SOAPException {
ohair@286 164 SOAPBodyElement newBodyElement =
ohair@286 165 (SOAPBodyElement) ElementFactory.createNamedElement(
ohair@286 166 ((SOAPDocument) getOwnerDocument()).getDocument(),
ohair@286 167 qname.getLocalPart(),
ohair@286 168 qname.getPrefix(),
ohair@286 169 qname.getNamespaceURI());
ohair@286 170 if (newBodyElement == null) {
ohair@286 171 newBodyElement = createBodyElement(qname);
ohair@286 172 }
ohair@286 173 addNode(newBodyElement);
ohair@286 174 return newBodyElement;
ohair@286 175 }
ohair@286 176
ohair@286 177 public void setParentElement(SOAPElement element) throws SOAPException {
ohair@286 178
ohair@286 179 if (!(element instanceof SOAPEnvelope)) {
ohair@286 180 log.severe("SAAJ0111.impl.body.parent.must.be.envelope");
ohair@286 181 throw new SOAPException("Parent of SOAPBody has to be a SOAPEnvelope");
ohair@286 182 }
ohair@286 183 super.setParentElement(element);
ohair@286 184 }
ohair@286 185
ohair@286 186 protected SOAPElement addElement(Name name) throws SOAPException {
ohair@286 187 return addBodyElement(name);
ohair@286 188 }
ohair@286 189
ohair@286 190 protected SOAPElement addElement(QName name) throws SOAPException {
ohair@286 191 return addBodyElement(name);
ohair@286 192 }
ohair@286 193
ohair@286 194 // public Node insertBefore(Node newElement, Node ref) throws DOMException {
ohair@286 195 // if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
ohair@286 196 // newElement = new ElementWrapper((ElementImpl) newElement);
ohair@286 197 // }
ohair@286 198 // return super.insertBefore(newElement, ref);
ohair@286 199 // }
ohair@286 200 //
ohair@286 201 // public Node replaceChild(Node newElement, Node ref) throws DOMException {
ohair@286 202 // if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
ohair@286 203 // newElement = new ElementWrapper((ElementImpl) newElement);
ohair@286 204 // }
ohair@286 205 // return super.replaceChild(newElement, ref);
ohair@286 206 // }
ohair@286 207
ohair@286 208 public SOAPBodyElement addDocument(Document document)
ohair@286 209 throws SOAPException {
ohair@286 210 /*
ohair@286 211
ohair@286 212 Element rootNode =
ohair@286 213 document.getDocumentElement();
ohair@286 214 // Causes all deferred nodes to be inflated
ohair@286 215 rootNode.normalize();
ohair@286 216 adoptElement(rootNode);
ohair@286 217 SOAPBodyElement bodyElement = (SOAPBodyElement) convertToSoapElement(rootNode);
ohair@286 218 addNode(bodyElement);
ohair@286 219 return bodyElement;
ohair@286 220 */
ohair@286 221 ///*
ohair@286 222 SOAPBodyElement newBodyElement = null;
ohair@286 223 DocumentFragment docFrag = document.createDocumentFragment();
ohair@286 224 Element rootElement = document.getDocumentElement();
ohair@286 225 if(rootElement != null) {
ohair@286 226 docFrag.appendChild(rootElement);
ohair@286 227
ohair@286 228 Document ownerDoc = getOwnerDocument();
ohair@286 229 // This copies the whole tree which could be very big so it's slow.
ohair@286 230 // However, it does have the advantage of actually working.
ohair@286 231 org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
ohair@286 232 // Adding replacingNode at the last of the children list of body
ohair@286 233 addNode(replacingNode);
ohair@286 234 Iterator i =
ohair@286 235 getChildElements(NameImpl.copyElementName(rootElement));
ohair@286 236 // Return the child element with the required name which is at the
ohair@286 237 // end of the list
ohair@286 238 while(i.hasNext())
ohair@286 239 newBodyElement = (SOAPBodyElement) i.next();
ohair@286 240 }
ohair@286 241 return newBodyElement;
ohair@286 242 //*/
ohair@286 243 }
ohair@286 244
ohair@286 245 protected SOAPElement convertToSoapElement(Element element) {
ohair@286 246 if ((element instanceof SOAPBodyElement) &&
ohair@286 247 //this check is required because ElementImpl currently
ohair@286 248 // implements SOAPBodyElement
ohair@286 249 !(element.getClass().equals(ElementImpl.class))) {
ohair@286 250 return (SOAPElement) element;
ohair@286 251 } else {
ohair@286 252 return replaceElementWithSOAPElement(
ohair@286 253 element,
ohair@286 254 (ElementImpl) createBodyElement(NameImpl
ohair@286 255 .copyElementName(element)));
ohair@286 256 }
ohair@286 257 }
ohair@286 258
ohair@286 259 public SOAPElement setElementQName(QName newName) throws SOAPException {
ohair@286 260 log.log(Level.SEVERE,
ohair@286 261 "SAAJ0146.impl.invalid.name.change.requested",
ohair@286 262 new Object[] {elementQName.getLocalPart(),
ohair@286 263 newName.getLocalPart()});
ohair@286 264 throw new SOAPException("Cannot change name for "
ohair@286 265 + elementQName.getLocalPart() + " to "
ohair@286 266 + newName.getLocalPart());
ohair@286 267 }
ohair@286 268
ohair@286 269 public Document extractContentAsDocument() throws SOAPException {
ohair@286 270
ohair@286 271 Iterator eachChild = getChildElements();
ohair@286 272 javax.xml.soap.Node firstBodyElement = null;
ohair@286 273
ohair@286 274 while (eachChild.hasNext() &&
ohair@286 275 !(firstBodyElement instanceof SOAPElement))
ohair@286 276 firstBodyElement = (javax.xml.soap.Node) eachChild.next();
ohair@286 277
ohair@286 278 boolean exactlyOneChildElement = true;
ohair@286 279 if (firstBodyElement == null)
ohair@286 280 exactlyOneChildElement = false;
ohair@286 281 else {
ohair@286 282 for (org.w3c.dom.Node node = firstBodyElement.getNextSibling();
ohair@286 283 node != null;
ohair@286 284 node = node.getNextSibling()) {
ohair@286 285
ohair@286 286 if (node instanceof Element) {
ohair@286 287 exactlyOneChildElement = false;
ohair@286 288 break;
ohair@286 289 }
ohair@286 290 }
ohair@286 291 }
ohair@286 292
ohair@286 293 if(!exactlyOneChildElement) {
ohair@286 294 log.log(Level.SEVERE,
ohair@286 295 "SAAJ0250.impl.body.should.have.exactly.one.child");
ohair@286 296 throw new SOAPException("Cannot extract Document from body");
ohair@286 297 }
ohair@286 298
ohair@286 299 Document document = null;
ohair@286 300 try {
ohair@286 301 DocumentBuilderFactory factory =
ohair@286 302 new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl();
ohair@286 303 factory.setNamespaceAware(true);
ohair@286 304 DocumentBuilder builder = factory.newDocumentBuilder();
ohair@286 305 document = builder.newDocument();
ohair@286 306
ohair@286 307 Element rootElement = (Element) document.importNode(
ohair@286 308 firstBodyElement,
ohair@286 309 true);
ohair@286 310
ohair@286 311 document.appendChild(rootElement);
ohair@286 312
ohair@286 313 } catch(Exception e) {
ohair@286 314 log.log(Level.SEVERE,
ohair@286 315 "SAAJ0251.impl.cannot.extract.document.from.body");
ohair@286 316 throw new SOAPExceptionImpl(
ohair@286 317 "Unable to extract Document from body", e);
ohair@286 318 }
ohair@286 319
ohair@286 320 firstBodyElement.detachNode();
ohair@286 321
ohair@286 322 return document;
ohair@286 323 }
ohair@286 324
ohair@286 325 }

mercurial