KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > xmlrpc > SnipSnapHandler


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
26 package org.snipsnap.xmlrpc;
27
28 import org.apache.xmlrpc.XmlRpcException;
29 import org.snipsnap.app.Application;
30 import org.snipsnap.app.ApplicationManager;
31 import org.snipsnap.config.Configuration;
32 import org.snipsnap.config.ConfigurationManager;
33 import org.snipsnap.config.ConfigurationProxy;
34 import org.snipsnap.config.Globals;
35 import org.snipsnap.config.InitializeDatabase;
36 import org.snipsnap.container.Components;
37 import org.snipsnap.snip.XMLSnipExport;
38 import org.snipsnap.snip.XMLSnipImport;
39 import org.snipsnap.snip.SnipSpace;
40 import org.snipsnap.snip.Snip;
41 import org.snipsnap.snip.Links;
42 import org.snipsnap.snip.storage.SnipSerializer;
43 import org.snipsnap.user.AuthenticationService;
44 import org.snipsnap.user.User;
45 import org.snipsnap.user.UserManager;
46 import org.snipsnap.render.filter.links.BackLinks;
47 import org.dom4j.io.XMLWriter;
48 import org.dom4j.io.OutputFormat;
49
50 import java.io.ByteArrayInputStream JavaDoc;
51 import java.io.ByteArrayOutputStream JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.OutputStreamWriter JavaDoc;
54 import java.io.File JavaDoc;
55 import java.util.Arrays JavaDoc;
56 import java.util.Hashtable JavaDoc;
57 import java.util.Iterator JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.Vector JavaDoc;
60
61 /**
62  * Handles XML-RPC calls for the SnipSnap API
63  *
64  * @author Stephan J. Schmidt
65  * @version $Id: SnipSnapHandler.java 1833 2005-09-19 13:26:23Z leo $
66  */

67
68 public class SnipSnapHandler extends AuthXmlRpcHandler implements XmlRpcHandler {
69   private final static List JavaDoc FREE_METHODS = Arrays.asList(new String JavaDoc[]{
70     "getVersion",
71     "authenticateUser"
72   });
73
74   private final static List JavaDoc PREFIX_METHODS = Arrays.asList(new String JavaDoc[]{
75     "getSnip",
76     "createSnip",
77     "removeSnip",
78     "getSnipAsXml",
79     "dumpXml",
80     "restoreXml",
81     "authenticateUser",
82   });
83
84   public static final String JavaDoc API_PREFIX = "snipSnap";
85
86   private SnipSpace space;
87   private AuthenticationService authenticationService;
88   private UserManager um;
89   private ApplicationManager applicationManager;
90
91   public SnipSnapHandler(AuthenticationService authenticationService,
92                          SnipSpace space,
93                          UserManager manager,
94                          ApplicationManager applicationManager) {
95     this.authenticationService = authenticationService;
96     this.um = manager;
97     this.space = space;
98     this.applicationManager = applicationManager;
99   }
100
101   protected boolean authenticate(String JavaDoc username, String JavaDoc password) {
102     Globals globals = ConfigurationProxy.getInstance();
103     if(password != null && password.equals(globals.getInstallKey())) {
104       return true;
105     }
106
107     User user = authenticationService.authenticate(username, password);
108     if (user != null && user.isAdmin()) {
109       Application.get().setUser(user);
110       return true;
111     }
112     System.err.println("XML-RPC authenticate: invalid login for " + username);
113     return false;
114   }
115
116   public Object JavaDoc execute(String JavaDoc method, Vector JavaDoc vector, String JavaDoc user, String JavaDoc password) throws Exception JavaDoc {
117     if(method.startsWith(API_PREFIX)) {
118         method = method.substring(API_PREFIX.length()+1);
119     }
120
121     if (PREFIX_METHODS.contains(method)) {
122       if(Application.get().getObject(Application.OID) == null || Application.get().getConfiguration() == null) {
123         if(!(vector.firstElement() instanceof String JavaDoc)) {
124           throw new Exception JavaDoc("You need to specify a prefix (/) to select an instance.");
125         }
126         String JavaDoc prefix = (String JavaDoc) vector.firstElement();
127         String JavaDoc appOid = applicationManager.getApplication(prefix);
128         Configuration appConfig = ConfigurationManager.getInstance().getConfiguration(appOid);
129         if (appConfig != null) {
130           if(prefix.equals(vector.get(0))) {
131             vector.remove(0);
132           }
133           Application.get().setConfiguration(appConfig);
134           Application.get().storeObject(Application.OID, appOid);
135         }
136       }
137     }
138
139     if (FREE_METHODS.contains(method)) {
140       return super.execute(method, vector);
141     } else {
142       return super.execute(method, vector, user, password);
143     }
144   }
145
146   public String JavaDoc getName() {
147     return API_PREFIX;
148   }
149
150   public String JavaDoc getSnipAsXml(String JavaDoc name) {
151     Snip snip = space.load(name);
152     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
153     OutputFormat outputFormat = OutputFormat.createCompactFormat();
154     outputFormat.setEncoding("UTF-8");
155     try {
156       XMLWriter writer = new XMLWriter(out, outputFormat);
157       writer.write(SnipSerializer.getInstance().serialize(snip));
158       writer.flush();
159     } catch (IOException JavaDoc e) {
160       e.printStackTrace();
161     }
162     return out.toString();
163   }
164
165   public String JavaDoc getSnip(String JavaDoc name) {
166     Snip snip = space.load(name);
167     return snip.getContent();
168   }
169
170   public String JavaDoc createSnip(String JavaDoc name, String JavaDoc content) {
171     Snip snip = space.create(name, content);
172     return name;
173   }
174
175   public String JavaDoc removeSnip(String JavaDoc name) {
176     Snip snip = space.load(name);
177     space.remove(snip);
178     return name;
179   }
180
181   /**
182    * Returns the SnipSnap version of this web application
183    *
184    * @return version version number
185    */

186   public String JavaDoc getVersion() {
187     Globals globals = ConfigurationProxy.getInstance();
188     return globals.getVersion();
189   }
190
191   /**
192    * Authenticate a user. This can be used for single sign on
193    * (e.g. the #java.de bot)
194    *
195    * @param login Login string to test
196    * @param passwd Password credential for the given login
197    *
198    * @return isAuthenticated True when the user can be authenticated
199    */

200   public boolean authenticateUser(String JavaDoc login, String JavaDoc passwd) throws XmlRpcException {
201     User user = authenticationService.authenticate(login, passwd);
202     return (null != user);
203   }
204
205   // PROTECTED METHODS
206

207   /**
208    * Dump the database contents.
209    * @return a XML stream containing the dump of the database
210    * @throws IOException
211    */

212   public byte[] dumpXml() throws IOException JavaDoc {
213     Configuration config = Application.get().getConfiguration();
214     ByteArrayOutputStream JavaDoc exportStream = new ByteArrayOutputStream JavaDoc();
215     XMLSnipExport.store(exportStream, space.getAll(), um.getAll(), null, null, config.getFilePath());
216     return exportStream.toByteArray();
217   }
218
219   public byte[] dumpXml(String JavaDoc match) throws IOException JavaDoc {
220     Configuration config = Application.get().getConfiguration();
221     ByteArrayOutputStream JavaDoc exportStream = new ByteArrayOutputStream JavaDoc();
222     XMLSnipExport.store(exportStream, Arrays.asList(space.match(match)), null, null, null, config.getFilePath());
223     return exportStream.toByteArray();
224   }
225
226   public boolean restoreXml(byte[] xmlData) throws IOException JavaDoc {
227     return restoreXml(xmlData, XMLSnipImport.IMPORT_SNIPS | XMLSnipImport.IMPORT_USERS | XMLSnipImport.OVERWRITE);
228   }
229
230   public boolean restoreXml(byte[] xmlData, int flags) throws IOException JavaDoc {
231     ByteArrayInputStream JavaDoc importStream = new ByteArrayInputStream JavaDoc(xmlData);
232     try {
233       XMLSnipImport.load(importStream, flags);
234     } catch (Exception JavaDoc e) {
235       System.err.println("SnipSnapHandler.restoreXml: unable to import snips: "+e);
236       throw new IOException JavaDoc(e.getMessage());
237     }
238     return true;
239   }
240
241   public String JavaDoc install(String JavaDoc prefix, Hashtable JavaDoc appConfig) throws Exception JavaDoc {
242     ConfigurationManager configManager = ConfigurationManager.getInstance();
243     ApplicationManager appManager = (ApplicationManager) Components.getComponent(ApplicationManager.class);
244     String JavaDoc appOid = appManager.getApplication(prefix);
245     Configuration config = configManager.getConfiguration(appOid);
246
247     // only set new values if config does not exits
248
if (null == config) {
249       config = ConfigurationProxy.newInstance();
250       Iterator JavaDoc optionIt = appConfig.keySet().iterator();
251       while (optionIt.hasNext()) {
252         String JavaDoc option = (String JavaDoc) optionIt.next();
253         String JavaDoc value = (String JavaDoc)appConfig.get(option);
254         config.set(option, value);
255       }
256       if (prefix != null && !"".equals(prefix)) {
257         if (!prefix.startsWith("/")) {
258           prefix = "/" + prefix;
259         }
260         config.setPrefix(prefix);
261       }
262       appOid = InitializeDatabase.init(config, new OutputStreamWriter JavaDoc(System.out));
263       return configManager.getConfiguration(appOid).getUrl(prefix);
264     }
265
266     return "a configuration for '"+prefix+"' already exists, aborting.";
267   }
268
269   /**
270    * Install a new instance with user name and password. Uses default configuration.
271    * @param prefix the instance prefix
272    * @param adminLogin admin login name
273    * @param passwd admin password
274    */

275   public String JavaDoc install(String JavaDoc prefix, String JavaDoc adminLogin, String JavaDoc passwd) throws Exception JavaDoc {
276     Hashtable JavaDoc appConfig = new Hashtable JavaDoc();
277     appConfig.put(Configuration.APP_ADMIN_LOGIN, adminLogin);
278     appConfig.put(Configuration.APP_ADMIN_PASSWORD, passwd);
279     return install(prefix, appConfig);
280   }
281
282   /**
283    * Install a new instance with user name and password. Uses the configuration as
284    * provided in the command line but overrides the admin user/password found in
285    * that file.
286    * @param prefix the instance prefix
287    * @param adminLogin admin login name
288    * @param passwd admin password
289    */

290   public String JavaDoc install(String JavaDoc prefix, String JavaDoc adminLogin, String JavaDoc passwd, Hashtable JavaDoc appConfig) throws Exception JavaDoc{
291     appConfig.put(Configuration.APP_ADMIN_LOGIN, adminLogin);
292     appConfig.put(Configuration.APP_ADMIN_PASSWORD, passwd);
293     return install(prefix, appConfig);
294   }
295
296 // public int removeBacklink(String regexp) {
297
// Configuration config = Application.get().getConfiguration();
298
// SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
299
// Iterator allSnipIt = space.getAll().iterator();
300
// while(allSnipIt.hasNext()) {
301
// Snip snip = (Snip)allSnipIt.next();
302
// Links links = snip.getBackLinks();
303
// links.iterator();
304
// }
305
//
306
// }
307
}
Popular Tags