1 18 19 package org.apache.roller.webservices.xmlrpc; 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.OutputStream ; 26 import java.io.StringBufferInputStream ; 27 import javax.servlet.ServletConfig ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.xmlrpc.XmlRpcServer; 35 36 37 43 public class RollerXMLRPCServlet extends HttpServlet { 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 57 public void init(ServletConfig config) throws ServletException { 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 e) { 67 mLogger.error("Initialization of XML-RPC servlet failed", e); 68 } 69 } 70 71 72 protected void service(HttpServletRequest request, 73 HttpServletResponse response) 74 throws ServletException , IOException { 75 76 InputStream is = request.getInputStream(); 77 78 if (mLogger.isDebugEnabled()) { 79 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 80 String line = null; 81 StringBuffer sb = new StringBuffer (); 82 while ((line = br.readLine()) != null) { 83 sb.append(line); 84 sb.append("\n"); 85 } 86 mLogger.debug(sb.toString()); 87 is = new StringBufferInputStream (sb.toString()); 88 } 89 90 byte[] result = mXmlRpcServer.execute(is); 92 93 if (mLogger.isDebugEnabled()) { 94 String output = new String (result); 95 mLogger.debug(output); 96 } 97 98 response.setContentType("text/xml"); 99 response.setContentLength(result.length); 100 OutputStream output = response.getOutputStream(); 101 output.write(result); 102 output.flush(); 103 } 104 105 } 106 | Popular Tags |