KickJava   Java API By Example, From Geeks To Geeks.

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


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.constants.Style;
27 import org.apache.axis.components.logger.LogFactory;
28 import org.apache.axis.description.OperationDesc;
29 import org.apache.axis.description.ParameterDesc;
30 import org.apache.axis.encoding.DeserializationContext;
31 import org.apache.axis.encoding.Deserializer;
32 import org.apache.axis.encoding.DeserializerImpl;
33 import org.apache.axis.encoding.XMLType;
34 import org.apache.axis.soap.SOAPConstants;
35 import org.apache.axis.utils.JavaUtils;
36 import org.apache.axis.utils.Messages;
37 import org.apache.commons.logging.Log;
38 import org.w3c.dom.Element JavaDoc;
39 import org.xml.sax.Attributes JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41
42 import javax.xml.namespace.QName JavaDoc;
43
44 /**
45  * This is the SOAPHandler which is called for each RPC parameter as we're
46  * deserializing the XML for a method call or return. In other words for
47  * this XML:
48  *
49  * <methodName>
50  * <param1 xsi:type="xsd:string">Hello!</param1>
51  * <param2>3.14159</param2>
52  * </methodName>
53  *
54  * ...we'll get onStartChild() events for <param1> and <param2>.
55  *
56  * @author Glen Daniels (gdaniels@apache.org)
57  */

58 public class RPCHandler extends SOAPHandler
59 {
60     protected static Log log =
61         LogFactory.getLog(RPCHandler.class.getName());
62     
63     private RPCElement rpcElem;
64     private RPCParam currentParam = null;
65     private boolean isResponse;
66     private OperationDesc operation;
67     private boolean isHeaderElement;
68
69     public RPCHandler(RPCElement rpcElem, boolean isResponse)
70         throws SAXException JavaDoc
71     {
72         this.rpcElem = rpcElem;
73         this.isResponse = isResponse;
74     }
75
76     public void setOperation(OperationDesc myOperation) {
77         this.operation = myOperation;
78     }
79
80     /**
81      * Indicate RPCHandler is processing header elements
82      * @param value boolean indicating whether
83      * header elements are being processed.
84      */

85     public void setHeaderElement(boolean value) {
86         isHeaderElement = true;
87     }
88
89     /**
90      * This method is invoked when an element start tag is encountered.
91      * The purpose of this method in RPCHandler is to reset variables
92      * (this allows re-use of RPCHandlers)
93      * @param namespace is the namespace of the element
94      * @param localName is the name of the element
95      * @param prefix is the prefix of the element
96      * @param attributes are the attributes on the element...used to get the type
97      * @param context is the DeserializationContext
98      */

99     public void startElement(String JavaDoc namespace, String JavaDoc localName,
100                              String JavaDoc prefix, Attributes JavaDoc attributes,
101                              DeserializationContext context)
102         throws SAXException JavaDoc
103     {
104         super.startElement(namespace, localName, prefix, attributes, context);
105         currentParam = null;
106     }
107
108     /**
109      * Register the start of a parameter (child element of the method call
110      * element).
111      *
112      * Our job here is to figure out a) which parameter this is (based on
113      * the QName of the element or its position), and b) what type it is
114      * (based on the xsi:type attribute or operation metadata) so we can
115      * successfully deserialize it.
116      */

117     public SOAPHandler onStartChild(String JavaDoc namespace,
118                                     String JavaDoc localName,
119                                     String JavaDoc prefix,
120                                     Attributes JavaDoc attributes,
121                                     DeserializationContext context)
122         throws SAXException JavaDoc
123     {
124         if (log.isDebugEnabled()) {
125             log.debug("Enter: RPCHandler.onStartChild()");
126         }
127
128         if (!context.isDoneParsing()) {
129             try {
130                 context.pushNewElement(new MessageElement(namespace, localName,
131                                                           prefix, attributes,
132                                                           context));
133             } catch (AxisFault axisFault) {
134                 throw new SAXException JavaDoc(axisFault);
135             }
136         }
137         
138         MessageElement curEl = context.getCurElement();
139         QName JavaDoc type = null;
140         QName JavaDoc qname = new QName JavaDoc(namespace, localName);
141         ParameterDesc paramDesc = null;
142
143         SOAPConstants soapConstants = context.getSOAPConstants();
144         if (soapConstants == SOAPConstants.SOAP12_CONSTANTS &&
145             Constants.QNAME_RPC_RESULT.equals(qname)) {
146             // TODO: fix it ... now we just skip it
147
return new DeserializerImpl();
148         }
149
150         // Create a new param if not the same element
151
if (currentParam == null ||
152             !currentParam.getQName().getNamespaceURI().equals(namespace) ||
153             !currentParam.getQName().getLocalPart().equals(localName)) {
154             currentParam = new RPCParam(namespace, localName, null);
155             rpcElem.addParam(currentParam);
156         }
157
158         // Grab xsi:type attribute if present, on either this element or
159
// the referent (if it's an href). MessageElement.getType() will
160
// automatically dig through to the referent if necessary.
161
type = curEl.getType();
162         if (type == null) {
163             type = context.getTypeFromAttributes(namespace,
164                                                  localName,
165                                                  attributes);
166         }
167
168         if (log.isDebugEnabled()) {
169             log.debug(Messages.getMessage("typeFromAttr00", "" + type));
170         }
171         
172
173         Class JavaDoc destClass = null;
174
175         // If we have an operation descriptor, try to associate this parameter
176
// with the appropriate ParameterDesc
177
if (operation != null) {
178             
179             // Try by name first
180
if (isResponse) {
181                 paramDesc = operation.getOutputParamByQName(qname);
182             } else {
183                 paramDesc = operation.getInputParamByQName(qname);
184             }
185
186             
187             // If that didn't work, try position
188
// FIXME : Do we need to be in EITHER named OR positional
189
// mode? I.e. will it screw us up to find something
190
// by position if we've already looked something up
191
// by name? I think so...
192
if (paramDesc == null) {
193                 if (isResponse) {
194                     paramDesc = operation.getReturnParamDesc();
195                 }
196                 else {
197                     paramDesc = operation.getParameter(rpcElem.getParams().size() - 1);
198                 }
199             }
200             
201             if (paramDesc == null) {
202                 throw new SAXException JavaDoc(Messages.getMessage("noParmDesc"));
203             }
204             // Make sure that we don't find body parameters that should
205
// be in the header
206
if (!isHeaderElement &&
207                 ((isResponse && paramDesc.isOutHeader()) ||
208                  (!isResponse && paramDesc.isInHeader()))) {
209                 throw new SAXException JavaDoc(
210                     Messages.getMessage("expectedHeaderParam",
211                                         paramDesc.getQName().toString()));
212             }
213
214             destClass = paramDesc.getJavaType();
215             if ((destClass != null) && (destClass.isArray())) {
216                 context.setDestinationClass(destClass);
217             }
218             
219             // Keep the association so we can use it later
220
// (see RPCProvider.processMessage())
221
currentParam.setParamDesc(paramDesc);
222             
223             if (type == null) {
224                 type = paramDesc.getTypeQName();
225             }
226         }
227
228         if (type != null && type.equals(XMLType.AXIS_VOID)) {
229             Deserializer nilDSer = new DeserializerImpl();
230             return (SOAPHandler) nilDSer;
231         }
232
233         // If the nil attribute is set, just
234
// return the base DeserializerImpl.
235
// Register the value target to set the value
236
// on the RPCParam. This is necessary for cases like
237
// <method>
238
// <foo>123</foo>
239
// <foo>456</foo>
240
// <foo xsi:nil="true" />
241
// </method>
242
// so that a list of 3 items is created.
243
// Failure to register the target would result in the last
244
// item not being added to the list
245
if (context.isNil(attributes)) {
246           Deserializer nilDSer = new DeserializerImpl();
247           nilDSer.registerValueTarget(new RPCParamTarget(currentParam));
248           return (SOAPHandler) nilDSer;
249         }
250         
251         Deserializer dser = null;
252         if ((type == null) && (namespace != null) && (!namespace.equals(""))) {
253             dser = context.getDeserializerForType(qname);
254         } else {
255             dser = context.getDeserializer(destClass, type);
256             // !!!
257
if (dser == null && destClass != null && destClass.isArray() &&
258                     operation.getStyle() == Style.DOCUMENT) {
259                 dser = context.getDeserializerForClass(destClass);
260             }
261             // !!!
262
}
263         
264         if (dser == null) {
265           if (type != null) {
266               dser = context.getDeserializerForType(type);
267               if(null != destClass && dser == null && Element JavaDoc.class.isAssignableFrom(destClass)){
268                 //If a DOM element is expected, as last resort always allow direct mapping
269
// of parameter's SOAP xml to a DOM element. Support of literal parms by default.
270
dser = context.getDeserializerForType(Constants.SOAP_ELEMENT);
271
272               }
273               if (dser == null) {
274                 dser = context.getDeserializerForClass(destClass);
275               }
276               if (dser == null) {
277                   throw new SAXException JavaDoc(Messages.getMessage(
278                           "noDeser01", localName,"" + type));
279               }
280               if (paramDesc != null && paramDesc.getJavaType() != null) {
281                   // If we have an xsi:type, make sure it makes sense
282
// with the current paramDesc type
283
Class JavaDoc xsiClass =
284                           context.getTypeMapping().getClassForQName(type);
285                   if (null != xsiClass && !JavaUtils.isConvertable(xsiClass, destClass)) {
286                       throw new SAXException JavaDoc("Bad types (" +
287                                              xsiClass + " -> " + destClass + ")"); // FIXME!
288
}
289               }
290           } else {
291               dser = context.getDeserializerForClass(destClass);
292               if (dser == null) {
293                   dser = new DeserializerImpl();
294               }
295           }
296         }
297
298         dser.setDefaultType(type);
299
300         dser.registerValueTarget(new RPCParamTarget(currentParam));
301
302         if (log.isDebugEnabled()) {
303             log.debug("Exit: RPCHandler.onStartChild()");
304         }
305         return (SOAPHandler)dser;
306     }
307
308     public void endElement(String JavaDoc namespace, String JavaDoc localName,
309                            DeserializationContext context)
310         throws SAXException JavaDoc
311     {
312         // endElement may not be called in all circumstances.
313
// In addition, onStartChild may be called after endElement
314
// (for header parameter/response processing).
315
// So please don't add important logic to this method.
316
if (log.isDebugEnabled()) {
317             log.debug(Messages.getMessage("setProp00",
318                     "MessageContext", "RPCHandler.endElement()."));
319         }
320         context.getMessageContext().setProperty("RPC", rpcElem);
321     }
322 }
323
Popular Tags