KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > message > SOAPEnvelope


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 import javax.xml.namespace.QName JavaDoc;
39 import javax.xml.soap.SOAPException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Enumeration JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.Vector JavaDoc;
45 import java.util.List JavaDoc;
46
47 /**
48  * Implementation of a SOAP Envelope
49  */

50 public class SOAPEnvelope extends MessageElement
51     implements javax.xml.soap.SOAPEnvelope JavaDoc
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 JavaDoc trailers = new Vector JavaDoc();
60     private SOAPConstants soapConstants;
61     private SchemaVersion schemaVersion = SchemaVersion.SCHEMA_2001;
62
63     // This is a hint to any service description to tell it what
64
// "type" of message we are. This might be "request", "response",
65
// or anything else your particular service descripton requires.
66
//
67
// This gets passed back into the service description during
68
// deserialization
69
public String JavaDoc 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         // FIX BUG http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18108
97
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         // FIX BUG http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18108
104

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 JavaDoc();
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 JavaDoc input) throws SAXException JavaDoc {
126         InputSource JavaDoc is = new InputSource JavaDoc(input);
127         // FIX BUG http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18108
128
//header = new SOAPHeader(this, soapConstants); // soapConstants = null!
129
header = new SOAPHeader(this, Constants.DEFAULT_SOAP_VERSION); // soapConstants = null!
130
// FIX BUG http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18108
131
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     /**
140      * Get the Message Type (REQUEST/RESPONSE)
141      * @return message type
142      */

143     public String JavaDoc getMessageType()
144     {
145         return messageType;
146     }
147
148     /**
149      * Set the Message Type (REQUEST/RESPONSE)
150      * @param messageType
151      */

152     public void setMessageType(String JavaDoc messageType)
153     {
154         this.messageType = messageType;
155     }
156
157     /**
158      * Get all the BodyElement's in the soap body
159      * @return vector with body elements
160      * @throws AxisFault
161      */

162     public Vector JavaDoc getBodyElements() throws AxisFault
163     {
164         if (body != null) {
165             return body.getBodyElements();
166         } else {
167             return new Vector JavaDoc();
168         }
169     }
170
171     /**
172      * Return trailers
173      * @return vector of some type
174      */

175     public Vector JavaDoc getTrailers()
176     {
177         return trailers;
178     }
179
180     /**
181      * Get the first BodyElement in the SOAP Body
182      * @return first Body Element
183      * @throws AxisFault
184      */

185     public SOAPBodyElement getFirstBody() throws AxisFault
186     {
187         if (body == null) {
188             return null;
189         } else {
190             return body.getFirstBody();
191         }
192     }
193
194     /**
195      * Get Headers
196      * @return Vector containing Header's
197      * @throws AxisFault
198      */

199     public Vector JavaDoc getHeaders() throws AxisFault
200     {
201         if (header != null) {
202             return header.getHeaders();
203         } else {
204             return new Vector JavaDoc();
205         }
206     }
207
208     /**
209      * Get all the headers targeted at a list of actors.
210      */

211     public Vector JavaDoc getHeadersByActor(ArrayList JavaDoc actors)
212     {
213         if (header != null) {
214             return header.getHeadersByActor(actors);
215         } else {
216             return new Vector JavaDoc();
217         }
218     }
219
220     /**
221      * Add a HeaderElement
222      * @param hdr
223      */

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     /**
235      * Add a SOAP Body Element
236      * @param element
237      */

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     /**
250      * Remove all headers
251      */

252     public void removeHeaders() {
253         if (header != null) {
254             removeChild(header);
255         }
256         header = null;
257     }
258
259     /**
260      * Set the SOAP Header
261      * @param hdr
262      */

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 JavaDoc ex) {
271             // class cast should never fail when parent is a SOAPEnvelope
272
log.fatal(Messages.getMessage("exception00"), ex);
273         }
274     }
275
276     /**
277      * Remove a Header Element from SOAP Header
278      * @param hdr
279      */

280     public void removeHeader(SOAPHeaderElement hdr)
281     {
282         if (header != null) {
283             header.removeHeader(hdr);
284             _isDirty = true;
285         }
286     }
287
288     /**
289      * Remove the SOAP Body
290      */

291     public void removeBody() {
292         if (body != null) {
293             removeChild(body);
294         }
295         body = null;
296     }
297
298     /**
299      * Set the soap body
300      * @param body
301      */

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 JavaDoc ex) {
310             // class cast should never fail when parent is a SOAPEnvelope
311
log.fatal(Messages.getMessage("exception00"), ex);
312         }
313     }
314
315     /**
316      * Remove a Body Element from the soap body
317      * @param element
318      */

319     public void removeBodyElement(SOAPBodyElement element)
320     {
321         if (body != null) {
322             body.removeBodyElement(element);
323             _isDirty = true;
324         }
325     }
326
327     /**
328      * Remove an element from the trailer
329      * @param element
330      */

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     /**
340      * clear the elements in the soap body
341      */

342     public void clearBody()
343     {
344         if (body != null) {
345             body.clearBody();
346             _isDirty = true;
347         }
348     }
349
350     /**
351      * Add an element to the trailer
352      * @param element
353      */

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     /**
364      * Get a header by name (always respecting the currently in-scope
365      * actors list)
366      */

367     public SOAPHeaderElement getHeaderByName(String JavaDoc namespace,
368                                              String JavaDoc localPart)
369         throws AxisFault
370     {
371         return getHeaderByName(namespace, localPart, false);
372     }
373
374     /**
375      * Get a header by name, filtering for headers targeted at this
376      * engine depending on the accessAllHeaders parameter.
377      */

378     public SOAPHeaderElement getHeaderByName(String JavaDoc namespace,
379                                              String JavaDoc 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     /**
393      * Get a body element given its name
394      * @param namespace
395      * @param localPart
396      * @return
397      * @throws AxisFault
398      */

399     public SOAPBodyElement getBodyByName(String JavaDoc namespace, String JavaDoc localPart)
400         throws AxisFault
401     {
402         if (body == null) {
403             return null;
404         } else {
405             return body.getBodyByName(namespace, localPart);
406         }
407     }
408
409     /**
410      * Get an enumeration of header elements given the namespace and localpart
411      * @param namespace
412      * @param localPart
413      * @return
414      * @throws AxisFault
415      */

416     public Enumeration JavaDoc getHeadersByName(String JavaDoc namespace, String JavaDoc localPart)
417         throws AxisFault
418     {
419         return getHeadersByName(namespace, localPart, false);
420     }
421
422     /**
423      * Return an Enumeration of headers which match the given namespace
424      * and localPart. Depending on the value of the accessAllHeaders
425      * parameter, we will attempt to filter on the current engine's list
426      * of actors.
427      *
428      * !!! NOTE THAT RIGHT NOW WE ALWAYS ASSUME WE'RE THE "ULTIMATE
429      * DESTINATION" (i.e. we match on null actor). IF WE WANT TO FULLY SUPPORT
430      * INTERMEDIARIES WE'LL NEED TO FIX THIS.
431      */

432     public Enumeration JavaDoc getHeadersByName(String JavaDoc namespace, String JavaDoc 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 JavaDoc().elements();
442         }
443     }
444
445     /** Should make SOAPSerializationException?
446      */

447     public void outputImpl(SerializationContext context)
448         throws Exception JavaDoc
449     {
450         boolean oldPretty = context.getPretty();
451         context.setPretty(true);
452
453         // Register namespace prefixes.
454
if (namespaces != null) {
455             for (Iterator JavaDoc i = namespaces.iterator(); i.hasNext(); ) {
456                 Mapping mapping = (Mapping)i.next();
457                 context.registerPrefixForURI(mapping.getPrefix(),
458                                              mapping.getNamespaceURI());
459             }
460         }
461
462         Enumeration JavaDoc enumeration;
463
464         // Output <SOAP-ENV:Envelope>
465
context.startElement(new QName JavaDoc(soapConstants.getEnvelopeURI(),
466                                        Constants.ELEM_ENVELOPE), attributes);
467
468         
469         // Output <SOAP-ENV:Envelope>'s each child as it appears.
470
Iterator JavaDoc 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         // Output trailers
486
enumeration = trailers.elements();
487         while (enumeration.hasMoreElements()) {
488             MessageElement element = (MessageElement)enumeration.nextElement();
489             element.output(context);
490             // Output this independent element
491
}
492
493         // Output </SOAP-ENV:Envelope>
494
context.endElement();
495
496         context.setPretty(oldPretty);
497     }
498
499     /**
500      * Get the soap constants for this envelope
501      * @return
502      */

503     public SOAPConstants getSOAPConstants() {
504         return soapConstants;
505     }
506
507     /**
508      * Set the soap constants for this envelope
509      * @param soapConstants
510      */

511     public void setSoapConstants(SOAPConstants soapConstants) {
512         this.soapConstants = soapConstants;
513     }
514
515     /**
516      * Get the schema version for this envelope
517      * @return
518      */

519     public SchemaVersion getSchemaVersion() {
520         return schemaVersion;
521     }
522  
523     /**
524      * Set the schema version for this envelope
525      * @param schemaVersion
526      */

527     public void setSchemaVersion(SchemaVersion schemaVersion) {
528         this.schemaVersion = schemaVersion;
529     }
530
531     /**
532      * Add a soap body if one does not exist
533      * @return
534      * @throws SOAPException
535      */

536     public javax.xml.soap.SOAPBody JavaDoc addBody() throws SOAPException JavaDoc {
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 JavaDoc(Messages.getMessage("bodyPresent"));
544         }
545     }
546
547     /**
548      * Add a soap header if one does not exist
549      * @return
550      * @throws SOAPException
551      */

552     public javax.xml.soap.SOAPHeader JavaDoc addHeader() throws SOAPException JavaDoc {
553         if (header == null) {
554             header = new SOAPHeader(this, soapConstants);
555             header.setOwnerDocument(getOwnerDocument());
556             return header;
557         } else {
558             throw new SOAPException JavaDoc(Messages.getMessage("headerPresent"));
559         }
560     }
561
562     /**
563      * create a Name given the local part
564      * @param localName
565      * @return
566      * @throws SOAPException
567      */

568     public javax.xml.soap.Name JavaDoc createName(String JavaDoc localName)
569         throws SOAPException JavaDoc {
570         return new PrefixedQName(null, localName, null);
571     }
572
573     /**
574      * Create a name given local part, prefix and uri
575      * @param localName
576      * @param prefix
577      * @param uri
578      * @return
579      * @throws SOAPException
580      */

581     public javax.xml.soap.Name JavaDoc createName(String JavaDoc localName,
582                                           String JavaDoc prefix,
583                                           String JavaDoc uri)
584         throws SOAPException JavaDoc {
585         return new PrefixedQName(uri, localName, prefix);
586     }
587
588     /**
589      * Get the soap body
590      * @return
591      * @throws SOAPException
592      */

593     public javax.xml.soap.SOAPBody JavaDoc getBody() throws SOAPException JavaDoc {
594         return body;
595     }
596
597     /**
598      * Get the soap header
599      * @return
600      * @throws SOAPException
601      */

602     public javax.xml.soap.SOAPHeader JavaDoc getHeader() throws SOAPException JavaDoc {
603         return header;
604     }
605
606     public void setSAAJEncodingCompliance(boolean comply) {
607         this.body.setSAAJEncodingCompliance(comply);
608     }
609     
610     public Node JavaDoc removeChild(Node JavaDoc oldChild) throws DOMException JavaDoc {
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 JavaDoc 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 JavaDoc ex) {
641                 // class cast should never fail when parent is a SOAPEnvelope
642
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 JavaDoc 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); // recursively
673
}
674     }
675 }
676
Popular Tags