< prev index next >

jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/soap/impl/BodyImpl.java

Print this page




  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
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.soap.impl;
  27 
  28 import java.util.Iterator;
  29 import java.util.Locale;
  30 import java.util.logging.Level;
  31 
  32 import javax.xml.namespace.QName;
  33 import javax.xml.soap.*;
  34 import javax.xml.stream.XMLStreamException;
  35 import javax.xml.stream.XMLStreamReader;
  36 import javax.xml.parsers.DocumentBuilder;
  37 import javax.xml.parsers.DocumentBuilderFactory;
  38 
  39 import com.sun.xml.internal.messaging.saaj.util.SAAJUtil;
  40 import org.w3c.dom.*;
  41 import org.w3c.dom.Node;
  42 
  43 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
  44 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
  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.name.NameImpl;












  48 
  49 /**
  50  * The implementation of SOAP-ENV:BODY or the SOAPBody abstraction.
  51  *
  52  * @author Anil Vijendran (anil@sun.com)
  53  */
  54 public abstract class BodyImpl extends ElementImpl implements SOAPBody {
  55     private SOAPFault fault;
  56 //  private XMLStreamReaderToXMLStreamWriter staxBridge;
  57     private StaxBridge staxBridge;
  58     private boolean payloadStreamRead = false;
  59 
  60     protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) {
  61         super(ownerDoc, bodyName);
  62     }
  63 
  64     public BodyImpl(SOAPDocumentImpl ownerDoc, Element domElement) {
  65         super(ownerDoc, domElement);
  66     }
  67 
  68     protected abstract NameImpl getFaultName(String name);
  69     protected abstract boolean isFault(SOAPElement child);
  70     protected abstract SOAPBodyElement createBodyElement(Name name);
  71     protected abstract SOAPBodyElement createBodyElement(QName name);
  72     protected abstract SOAPFault createFaultElement();
  73     protected abstract QName getDefaultFaultCode();
  74 

  75     public SOAPFault addFault() throws SOAPException {
  76         if (hasFault()) {
  77             log.severe("SAAJ0110.impl.fault.already.exists");
  78             throw new SOAPExceptionImpl("Error: Fault already exists");
  79         }
  80 
  81         fault = createFaultElement();
  82 
  83         addNode(fault);
  84 
  85         fault.setFaultCode(getDefaultFaultCode());
  86         fault.setFaultString("Fault string, and possibly fault code, not set");
  87 
  88         return fault;
  89     }
  90 

  91     public SOAPFault addFault(
  92         Name faultCode,
  93         String faultString,
  94         Locale locale)
  95         throws SOAPException {
  96 
  97         SOAPFault fault = addFault();
  98         fault.setFaultCode(faultCode);
  99         fault.setFaultString(faultString, locale);
 100         return fault;
 101     }
 102 

 103    public SOAPFault addFault(
 104         QName faultCode,
 105         String faultString,
 106         Locale locale)
 107         throws SOAPException {
 108 
 109         SOAPFault fault = addFault();
 110         fault.setFaultCode(faultCode);
 111         fault.setFaultString(faultString, locale);
 112         return fault;
 113     }
 114 

 115     public SOAPFault addFault(Name faultCode, String faultString)
 116         throws SOAPException {
 117 
 118         SOAPFault fault = addFault();
 119         fault.setFaultCode(faultCode);
 120         fault.setFaultString(faultString);
 121         return fault;
 122     }
 123 

 124     public SOAPFault addFault(QName faultCode, String faultString)
 125         throws SOAPException {
 126 
 127         SOAPFault fault = addFault();
 128         fault.setFaultCode(faultCode);
 129         fault.setFaultString(faultString);
 130         return fault;
 131     }
 132 
 133     void initializeFault() {
 134         FaultImpl flt = (FaultImpl) findFault();
 135         fault = flt;
 136     }
 137 
 138     protected SOAPElement findFault() {
 139         Iterator<Node> eachChild = getChildElementNodes();
 140         while (eachChild.hasNext()) {
 141             SOAPElement child = (SOAPElement) eachChild.next();
 142             if (isFault(child)) {
 143                 return child;
 144             }
 145         }
 146 
 147         return null;
 148     }
 149 

 150     public boolean hasFault() {
 151         QName payloadQName = getPayloadQName();
 152         return getFaultQName().equals(payloadQName);
 153     }
 154 
 155     private Object getFaultQName() {
 156         return new QName(getNamespaceURI(), "Fault");
 157     }
 158 

 159     public SOAPFault getFault() {
 160         if (hasFault()) {
 161             if (fault == null) {
 162                 //initialize fault member
 163                 fault = (SOAPFault) getSoapDocument().find(getFirstChildElement());
 164             }
 165             return fault;
 166         }
 167         return null;
 168     }
 169 

 170     public SOAPBodyElement addBodyElement(Name name) throws SOAPException {
 171         SOAPBodyElement newBodyElement =
 172             (SOAPBodyElement) ElementFactory.createNamedElement(
 173                 ((SOAPDocument) getOwnerDocument()).getDocument(),
 174                 name.getLocalName(),
 175                 name.getPrefix(),
 176                 name.getURI());
 177         if (newBodyElement == null) {
 178             newBodyElement = createBodyElement(name);
 179         }
 180         addNode(newBodyElement);
 181         return newBodyElement;
 182     }
 183 

 184     public SOAPBodyElement addBodyElement(QName qname) throws SOAPException {
 185         SOAPBodyElement newBodyElement =
 186             (SOAPBodyElement) ElementFactory.createNamedElement(
 187                 ((SOAPDocument) getOwnerDocument()).getDocument(),
 188                 qname.getLocalPart(),
 189                 qname.getPrefix(),
 190                 qname.getNamespaceURI());
 191         if (newBodyElement == null) {
 192             newBodyElement = createBodyElement(qname);
 193         }
 194         addNode(newBodyElement);
 195         return newBodyElement;
 196     }
 197 

 198     public void setParentElement(SOAPElement element) throws SOAPException {
 199 
 200         if (!(element instanceof SOAPEnvelope)) {
 201             log.severe("SAAJ0111.impl.body.parent.must.be.envelope");
 202             throw new SOAPException("Parent of SOAPBody has to be a SOAPEnvelope");
 203         }
 204         super.setParentElement(element);
 205     }
 206 

 207     protected SOAPElement addElement(Name name) throws SOAPException {
 208         return addBodyElement(name);
 209     }
 210 

 211     protected SOAPElement addElement(QName name) throws SOAPException {
 212         return addBodyElement(name);
 213     }
 214 
 215     //    public Node insertBefore(Node newElement, Node ref) throws DOMException {
 216     //        if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
 217     //            newElement = new ElementWrapper((ElementImpl) newElement);
 218     //        }
 219     //        return super.insertBefore(newElement, ref);
 220     //    }
 221     //
 222     //    public Node replaceChild(Node newElement, Node ref) throws DOMException {
 223     //        if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
 224     //            newElement = new ElementWrapper((ElementImpl) newElement);
 225     //        }
 226     //        return super.replaceChild(newElement, ref);
 227     //    }
 228 

 229     public SOAPBodyElement addDocument(Document document)
 230         throws SOAPException {
 231         /*
 232 
 233                 Element rootNode =
 234                     document.getDocumentElement();
 235                 // Causes all deferred nodes to be inflated
 236                 rootNode.normalize();
 237                 adoptElement(rootNode);
 238                 SOAPBodyElement bodyElement = (SOAPBodyElement) convertToSoapElement(rootNode);
 239                 addNode(bodyElement);
 240                 return bodyElement;
 241         */
 242         ///*
 243         SOAPBodyElement newBodyElement = null;
 244         DocumentFragment docFrag = document.createDocumentFragment();
 245         Element rootElement = document.getDocumentElement();
 246         if(rootElement != null) {
 247             docFrag.appendChild(rootElement);
 248 
 249             Document ownerDoc = getOwnerDocument();
 250             // This copies the whole tree which could be very big so it's slow.
 251             // However, it does have the advantage of actually working.
 252             org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
 253             // Adding replacingNode at the last of the children list of body
 254             addNode(replacingNode);
 255             Iterator<Node> i =
 256                 getChildElements(NameImpl.copyElementName(rootElement));
 257             // Return the child element with the required name which is at the
 258             // end of the list
 259             while(i.hasNext())
 260                 newBodyElement = (SOAPBodyElement) i.next();
 261         }
 262         return newBodyElement;
 263         //*/
 264     }
 265 

 266     protected SOAPElement convertToSoapElement(Element element) {
 267         final Node soapNode = getSoapDocument().findIfPresent(element);
 268         if ((soapNode instanceof SOAPBodyElement) &&
 269             //this check is required because ElementImpl currently
 270             // implements SOAPBodyElement
 271             !(soapNode.getClass().equals(ElementImpl.class))) {
 272             return (SOAPElement) soapNode;
 273         } else {
 274             return replaceElementWithSOAPElement(
 275                 element,
 276                 (ElementImpl) createBodyElement(NameImpl
 277                     .copyElementName(element)));
 278         }
 279     }
 280 

 281     public SOAPElement setElementQName(QName newName) throws SOAPException {
 282         log.log(Level.SEVERE,
 283                 "SAAJ0146.impl.invalid.name.change.requested",
 284                 new Object[] {elementQName.getLocalPart(),
 285                               newName.getLocalPart()});
 286         throw new SOAPException("Cannot change name for "
 287                                 + elementQName.getLocalPart() + " to "
 288                                 + newName.getLocalPart());
 289     }
 290 

 291     public Document extractContentAsDocument() throws SOAPException {
 292 
 293         Iterator<Node> eachChild = getChildElements();
 294         javax.xml.soap.Node firstBodyElement = null;
 295 
 296         while (eachChild.hasNext() &&
 297                !(firstBodyElement instanceof SOAPElement))
 298             firstBodyElement = (javax.xml.soap.Node) eachChild.next();
 299 
 300         boolean exactlyOneChildElement = true;
 301         if (firstBodyElement == null)
 302             exactlyOneChildElement = false;
 303         else {
 304             for (org.w3c.dom.Node node = firstBodyElement.getNextSibling();
 305                  node != null;
 306                  node = node.getNextSibling()) {
 307 
 308                 if (node instanceof Element) {
 309                     exactlyOneChildElement = false;
 310                     break;
 311                 }
 312             }
 313         }




  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
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.soap.impl;
  27 
  28 import java.util.Iterator;
  29 import java.util.Locale;
  30 import java.util.logging.Level;
  31 
  32 import javax.xml.namespace.QName;

  33 import javax.xml.stream.XMLStreamException;
  34 import javax.xml.stream.XMLStreamReader;
  35 import javax.xml.parsers.DocumentBuilder;
  36 import javax.xml.parsers.DocumentBuilderFactory;
  37 
  38 import com.sun.xml.internal.messaging.saaj.util.SAAJUtil;


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


< prev index next >