KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > XmlRpcClientResponseProcessor


1 /*
2  * Copyright 1999,2005 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
18 package org.apache.xmlrpc;
19
20 import java.util.Hashtable JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 import org.xml.sax.AttributeList JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 /**
27  * Process an XML-RPC server response from a byte array or an
28  * InputStream into an Object. Optionally throw the result object
29  * if it is an exception.
30  *
31  * @author <a HREF="mailto:hannes@apache.org">Hannes Wallnoefer</a>
32  * @author <a HREF="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
33  * @version $Id: XmlRpcClientResponseProcessor.java,v 1.4 2005/05/14 21:31:48 jochen Exp $
34  * @since 2.0
35  */

36 public class XmlRpcClientResponseProcessor extends XmlRpc
37 {
38     /** The result of the XML-RPC operation. Possibly an XmlRpcException */
39     protected Object JavaDoc result;
40
41     /** Set to true if a fault occured on the server. */
42     protected boolean fault;
43
44     /**
45      * Creates a new instance.
46      */

47     public XmlRpcClientResponseProcessor()
48     {
49     }
50
51     /**
52      * Decode an XML-RPC response from the specified InputStream.
53      *
54      * @param is The stream from which to read the response.
55      * @return The response, which will be a XmlRpcException if an
56      * error occured.
57      * @exception XmlRpcClientException
58      */

59     public Object JavaDoc decodeResponse(InputStream JavaDoc is)
60         throws XmlRpcClientException
61     {
62         result = null;
63         fault = false;
64         try
65         {
66             parse(is);
67             if (fault)
68             {
69                 return decodeException(result);
70             }
71             else
72             {
73                 return result;
74             }
75         }
76         catch (Exception JavaDoc x)
77         {
78             throw new XmlRpcClientException("Error decoding XML-RPC response", x);
79         }
80     }
81
82     /**
83      * Decode an exception from the result returned from the remote server.
84      * This method both returns and throws an XmlRpcException. If it returns an
85      * XmlRpcException then that is the exception thrown on the remote side. If
86      * it throws an exception then an exception occured locally when decoding
87      * the response
88      *
89      * @param result The response from the remote XML-RPC server.
90      * @return A XmlRpcException describing the error which occurred.
91      * @exception XmlRpcClientException if the result could not be processed.
92      * @return XmlRpcException the processed response from the server.
93      */

94     protected XmlRpcException decodeException(Object JavaDoc result)
95         throws XmlRpcClientException
96     {
97         Hashtable JavaDoc exceptionData;
98         
99         try
100         {
101             exceptionData = (Hashtable JavaDoc) result;
102             return new XmlRpcException(
103                 Integer.parseInt(exceptionData.get("faultCode").toString()),
104                 (String JavaDoc) exceptionData.get("faultString")
105             );
106         }
107         catch (Exception JavaDoc x)
108         {
109             throw new XmlRpcClientException("Error decoding XML-RPC exception response", x);
110         }
111     }
112
113     protected void objectParsed(Object JavaDoc what)
114     {
115         result = what;
116     }
117
118     /**
119      * Overrides method in XmlRpc to handle fault repsonses.
120      */

121     public void startElement(String JavaDoc name, AttributeList JavaDoc atts)
122             throws SAXException JavaDoc
123     {
124         if ("fault".equals(name))
125         {
126             fault = true;
127         }
128         else
129         {
130             super.startElement(name, atts);
131         }
132     }
133
134     /**
135      * Called by the worker management framework to see if this worker can be
136      * re-used. Must attempt to clean up any state, and return true if it can
137      * be re-used.
138      *
139      * @return boolean true if this worker has been cleaned up and may be re-used.
140      */

141     protected boolean canReUse()
142     {
143         result = null;
144         fault = false;
145         return true;
146     }
147 }
148
Popular Tags