KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > 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.jboss.axis.message;
17
18 import org.jboss.axis.AxisFault;
19 import org.jboss.axis.Constants;
20 import org.jboss.axis.MessageContext;
21 import org.jboss.axis.description.FaultDesc;
22 import org.jboss.axis.description.OperationDesc;
23 import org.jboss.axis.encoding.Callback;
24 import org.jboss.axis.encoding.CallbackTarget;
25 import org.jboss.axis.encoding.DeserializationContext;
26 import org.jboss.axis.encoding.Deserializer;
27 import org.jboss.axis.encoding.DeserializerImpl;
28 import org.jboss.axis.soap.SOAPConstants;
29 import org.jboss.axis.utils.ClassUtils;
30 import org.jboss.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
36 /**
37  * Handle deserializing fault details.
38  *
39  * @author Glen Daniels (gdaniels@apache.org)
40  * @author Tom Jordahl (tomj@macromedia.com)
41  */

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

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