KickJava   Java API By Example, From Geeks To Geeks.

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


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.radeox.util.logging.Logger;
30 import org.snipsnap.app.Application;
31 import org.snipsnap.container.Components;
32 import org.snipsnap.net.FileUploadServlet;
33 import org.snipsnap.semanticweb.rss.BlogFeeder;
34 import org.snipsnap.snip.Blog;
35 import org.snipsnap.snip.Snip;
36 import org.snipsnap.snip.SnipSpace;
37 import org.snipsnap.snip.SnipSpaceFactory;
38 import org.snipsnap.snip.attachment.Attachment;
39 import org.snipsnap.snip.attachment.storage.AttachmentStorage;
40 import org.snipsnap.user.AuthenticationService;
41 import org.snipsnap.user.Permissions;
42 import org.snipsnap.user.Security;
43 import org.snipsnap.user.User;
44
45 import java.io.File JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.OutputStream JavaDoc;
48 import java.util.*;
49 import org.snipsnap.snip.BlogKit;
50 import org.snipsnap.snip.attachment.Attachments;
51 import org.snipsnap.feeder.Feeder;
52
53 /**
54  * Handles XML-RPC calls for the MetaWeblog API
55  * http://www.xmlrpc.com/metaWeblogApi
56  * <p/>
57  * Some design ideas taken from Blojsom, thanks.
58  *
59  * @author Stephan J. Schmidt
60  * @version $Id: MetaWeblogHandler.java 1614 2004-05-25 10:18:11Z stephan $
61  */

62
63 public class MetaWeblogHandler extends XmlRpcSupport implements MetaWeblogAPI {
64     public String JavaDoc API_PREFIX = "metaWeblog";
65
66     public MetaWeblogHandler(AuthenticationService authenticationService) {
67         this.authenticationService = authenticationService;
68     }
69
70     public String JavaDoc getName() {
71         return API_PREFIX;
72     }
73
74     ///
75
/// Blogger API methods
76
///
77

78     public String JavaDoc newPost(String JavaDoc appkey,
79                           String JavaDoc blogid,
80                           String JavaDoc username,
81                           String JavaDoc password,
82                           String JavaDoc body,
83                           boolean publish) throws Exception JavaDoc {
84         Hashtable content = new Hashtable(1);
85         content.put("description", body);
86         return newPost(blogid, username, password, content, publish);
87     }
88
89     public boolean editPost(String JavaDoc appkey,
90                             String JavaDoc postId,
91                             String JavaDoc username,
92                             String JavaDoc password,
93                             String JavaDoc body,
94                             boolean publish) throws Exception JavaDoc {
95         Hashtable content = new Hashtable(1);
96         content.put("description", body);
97         return editPost(postId, username, password, content, publish);
98     }
99
100     public Vector getUsersBlogs(String JavaDoc appkey, String JavaDoc username, String JavaDoc password) throws Exception JavaDoc {
101         User user = authenticate(username, password);
102
103         Hashtable blog = new Hashtable(3);
104         blog.put("url", Application.get().getConfiguration().getUrl());
105         blog.put("blogid", "0");
106         blog.put("blogName", Application.get().getConfiguration().getName());
107         Vector vector = new Vector(1);
108         vector.add(blog);
109         return vector;
110     }
111
112     public Hashtable getPost(String JavaDoc appkeyIgnored,
113                              String JavaDoc postId,
114                              String JavaDoc username,
115                              String JavaDoc password) throws Exception JavaDoc {
116         return getPost(postId, username, password);
117     }
118
119     public boolean deletePost(String JavaDoc appkey,
120                               String JavaDoc postId,
121                               String JavaDoc username,
122                               String JavaDoc password,
123                               boolean publish) throws Exception JavaDoc {
124         User user = authenticate(username, password);
125         Snip snip = SnipSpaceFactory.getInstance().load(postId);
126         SnipSpaceFactory.getInstance().remove(snip);
127         return true;
128     }
129
130     public Vector getRecentPosts(String JavaDoc ignoredAppkey,
131                                  String JavaDoc postId,
132                                  String JavaDoc username,
133                                  String JavaDoc password,
134                                  int numberOfPosts) throws Exception JavaDoc {
135         return getRecentPosts(postId, username, password, numberOfPosts);
136     }
137
138
139
140     ///
141
/// MetaWeblog API methods
142
///
143

144
145     public String JavaDoc newPost(String JavaDoc blogid, String JavaDoc username, String JavaDoc password, Hashtable postcontent, boolean publish) throws Exception JavaDoc {
146         User user = authenticate(username, password);
147
148         Blog blog = SnipSpaceFactory.getInstance().getBlog(blogid);
149
150         String JavaDoc title = (String JavaDoc) postcontent.get("title");
151         String JavaDoc content = (String JavaDoc) postcontent.get("description");
152
153         Snip snip = null;
154         if (null == title) {
155             snip = blog.post(content);
156         } else {
157             snip = blog.post(content, title);
158         }
159
160         return snip.getName();
161     }
162
163     public Hashtable getPost(String JavaDoc postId,
164                              String JavaDoc username,
165                              String JavaDoc password) throws XmlRpcException {
166         User user = authenticate(username, password);
167         Snip snip = SnipSpaceFactory.getInstance().load(postId);
168         Hashtable post = new Hashtable();
169         post.put("content", snip.getXMLContent());
170         post.put("userid", snip.getOUser()); //todo: why does getOUser work here but not with RSSSnip? (see getRecentPosts())
171
post.put("postid", snip.getName()); //todo: remove? I could not find postid in spec, should be guid, no?
172
post.put("dateCreated", snip.getCTime());//todo: remove? I could not 'dateCreated' in spec. Should be 'pubDate' no?
173

174         post.put("pubDate", snip.getCTime());
175         post.put("title", snip.getTitle()); //can name be null?
176
post.put("link", (snip.getLink() == null) ? "" : snip.getLink()); //todo: why does RssSnip return null?
177

178         post.put("description", snip.getContent());
179         post.put("content", snip.getXMLContent()); //todo: verify. I could not find 'content' in the spec. (is in rss jsp though)
180
post.put("guid", snip.getName());
181
182         try {
183             post.put("comments", snip.getComments().getPostUrl());
184         } catch (IOException JavaDoc e) {
185             //todo: ignore or log?
186
//todo: This exception does not look like it should be thrown to start off with.
187
//todo: how can getting a url throw an io exception?
188
}
189
190         return post;
191     }
192
193     public boolean editPost(String JavaDoc postId, String JavaDoc username, String JavaDoc password, Hashtable content, boolean publish) throws Exception JavaDoc {
194         User user = authenticate(username, password);
195         SnipSpace space = SnipSpaceFactory.getInstance();
196         Snip snip = space.load(postId);
197         String JavaDoc title = (String JavaDoc) content.get("title");
198         String JavaDoc body = (String JavaDoc) content.get("description");
199         //could deal with "dateCreated" later
200
snip.setContent(BlogKit.getContent(title, body));
201         space.systemStore(snip);
202         return true;
203     }
204
205
206     public Vector getRecentPosts(String JavaDoc blogid,
207                                  String JavaDoc username,
208                                  String JavaDoc password,
209                                  int numberOfPosts) throws Exception JavaDoc {
210         User user = authenticate(username, password);
211
212         Feeder feeder = new BlogFeeder(blogid);
213         List children = feeder.getFeed();
214
215         Vector posts = new Vector(children.size());
216         for (Iterator i = children.iterator(); i.hasNext() && numberOfPosts-- > 0;) {
217             Snip snip = (Snip) i.next();
218             Hashtable data = new Hashtable();
219             //todo: decide what the policy for null values should be.
220
//todo: will require deciding (then comenting) what the postconditions of
221
//todo: the Snip methods is.
222
data.put("userid", snip.getCUser()); //todo: verify getOUser did not work with RSSSnips
223
data.put("title", snip.getTitle()); //can name be null?
224
data.put("link", (snip.getLink() == null) ? "" : snip.getLink()); //todo: why does RssSnip return null?
225
data.put("dateCreated", snip.getCTime());//todo: remove? I could not find 'dateCreated' in spec. Should be 'pubDate' no?
226

227             data.put("pubDate", snip.getCTime());
228             data.put("description", snip.getContent());
229             data.put("content", snip.getXMLContent()); //todo: verify. I could not find 'content' in the spec.
230
//todo: getName() for RSSSnip does not return the same as for the snip returned by newPost().
231
data.put("guid", snip.getName());
232             try {
233                 data.put("comments", snip.getComments().getPostUrl());
234             } catch (IOException JavaDoc e) {
235                 //todo: ignore or log?
236
//todo: This exception does not look like it should be thrown to start off with.
237
//todo: how can getting a url throw an io exception?
238
}
239             posts.add(data);
240         }
241         return posts;
242     }
243
244     //todo: need comment about the "postid" data
245
public Hashtable newMediaObject(String JavaDoc blogid,
246                                     String JavaDoc username,
247                                     String JavaDoc password,
248                                     Hashtable content) throws Exception JavaDoc {
249         User user = authenticate(username, password);
250         System.out.println("new media object.");
251
252         byte[] data = (byte[]) content.get("bits");
253         String JavaDoc fileName = (String JavaDoc) content.get("name");
254         String JavaDoc mime = (String JavaDoc) content.get("key");
255         String JavaDoc snipId = (String JavaDoc) content.get("postid");
256         System.out.println("the postid is: "+snipId);
257
258         SnipSpace space = SnipSpaceFactory.getInstance();
259         Snip snip = space.load(snipId);
260
261         AttachmentStorage attachmentStorage = (AttachmentStorage) Components.getComponent(AttachmentStorage.class);
262
263         if (!Security.checkPermission(Permissions.ATTACH_TO_SNIP, user, snip)) {
264             throw new XmlRpcException(0, "Do not have write access to this snip");
265             //todo: on inspecting the checkPersmissions code I had the fleeting
266
//impression that everyone will have permission to attach to snips. verify.
267
}
268
269         //upload file
270
try {
271             if (data.length == 0) {//then we have to delete this attachment
272
Attachments attachments = snip.getAttachments();
273               Attachment attachment = attachments.getAttachment(fileName);
274                 if (null != attachment) {
275                   attachmentStorage.delete(attachment);
276                   attachments.removeAttachment(attachment);
277                 }
278               // make sure to store the changed snip
279
SnipSpaceFactory.getInstance().store(snip);
280               Hashtable result = new Hashtable(1);
281               result.put("url", "");
282               return result;
283             }
284             fileName = FileUploadServlet.getCanonicalFileName(fileName);
285             if (snipId != null && snipId.length() > 0 &&
286                     mime != null && mime.length() > 0) {
287                 File JavaDoc relativeFileLocation = new File JavaDoc(snip.getName(), fileName);
288                 Attachment attachment =
289                         new Attachment(relativeFileLocation.getName(),
290                                 mime, 0, new Date(),
291                                 relativeFileLocation.getPath());
292                 OutputStream JavaDoc out = attachmentStorage.getOutputStream(attachment);
293                 out.write(data);
294                 out.flush();
295                 out.close();
296    //todo: what happens if the file name allready exists? verify.
297
attachment.setSize(data.length);
298                 snip.getAttachments().addAttachment(attachment);
299
300                 SnipSpaceFactory.getInstance().store(snip);
301                 Hashtable result = new Hashtable(1);
302                 result.put("url", attachment.getLocation());//does this return a url?
303
//todo: find out what else one can return
304
return result;
305
306             }
307
308         } catch (IOException JavaDoc e) {
309             //TODO: check that 0 is an acceptable code
310
throw new XmlRpcException(0, "Error writing file to disk: " + e);
311         }
312         return null;
313     }
314
315
316 }
317
Popular Tags