KickJava   Java API By Example, From Geeks To Geeks.

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


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.MessageContext;
60 import org.jboss.axis.encoding.DeserializationContext;
61 import org.jboss.axis.soap.SOAPConstants;
62 import org.jboss.axis.utils.Messages;
63 import org.xml.sax.Attributes JavaDoc;
64 import org.xml.sax.SAXException JavaDoc;
65
66 import javax.xml.namespace.QName JavaDoc;
67
68 /**
69  * The EnvelopeBuilder is responsible for parsing the top-level
70  * SOAP envelope stuff (Envelope, Body, Header), and spawning off
71  * HeaderBuilder and BodyBuilders.
72  *
73  * @author Glen Daniels (gdaniels@allaire.com)
74  * @author Andras Avar (andras.avar@nokia.com)
75  */

76 public class EnvelopeBuilder extends SOAPHandler
77 {
78    private SOAPEnvelopeAxisImpl envelope;
79    private SOAPConstants soapConstants = SOAPConstants.SOAP11_CONSTANTS;
80
81    private boolean gotHeader = false;
82    private boolean gotBody = false;
83
84    public EnvelopeBuilder(String JavaDoc messageType, SOAPConstants soapConstants)
85    {
86       envelope = new SOAPEnvelopeAxisImpl(false, soapConstants);
87       envelope.setMessageType(messageType);
88       myElement = envelope;
89    }
90
91    public EnvelopeBuilder(SOAPEnvelopeAxisImpl env, String JavaDoc messageType)
92    {
93       envelope = env;
94       envelope.setMessageType(messageType);
95       myElement = envelope;
96    }
97
98    public SOAPEnvelopeAxisImpl getEnvelope()
99    {
100       return envelope;
101    }
102
103    public void startElement(String JavaDoc namespace, String JavaDoc localName,
104                             String JavaDoc prefix, Attributes JavaDoc attributes,
105                             DeserializationContext context)
106            throws SAXException JavaDoc
107    {
108       if (!localName.equals(Constants.ELEM_ENVELOPE))
109          throw new SAXException JavaDoc(Messages.getMessage("badTag00", localName));
110
111       // See if we're only supporting a single SOAP version at this endpoint
112
MessageContext msgContext = context.getMessageContext();
113       SOAPConstants singleVersion = null;
114       if (msgContext != null)
115       {
116          singleVersion = (SOAPConstants)msgContext.getProperty(Constants.MC_SINGLE_SOAP_VERSION);
117       }
118
119       if (namespace.equals(Constants.URI_SOAP11_ENV))
120       {
121          // SOAP 1.1
122
soapConstants = SOAPConstants.SOAP11_CONSTANTS;
123       }
124       else if (namespace.equals(Constants.URI_SOAP12_ENV))
125       {
126          // SOAP 1.2
127
soapConstants = SOAPConstants.SOAP12_CONSTANTS;
128       }
129       else
130       {
131          soapConstants = null;
132       }
133
134       if ((soapConstants == null) ||
135               (singleVersion != null && soapConstants != singleVersion))
136       {
137          // Mismatch of some sort, either an unknown namespace or not
138
// the one we want. Send back an appropriate fault.
139

140          // Right now we only send back SOAP 1.1 faults for this case. Do
141
// we want to send SOAP 1.2 faults back to SOAP 1.2 endpoints?
142
soapConstants = SOAPConstants.SOAP11_CONSTANTS;
143          if (singleVersion == null) singleVersion = soapConstants;
144
145          try
146          {
147             AxisFault fault = new AxisFault(soapConstants.getVerMismatchFaultCodeQName(),
148                     null, Messages.getMessage("versionMissmatch00"), null, null, null);
149
150             SOAPHeaderElementAxisImpl newHeader = new
151                     SOAPHeaderElementAxisImpl(soapConstants.getEnvelopeURI(),
152                             Constants.ELEM_UPGRADE);
153
154             // TODO: insert soap 1.1 upgrade header in case of soap 1.2 response if
155
// axis supports both simultaneously
156
SOAPElementAxisImpl innerHeader = new
157                     SOAPElementAxisImpl(soapConstants.getEnvelopeURI(),
158                             Constants.ELEM_SUPPORTEDENVELOPE);
159             innerHeader.addAttribute(null, Constants.ATTR_QNAME,
160                     new QName JavaDoc(singleVersion.getEnvelopeURI(), Constants.ELEM_ENVELOPE));
161
162             newHeader.addChildElement(innerHeader);
163             fault.addHeader(newHeader);
164
165             throw new SAXException JavaDoc(fault);
166
167          }
168          catch (javax.xml.soap.SOAPException JavaDoc e)
169          {
170             throw new SAXException JavaDoc(e);
171          }
172       }
173
174       // Indicate what version of SOAP we're using to anyone else involved
175
// in processing this message.
176
if (context.getMessageContext() != null)
177          context.getMessageContext().setSOAPConstants(soapConstants);
178
179       if (soapConstants == SOAPConstants.SOAP12_CONSTANTS &&
180               attributes.getValue(Constants.URI_SOAP12_ENV, Constants.ATTR_ENCODING_STYLE) != null)
181       {
182
183          AxisFault fault = new AxisFault(Constants.FAULT_SOAP12_SENDER,
184                  null, Messages.getMessage("noEncodingStyleAttrAppear", "Envelope"), null, null, null);
185
186          throw new SAXException JavaDoc(fault);
187       }
188
189       envelope.setPrefix(prefix);
190       envelope.setNamespaceURI(namespace);
191       envelope.setNSMappings(context.getCurrentNSMappings());
192       envelope.setSoapConstants(soapConstants);
193       context.pushNewElement(envelope);
194    }
195
196    public SOAPHandler onStartChild(String JavaDoc namespace,
197                                    String JavaDoc localName,
198                                    String JavaDoc prefix,
199                                    Attributes JavaDoc attributes,
200                                    DeserializationContext context)
201            throws SAXException JavaDoc
202    {
203       QName JavaDoc thisQName = new QName JavaDoc(namespace, localName);
204       if (thisQName.equals(soapConstants.getHeaderQName()))
205       {
206          if (gotHeader)
207             throw new SAXException JavaDoc(Messages.getMessage("only1Header00"));
208
209          gotHeader = true;
210          return new HeaderBuilder(envelope);
211       }
212
213       if (thisQName.equals(soapConstants.getBodyQName()))
214       {
215          if (gotBody)
216             throw new SAXException JavaDoc(Messages.getMessage("only1Body00"));
217
218          gotBody = true;
219          return new BodyBuilder(envelope);
220       }
221
222       if (!gotBody)
223          throw new SAXException JavaDoc(Messages.getMessage("noCustomElems00"));
224
225       if (soapConstants == SOAPConstants.SOAP12_CONSTANTS)
226       {
227          throw new SAXException JavaDoc(Messages.getMessage("noElemAfterBody12"));
228       }
229
230       try
231       {
232          SOAPElementAxisImpl element = new SOAPElementAxisImpl(namespace, localName, prefix,
233                  attributes, context);
234
235          if (element.getFixupDeserializer() != null)
236             return (SOAPHandler)element.getFixupDeserializer();
237       }
238       catch (AxisFault axisFault)
239       {
240          throw new SAXException JavaDoc(axisFault);
241       }
242
243       return null;
244    }
245
246    public void onEndChild(String JavaDoc namespace, String JavaDoc localName,
247                           DeserializationContext context)
248    {
249    }
250
251    public void endElement(String JavaDoc namespace, String JavaDoc localName,
252                           DeserializationContext context)
253            throws SAXException JavaDoc
254    {
255       // Envelope isn't dirty yet by default...
256
envelope.setDirty(false);
257    }
258 }
259
Popular Tags