KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constants;
19 import org.apache.axis.encoding.DeserializationContext;
20 import org.apache.axis.soap.SOAPConstants;
21 import org.apache.axis.utils.Messages;
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.apache.axis.AxisFault;
25 import org.apache.axis.MessageContext;
26
27 import javax.xml.namespace.QName JavaDoc;
28
29 /**
30  * The EnvelopeBuilder is responsible for parsing the top-level
31  * SOAP envelope stuff (Envelope, Body, Header), and spawning off
32  * HeaderBuilder and BodyBuilders.
33  *
34  * @author Glen Daniels (gdaniels@allaire.com)
35  * @author Andras Avar (andras.avar@nokia.com)
36  */

37 public class EnvelopeBuilder extends SOAPHandler
38 {
39     private SOAPEnvelope envelope;
40     private SOAPConstants soapConstants = SOAPConstants.SOAP11_CONSTANTS;
41
42     private boolean gotHeader = false;
43     private boolean gotBody = false;
44
45     public EnvelopeBuilder(String JavaDoc messageType, SOAPConstants soapConstants)
46     {
47         envelope = new SOAPEnvelope(false, soapConstants);
48         envelope.setMessageType(messageType);
49         myElement = envelope;
50     }
51
52     public EnvelopeBuilder(SOAPEnvelope env, String JavaDoc messageType)
53     {
54         envelope = env ;
55         envelope.setMessageType(messageType);
56         myElement = envelope;
57     }
58
59     public SOAPEnvelope getEnvelope()
60     {
61         return envelope;
62     }
63
64     public void startElement(String JavaDoc namespace, String JavaDoc localName,
65                              String JavaDoc prefix, Attributes JavaDoc attributes,
66                              DeserializationContext context)
67         throws SAXException JavaDoc
68     {
69         if (!localName.equals(Constants.ELEM_ENVELOPE))
70             throw new SAXException JavaDoc(
71                     Messages.getMessage("badTag00", localName));
72         
73         // See if we're only supporting a single SOAP version at this endpoint
74
MessageContext msgContext = context.getMessageContext();
75         SOAPConstants singleVersion = null;
76         if (msgContext != null) {
77             singleVersion = (SOAPConstants)msgContext.getProperty(
78                                             Constants.MC_SINGLE_SOAP_VERSION);
79         }
80
81         if (namespace.equals(Constants.URI_SOAP11_ENV)) {
82             // SOAP 1.1
83
soapConstants = SOAPConstants.SOAP11_CONSTANTS;
84         } else if (namespace.equals(Constants.URI_SOAP12_ENV)) {
85             // SOAP 1.2
86
soapConstants = SOAPConstants.SOAP12_CONSTANTS;
87         } else {
88             soapConstants = null;
89         }
90         
91         if ((soapConstants == null) ||
92                 (singleVersion != null && soapConstants != singleVersion)) {
93             // Mismatch of some sort, either an unknown namespace or not
94
// the one we want. Send back an appropriate fault.
95

96             // Right now we only send back SOAP 1.1 faults for this case. Do
97
// we want to send SOAP 1.2 faults back to SOAP 1.2 endpoints?
98
soapConstants = SOAPConstants.SOAP11_CONSTANTS;
99             if (singleVersion == null) singleVersion = soapConstants;
100             
101             try {
102                 AxisFault fault = new AxisFault(soapConstants.getVerMismatchFaultCodeQName(),
103                     null, Messages.getMessage("versionMissmatch00"), null, null, null);
104
105                 SOAPHeaderElement newHeader = new
106                                 SOAPHeaderElement(soapConstants.getEnvelopeURI(),
107                                                   Constants.ELEM_UPGRADE);
108
109                 // TODO: insert soap 1.1 upgrade header in case of soap 1.2 response if
110
// axis supports both simultaneously
111
MessageElement innerHeader = new
112                                 MessageElement(soapConstants.getEnvelopeURI(),
113                                                   Constants.ELEM_SUPPORTEDENVELOPE);
114                 innerHeader.addAttribute(null, Constants.ATTR_QNAME,
115                     new QName JavaDoc(singleVersion.getEnvelopeURI(), Constants.ELEM_ENVELOPE));
116
117                 newHeader.addChildElement(innerHeader);
118                 fault.addHeader(newHeader);
119
120                 throw new SAXException JavaDoc(fault);
121
122             } catch (javax.xml.soap.SOAPException JavaDoc e) {
123                 throw new SAXException JavaDoc(e);
124             }
125         }
126
127         // Indicate what version of SOAP we're using to anyone else involved
128
// in processing this message.
129
if(context.getMessageContext() != null)
130             context.getMessageContext().setSOAPConstants(soapConstants);
131
132         if (soapConstants == SOAPConstants.SOAP12_CONSTANTS &&
133             attributes.getValue(Constants.URI_SOAP12_ENV, Constants.ATTR_ENCODING_STYLE) != null) {
134
135             AxisFault fault = new AxisFault(Constants.FAULT_SOAP12_SENDER,
136                 null, Messages.getMessage("noEncodingStyleAttrAppear", "Envelope"), null, null, null);
137
138             throw new SAXException JavaDoc(fault);
139         }
140
141         envelope.setPrefix(prefix);
142         envelope.setNamespaceURI(namespace);
143         envelope.setNSMappings(context.getCurrentNSMappings());
144         envelope.setSoapConstants(soapConstants);
145         context.pushNewElement(envelope);
146     }
147
148     public SOAPHandler onStartChild(String JavaDoc namespace,
149                                     String JavaDoc localName,
150                                     String JavaDoc prefix,
151                                     Attributes JavaDoc attributes,
152                                     DeserializationContext context)
153         throws SAXException JavaDoc
154     {
155         QName JavaDoc thisQName = new QName JavaDoc(namespace, localName);
156         if (thisQName.equals(soapConstants.getHeaderQName())) {
157             if (gotHeader)
158                 throw new SAXException JavaDoc(Messages.getMessage("only1Header00"));
159
160             gotHeader = true;
161             return new HeaderBuilder(envelope);
162         }
163
164         if (thisQName.equals(soapConstants.getBodyQName())) {
165             if (gotBody)
166                 throw new SAXException JavaDoc(Messages.getMessage("only1Body00"));
167
168             gotBody = true;
169             return new BodyBuilder(envelope);
170         }
171
172         if (!gotBody)
173             throw new SAXException JavaDoc(Messages.getMessage("noCustomElems00"));
174
175         if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
176             throw new SAXException JavaDoc(Messages.getMessage("noElemAfterBody12"));
177         }
178
179         try {
180             MessageElement element = new MessageElement(namespace, localName, prefix,
181                                          attributes, context);
182
183             if (element.getFixupDeserializer() != null)
184                 return (SOAPHandler)element.getFixupDeserializer();
185         } catch (AxisFault axisFault) {
186             throw new SAXException JavaDoc(axisFault);
187         }
188
189         return null;
190     }
191
192     public void onEndChild(String JavaDoc namespace, String JavaDoc localName,
193                            DeserializationContext context)
194     {
195     }
196
197     public void endElement(String JavaDoc namespace, String JavaDoc localName,
198                            DeserializationContext context)
199         throws SAXException JavaDoc
200     {
201         // Envelope isn't dirty yet by default...
202
envelope.setDirty(false);
203     }
204 }
205
Popular Tags