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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/BodyImpl.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,325 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.messaging.saaj.soap.impl;
    1.30 +
    1.31 +import java.util.Iterator;
    1.32 +import java.util.Locale;
    1.33 +import java.util.logging.Level;
    1.34 +
    1.35 +import javax.xml.namespace.QName;
    1.36 +import javax.xml.soap.*;
    1.37 +import javax.xml.parsers.DocumentBuilder;
    1.38 +import javax.xml.parsers.DocumentBuilderFactory;
    1.39 +
    1.40 +import org.w3c.dom.*;
    1.41 +
    1.42 +import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
    1.43 +import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
    1.44 +import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
    1.45 +import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
    1.46 +
    1.47 +/**
    1.48 + * The implementation of SOAP-ENV:BODY or the SOAPBody abstraction.
    1.49 + *
    1.50 + * @author Anil Vijendran (anil@sun.com)
    1.51 + */
    1.52 +public abstract class BodyImpl extends ElementImpl implements SOAPBody {
    1.53 +    private SOAPFault fault;
    1.54 +
    1.55 +    protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) {
    1.56 +        super(ownerDoc, bodyName);
    1.57 +    }
    1.58 +
    1.59 +    protected abstract NameImpl getFaultName(String name);
    1.60 +    protected abstract boolean isFault(SOAPElement child);
    1.61 +    protected abstract SOAPBodyElement createBodyElement(Name name);
    1.62 +    protected abstract SOAPBodyElement createBodyElement(QName name);
    1.63 +    protected abstract SOAPFault createFaultElement();
    1.64 +    protected abstract QName getDefaultFaultCode();
    1.65 +
    1.66 +    public SOAPFault addFault() throws SOAPException {
    1.67 +        if (hasFault()) {
    1.68 +            log.severe("SAAJ0110.impl.fault.already.exists");
    1.69 +            throw new SOAPExceptionImpl("Error: Fault already exists");
    1.70 +        }
    1.71 +
    1.72 +        fault = createFaultElement();
    1.73 +
    1.74 +        addNode(fault);
    1.75 +
    1.76 +        fault.setFaultCode(getDefaultFaultCode());
    1.77 +        fault.setFaultString("Fault string, and possibly fault code, not set");
    1.78 +
    1.79 +        return fault;
    1.80 +    }
    1.81 +
    1.82 +    public SOAPFault addFault(
    1.83 +        Name faultCode,
    1.84 +        String faultString,
    1.85 +        Locale locale)
    1.86 +        throws SOAPException {
    1.87 +
    1.88 +        SOAPFault fault = addFault();
    1.89 +        fault.setFaultCode(faultCode);
    1.90 +        fault.setFaultString(faultString, locale);
    1.91 +        return fault;
    1.92 +    }
    1.93 +
    1.94 +   public SOAPFault addFault(
    1.95 +        QName faultCode,
    1.96 +        String faultString,
    1.97 +        Locale locale)
    1.98 +        throws SOAPException {
    1.99 +
   1.100 +        SOAPFault fault = addFault();
   1.101 +        fault.setFaultCode(faultCode);
   1.102 +        fault.setFaultString(faultString, locale);
   1.103 +        return fault;
   1.104 +    }
   1.105 +
   1.106 +    public SOAPFault addFault(Name faultCode, String faultString)
   1.107 +        throws SOAPException {
   1.108 +
   1.109 +        SOAPFault fault = addFault();
   1.110 +        fault.setFaultCode(faultCode);
   1.111 +        fault.setFaultString(faultString);
   1.112 +        return fault;
   1.113 +    }
   1.114 +
   1.115 +    public SOAPFault addFault(QName faultCode, String faultString)
   1.116 +        throws SOAPException {
   1.117 +
   1.118 +        SOAPFault fault = addFault();
   1.119 +        fault.setFaultCode(faultCode);
   1.120 +        fault.setFaultString(faultString);
   1.121 +        return fault;
   1.122 +    }
   1.123 +
   1.124 +    void initializeFault() {
   1.125 +        FaultImpl flt = (FaultImpl) findFault();
   1.126 +        fault = flt;
   1.127 +    }
   1.128 +
   1.129 +    protected SOAPElement findFault() {
   1.130 +        Iterator eachChild = getChildElementNodes();
   1.131 +        while (eachChild.hasNext()) {
   1.132 +            SOAPElement child = (SOAPElement) eachChild.next();
   1.133 +            if (isFault(child)) {
   1.134 +                return child;
   1.135 +            }
   1.136 +        }
   1.137 +
   1.138 +        return null;
   1.139 +    }
   1.140 +
   1.141 +    public boolean hasFault() {
   1.142 +        initializeFault();
   1.143 +        return fault != null;
   1.144 +    }
   1.145 +
   1.146 +    public SOAPFault getFault() {
   1.147 +        if (hasFault())
   1.148 +            return fault;
   1.149 +        return null;
   1.150 +    }
   1.151 +
   1.152 +    public SOAPBodyElement addBodyElement(Name name) throws SOAPException {
   1.153 +        SOAPBodyElement newBodyElement =
   1.154 +            (SOAPBodyElement) ElementFactory.createNamedElement(
   1.155 +                ((SOAPDocument) getOwnerDocument()).getDocument(),
   1.156 +                name.getLocalName(),
   1.157 +                name.getPrefix(),
   1.158 +                name.getURI());
   1.159 +        if (newBodyElement == null) {
   1.160 +            newBodyElement = createBodyElement(name);
   1.161 +        }
   1.162 +        addNode(newBodyElement);
   1.163 +        return newBodyElement;
   1.164 +    }
   1.165 +
   1.166 +    public SOAPBodyElement addBodyElement(QName qname) throws SOAPException {
   1.167 +        SOAPBodyElement newBodyElement =
   1.168 +            (SOAPBodyElement) ElementFactory.createNamedElement(
   1.169 +                ((SOAPDocument) getOwnerDocument()).getDocument(),
   1.170 +                qname.getLocalPart(),
   1.171 +                qname.getPrefix(),
   1.172 +                qname.getNamespaceURI());
   1.173 +        if (newBodyElement == null) {
   1.174 +            newBodyElement = createBodyElement(qname);
   1.175 +        }
   1.176 +        addNode(newBodyElement);
   1.177 +        return newBodyElement;
   1.178 +    }
   1.179 +
   1.180 +    public void setParentElement(SOAPElement element) throws SOAPException {
   1.181 +
   1.182 +        if (!(element instanceof SOAPEnvelope)) {
   1.183 +            log.severe("SAAJ0111.impl.body.parent.must.be.envelope");
   1.184 +            throw new SOAPException("Parent of SOAPBody has to be a SOAPEnvelope");
   1.185 +        }
   1.186 +        super.setParentElement(element);
   1.187 +    }
   1.188 +
   1.189 +    protected SOAPElement addElement(Name name) throws SOAPException {
   1.190 +        return addBodyElement(name);
   1.191 +    }
   1.192 +
   1.193 +    protected SOAPElement addElement(QName name) throws SOAPException {
   1.194 +        return addBodyElement(name);
   1.195 +    }
   1.196 +
   1.197 +    //    public Node insertBefore(Node newElement, Node ref) throws DOMException {
   1.198 +    //        if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
   1.199 +    //            newElement = new ElementWrapper((ElementImpl) newElement);
   1.200 +    //        }
   1.201 +    //        return super.insertBefore(newElement, ref);
   1.202 +    //    }
   1.203 +    //
   1.204 +    //    public Node replaceChild(Node newElement, Node ref) throws DOMException {
   1.205 +    //        if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
   1.206 +    //            newElement = new ElementWrapper((ElementImpl) newElement);
   1.207 +    //        }
   1.208 +    //        return super.replaceChild(newElement, ref);
   1.209 +    //    }
   1.210 +
   1.211 +    public SOAPBodyElement addDocument(Document document)
   1.212 +        throws SOAPException {
   1.213 +        /*
   1.214 +
   1.215 +                Element rootNode =
   1.216 +                    document.getDocumentElement();
   1.217 +                // Causes all deferred nodes to be inflated
   1.218 +                rootNode.normalize();
   1.219 +                adoptElement(rootNode);
   1.220 +                SOAPBodyElement bodyElement = (SOAPBodyElement) convertToSoapElement(rootNode);
   1.221 +                addNode(bodyElement);
   1.222 +                return bodyElement;
   1.223 +        */
   1.224 +        ///*
   1.225 +        SOAPBodyElement newBodyElement = null;
   1.226 +        DocumentFragment docFrag = document.createDocumentFragment();
   1.227 +        Element rootElement = document.getDocumentElement();
   1.228 +        if(rootElement != null) {
   1.229 +            docFrag.appendChild(rootElement);
   1.230 +
   1.231 +            Document ownerDoc = getOwnerDocument();
   1.232 +            // This copies the whole tree which could be very big so it's slow.
   1.233 +            // However, it does have the advantage of actually working.
   1.234 +            org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
   1.235 +            // Adding replacingNode at the last of the children list of body
   1.236 +            addNode(replacingNode);
   1.237 +            Iterator i =
   1.238 +                getChildElements(NameImpl.copyElementName(rootElement));
   1.239 +            // Return the child element with the required name which is at the
   1.240 +            // end of the list
   1.241 +            while(i.hasNext())
   1.242 +                newBodyElement = (SOAPBodyElement) i.next();
   1.243 +        }
   1.244 +        return newBodyElement;
   1.245 +        //*/
   1.246 +    }
   1.247 +
   1.248 +    protected SOAPElement convertToSoapElement(Element element) {
   1.249 +        if ((element instanceof SOAPBodyElement) &&
   1.250 +            //this check is required because ElementImpl currently
   1.251 +            // implements SOAPBodyElement
   1.252 +            !(element.getClass().equals(ElementImpl.class))) {
   1.253 +            return (SOAPElement) element;
   1.254 +        } else {
   1.255 +            return replaceElementWithSOAPElement(
   1.256 +                element,
   1.257 +                (ElementImpl) createBodyElement(NameImpl
   1.258 +                    .copyElementName(element)));
   1.259 +        }
   1.260 +    }
   1.261 +
   1.262 +    public SOAPElement setElementQName(QName newName) throws SOAPException {
   1.263 +        log.log(Level.SEVERE,
   1.264 +                "SAAJ0146.impl.invalid.name.change.requested",
   1.265 +                new Object[] {elementQName.getLocalPart(),
   1.266 +                              newName.getLocalPart()});
   1.267 +        throw new SOAPException("Cannot change name for "
   1.268 +                                + elementQName.getLocalPart() + " to "
   1.269 +                                + newName.getLocalPart());
   1.270 +    }
   1.271 +
   1.272 +    public Document extractContentAsDocument() throws SOAPException {
   1.273 +
   1.274 +        Iterator eachChild = getChildElements();
   1.275 +        javax.xml.soap.Node firstBodyElement = null;
   1.276 +
   1.277 +        while (eachChild.hasNext() &&
   1.278 +               !(firstBodyElement instanceof SOAPElement))
   1.279 +            firstBodyElement = (javax.xml.soap.Node) eachChild.next();
   1.280 +
   1.281 +        boolean exactlyOneChildElement = true;
   1.282 +        if (firstBodyElement == null)
   1.283 +            exactlyOneChildElement = false;
   1.284 +        else {
   1.285 +            for (org.w3c.dom.Node node = firstBodyElement.getNextSibling();
   1.286 +                 node != null;
   1.287 +                 node = node.getNextSibling()) {
   1.288 +
   1.289 +                if (node instanceof Element) {
   1.290 +                    exactlyOneChildElement = false;
   1.291 +                    break;
   1.292 +                }
   1.293 +            }
   1.294 +        }
   1.295 +
   1.296 +        if(!exactlyOneChildElement) {
   1.297 +            log.log(Level.SEVERE,
   1.298 +                    "SAAJ0250.impl.body.should.have.exactly.one.child");
   1.299 +            throw new SOAPException("Cannot extract Document from body");
   1.300 +        }
   1.301 +
   1.302 +        Document document = null;
   1.303 +        try {
   1.304 +            DocumentBuilderFactory factory =
   1.305 +                new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl();
   1.306 +            factory.setNamespaceAware(true);
   1.307 +            DocumentBuilder builder = factory.newDocumentBuilder();
   1.308 +            document = builder.newDocument();
   1.309 +
   1.310 +            Element rootElement = (Element) document.importNode(
   1.311 +                                                firstBodyElement,
   1.312 +                                                true);
   1.313 +
   1.314 +            document.appendChild(rootElement);
   1.315 +
   1.316 +        } catch(Exception e) {
   1.317 +            log.log(Level.SEVERE,
   1.318 +                    "SAAJ0251.impl.cannot.extract.document.from.body");
   1.319 +            throw new SOAPExceptionImpl(
   1.320 +                "Unable to extract Document from body", e);
   1.321 +        }
   1.322 +
   1.323 +        firstBodyElement.detachNode();
   1.324 +
   1.325 +        return document;
   1.326 +    }
   1.327 +
   1.328 +}

mercurial