KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > xmlrpc > RollerXMLRPCServlet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.webservices.xmlrpc;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.StringBufferInputStream JavaDoc;
27 import javax.servlet.ServletConfig JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.http.HttpServlet JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.xmlrpc.XmlRpcServer;
35
36
37 /**
38  * Roller's XML RPC Servlet sets up XmlRpcHandler for Blogger & MetaWeblog API.
39  *
40  * @web.servlet name="RollerXMLRPCServlet"
41  * @web.servlet-mapping url-pattern="/roller-services/xmlrpc"
42  */

43 public class RollerXMLRPCServlet extends HttpServlet JavaDoc {
44     
45     static final long serialVersionUID = -4424719615968330852L;
46     
47     private static Log mLogger = LogFactory.getLog(RollerXMLRPCServlet.class);
48     
49     private transient XmlRpcServer mXmlRpcServer = new XmlRpcServer();
50     private BloggerAPIHandler mBloggerHandler = null;
51     private MetaWeblogAPIHandler mMetaWeblogHandler = null;
52     
53     
54     /**
55      * Initializes the servlet.
56      */

57     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
58         
59         super.init(config);
60         try {
61             mBloggerHandler = new BloggerAPIHandler();
62             mXmlRpcServer.addHandler("blogger", mBloggerHandler);
63             
64             mMetaWeblogHandler = new MetaWeblogAPIHandler();
65             mXmlRpcServer.addHandler("metaWeblog", mMetaWeblogHandler);
66         } catch (Exception JavaDoc e) {
67             mLogger.error("Initialization of XML-RPC servlet failed", e);
68         }
69     }
70     
71     
72     protected void service(HttpServletRequest JavaDoc request,
73             HttpServletResponse JavaDoc response)
74             throws ServletException JavaDoc, IOException JavaDoc {
75         
76         InputStream JavaDoc is = request.getInputStream();
77         
78         if (mLogger.isDebugEnabled()) {
79             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
80             String JavaDoc line = null;
81             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
82             while ((line = br.readLine()) != null) {
83                 sb.append(line);
84                 sb.append("\n");
85             }
86             mLogger.debug(sb.toString());
87             is = new StringBufferInputStream JavaDoc(sb.toString());
88         }
89         
90         // execute XML-RPC request
91
byte[] result = mXmlRpcServer.execute(is);
92         
93         if (mLogger.isDebugEnabled()) {
94             String JavaDoc output = new String JavaDoc(result);
95             mLogger.debug(output);
96         }
97         
98         response.setContentType("text/xml");
99         response.setContentLength(result.length);
100         OutputStream JavaDoc output = response.getOutputStream();
101         output.write(result);
102         output.flush();
103     }
104     
105 }
106
Popular Tags