1 16 package org.apache.axis.message; 17 18 import org.apache.axis.AxisFault; 19 import org.apache.axis.Constants; 20 import org.apache.axis.Message; 21 import org.apache.axis.MessageContext; 22 import org.apache.axis.client.AxisClient; 23 import org.apache.axis.components.logger.LogFactory; 24 import org.apache.axis.configuration.NullProvider; 25 import org.apache.axis.encoding.DeserializationContext; 26 import org.apache.axis.encoding.SerializationContext; 27 import org.apache.axis.schema.SchemaVersion; 28 import org.apache.axis.soap.SOAPConstants; 29 import org.apache.axis.utils.Mapping; 30 import org.apache.axis.utils.Messages; 31 import org.apache.commons.logging.Log; 32 import org.w3c.dom.DOMException ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 38 import javax.xml.namespace.QName ; 39 import javax.xml.soap.SOAPException ; 40 import java.io.InputStream ; 41 import java.util.ArrayList ; 42 import java.util.Enumeration ; 43 import java.util.Iterator ; 44 import java.util.Vector ; 45 import java.util.List ; 46 47 50 public class SOAPEnvelope extends MessageElement 51 implements javax.xml.soap.SOAPEnvelope 52 { 53 protected static Log log = 54 LogFactory.getLog(SOAPEnvelope.class.getName()); 55 56 private SOAPHeader header; 57 private SOAPBody body; 58 59 public Vector trailers = new Vector (); 60 private SOAPConstants soapConstants; 61 private SchemaVersion schemaVersion = SchemaVersion.SCHEMA_2001; 62 63 public String messageType; 70 71 public SOAPEnvelope() 72 { 73 this(true, SOAPConstants.SOAP11_CONSTANTS); 74 } 75 76 public SOAPEnvelope(SOAPConstants soapConstants) 77 { 78 this(true, soapConstants); 79 } 80 81 public SOAPEnvelope(SOAPConstants soapConstants, 82 SchemaVersion schemaVersion) 83 { 84 this(true, soapConstants, schemaVersion); 85 } 86 87 public SOAPEnvelope(boolean registerPrefixes, SOAPConstants soapConstants) 88 { 89 this (registerPrefixes, soapConstants, SchemaVersion.SCHEMA_2001); 90 } 91 92 public SOAPEnvelope(boolean registerPrefixes, 93 SOAPConstants soapConstants, 94 SchemaVersion schemaVersion) 95 { 96 super(Constants.ELEM_ENVELOPE, 98 Constants.NS_PREFIX_SOAP_ENV, 99 (soapConstants != null) ? soapConstants.getEnvelopeURI() : Constants.DEFAULT_SOAP_VERSION.getEnvelopeURI()); 100 101 if (soapConstants == null) 102 soapConstants = Constants.DEFAULT_SOAP_VERSION; 103 105 this.soapConstants = soapConstants; 106 this.schemaVersion = schemaVersion; 107 header = new SOAPHeader(this, soapConstants); 108 body = new SOAPBody(this, soapConstants); 109 110 if (registerPrefixes) { 111 if (namespaces == null) 112 namespaces = new ArrayList (); 113 114 namespaces.add(new Mapping(soapConstants.getEnvelopeURI(), 115 Constants.NS_PREFIX_SOAP_ENV)); 116 namespaces.add(new Mapping(schemaVersion.getXsdURI(), 117 Constants.NS_PREFIX_SCHEMA_XSD)); 118 namespaces.add(new Mapping(schemaVersion.getXsiURI(), 119 Constants.NS_PREFIX_SCHEMA_XSI)); 120 } 121 122 setDirty(true); 123 } 124 125 public SOAPEnvelope(InputStream input) throws SAXException { 126 InputSource is = new InputSource (input); 127 header = new SOAPHeader(this, Constants.DEFAULT_SOAP_VERSION); DeserializationContext dser = null ; 132 AxisClient tmpEngine = new AxisClient(new NullProvider()); 133 MessageContext msgContext = new MessageContext(tmpEngine); 134 dser = new DeserializationContext(is, msgContext, 135 Message.REQUEST, this ); 136 dser.parse(); 137 } 138 139 143 public String getMessageType() 144 { 145 return messageType; 146 } 147 148 152 public void setMessageType(String messageType) 153 { 154 this.messageType = messageType; 155 } 156 157 162 public Vector getBodyElements() throws AxisFault 163 { 164 if (body != null) { 165 return body.getBodyElements(); 166 } else { 167 return new Vector (); 168 } 169 } 170 171 175 public Vector getTrailers() 176 { 177 return trailers; 178 } 179 180 185 public SOAPBodyElement getFirstBody() throws AxisFault 186 { 187 if (body == null) { 188 return null; 189 } else { 190 return body.getFirstBody(); 191 } 192 } 193 194 199 public Vector getHeaders() throws AxisFault 200 { 201 if (header != null) { 202 return header.getHeaders(); 203 } else { 204 return new Vector (); 205 } 206 } 207 208 211 public Vector getHeadersByActor(ArrayList actors) 212 { 213 if (header != null) { 214 return header.getHeadersByActor(actors); 215 } else { 216 return new Vector (); 217 } 218 } 219 220 224 public void addHeader(SOAPHeaderElement hdr) 225 { 226 if (header == null) { 227 header = new SOAPHeader(this, soapConstants); 228 } 229 hdr.setEnvelope(this); 230 header.addHeader(hdr); 231 _isDirty = true; 232 } 233 234 238 public void addBodyElement(SOAPBodyElement element) 239 { 240 if (body == null) { 241 body = new SOAPBody(this, soapConstants); 242 } 243 element.setEnvelope(this); 244 body.addBodyElement(element); 245 246 _isDirty = true; 247 } 248 249 252 public void removeHeaders() { 253 if (header != null) { 254 removeChild(header); 255 } 256 header = null; 257 } 258 259 263 public void setHeader(SOAPHeader hdr) { 264 if(this.header != null) { 265 removeChild(this.header); 266 } 267 header = hdr; 268 try { 269 header.setParentElement(this); 270 } catch (SOAPException ex) { 271 log.fatal(Messages.getMessage("exception00"), ex); 273 } 274 } 275 276 280 public void removeHeader(SOAPHeaderElement hdr) 281 { 282 if (header != null) { 283 header.removeHeader(hdr); 284 _isDirty = true; 285 } 286 } 287 288 291 public void removeBody() { 292 if (body != null) { 293 removeChild(body); 294 } 295 body = null; 296 } 297 298 302 public void setBody(SOAPBody body) { 303 if(this.body != null) { 304 removeChild(this.body); 305 } 306 this.body = body; 307 try { 308 body.setParentElement(this); 309 } catch (SOAPException ex) { 310 log.fatal(Messages.getMessage("exception00"), ex); 312 } 313 } 314 315 319 public void removeBodyElement(SOAPBodyElement element) 320 { 321 if (body != null) { 322 body.removeBodyElement(element); 323 _isDirty = true; 324 } 325 } 326 327 331 public void removeTrailer(MessageElement element) 332 { 333 if (log.isDebugEnabled()) 334 log.debug(Messages.getMessage("removeTrailer00")); 335 trailers.removeElement(element); 336 _isDirty = true; 337 } 338 339 342 public void clearBody() 343 { 344 if (body != null) { 345 body.clearBody(); 346 _isDirty = true; 347 } 348 } 349 350 354 public void addTrailer(MessageElement element) 355 { 356 if (log.isDebugEnabled()) 357 log.debug(Messages.getMessage("removeTrailer00")); 358 element.setEnvelope(this); 359 trailers.addElement(element); 360 _isDirty = true; 361 } 362 363 367 public SOAPHeaderElement getHeaderByName(String namespace, 368 String localPart) 369 throws AxisFault 370 { 371 return getHeaderByName(namespace, localPart, false); 372 } 373 374 378 public SOAPHeaderElement getHeaderByName(String namespace, 379 String localPart, 380 boolean accessAllHeaders) 381 throws AxisFault 382 { 383 if (header != null) { 384 return header.getHeaderByName(namespace, 385 localPart, 386 accessAllHeaders); 387 } else { 388 return null; 389 } 390 } 391 392 399 public SOAPBodyElement getBodyByName(String namespace, String localPart) 400 throws AxisFault 401 { 402 if (body == null) { 403 return null; 404 } else { 405 return body.getBodyByName(namespace, localPart); 406 } 407 } 408 409 416 public Enumeration getHeadersByName(String namespace, String localPart) 417 throws AxisFault 418 { 419 return getHeadersByName(namespace, localPart, false); 420 } 421 422 432 public Enumeration getHeadersByName(String namespace, String localPart, 433 boolean accessAllHeaders) 434 throws AxisFault 435 { 436 if (header != null) { 437 return header.getHeadersByName(namespace, 438 localPart, 439 accessAllHeaders); 440 } else { 441 return new Vector ().elements(); 442 } 443 } 444 445 447 public void outputImpl(SerializationContext context) 448 throws Exception 449 { 450 boolean oldPretty = context.getPretty(); 451 context.setPretty(true); 452 453 if (namespaces != null) { 455 for (Iterator i = namespaces.iterator(); i.hasNext(); ) { 456 Mapping mapping = (Mapping)i.next(); 457 context.registerPrefixForURI(mapping.getPrefix(), 458 mapping.getNamespaceURI()); 459 } 460 } 461 462 Enumeration enumeration; 463 464 context.startElement(new QName (soapConstants.getEnvelopeURI(), 466 Constants.ELEM_ENVELOPE), attributes); 467 468 469 Iterator i = getChildElements(); 471 while (i.hasNext()) { 472 NodeImpl node = (NodeImpl)i.next(); 473 474 if (node instanceof SOAPHeader) { 475 header.outputImpl(context); 476 } else if (node instanceof SOAPBody) { 477 body.outputImpl(context); 478 } else if (node instanceof MessageElement) { 479 ((MessageElement)node).output(context); 480 } else { 481 node.output(context); 482 } 483 } 484 485 enumeration = trailers.elements(); 487 while (enumeration.hasMoreElements()) { 488 MessageElement element = (MessageElement)enumeration.nextElement(); 489 element.output(context); 490 } 492 493 context.endElement(); 495 496 context.setPretty(oldPretty); 497 } 498 499 503 public SOAPConstants getSOAPConstants() { 504 return soapConstants; 505 } 506 507 511 public void setSoapConstants(SOAPConstants soapConstants) { 512 this.soapConstants = soapConstants; 513 } 514 515 519 public SchemaVersion getSchemaVersion() { 520 return schemaVersion; 521 } 522 523 527 public void setSchemaVersion(SchemaVersion schemaVersion) { 528 this.schemaVersion = schemaVersion; 529 } 530 531 536 public javax.xml.soap.SOAPBody addBody() throws SOAPException { 537 if (body == null) { 538 body = new SOAPBody(this, soapConstants); 539 _isDirty = true; 540 body.setOwnerDocument(getOwnerDocument()); 541 return body; 542 } else { 543 throw new SOAPException (Messages.getMessage("bodyPresent")); 544 } 545 } 546 547 552 public javax.xml.soap.SOAPHeader addHeader() throws SOAPException { 553 if (header == null) { 554 header = new SOAPHeader(this, soapConstants); 555 header.setOwnerDocument(getOwnerDocument()); 556 return header; 557 } else { 558 throw new SOAPException (Messages.getMessage("headerPresent")); 559 } 560 } 561 562 568 public javax.xml.soap.Name createName(String localName) 569 throws SOAPException { 570 return new PrefixedQName(null, localName, null); 571 } 572 573 581 public javax.xml.soap.Name createName(String localName, 582 String prefix, 583 String uri) 584 throws SOAPException { 585 return new PrefixedQName(uri, localName, prefix); 586 } 587 588 593 public javax.xml.soap.SOAPBody getBody() throws SOAPException { 594 return body; 595 } 596 597 602 public javax.xml.soap.SOAPHeader getHeader() throws SOAPException { 603 return header; 604 } 605 606 public void setSAAJEncodingCompliance(boolean comply) { 607 this.body.setSAAJEncodingCompliance(comply); 608 } 609 610 public Node removeChild(Node oldChild) throws DOMException { 611 if(oldChild == header) { 612 header = null; 613 } else if(oldChild == body) { 614 body = null; 615 } 616 return super.removeChild(oldChild); 617 } 618 619 public Node cloneNode(boolean deep) 620 { 621 SOAPEnvelope envelope = (SOAPEnvelope)super.cloneNode( deep ); 622 623 if( !deep ) 624 { 625 envelope.body = null; 626 envelope.header = null; 627 } 628 629 return envelope; 630 } 631 632 protected void childDeepCloned( NodeImpl oldNode, NodeImpl newNode ) 633 { 634 if( oldNode == body ) 635 { 636 body = (SOAPBody)newNode; 637 638 try { 639 body.setParentElement(this); 640 } catch (SOAPException ex) { 641 log.fatal(Messages.getMessage("exception00"), ex); 643 } 644 } 645 else 646 if( oldNode == header ) 647 { 648 header = (SOAPHeader)newNode; 649 } 650 } 651 652 public void setOwnerDocument(org.apache.axis.SOAPPart sp) { 653 super.setOwnerDocument(sp); 654 if(body != null) { 655 body.setOwnerDocument(sp); 656 setOwnerDocumentForChildren(((NodeImpl)body).children, sp); 657 } 658 if(header != null){ 659 header.setOwnerDocument(sp); 660 setOwnerDocumentForChildren(((NodeImpl)body).children, sp); 661 } 662 } 663 664 private void setOwnerDocumentForChildren(List children, org.apache.axis.SOAPPart sp) { 665 if (children == null) { 666 return; 667 } 668 int size = children.size(); 669 for (int i = 0; i < size; i++) { 670 NodeImpl node = (NodeImpl) children.get(i); 671 node.setOwnerDocument(sp); 672 setOwnerDocumentForChildren(node.children, sp); } 674 } 675 } 676 | Popular Tags |