KickJava   Java API By Example, From Geeks To Geeks.

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


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.AxisFault;
19 import org.apache.axis.Constants;
20 import org.apache.axis.MessageContext;
21 import org.apache.axis.description.FaultDesc;
22 import org.apache.axis.description.OperationDesc;
23 import org.apache.axis.encoding.Callback;
24 import org.apache.axis.encoding.CallbackTarget;
25 import org.apache.axis.encoding.DeserializationContext;
26 import org.apache.axis.encoding.Deserializer;
27 import org.apache.axis.encoding.DeserializerImpl;
28 import org.apache.axis.soap.SOAPConstants;
29 import org.apache.axis.utils.ClassUtils;
30 import org.apache.axis.utils.Messages;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 import javax.xml.namespace.QName JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38  * Handle deserializing fault details.
39  *
40  * @author Glen Daniels (gdaniels@apache.org)
41  * @author Tom Jordahl (tomj@macromedia.com)
42  */

43 public class SOAPFaultDetailsBuilder extends SOAPHandler implements Callback
44 {
45     protected SOAPFaultBuilder builder;
46     
47     public SOAPFaultDetailsBuilder(SOAPFaultBuilder builder) {
48         this.builder = builder;
49     }
50
51
52     public void startElement(String JavaDoc namespace, String JavaDoc localName,
53                              String JavaDoc prefix, Attributes JavaDoc attributes,
54                              DeserializationContext context)
55         throws SAXException JavaDoc
56     {
57         SOAPConstants soapConstants = context.getSOAPConstants();
58
59         if (soapConstants == SOAPConstants.SOAP12_CONSTANTS &&
60             attributes.getValue(Constants.URI_SOAP12_ENV, Constants.ATTR_ENCODING_STYLE) != null) {
61
62             AxisFault fault = new AxisFault(Constants.FAULT_SOAP12_SENDER,
63                 null, Messages.getMessage("noEncodingStyleAttrAppear", "Detail"), null, null, null);
64
65             throw new SAXException JavaDoc(fault);
66         }
67
68         super.startElement(namespace, localName, prefix, attributes, context);
69     }
70
71     public SOAPHandler onStartChild(String JavaDoc namespace,
72                                     String JavaDoc name,
73                                     String JavaDoc prefix,
74                                     Attributes JavaDoc attributes,
75                                     DeserializationContext context)
76         throws SAXException JavaDoc
77     {
78         // Get QName of element
79
QName JavaDoc qn = new QName JavaDoc(namespace, name);
80                         
81         // Look for <exceptionName> element and create a class
82
// with that name - this is Axis specific and is
83
// replaced by the Exception map
84
if (name.equals("exceptionName")) {
85             // Set up deser of exception name string
86
Deserializer dser = context.getDeserializerForType(Constants.XSD_STRING);
87             dser.registerValueTarget(new CallbackTarget(this, "exceptionName"));
88             return (SOAPHandler)dser;
89         }
90                         
91         // Look up this element in our faultMap
92
// if we find a match, this element is the fault data
93
MessageContext msgContext = context.getMessageContext();
94         SOAPConstants soapConstants = Constants.DEFAULT_SOAP_VERSION;
95         OperationDesc op = null;
96         if (msgContext != null) {
97             soapConstants = msgContext.getSOAPConstants();
98             op = msgContext.getOperation();
99         }
100         Class JavaDoc faultClass = null;
101         QName JavaDoc faultXmlType = null;
102         if (op != null) {
103             FaultDesc faultDesc = null;
104             // allow fault type to be denoted in xsi:type
105
faultXmlType = context.getTypeFromAttributes(namespace,
106                                                          name,
107                                                          attributes);
108             if (faultXmlType != null) {
109                 faultDesc = op.getFaultByXmlType(faultXmlType);
110             }
111
112             // If we didn't get type information, look up QName of fault
113
if (faultDesc == null) {
114                 faultDesc = op.getFaultByQName(qn);
115                 if ((faultXmlType == null) && (faultDesc != null)) {
116                     faultXmlType = faultDesc.getXmlType();
117                 }
118             }
119
120             if (faultDesc == null && op.getFaults() != null) {
121                 Iterator JavaDoc i = op.getFaults().iterator();
122                 while(i.hasNext()) {
123                     FaultDesc fdesc = (FaultDesc) i.next();
124                     if(fdesc.getClassName().equals(name)) {
125                         faultDesc = fdesc;
126                         faultXmlType = fdesc.getXmlType();
127                         break;
128                     }
129                 }
130             }
131             
132             // Set the class if we found a description
133
if (faultDesc != null) {
134                 try {
135                     faultClass = ClassUtils.forName(faultDesc.getClassName());
136                 } catch (ClassNotFoundException JavaDoc e) {
137                     // Just create an AxisFault, no custom exception
138
}
139             }
140         } else {
141             faultXmlType = context.getTypeFromAttributes(namespace,
142                                                        name,
143                                                        attributes);
144         }
145
146         if (faultClass == null) {
147             faultClass =
148                     context.getTypeMapping().getClassForQName(faultXmlType);
149         }
150         
151         if(faultClass != null && faultXmlType != null) {
152             builder.setFaultClass(faultClass);
153             builder.setWaiting(true);
154             // register callback for the data, use the xmlType from fault info
155
Deserializer dser = null;
156             if (attributes.getValue(soapConstants.getAttrHref()) == null) {
157                 dser = context.getDeserializerForType(faultXmlType);
158             } else {
159                 dser = new DeserializerImpl();
160                 dser.setDefaultType(faultXmlType);
161             }
162             if (dser != null) {
163                 dser.registerValueTarget(new CallbackTarget(this, "faultData"));
164             }
165             return (SOAPHandler)dser;
166         }
167         return null;
168     }
169
170     /*
171      * Defined by Callback.
172      * This method gets control when the callback is invoked.
173      * @param is the value to set.
174      * @param hint is an Object that provide additional hint information.
175      */

176     public void setValue(Object JavaDoc value, Object JavaDoc hint)
177     {
178         if ("faultData".equals(hint)) {
179             builder.setFaultData(value);
180         } else if ("exceptionName".equals(hint)) {
181             String JavaDoc faultClassName = (String JavaDoc) value;
182             try {
183                 Class JavaDoc faultClass = ClassUtils.forName(faultClassName);
184                 builder.setFaultClass(faultClass);
185             } catch (ClassNotFoundException JavaDoc e) {
186                 // Just create an AxisFault, no custom exception
187
}
188         }
189         
190     }
191 }
192
193
194
Popular Tags