package org.xwt.server; import java.util.*; import org.apache.xmlrpc.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.Vector; import org.bouncycastle.util.encoders.Base64; /** Simple Servlet to handle /RPC2 */ public class RPC2 extends HttpServlet { public static XmlRpcServer xmlrpc = new XmlRpcServer(); // FIXME static { XmlRpc.setDebug(true); } public void init (ServletConfig config) throws ServletException { String url = config.getInitParameter ("url"); xmlrpc = new XmlRpcServer(); Enumeration e = config.getInitParameterNames(); while(e.hasMoreElements()) { String line = (String)e.nextElement(); String classname = line.indexOf('(') == -1 ? line : line.substring(0, line.indexOf('(')); String arg = line.indexOf('(') == -1 ? null : line.substring(line.indexOf('(') + 1, line.indexOf(')')); System.out.println("org.xwt.server.RPC2: loading class " + classname); try { Object o = arg == null ? Class.forName(classname).newInstance() : Class.forName(classname).getConstructor(new Class[] { String.class }).newInstance(new Object[] { arg }); String objname = config.getInitParameter(line); System.out.println("org.xwt.server.RPC2: registering as object " + objname); xmlrpc.addHandler(objname, o); } catch (Exception ex) { ex.printStackTrace(); } } } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String user = null; String password = null; if (req.getHeader("Authorization") != null) { String uidpw = new String(Base64.decode(req.getHeader("Authorization").substring(6))); user = uidpw.substring(0, uidpw.indexOf(':')); password = uidpw.substring(uidpw.indexOf(':') + 1); } try { byte[] result = xmlrpc.execute(req.getInputStream(), user, password); res.setContentType("text/xml"); res.setContentLength(result.length); OutputStream output = res.getOutputStream(); output.write(result); output.flush(); } catch (XmlRpcServer.AuthenticationRequiredException are) { res.addHeader("WWW-Authenticate", "Basic realm=XMLRPC"); res.sendError(HttpServletResponse.SC_UNAUTHORIZED); } } }