KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 import javax.servlet.ServletConfig JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServlet JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 /**
30  * A <code>HttpServlet</code> that acts as a XML-RPC proxy.
31  *
32  * The URL of the server to connect to is taken from the servlet
33  * initialization parameter <code>url</code>.
34  *
35  * @author <a HREF="mailto:hannes@apache.org">Hannes Wallnoefer</a>
36  * @version $Id: XmlRpcProxyServlet.java,v 1.5 2005/04/22 10:25:57 hgomez Exp $
37  */

38 public class XmlRpcProxyServlet extends HttpServlet JavaDoc
39 {
40     private XmlRpcServer xmlrpc;
41
42     /**
43      *
44      * @param config
45      * @throws ServletException
46      */

47     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
48     {
49         if ("true".equalsIgnoreCase(config.getInitParameter("debug")))
50         {
51             XmlRpc.setDebug(true);
52         }
53         String JavaDoc url = config.getInitParameter("url");
54         xmlrpc = new XmlRpcServer();
55         try
56         {
57             xmlrpc.addHandler("$default", new XmlRpcClientLite(url));
58         }
59         catch (Exception JavaDoc x)
60         {
61             throw new ServletException JavaDoc("Invalid URL: " + url + " ("
62                     + x.toString () + ")");
63         }
64     }
65
66     /**
67      *
68      * @param req
69      * @param res
70      * @throws ServletException
71      * @throws IOException
72      */

73     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
74             throws ServletException JavaDoc, IOException JavaDoc
75     {
76         byte[] result = xmlrpc.execute(req.getInputStream ());
77         res.setContentType("text/xml");
78         res.setContentLength(result.length);
79         OutputStream JavaDoc output = res.getOutputStream();
80         output.write(result);
81         output.flush();
82     }
83 }
84
Popular Tags