1 25 package org.snipsnap.net; 26 27 import org.apache.xmlrpc.XmlRpcServer; 28 import org.apache.xmlrpc.XmlRpc; 29 import org.picocontainer.PicoContainer; 30 import org.snipsnap.config.ConfigurationProxy; 31 import org.snipsnap.config.Globals; 32 import org.snipsnap.container.Components; 33 import org.snipsnap.util.Base64; 34 import org.snipsnap.xmlrpc.XmlRpcHandler; 35 import org.xml.sax.SAXException ; 36 import org.dom4j.io.SAXReader; 37 import org.dom4j.Document; 38 import org.dom4j.DocumentException; 39 40 import javax.servlet.ServletConfig ; 41 import javax.servlet.ServletException ; 42 import javax.servlet.http.HttpServlet ; 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import javax.xml.parsers.SAXParserFactory ; 46 import javax.xml.parsers.ParserConfigurationException ; 47 import javax.xml.parsers.FactoryConfigurationError ; 48 import java.io.IOException ; 49 import java.io.OutputStream ; 50 import java.io.InputStream ; 51 import java.io.BufferedInputStream ; 52 import java.io.ByteArrayOutputStream ; 53 import java.io.OutputStreamWriter ; 54 import java.io.ByteArrayInputStream ; 55 import java.util.Iterator ; 56 57 63 64 public class XmlRpcServlet extends HttpServlet { 65 private XmlRpcServer xmlrpc; 66 private boolean initalized = false; 67 68 public void init(ServletConfig servletConfig) throws ServletException { 69 xmlrpc = new XmlRpcServer(); 70 } 71 72 private void initialize() { 73 PicoContainer container = Components.getContainer(); 74 Iterator iterator = container.getComponentInstances().iterator(); 75 while (iterator.hasNext()) { 76 Object o = iterator.next(); 77 if (o instanceof XmlRpcHandler) { 78 XmlRpcHandler handler = (XmlRpcHandler) o; 79 xmlrpc.addHandler(handler.getName(), handler); 80 } 81 } 82 initalized = true; 83 } 84 85 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 86 doGet(request, response); 87 } 88 89 public void doGet(HttpServletRequest request, HttpServletResponse response) 90 throws IOException , ServletException { 91 Globals globals = ConfigurationProxy.getInstance(); 92 if (!globals.isInstalled()) { 93 throw new ServletException ("Please finish basic database configuration first."); 94 } 95 96 if (!initalized) { 97 initialize(); 98 } 99 100 String auth = request.getHeader("Authorization"); 101 String login = "", password = ""; 102 103 byte[] result = null; 104 if (auth != null) { 105 auth = new String (Base64.decode(auth.substring(auth.indexOf(' ') + 1))); 106 login = auth.substring(0, auth.indexOf(':')); 107 password = auth.substring(auth.indexOf(':') + 1); 108 109 result = xmlrpc.execute(request.getInputStream(), login, password); 110 } else { 111 result = xmlrpc.execute(request.getInputStream()); 112 } 113 114 response.setContentType("text/xml"); 115 response.setContentLength(result.length); 116 OutputStream out = response.getOutputStream(); 117 out.write(result); 118 out.flush(); 119 } 120 } | Popular Tags |