001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.webservices.xmlrpc;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.OutputStream;
026: import java.io.StringBufferInputStream;
027: import javax.servlet.ServletConfig;
028: import javax.servlet.ServletException;
029: import javax.servlet.http.HttpServlet;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.xmlrpc.XmlRpcServer;
035:
036: /**
037: * Roller's XML RPC Servlet sets up XmlRpcHandler for Blogger & MetaWeblog API.
038: *
039: * @web.servlet name="RollerXMLRPCServlet"
040: * @web.servlet-mapping url-pattern="/roller-services/xmlrpc"
041: */
042: public class RollerXMLRPCServlet extends HttpServlet {
043:
044: static final long serialVersionUID = -4424719615968330852L;
045:
046: private static Log mLogger = LogFactory
047: .getLog(RollerXMLRPCServlet.class);
048:
049: private transient XmlRpcServer mXmlRpcServer = new XmlRpcServer();
050: private BloggerAPIHandler mBloggerHandler = null;
051: private MetaWeblogAPIHandler mMetaWeblogHandler = null;
052:
053: /**
054: * Initializes the servlet.
055: */
056: public void init(ServletConfig config) throws ServletException {
057:
058: super .init(config);
059: try {
060: mBloggerHandler = new BloggerAPIHandler();
061: mXmlRpcServer.addHandler("blogger", mBloggerHandler);
062:
063: mMetaWeblogHandler = new MetaWeblogAPIHandler();
064: mXmlRpcServer.addHandler("metaWeblog", mMetaWeblogHandler);
065: } catch (Exception e) {
066: mLogger
067: .error("Initialization of XML-RPC servlet failed",
068: e);
069: }
070: }
071:
072: protected void service(HttpServletRequest request,
073: HttpServletResponse response) throws ServletException,
074: IOException {
075:
076: InputStream is = request.getInputStream();
077:
078: if (mLogger.isDebugEnabled()) {
079: BufferedReader br = new BufferedReader(
080: new InputStreamReader(is));
081: String line = null;
082: StringBuffer sb = new StringBuffer();
083: while ((line = br.readLine()) != null) {
084: sb.append(line);
085: sb.append("\n");
086: }
087: mLogger.debug(sb.toString());
088: is = new StringBufferInputStream(sb.toString());
089: }
090:
091: // execute XML-RPC request
092: byte[] result = mXmlRpcServer.execute(is);
093:
094: if (mLogger.isDebugEnabled()) {
095: String output = new String(result);
096: mLogger.debug(output);
097: }
098:
099: response.setContentType("text/xml");
100: response.setContentLength(result.length);
101: OutputStream output = response.getOutputStream();
102: output.write(result);
103: output.flush();
104: }
105:
106: }
|