KickJava   Java API By Example, From Geeks To Geeks.

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


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
56 package org.jboss.axis.message;
57
58 /**
59  *
60  * @author Glen Daniels (gdaniels@allaire.com)
61  */

62
63 import org.jboss.axis.AxisFault;
64 import org.jboss.axis.Constants;
65 import org.jboss.axis.MessageContext;
66 import org.jboss.axis.description.OperationDesc;
67 import org.jboss.axis.encoding.DeserializationContext;
68 import org.jboss.axis.enums.Style;
69 import org.jboss.axis.soap.SOAPConstants;
70 import org.jboss.axis.utils.Messages;
71 import org.jboss.logging.Logger;
72 import org.xml.sax.Attributes JavaDoc;
73 import org.xml.sax.SAXException JavaDoc;
74
75 import javax.xml.namespace.QName JavaDoc;
76
77 public class BodyBuilder extends SOAPHandler
78 {
79    private static Logger log = Logger.getLogger(BodyBuilder.class.getName());
80
81    boolean gotRPCElement = false;
82
83    private SOAPEnvelopeAxisImpl envelope;
84
85    BodyBuilder(SOAPEnvelopeAxisImpl envelope)
86    {
87       this.envelope = envelope;
88    }
89
90    public void startElement(String JavaDoc namespace, String JavaDoc localName,
91                             String JavaDoc prefix, Attributes JavaDoc attributes,
92                             DeserializationContext context)
93            throws SAXException JavaDoc
94    {
95       SOAPConstants soapConstants = Constants.DEFAULT_SOAP_VERSION;
96       if (context.getMessageContext() != null)
97          soapConstants = context.getMessageContext().getSOAPConstants();
98
99       if (soapConstants == SOAPConstants.SOAP12_CONSTANTS &&
100               attributes.getValue(Constants.URI_SOAP12_ENV, Constants.ATTR_ENCODING_STYLE) != null)
101       {
102
103          AxisFault fault = new AxisFault(Constants.FAULT_SOAP12_SENDER,
104                  null, Messages.getMessage("noEncodingStyleAttrAppear", "Body"), null, null, null);
105
106          throw new SAXException JavaDoc(fault);
107       }
108
109       // make a new body element
110
if (!context.isDoneParsing())
111       {
112          if (!context.isProcessingRef())
113          {
114             if (myElement == null)
115             {
116                try
117                {
118                   myElement = new SOAPBodyAxisImpl(namespace, localName, prefix,
119                           attributes, context, envelope.getSOAPConstants());
120                }
121                catch (AxisFault axisFault)
122                {
123                   throw new SAXException JavaDoc(axisFault);
124                }
125             }
126             context.pushNewElement(myElement);
127          }
128          envelope.setBody((SOAPBodyAxisImpl)myElement);
129       }
130    }
131
132    // FIX: do we need this method ?
133
public SOAPElementAxisImpl makeNewElement(String JavaDoc namespace, String JavaDoc localName,
134                                              String JavaDoc prefix, Attributes JavaDoc attributes,
135                                              DeserializationContext context)
136            throws AxisFault
137    {
138       SOAPConstants soapConstants = context.getMessageContext() == null ?
139               SOAPConstants.SOAP11_CONSTANTS :
140               context.getMessageContext().getSOAPConstants();
141       return new SOAPBodyAxisImpl(namespace,
142               localName,
143               prefix,
144               attributes,
145               context,
146               soapConstants);
147    }
148
149    public SOAPHandler onStartChild(String JavaDoc namespace,
150                                    String JavaDoc localName,
151                                    String JavaDoc prefix,
152                                    Attributes JavaDoc attributes,
153                                    DeserializationContext context)
154            throws SAXException JavaDoc
155    {
156       SOAPBodyElementAxisImpl element = null;
157       if (log.isDebugEnabled())
158       {
159          log.debug("Enter: BodyBuilder::onStartChild()");
160       }
161
162       QName JavaDoc qname = new QName JavaDoc(namespace, localName);
163       SOAPHandler handler = null;
164
165       /** We're about to create a body element. So we really need
166        * to know at this point if this is an RPC service or not. It's
167        * possible that no one has set the service up until this point,
168        * so if that's the case we should attempt to set it based on the
169        * namespace of the first root body element. Setting the
170        * service may (should?) result in setting the service
171        * description, which can then tell us what to create.
172        */

173       boolean isRoot = true;
174       String JavaDoc root = attributes.getValue(Constants.URI_DEFAULT_SOAP_ENC,
175               Constants.ATTR_ROOT);
176       if ((root != null) && root.equals("0")) isRoot = false;
177
178       MessageContext msgContext = context.getMessageContext();
179       OperationDesc[] operations = null;
180       try
181       {
182          if (msgContext != null)
183          {
184             operations = msgContext.getPossibleOperationsByQName(qname);
185          }
186
187          // If there's only one match, set it in the MC now
188
if ((operations != null) && (operations.length == 1))
189             msgContext.setOperation(operations[0]);
190       }
191       catch (org.jboss.axis.AxisFault e)
192       {
193          // SAXException is already known to this method, so I
194
// don't have an exception-handling propogation explosion.
195
throw new SAXException JavaDoc(e);
196       }
197
198       Style style = operations == null ? Style.RPC : operations[0].getStyle();
199       MessageContext messageContext = context.getMessageContext();
200       SOAPConstants soapConstants = messageContext == null ? SOAPConstants.SOAP11_CONSTANTS : messageContext.getSOAPConstants();
201
202       /** Now we make a plain SOAPBodyElement IF we either:
203        * a) have an non-root element, or
204        * b) have a non-RPC service
205        */

206       if (Constants.ELEM_FAULT.equals(localName) && namespace.equals(soapConstants.getEnvelopeURI()))
207       {
208          try
209          {
210             element = new SOAPFaultImpl(namespace, localName, prefix,
211                     attributes, context);
212          }
213          catch (AxisFault axisFault)
214          {
215             throw new SAXException JavaDoc(axisFault);
216          }
217          element.setEnvelope(context.getEnvelope());
218          handler = new SOAPFaultBuilder((SOAPFaultImpl)element,
219                  context);
220       }
221       else if (!gotRPCElement)
222       {
223          if (isRoot && (style != Style.MESSAGE))
224          {
225             gotRPCElement = true;
226
227             try
228             {
229
230                element = new RPCElement(namespace, localName, prefix,
231                        attributes, context, operations);
232
233             }
234             catch (org.jboss.axis.AxisFault e)
235             {
236                // SAXException is already known to this method, so I
237
// don't have an exception-handling propogation explosion.
238
//
239
throw new SAXException JavaDoc(e);
240             }
241
242             // SBFIX : If we're here with no operations, we're going to have
243
// a dispatch problem. If SOAP12, fault.
244
if (operations == null &&
245                     (msgContext != null && !msgContext.isClient() &&
246                     (msgContext.getProperty(Constants.MC_NO_OPERATION_OK) == null)) &&
247                     soapConstants == SOAPConstants.SOAP12_CONSTANTS)
248             {
249                AxisFault fault =
250                        new AxisFault(Constants.FAULT_SOAP12_SENDER,
251                                "No such procedure", null, null);
252                fault.addFaultSubCode(Constants.FAULT_SUBCODE_PROC_NOT_PRESENT);
253                throw new SAXException JavaDoc(fault);
254             }
255
256             // Only deserialize this way if there is a unique operation
257
// for this QName. If there are overloads,
258
// we'll need to start recording. If we're making a high-
259
// fidelity recording anyway, don't bother (for now).
260
if (msgContext != null && !msgContext.isHighFidelity())
261             {
262                if (operations == null || operations.length == 1)
263                {
264                   ((RPCElement)element).setNeedDeser(false);
265                   handler = new RPCHandler((RPCElement)element, false);
266                   if (operations != null)
267                   {
268                      ((RPCHandler)handler).setOperation(operations[0]);
269                      msgContext.setOperation(operations[0]);
270                   }
271                }
272             }
273
274             if (handler == null)
275                handler = new RPCElementHandler();
276          }
277       }
278
279       if (element == null)
280       {
281          if ((style == Style.RPC) &&
282                  soapConstants == SOAPConstants.SOAP12_CONSTANTS)
283          {
284             throw new SAXException JavaDoc(Messages.getMessage("onlyOneBodyFor12"));
285          }
286          try
287          {
288             element = new SOAPBodyElementAxisImpl(namespace, localName, prefix,
289                     attributes, context);
290          }
291          catch (AxisFault axisFault)
292          {
293             throw new SAXException JavaDoc(axisFault);
294          }
295          if (element.getFixupDeserializer() != null)
296             handler = (SOAPHandler)element.getFixupDeserializer();
297       }
298
299       if (handler == null)
300          handler = new SOAPHandler();
301
302       handler.myElement = element;
303
304       //context.pushNewElement(element);
305

306       if (log.isDebugEnabled())
307       {
308          log.debug("Exit: BodyBuilder::onStartChild()");
309       }
310       return handler;
311    }
312
313    public void onEndChild(String JavaDoc namespace, String JavaDoc localName,
314                           DeserializationContext context)
315    {
316    }
317 }
318
Popular Tags