KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 /**
25  * Process an XML-RPC client request into a byte array or directly onto
26  * an OutputStream.
27  *
28  * @author <a HREF="mailto:hannes@apache.org">Hannes Wallnoefer</a>
29  * @author <a HREF="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
30  * @version $Id: XmlRpcClientRequestProcessor.java,v 1.5 2005/05/14 21:31:48 jochen Exp $
31  * @since 1.2
32  */

33 public class XmlRpcClientRequestProcessor
34 {
35     /**
36      * Creates a new instance.
37      */

38     public XmlRpcClientRequestProcessor()
39     {
40     }
41     
42     /**
43      * Encode a request from the XmlClientRpcRequest implementation to an
44      * output stream in the specified character encoding.
45      *
46      * @param request the request to encode.
47      * @param encoding the Java name for the encoding to use.
48      * @return byte [] the encoded request.
49      */

50     public void encodeRequest(XmlRpcClientRequest request, String JavaDoc encoding,
51                               OutputStream JavaDoc out)
52         throws XmlRpcClientException, IOException JavaDoc
53     {
54         XmlWriter writer;
55
56         writer = new XmlWriter(out, encoding);
57        
58         writer.startElement("methodCall");
59         writer.startElement("methodName");
60         writer.write(request.getMethodName());
61         writer.endElement("methodName");
62         writer.startElement("params");
63
64         int l = request.getParameterCount();
65         for (int i = 0; i < l; i++)
66         {
67             writer.startElement("param");
68             try
69             {
70                 writer.writeObject(request.getParameter(i));
71             }
72             catch (XmlRpcException e)
73             {
74                 throw new XmlRpcClientException("Failure writing request", e);
75             }
76             writer.endElement("param");
77         }
78         writer.endElement("params");
79         writer.endElement("methodCall");
80         writer.flush();
81     }
82
83     /**
84      * Encode a request from the XmlRpcClientRequest implementation to a
85      * byte array representing the XML-RPC call, in the specified character
86      * encoding.
87      *
88      * @param request the request to encode.
89      * @param encoding the Java name for the encoding to use.
90      * @return byte [] the encoded request.
91      */

92     public byte [] encodeRequestBytes(XmlRpcClientRequest request, String JavaDoc encoding)
93     throws XmlRpcClientException
94     {
95         ByteArrayOutputStream JavaDoc buffer;
96
97         try
98         {
99             buffer = new ByteArrayOutputStream JavaDoc();
100             encodeRequest(request, encoding, buffer);
101             return buffer.toByteArray();
102         }
103         catch (IOException JavaDoc ioe)
104         {
105             throw new XmlRpcClientException("Error occured encoding XML-RPC request", ioe);
106         }
107     }
108
109     /**
110      * Called by the worker management framework to see if this object can be
111      * re-used. Must attempt to clean up any state, and return true if it can
112      * be re-used.
113      *
114      * @return boolean true if this objcet has been cleaned up and may be re-used.
115      */

116     protected boolean canReUse()
117     {
118         return true;
119     }
120 }
121
Popular Tags