在我国应用较为广泛的有Zoundry Blog Writer和微软的windows live write两个blog离线编辑器,而以上两个离线编辑器都对metaweblog有着良好的支持,因此我们采用metablog不失为一个快速实现blog离线编辑的好办法。
MetaWeblog-简介
MetaWeblog 是BLOG离线编辑(包括上传和下载删除等基本功能)的接口规范,它建立在XML-RPC通信协议之上。MetaWeblog发展和继承了Blogger API。
首先我们要创建 zblog或live write 请求的API URL也就是Servlet (webservices),(笔者建议下载(Roller )的原代码以做参考。roller中metablog的webservices实现可做参考),代码如下:
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.StringBufferInputStream;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.xmlrpc.XmlRpcServer;
- /**
- * Roller's XML RPC Servlet sets up XmlRpcHandler for Blogger & MetaWeblog API.
- *
- * @web.servlet name="RollerXMLRPCServlet"
- * @web.servlet-mapping url-pattern="/roller-services/xmlrpc"
- */
- public class RollerXMLRPCServlet extends HttpServlet {
- static final long serialVersionUID = -4424719615968330852L;
- private static Log mLogger = LogFactory.getLog(RollerXMLRPCServlet.class);
- private transient XmlRpcServer mXmlRpcServer = new XmlRpcServer();
- private BloggerAPIHandler mBloggerHandler = null;
- private MetaWeblogAPIHandler mMetaWeblogHandler = null;
- /**
- * Initializes the servlet.
- */
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- try {
- org.apache.xmlrpc.XmlRpc.setDebug(true); //是否输出DUG信息
- org.apache.xmlrpc.XmlRpc.setEncoding("UTF8");
- mBloggerHandler = new BloggerAPIHandler();
- mXmlRpcServer.addHandler("blogger", mBloggerHandler);
- mMetaWeblogHandler = new MetaWeblogAPIHandler();
- mXmlRpcServer.addHandler("metaWeblog", mMetaWeblogHandler);
- } catch (Exception e) {
- mLogger.error("Initialization of XML-RPC servlet failed", e);
- }
- }
- protected void service(HttpServletRequest request,
- HttpServletResponse response)
- throws ServletException, IOException {
- //请求以流的形式得到,所以在此监测不到任何的请求参数(XML-RPC通信协议)
- InputStream is = request.getInputStream();
- //将请求交由mXmlRpcServer.execute处理。
- byte[] result = mXmlRpcServer.execute(is);
- if (mLogger.isDebugEnabled()) {
- String output = new String(result);
- mLogger.debug(output);
- }
- response.setContentType("text/xml");
- response.setContentLength(result.length);
- OutputStream output = response.getOutputStream();
- output.write(result);
- output.flush();
- }
- }
待续...
java 代码