KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > Envelope


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 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 "SOAP" 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 and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap;
59
60 import java.io.*;
61 import java.util.*;
62 import org.w3c.dom.*;
63 import org.apache.soap.util.*;
64 import org.apache.soap.util.xml.*;
65 import org.apache.soap.encoding.*;
66 import org.apache.soap.rpc.SOAPContext;
67
68 /**
69  * An <code>Envelope</code> object represents the contents and semantics
70  * of an <code>&lt;SOAP-ENV:Envelope&gt;</code> element.
71  *
72  * @author Matthew J. Duftler (duftler@us.ibm.com)
73  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
74  */

75 public class Envelope
76 {
77   private Header header = null;
78   private Body body = null;
79   private Vector envelopeEntries = null;
80   private AttributeHandler attrHandler = new AttributeHandler();
81
82   public Envelope()
83   {
84     // Declare the "SOAP-ENV" namespace.
85
declareNamespace(Constants.NS_PRE_SOAP_ENV,
86                      Constants.NS_URI_SOAP_ENV);
87
88     // Declare the "xsi" namespace.
89
declareNamespace(Constants.NS_PRE_SCHEMA_XSI,
90                      Constants.NS_URI_CURRENT_SCHEMA_XSI);
91
92     // Declare the "xsd" namespace.
93
declareNamespace(Constants.NS_PRE_SCHEMA_XSD,
94                      Constants.NS_URI_CURRENT_SCHEMA_XSD);
95   }
96
97   public void setAttribute(QName attrQName, String JavaDoc value)
98   {
99     attrHandler.setAttribute(attrQName, value);
100   }
101
102   public String JavaDoc getAttribute(QName attrQName)
103   {
104     return attrHandler.getAttribute(attrQName);
105   }
106
107   public void removeAttribute(QName attrQName)
108   {
109     attrHandler.removeAttribute(attrQName);
110   }
111
112   public void declareNamespace(String JavaDoc nsPrefix, String JavaDoc namespaceURI)
113   {
114     attrHandler.declareNamespace(nsPrefix, namespaceURI);
115   }
116
117   public void setHeader(Header header)
118   {
119     this.header = header;
120   }
121
122   public Header getHeader()
123   {
124     return header;
125   }
126
127   public void setBody(Body body)
128   {
129     this.body = body;
130   }
131
132   public Body getBody()
133   {
134     return body;
135   }
136
137   public void setEnvelopeEntries(Vector envelopeEntries)
138   {
139     this.envelopeEntries = envelopeEntries;
140   }
141
142   public Vector getEnvelopeEntries()
143   {
144     return envelopeEntries;
145   }
146
147   /**
148    * Supported for backward compatibility.
149    *
150    * @see #marshall(java.io.Writer, org.apache.soap.util.xml.XMLJavaMappingRegistry, org.apache.soap.rpc.SOAPContext)
151    */

152   public void marshall(Writer sink, XMLJavaMappingRegistry xjmr)
153     throws IllegalArgumentException JavaDoc, IOException {
154     marshall(sink, xjmr, new SOAPContext());
155   }
156
157   public void marshall(Writer sink, XMLJavaMappingRegistry xjmr,
158                        SOAPContext ctx)
159     throws IllegalArgumentException JavaDoc, IOException
160   {
161     // Initialize the namespace stack.
162
NSStack nsStack = new NSStack();
163
164     attrHandler.populateNSStack(nsStack);
165
166     Header header = getHeader();
167     Body body = getBody();
168     Vector envelopeEntries = getEnvelopeEntries();
169     String JavaDoc declEncStyle = getAttribute(new QName(
170       Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE));
171
172     // Determine the prefix associated with the NS_URI_SOAP_ENV namespace URI.
173
String JavaDoc soapEnvNSPrefix = attrHandler.getUniquePrefixFromURI(
174       Constants.NS_URI_SOAP_ENV, Constants.NS_PRE_SOAP_ENV, nsStack);
175
176     // Write XML declaration
177
sink.write(Constants.XML_DECL);
178
179     sink.write('<' + soapEnvNSPrefix + ':' + Constants.ELEM_ENVELOPE);
180
181     // Serialize any envelope attributes.
182
attrHandler.marshall(sink, ctx);
183
184     sink.write('>' + StringUtils.lineSeparator);
185
186     // If there is a header, serialize it.
187
if (header != null)
188     {
189       header.marshall(sink, nsStack, xjmr, ctx);
190     }
191
192     // There must always be a <SOAP-ENV:Body>.
193
if (body != null)
194     {
195       body.marshall(declEncStyle, sink, nsStack, xjmr, ctx);
196     }
197     else
198     {
199       throw new IllegalArgumentException JavaDoc("An '" + Constants.Q_ELEM_ENVELOPE +
200                                          "' must contain a: '" +
201                                          Constants.Q_ELEM_BODY + "'.");
202     }
203
204     // Serialize any envelope entries (in addition to <SOAP-ENV:Body>).
205
if (envelopeEntries != null)
206     {
207       for (Enumeration e = envelopeEntries.elements(); e.hasMoreElements();)
208       {
209         Element envelopeEntryEl = (Element)e.nextElement();
210
211         Utils.marshallNode(envelopeEntryEl, sink);
212
213         sink.write(StringUtils.lineSeparator);
214       }
215     }
216
217     sink.write("</" + soapEnvNSPrefix + ':' + Constants.ELEM_ENVELOPE +
218                '>' + StringUtils.lineSeparator);
219   }
220
221   /**
222    * Supported for backward compatibility.
223    *
224    * @see #unmarshall(org.w3c.dom.Node, org.apache.soap.rpc.SOAPContext)
225    */

226   public static Envelope unmarshall(Node src)
227     throws IllegalArgumentException JavaDoc {
228       return unmarshall(src, new SOAPContext());
229   }
230
231   public static Envelope unmarshall(Node src, SOAPContext ctx)
232     throws IllegalArgumentException JavaDoc
233   {
234     Element root = (Element)src;
235     Envelope env = new Envelope();
236
237     if (Constants.Q_ELEM_ENVELOPE.matches(root))
238     {
239       // Deserialize any envelope attributes.
240
env.attrHandler = AttributeHandler.unmarshall(root, ctx);
241
242       // Examine the subelements of the envelope.
243
Element headerEl = null;
244       Element bodyEl = null;
245       Element tempEl = DOMUtils.getFirstChildElement(root);
246
247       if (Constants.Q_ELEM_HEADER.matches(tempEl))
248       {
249         headerEl = tempEl;
250         tempEl = DOMUtils.getNextSiblingElement(tempEl);
251       }
252
253       if (Constants.Q_ELEM_BODY.matches(tempEl))
254       {
255         bodyEl = tempEl;
256         tempEl = DOMUtils.getNextSiblingElement(tempEl);
257       }
258
259       // Deserialize any header entries.
260
if (headerEl != null)
261       {
262         Header header = Header.unmarshall(headerEl, ctx);
263
264         env.setHeader(header);
265       }
266
267       // Deserialize any body entries.
268
if (bodyEl != null)
269       {
270         Body body = Body.unmarshall(bodyEl, ctx);
271
272         env.setBody(body);
273       }
274       else
275       {
276         throw new IllegalArgumentException JavaDoc("An '" + Constants.Q_ELEM_ENVELOPE +
277                                            "' element must contain a: '" +
278                                            Constants.Q_ELEM_BODY +
279                                            "' element.");
280       }
281
282       // Deserialize any envelope entries (in addition to <SOAP-ENV:Body>).
283
if (tempEl != null)
284       {
285         Vector envelopeEntries = new Vector();
286
287         while (tempEl != null)
288         {
289           envelopeEntries.addElement(tempEl);
290           tempEl = DOMUtils.getNextSiblingElement(tempEl);
291         }
292
293         env.setEnvelopeEntries(envelopeEntries);
294       }
295     }
296     else
297     {
298       String JavaDoc localName = root.getLocalName();
299
300       if (localName != null && localName.equals(Constants.ELEM_ENVELOPE))
301       {
302         throw new IllegalArgumentException JavaDoc(Constants.ERR_MSG_VERSION_MISMATCH);
303       }
304       else
305       {
306         throw new IllegalArgumentException JavaDoc("Root element of a SOAP message " +
307                                            "must be: '" +
308                                            Constants.Q_ELEM_ENVELOPE + "'.");
309       }
310     }
311
312     return env;
313   }
314
315   public String JavaDoc toString()
316   {
317     StringWriter sw = new StringWriter();
318     PrintWriter pw = new PrintWriter(sw);
319
320     pw.print("[Attributes=" + attrHandler + "] " +
321              "[Header=" + header + "] " +
322              "[Body=" + body + "] " +
323              "[EnvelopeEntries=");
324
325     if (envelopeEntries != null)
326     {
327       pw.println();
328
329       for (int i = 0; i < envelopeEntries.size(); i++)
330       {
331         pw.println("[(" + i + ")=" +
332              DOM2Writer.nodeToString((Element)envelopeEntries.elementAt(i)) +
333              "]");
334       }
335     }
336
337     pw.print("]");
338
339     return sw.toString();
340   }
341 }
342
Popular Tags