KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > XmlRpcServlet


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
36 import org.dom4j.io.SAXReader;
37 import org.dom4j.Document;
38 import org.dom4j.DocumentException;
39
40 import javax.servlet.ServletConfig JavaDoc;
41 import javax.servlet.ServletException JavaDoc;
42 import javax.servlet.http.HttpServlet JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import javax.xml.parsers.SAXParserFactory JavaDoc;
46 import javax.xml.parsers.ParserConfigurationException JavaDoc;
47 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.OutputStream JavaDoc;
50 import java.io.InputStream JavaDoc;
51 import java.io.BufferedInputStream JavaDoc;
52 import java.io.ByteArrayOutputStream JavaDoc;
53 import java.io.OutputStreamWriter JavaDoc;
54 import java.io.ByteArrayInputStream JavaDoc;
55 import java.util.Iterator JavaDoc;
56
57 /**
58  * XmlRpc handler servlet
59  *
60  * @author Stephan J. Schmidt
61  * @version $Id: XmlRpcServlet.java 1653 2004-06-11 20:04:56Z leo $
62  */

63
64 public class XmlRpcServlet extends HttpServlet JavaDoc {
65   private XmlRpcServer xmlrpc;
66   private boolean initalized = false;
67
68   public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc {
69     xmlrpc = new XmlRpcServer();
70   }
71
72   private void initialize() {
73     PicoContainer container = Components.getContainer();
74     Iterator JavaDoc iterator = container.getComponentInstances().iterator();
75     while (iterator.hasNext()) {
76       Object JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
86     doGet(request, response);
87   }
88
89   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
90     throws IOException JavaDoc, ServletException JavaDoc {
91     Globals globals = ConfigurationProxy.getInstance();
92     if (!globals.isInstalled()) {
93       throw new ServletException JavaDoc("Please finish basic database configuration first.");
94     }
95
96     if (!initalized) {
97       initialize();
98     }
99
100     String JavaDoc auth = request.getHeader("Authorization");
101     String JavaDoc login = "", password = "";
102
103     byte[] result = null;
104     if (auth != null) {
105       auth = new String JavaDoc(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 JavaDoc out = response.getOutputStream();
117     out.write(result);
118     out.flush();
119   }
120 }
Popular Tags