KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > SOAPEnvelopeAxisImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.message;
56
57 import org.jboss.axis.AxisFault;
58 import org.jboss.axis.Constants;
59 import org.jboss.axis.Message;
60 import org.jboss.axis.MessageContext;
61 import org.jboss.axis.client.AxisClient;
62 import org.jboss.axis.configuration.NullProvider;
63 import org.jboss.axis.encoding.DeserializationContext;
64 import org.jboss.axis.encoding.DeserializationContextImpl;
65 import org.jboss.axis.encoding.SerializationContext;
66 import org.jboss.axis.schema.SchemaVersion;
67 import org.jboss.axis.soap.SOAPConstants;
68 import org.jboss.axis.utils.Mapping;
69 import org.jboss.axis.utils.Messages;
70 import org.jboss.logging.Logger;
71 import org.w3c.dom.DOMException JavaDoc;
72 import org.w3c.dom.Node JavaDoc;
73 import org.xml.sax.InputSource JavaDoc;
74 import org.xml.sax.SAXException JavaDoc;
75
76 import javax.xml.namespace.QName JavaDoc;
77 import javax.xml.soap.SOAPException JavaDoc;
78 import java.io.InputStream JavaDoc;
79 import java.util.ArrayList JavaDoc;
80 import java.util.Enumeration JavaDoc;
81 import java.util.Iterator JavaDoc;
82 import java.util.Vector JavaDoc;
83
84 /**
85  * Implementation of a SOAP Envelope
86  */

87 public class SOAPEnvelopeAxisImpl extends SOAPEnvelopeImpl
88 {
89
90    private static Logger log = Logger.getLogger(SOAPEnvelopeAxisImpl.class.getName());
91
92    private SOAPHeaderAxisImpl header;
93    private SOAPBodyAxisImpl body;
94
95    public Vector JavaDoc trailers = new Vector JavaDoc();
96    private SOAPConstants soapConstants;
97    private SchemaVersion schemaVersion = SchemaVersion.SCHEMA_2001;
98
99    // This is a hint to any service description to tell it what
100
// "type" of message we are. This might be "request", "response",
101
// or anything else your particular service descripton requires.
102
//
103
// This gets passed back into the service description during
104
// deserialization
105
public String JavaDoc messageType;
106
107    // Set to mark the envelope as modified
108
private boolean modified;
109
110    // The rpc invoker can set this flag during processing, this will
111
// - turn on SOAPElement immutable protection
112
// - turn off MessagePart as string caching
113
private boolean processingRPCInvocation;
114
115    public SOAPEnvelopeAxisImpl()
116    {
117       this(true, SOAPConstants.SOAP11_CONSTANTS);
118    }
119
120    public SOAPEnvelopeAxisImpl(SOAPConstants soapConstants)
121    {
122       this(true, soapConstants);
123    }
124
125    public SOAPEnvelopeAxisImpl(SOAPConstants soapConstants,
126                                SchemaVersion schemaVersion)
127    {
128       this(true, soapConstants, schemaVersion);
129    }
130
131    public SOAPEnvelopeAxisImpl(boolean registerPrefixes, SOAPConstants soapConstants)
132    {
133       this(registerPrefixes, soapConstants, SchemaVersion.SCHEMA_2001);
134    }
135
136    public SOAPEnvelopeAxisImpl(boolean registerPrefixes, SOAPConstants soapConstants, SchemaVersion schemaVersion)
137    {
138
139       // FIX BUG http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18108
140
super(Constants.ELEM_ENVELOPE, Constants.NS_PREFIX_SOAP_ENV,
141               (soapConstants != null) ? soapConstants.getEnvelopeURI() : Constants.DEFAULT_SOAP_VERSION.getEnvelopeURI());
142
143       if (soapConstants == null)
144          soapConstants = Constants.DEFAULT_SOAP_VERSION;
145
146       // FIX BUG http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18108
147

148       this.soapConstants = soapConstants;
149       this.schemaVersion = schemaVersion;
150
151       setHeader(new SOAPHeaderAxisImpl(this, soapConstants));
152       setBody(new SOAPBodyAxisImpl(this, soapConstants));
153
154       if (registerPrefixes)
155       {
156          if (namespaces == null)
157             namespaces = new ArrayList JavaDoc();
158
159          namespaces.add(new Mapping(soapConstants.getEnvelopeURI(),
160                  Constants.NS_PREFIX_SOAP_ENV));
161          namespaces.add(new Mapping(schemaVersion.getXsdURI(),
162                  Constants.NS_PREFIX_SCHEMA_XSD));
163          namespaces.add(new Mapping(schemaVersion.getXsiURI(),
164                  Constants.NS_PREFIX_SCHEMA_XSI));
165       }
166
167       setDirty(true);
168    }
169
170    public static SOAPEnvelopeAxisImpl newSOAPEnvelope(InputStream JavaDoc input) throws SAXException JavaDoc
171    {
172
173       SOAPEnvelopeAxisImpl env = new SOAPEnvelopeAxisImpl();
174
175       InputSource JavaDoc is = new InputSource JavaDoc(input);
176
177       // FIX BUG http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18108
178
AxisClient tmpEngine = new AxisClient(new NullProvider());
179       MessageContext msgContext = new MessageContext(tmpEngine);
180       DeserializationContext dser = new DeserializationContextImpl(is, msgContext, Message.REQUEST, env);
181       dser.parse();
182
183       return env;
184    }
185
186    /**
187     * Get the Message Type (REQUEST/RESPONSE)
188     *
189     * @return message type
190     */

191    public String JavaDoc getMessageType()
192    {
193       return messageType;
194    }
195
196    /**
197     * Set the Message Type (REQUEST/RESPONSE)
198     *
199     * @param messageType
200     */

201    public void setMessageType(String JavaDoc messageType)
202    {
203       this.messageType = messageType;
204    }
205
206    /**
207     * Get all the BodyElement's in the soap body
208     *
209     * @return vector with body elements
210     * @throws AxisFault
211     */

212    public Vector JavaDoc getBodyElements() throws AxisFault
213    {
214       if (body != null)
215       {
216          return new Vector JavaDoc(body.getBodyElements());
217       }
218       else
219       {
220          return new Vector JavaDoc();
221       }
222    }
223
224    /**
225     * Return trailers
226     *
227     * @return
228     */

229    public Vector JavaDoc getTrailers()
230    {
231       return trailers;
232    }
233
234    /**
235     * Get the first BodyElement in the SOAP Body
236     *
237     * @return first Body Element
238     * @throws AxisFault
239     */

240    public SOAPBodyElementAxisImpl getFirstBody() throws AxisFault
241    {
242       return (body != null ? body.getFirstBody() : null);
243    }
244
245    public boolean isModified()
246    {
247       return modified;
248    }
249
250    public void setModified(boolean modified)
251    {
252       this.modified = modified;
253       log.debug("setModifiedAfterSerialization: " + modified);
254    }
255
256    public boolean isProcessingRPCInvocation()
257    {
258       return processingRPCInvocation;
259    }
260
261    public void setProcessingRPCInvocation(boolean flag)
262    {
263       log.debug("setProcessingRPCInvocation: " + flag);
264       this.processingRPCInvocation = flag;
265    }
266
267    /**
268     * Get Headers
269     *
270     * @return Vector containing Header's
271     * @throws AxisFault
272     */

273    public Vector JavaDoc getHeaders() throws AxisFault
274    {
275       if (header != null)
276       {
277          return new Vector JavaDoc(header.getChildren());
278       }
279       else
280       {
281          return new Vector JavaDoc();
282       }
283    }
284
285    /**
286     * Add a HeaderElement
287     *
288     * @param hdr
289     */

290    public void addHeader(SOAPHeaderElementAxisImpl hdr)
291    {
292       if (header == null)
293       {
294          header = new SOAPHeaderAxisImpl(this, soapConstants);
295       }
296       hdr.setEnvelope(this);
297       header.addHeader(hdr);
298       setDirty(true);
299    }
300
301    /**
302     * Add a SOAP Body Element
303     *
304     * @param element
305     */

306    public void addBodyElement(SOAPBodyElementAxisImpl element)
307    {
308       element.setEnvelope(this);
309       body.addBodyElement(element);
310       setDirty(true);
311    }
312
313    /**
314     * Remove all headers
315     */

316    public void removeHeaders()
317    {
318       header = null;
319    }
320
321    /**
322     * Set the SOAP Header
323     */

324    public void setHeader(SOAPHeaderAxisImpl header)
325    {
326       try
327       {
328          if (this.header != null)
329             removeChild(this.header);
330
331          // cast to force exception if wrong type
332
header.setParentElement(this);
333          this.header = header;
334       }
335       catch (SOAPException JavaDoc ex)
336       {
337          // class cast should never fail when parent is a SOAPEnvelope
338
log.fatal(Messages.getMessage("exception00"), ex);
339       }
340    }
341
342    /**
343     * Remove a Header Element from SOAP Header
344     *
345     * @param hdr
346     */

347    public void removeHeader(SOAPHeaderElementAxisImpl hdr)
348    {
349       if (header != null)
350       {
351          header.removeHeader(hdr);
352          setDirty(true);
353       }
354    }
355
356    public Node JavaDoc removeChild(Node JavaDoc oldChild) throws DOMException JavaDoc
357    {
358       super.removeChild(oldChild);
359
360       if (oldChild instanceof SOAPHeaderAxisImpl)
361          removeHeaders();
362
363       else if (oldChild instanceof SOAPBodyAxisImpl)
364          removeBody();
365
366       return oldChild;
367    }
368
369    /**
370     * Remove the SOAP Body
371     */

372    public void removeBody()
373    {
374       body = null;
375    }
376
377    /**
378     * Set the soap body
379     *
380     * @param newBody
381     */

382    public void setBody(SOAPBodyAxisImpl newBody)
383    {
384
385       if (body != null)
386       {
387          removeChild(body);
388       }
389
390       try
391       {
392          newBody.setParentElement(this);
393          body = newBody;
394       }
395       catch (SOAPException JavaDoc ex)
396       {
397          // class cast should never fail when parent is a SOAPEnvelope
398
log.fatal(Messages.getMessage("exception00"), ex);
399       }
400    }
401
402    /**
403     * Remove a Body Element from the soap body
404     *
405     * @param element
406     */

407    public void removeBodyElement(SOAPBodyElementAxisImpl element)
408    {
409       if (body != null)
410       {
411          body.removeBodyElement(element);
412          setDirty(true);
413       }
414    }
415
416    /**
417     * Remove an element from the trailer
418     *
419     * @param element
420     */

421    public void removeTrailer(SOAPElementAxisImpl element)
422    {
423       if (log.isDebugEnabled())
424          log.debug(Messages.getMessage("removeTrailer00"));
425       trailers.removeElement(element);
426       setDirty(true);
427    }
428
429    /**
430     * clear the elements in the soap body
431     */

432    public void clearBody()
433    {
434       if (body != null)
435       {
436          body.clearBody();
437          setDirty(true);
438       }
439    }
440
441    /**
442     * Add an element to the trailer
443     *
444     * @param element
445     */

446    public void addTrailer(SOAPElementAxisImpl element)
447    {
448       if (log.isDebugEnabled())
449          log.debug(Messages.getMessage("removeTrailer00"));
450       element.setEnvelope(this);
451       trailers.addElement(element);
452       setDirty(true);
453    }
454
455    /**
456     * Get a header by name (always respecting the currently in-scope
457     * actors list)
458     */

459    public SOAPHeaderElementAxisImpl getHeaderByName(String JavaDoc namespace,
460                                                     String JavaDoc localPart)
461            throws AxisFault
462    {
463       return getHeaderByName(namespace, localPart, false);
464    }
465
466    /**
467     * Get a header by name, filtering for headers targeted at this
468     * engine depending on the accessAllHeaders parameter.
469     */

470    public SOAPHeaderElementAxisImpl getHeaderByName(String JavaDoc namespace,
471                                                     String JavaDoc localPart,
472                                                     boolean accessAllHeaders)
473            throws AxisFault
474    {
475       if (header != null)
476       {
477          return header.getHeaderByName(namespace,
478                  localPart,
479                  accessAllHeaders);
480       }
481       else
482       {
483          return null;
484       }
485    }
486
487    /**
488     * Get a body element given its name
489     *
490     * @param namespace
491     * @param localPart
492     * @return
493     * @throws AxisFault
494     */

495    public SOAPBodyElementAxisImpl getBodyByName(String JavaDoc namespace, String JavaDoc localPart) throws AxisFault
496    {
497       return (body != null ? body.getBodyByName(namespace, localPart) : null);
498    }
499
500    /**
501     * Get an enumeration of header elements given the namespace and localpart
502     *
503     * @param namespace
504     * @param localPart
505     * @return
506     * @throws AxisFault
507     */

508    public Enumeration JavaDoc getHeadersByName(String JavaDoc namespace, String JavaDoc localPart)
509            throws AxisFault
510    {
511       return getHeadersByName(namespace, localPart, false);
512    }
513
514    /**
515     * Return an Enumeration of headers which match the given namespace
516     * and localPart. Depending on the value of the accessAllHeaders
517     * parameter, we will attempt to filter on the current engine's list
518     * of actors.
519     * <p/>
520     * !!! NOTE THAT RIGHT NOW WE ALWAYS ASSUME WE'RE THE "ULTIMATE
521     * DESTINATION" (i.e. we match on null actor). IF WE WANT TO FULLY SUPPORT
522     * INTERMEDIARIES WE'LL NEED TO FIX THIS.
523     */

524    public Enumeration JavaDoc getHeadersByName(String JavaDoc namespace, String JavaDoc localPart,
525                                        boolean accessAllHeaders)
526            throws AxisFault
527    {
528       if (header != null)
529       {
530          return header.getHeadersByName(namespace,
531                  localPart,
532                  accessAllHeaders);
533       }
534       else
535       {
536          return new Vector JavaDoc().elements();
537       }
538    }
539
540    /**
541     * Should make SOAPSerializationException?
542     */

543    public void outputImpl(SerializationContext context) throws Exception JavaDoc
544    {
545
546       boolean oldPretty = context.getPretty();
547       context.setPretty(true);
548
549       // Register namespace prefixes.
550
if (namespaces != null)
551       {
552          for (Iterator JavaDoc i = namespaces.iterator(); i.hasNext();)
553          {
554             Mapping mapping = (Mapping)i.next();
555             context.registerPrefixForURI(mapping.getPrefix(),
556                     mapping.getNamespaceURI());
557          }
558       }
559
560       Enumeration JavaDoc en;
561
562       // Output <SOAP-ENV:Envelope>
563
context.startElement(new QName JavaDoc(soapConstants.getEnvelopeURI(),
564               Constants.ELEM_ENVELOPE), attributes);
565
566       // Output non-SOAPHeader and non-SOAPBody stuff.
567
for (Iterator JavaDoc it = getChildElements(); it.hasNext();)
568       {
569          Node JavaDoc childNode = (Node JavaDoc)it.next();
570          if (childNode instanceof SOAPHeaderAxisImpl || childNode instanceof SOAPBodyAxisImpl)
571             continue;
572
573          if (childNode instanceof SOAPElementAxisImpl)
574             ((SOAPElementAxisImpl)childNode).output(context);
575          else if (childNode instanceof TextImpl)
576             context.writeString(childNode.getNodeValue());
577       }
578
579       // Output headers
580
if (header != null)
581       {
582          header.outputImpl(context);
583       }
584
585       // Output body
586
if (body != null)
587       {
588          body.outputImpl(context);
589       }
590
591       // Output trailers
592
en = trailers.elements();
593       while (en.hasMoreElements())
594       {
595          SOAPElementAxisImpl element = (SOAPElementAxisImpl)en.nextElement();
596          element.output(context);
597          // Output this independent element
598
}
599
600       // Output </SOAP-ENV:Envelope>
601
context.endElement();
602
603       context.setPretty(oldPretty);
604
605       setModified(false);
606    }
607
608    /**
609     * Get the soap constants for this envelope
610     *
611     * @return
612     */

613    public SOAPConstants getSOAPConstants()
614    {
615       return soapConstants;
616    }
617
618    /**
619     * Set the soap constants for this envelope
620     *
621     * @param soapConstants
622     */

623    public void setSoapConstants(SOAPConstants soapConstants)
624    {
625       this.soapConstants = soapConstants;
626    }
627
628    /**
629     * Get the schema version for this envelope
630     *
631     * @return
632     */

633    public SchemaVersion getSchemaVersion()
634    {
635       return schemaVersion;
636    }
637
638    /**
639     * Set the schema version for this envelope
640     *
641     * @param schemaVersion
642     */

643    public void setSchemaVersion(SchemaVersion schemaVersion)
644    {
645       this.schemaVersion = schemaVersion;
646    }
647
648    /**
649     * Add a soap body if one does not exist
650     *
651     * @return
652     * @throws SOAPException
653     */

654    public javax.xml.soap.SOAPBody JavaDoc addBody() throws SOAPException JavaDoc
655    {
656       if (body == null)
657       {
658          setBody(new SOAPBodyAxisImpl(this, soapConstants));
659          return body;
660       }
661       else
662       {
663          throw new SOAPException JavaDoc(Messages.getMessage("bodyPresent"));
664       }
665    }
666
667    /**
668     * Add a soap header if one does not exist
669     *
670     * @return
671     * @throws SOAPException
672     */

673    public javax.xml.soap.SOAPHeader JavaDoc addHeader() throws SOAPException JavaDoc
674    {
675       if (header == null)
676       {
677          header = new SOAPHeaderAxisImpl(this, soapConstants);
678          return header;
679       }
680       else
681       {
682          throw new SOAPException JavaDoc(Messages.getMessage("headerPresent"));
683       }
684    }
685
686    /**
687     * create a Name given the local part
688     *
689     * @param localName
690     * @return
691     * @throws SOAPException
692     */

693    public javax.xml.soap.Name JavaDoc createName(String JavaDoc localName)
694            throws SOAPException JavaDoc
695    {
696       return new NameImpl(localName);
697    }
698
699    /**
700     * Create a name given local part, prefix and uri
701     *
702     * @param localName
703     * @param prefix
704     * @param uri
705     * @return
706     * @throws SOAPException
707     */

708    public javax.xml.soap.Name JavaDoc createName(String JavaDoc localName,
709                                          String JavaDoc prefix,
710                                          String JavaDoc uri)
711            throws SOAPException JavaDoc
712    {
713       return new NameImpl(localName, prefix, uri);
714    }
715
716    /**
717     * Get the soap body
718     */

719    public javax.xml.soap.SOAPBody JavaDoc getBody()
720    {
721       return body;
722    }
723
724    /**
725     * Get the soap header
726     *
727     * @return
728     */

729    public javax.xml.soap.SOAPHeader JavaDoc getHeader()
730    {
731       return header;
732    }
733
734    public void setSAAJEncodingCompliance(boolean comply)
735    {
736       this.body.setSAAJEncodingCompliance(comply);
737    }
738 }
739
Popular Tags