KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package org.apache.axis.message;
18
19 /**
20  *
21  * @author Glen Daniels (gdaniels@allaire.com)
22  */

23
24 import org.apache.axis.AxisFault;
25 import org.apache.axis.Constants;
26 import org.apache.axis.MessageContext;
27 import org.apache.axis.Message;
28 import org.apache.axis.components.logger.LogFactory;
29 import org.apache.axis.description.OperationDesc;
30 import org.apache.axis.encoding.DeserializationContext;
31 import org.apache.axis.constants.Style;
32 import org.apache.axis.soap.SOAPConstants;
33 import org.apache.axis.utils.Messages;
34 import org.apache.commons.logging.Log;
35 import org.xml.sax.Attributes JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 import javax.xml.namespace.QName JavaDoc;
39
40 public class BodyBuilder extends SOAPHandler
41 {
42     protected static Log log =
43         LogFactory.getLog(BodyBuilder.class.getName());
44
45     boolean gotRPCElement = false;
46
47     private SOAPEnvelope envelope;
48
49     BodyBuilder(SOAPEnvelope envelope)
50     {
51         this.envelope = envelope;
52     }
53
54     public void startElement(String JavaDoc namespace, String JavaDoc localName,
55                              String JavaDoc prefix, Attributes JavaDoc attributes,
56                              DeserializationContext context)
57         throws SAXException JavaDoc
58     {
59         SOAPConstants soapConstants = context.getSOAPConstants();
60
61         if (soapConstants == SOAPConstants.SOAP12_CONSTANTS &&
62             attributes.getValue(Constants.URI_SOAP12_ENV, Constants.ATTR_ENCODING_STYLE) != null) {
63
64             AxisFault fault = new AxisFault(Constants.FAULT_SOAP12_SENDER,
65                 null, Messages.getMessage("noEncodingStyleAttrAppear", "Body"), null, null, null);
66
67             throw new SAXException JavaDoc(fault);
68         }
69
70         // make a new body element
71
if (!context.isDoneParsing()) {
72             if (!context.isProcessingRef()) {
73                 if (myElement == null) {
74                     try {
75                         myElement = new SOAPBody(namespace, localName, prefix,
76                                             attributes, context, envelope.getSOAPConstants());
77                     } catch (AxisFault axisFault) {
78                         throw new SAXException JavaDoc(axisFault);
79                     }
80                 }
81                 context.pushNewElement(myElement);
82             }
83             envelope.setBody((SOAPBody)myElement);
84         }
85     }
86
87     // FIX: do we need this method ?
88
public MessageElement makeNewElement(String JavaDoc namespace, String JavaDoc localName,
89                                          String JavaDoc prefix, Attributes JavaDoc attributes,
90                                          DeserializationContext context)
91         throws AxisFault {
92         return new SOAPBody(namespace,
93                             localName,
94                             prefix,
95                             attributes,
96                             context,
97                             context.getSOAPConstants());
98     }
99
100     public SOAPHandler onStartChild(String JavaDoc namespace,
101                                      String JavaDoc localName,
102                                      String JavaDoc prefix,
103                                      Attributes JavaDoc attributes,
104                                      DeserializationContext context)
105         throws SAXException JavaDoc
106     {
107         SOAPBodyElement element = null;
108         if (log.isDebugEnabled()) {
109             log.debug("Enter: BodyBuilder::onStartChild()");
110         }
111
112         QName JavaDoc qname = new QName JavaDoc(namespace, localName);
113         SOAPHandler handler = null;
114
115         /** We're about to create a body element. So we really need
116          * to know at this point if this is an RPC service or not. It's
117          * possible that no one has set the service up until this point,
118          * so if that's the case we should attempt to set it based on the
119          * namespace of the first root body element. Setting the
120          * service may (should?) result in setting the service
121          * description, which can then tell us what to create.
122          */

123         boolean isRoot = true;
124         String JavaDoc root = attributes.getValue(Constants.URI_DEFAULT_SOAP_ENC,
125                                           Constants.ATTR_ROOT);
126         if ((root != null) && root.equals("0")) isRoot = false;
127
128         MessageContext msgContext = context.getMessageContext();
129         OperationDesc [] operations = null;
130         try {
131             if(msgContext != null) {
132                  operations = msgContext.getPossibleOperationsByQName(qname);
133             }
134
135             // If there's only one match, set it in the MC now
136
if ((operations != null) && (operations.length == 1))
137                 msgContext.setOperation(operations[0]);
138         } catch (org.apache.axis.AxisFault e) {
139             // SAXException is already known to this method, so I
140
// don't have an exception-handling propogation explosion.
141
throw new SAXException JavaDoc(e);
142         }
143
144         Style style = operations == null ? Style.RPC : operations[0].getStyle();
145         SOAPConstants soapConstants = context.getSOAPConstants();
146
147         /** Now we make a plain SOAPBodyElement IF we either:
148          * a) have an non-root element, or
149          * b) have a non-RPC service
150          */

151         if (localName.equals(Constants.ELEM_FAULT) &&
152             namespace.equals(soapConstants.getEnvelopeURI())) {
153             try {
154                 element = new SOAPFault(namespace, localName, prefix,
155                                                attributes, context);
156             } catch (AxisFault axisFault) {
157                 throw new SAXException JavaDoc(axisFault);
158             }
159             element.setEnvelope(context.getEnvelope());
160             handler = new SOAPFaultBuilder((SOAPFault)element,
161                                            context);
162         } else if (!gotRPCElement) {
163             if (isRoot && (style != Style.MESSAGE)) {
164                 gotRPCElement = true;
165
166                 try {
167
168                     element = new RPCElement(namespace, localName, prefix,
169                             attributes, context, operations);
170
171                 } catch (org.apache.axis.AxisFault e) {
172                     // SAXException is already known to this method, so I
173
// don't have an exception-handling propogation explosion.
174
//
175
throw new SAXException JavaDoc(e);
176                 }
177
178                 // Only deserialize this way if there is a unique operation
179
// for this QName. If there are overloads,
180
// we'll need to start recording. If we're making a high-
181
// fidelity recording anyway, don't bother (for now).
182
if (msgContext != null && !msgContext.isHighFidelity() &&
183                         (operations == null || operations.length == 1)) {
184                     ((RPCElement)element).setNeedDeser(false);
185                     boolean isResponse = false;
186                     if (msgContext.getCurrentMessage() != null &&
187                                 Message.RESPONSE.equals(msgContext.getCurrentMessage().getMessageType()))
188                         isResponse = true;
189                     handler = new RPCHandler((RPCElement)element,
190                                              isResponse);
191                     if (operations != null) {
192                         ((RPCHandler)handler).setOperation(operations[0]);
193                         msgContext.setOperation(operations[0]);
194                     }
195                 }
196             }
197         }
198
199         if (element == null) {
200             if ((style == Style.RPC) &&
201                 soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
202                 throw new SAXException JavaDoc(Messages.getMessage("onlyOneBodyFor12"));
203             }
204             try {
205                 element = new SOAPBodyElement(namespace, localName, prefix,
206                                           attributes, context);
207             } catch (AxisFault axisFault) {
208                 throw new SAXException JavaDoc(axisFault);
209             }
210             if (element.getFixupDeserializer() != null)
211                 handler = (SOAPHandler)element.getFixupDeserializer();
212         }
213
214         if (handler == null)
215             handler = new SOAPHandler();
216
217         handler.myElement = element;
218
219         //context.pushNewElement(element);
220

221         if (log.isDebugEnabled()) {
222             log.debug("Exit: BodyBuilder::onStartChild()");
223         }
224         return handler;
225     }
226
227     public void onEndChild(String JavaDoc namespace, String JavaDoc localName,
228                            DeserializationContext context)
229     {
230     }
231 }
232
Popular Tags