1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
package net.sourceforge.pebble.webservice; |
33 | |
|
34 | |
import java.io.IOException; |
35 | |
import java.util.Collection; |
36 | |
import java.util.Date; |
37 | |
import java.util.Hashtable; |
38 | |
import java.util.Iterator; |
39 | |
import java.util.Vector; |
40 | |
|
41 | |
import net.sourceforge.pebble.PebbleContext; |
42 | |
import net.sourceforge.pebble.domain.Blog; |
43 | |
import net.sourceforge.pebble.domain.BlogEntry; |
44 | |
import net.sourceforge.pebble.domain.BlogService; |
45 | |
import net.sourceforge.pebble.domain.BlogServiceException; |
46 | |
import net.sourceforge.pebble.domain.Category; |
47 | |
import net.sourceforge.pebble.domain.Comment; |
48 | |
import net.sourceforge.pebble.domain.FileManager; |
49 | |
import net.sourceforge.pebble.domain.FileMetaData; |
50 | |
import net.sourceforge.pebble.domain.IllegalFileAccessException; |
51 | |
import net.sourceforge.pebble.domain.Tag; |
52 | |
|
53 | |
import org.apache.commons.logging.Log; |
54 | |
import org.apache.commons.logging.LogFactory; |
55 | |
import org.apache.xmlrpc.XmlRpcException; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 4 | @SuppressWarnings("unchecked") |
63 | 112 | public class MetaWeblogAPIHandler extends AbstractAPIHandler { |
64 | |
|
65 | |
static final String BODY = "body"; |
66 | |
static final String AUTHOR = "author"; |
67 | |
static final String EMAIL = "email"; |
68 | |
static final String DATE = "date"; |
69 | |
static final String WEBSITE = "website"; |
70 | |
static final String IP_ADDRESS = "ipAddress"; |
71 | |
static final String URL = "url"; |
72 | |
static final String BLOG_ID = "blogid"; |
73 | |
static final String BLOG_NAME = "blogName"; |
74 | |
static final String DATE_CREATED = "dateCreated"; |
75 | |
static final String USER_ID = "userId"; |
76 | |
static final String POST_ID = "postid"; |
77 | |
static final String TITLE = "title"; |
78 | |
static final String DESCRIPTION = "description"; |
79 | |
static final String PERMALINK = "permaLink"; |
80 | |
static final String PUB_DATE = "pubDate"; |
81 | |
static final String CATEGORIES = "categories"; |
82 | |
static final String TAGS = "tags"; |
83 | |
static final String NAME = "name"; |
84 | |
static final String TYPE = "type"; |
85 | |
static final String BITS = "bits"; |
86 | |
static final String HTML_URL = "htmlUrl"; |
87 | |
static final String RSS_URL = "rssUrl"; |
88 | |
static final String COMMENTS = "comments"; |
89 | |
|
90 | |
|
91 | 4 | private static Log log = LogFactory.getLog(MetaWeblogAPIHandler.class); |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public Hashtable newMediaObject(String blogid, String username, String password, Hashtable struct) throws XmlRpcException { |
103 | 0 | log.debug("metaWeblog.newMediaObject(" + |
104 | |
blogid + ", " + |
105 | |
username + ", " + |
106 | |
"********)"); |
107 | 0 | log.debug(" name = " + struct.get(NAME)); |
108 | 0 | log.debug(" type = " + struct.get(TYPE)); |
109 | |
|
110 | 0 | Blog blog = getBlogWithBlogId(blogid); |
111 | 0 | authenticate(blog, username, password); |
112 | |
|
113 | 0 | Hashtable ht = new Hashtable(); |
114 | |
|
115 | 0 | String name = (String)struct.get(NAME); |
116 | |
FileManager manager; |
117 | 0 | if (name.startsWith("files/")) { |
118 | 0 | manager = new FileManager(blog, FileMetaData.BLOG_FILE); |
119 | 0 | name = name.substring(name.indexOf("/")); |
120 | 0 | } else if (name.startsWith("images/")) { |
121 | 0 | manager = new FileManager(blog, FileMetaData.BLOG_IMAGE); |
122 | 0 | name = name.substring(name.indexOf("/")); |
123 | |
} else { |
124 | 0 | manager = new FileManager(blog, FileMetaData.BLOG_IMAGE); |
125 | |
|
126 | |
} |
127 | |
|
128 | 0 | log.debug("Saving to " + name); |
129 | |
try { |
130 | 0 | byte bytes[] = (byte[])struct.get(BITS); |
131 | 0 | long itemSize = bytes.length/1024; |
132 | 0 | if (FileManager.hasEnoughSpace(blog, itemSize)) { |
133 | 0 | FileMetaData file = manager.saveFile(name, bytes); |
134 | 0 | ht.put(URL, file.getUrl()); |
135 | 0 | } else { |
136 | 0 | throw new XmlRpcException(0, "You do not have enough free space - please free some space by removing unused files or asking your system administrator to increase your quota from " + PebbleContext.getInstance().getConfiguration().getFileUploadQuota() + " KB."); |
137 | |
} |
138 | 0 | } catch (IOException e) { |
139 | 0 | e.printStackTrace(); |
140 | 0 | throw new XmlRpcException(0, "IOException"); |
141 | 0 | } catch (IllegalFileAccessException e) { |
142 | 0 | e.printStackTrace(); |
143 | 0 | throw new XmlRpcException(0, "Access forbidden"); |
144 | 0 | } |
145 | |
|
146 | 0 | return ht; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public Hashtable getCategories(String blogid, String username, String password) throws XmlRpcException { |
159 | 32 | log.debug("metaWeblog.getCategories(" + |
160 | |
blogid + ", " + |
161 | |
username + ", " + |
162 | |
"********)"); |
163 | |
|
164 | 32 | Blog blog = getBlogWithBlogId(blogid); |
165 | 20 | authenticate(blog, username, password); |
166 | |
|
167 | 16 | Hashtable categories = new Hashtable(); |
168 | 16 | Iterator it = blog.getCategories().iterator(); |
169 | |
Category category; |
170 | 48 | while (it.hasNext()) { |
171 | 32 | category = (Category)it.next(); |
172 | 32 | if (!category.isRootCategory()) { |
173 | 16 | Hashtable struct = new Hashtable(); |
174 | 16 | struct.put(DESCRIPTION, category.getId()); |
175 | 16 | struct.put(HTML_URL, category.getPermalink()); |
176 | 16 | struct.put(RSS_URL, blog.getUrl() + "rss.xml?category=" + category.getId()); |
177 | 16 | categories.put(category.getId(), struct); |
178 | 16 | } |
179 | |
} |
180 | |
|
181 | 16 | return categories; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
public Vector getRecentPosts(String blogid, String username, String password, int numberOfPosts) throws XmlRpcException { |
195 | 32 | log.debug("metaWeblog.getRecentPosts(" + |
196 | |
blogid + ", " + |
197 | |
username + ", " + |
198 | |
"********)"); |
199 | |
|
200 | 32 | Blog blog = getBlogWithBlogId(blogid); |
201 | 32 | authenticate(blog, username, password); |
202 | |
|
203 | 24 | Vector posts = new Vector(); |
204 | 24 | Collection coll = blog.getRecentBlogEntries(numberOfPosts); |
205 | |
|
206 | 24 | Iterator it = coll.iterator(); |
207 | |
BlogEntry entry; |
208 | 48 | while (it.hasNext()) { |
209 | 24 | entry = (BlogEntry)it.next(); |
210 | 24 | posts.add(adaptBlogEntry(entry)); |
211 | |
} |
212 | |
|
213 | 24 | return posts; |
214 | |
} |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public Hashtable getPost(String postid, String username, String password) throws XmlRpcException { |
226 | 40 | log.debug("metaWeblog.getPost(" + |
227 | |
postid + ", " + |
228 | |
username + ", " + |
229 | |
"********)"); |
230 | |
|
231 | 40 | Blog blog = getBlogWithPostId(postid); |
232 | 32 | postid = getPostId(postid); |
233 | 32 | authenticate(blog, username, password); |
234 | 24 | BlogService service = new BlogService(); |
235 | 24 | BlogEntry entry = null; |
236 | |
try { |
237 | 24 | entry = service.getBlogEntry(blog, postid); |
238 | 0 | } catch (BlogServiceException e) { |
239 | 0 | throw new XmlRpcException(0, "Blog entry with ID of " + postid + " could not be loaded"); |
240 | 24 | } |
241 | |
|
242 | 24 | if (entry != null) { |
243 | 8 | return adaptBlogEntry(entry); |
244 | |
} else { |
245 | 16 | throw new XmlRpcException(0, "Blog entry with ID of " + postid + " was not found."); |
246 | |
} |
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public String newPost(String blogid, String username, String password, Hashtable struct, boolean publish) throws XmlRpcException { |
261 | 36 | log.debug("metaWeblog.newPost(" + |
262 | |
blogid + ", " + |
263 | |
username + ", " + |
264 | |
"********, " + |
265 | |
struct + ", " + |
266 | |
publish + ")"); |
267 | |
|
268 | |
try { |
269 | 36 | Blog blog = getBlogWithBlogId(blogid); |
270 | 36 | authenticate(blog, username, password); |
271 | |
|
272 | 28 | BlogEntry entry = new BlogEntry(blog); |
273 | |
|
274 | 28 | if (struct.containsKey(PUB_DATE)) { |
275 | 4 | Date date = (Date)struct.get(PUB_DATE); |
276 | 4 | entry.setDate(date); |
277 | |
} |
278 | |
|
279 | 28 | populateEntry(entry, struct, username); |
280 | 28 | entry.setPublished(publish); |
281 | |
|
282 | 28 | BlogService service = new BlogService(); |
283 | 28 | service.putBlogEntry(entry); |
284 | |
|
285 | 28 | return formatPostId(blogid, entry.getId()); |
286 | 0 | } catch (BlogServiceException be) { |
287 | 0 | throw new XmlRpcException(0, be.getMessage()); |
288 | |
} |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
public boolean editPost(String postid, String username, String password, Hashtable struct, boolean publish) throws XmlRpcException { |
303 | 40 | log.debug("BloggerAPI.editPost(" + |
304 | |
postid + ", " + |
305 | |
username + ", " + |
306 | |
"********, " + |
307 | |
struct + ", " + |
308 | |
publish + ")"); |
309 | |
|
310 | |
try { |
311 | 40 | Blog blog = getBlogWithPostId(postid); |
312 | 32 | postid = getPostId(postid); |
313 | 32 | authenticate(blog, username, password); |
314 | 24 | BlogService service = new BlogService(); |
315 | 24 | BlogEntry entry = service.getBlogEntry(blog, postid); |
316 | |
|
317 | 24 | if (entry != null) { |
318 | 8 | populateEntry(entry, struct, username); |
319 | 8 | entry.setPublished(publish); |
320 | |
|
321 | 8 | service.putBlogEntry(entry); |
322 | |
} else { |
323 | 16 | throw new XmlRpcException(0, "Blog entry with ID of " + postid + " was not found."); |
324 | |
} |
325 | |
|
326 | 8 | return true; |
327 | 0 | } catch (BlogServiceException be) { |
328 | 0 | throw new XmlRpcException(0, be.getMessage()); |
329 | |
} |
330 | |
} |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
private Hashtable adaptBlogEntry(BlogEntry entry) { |
339 | 32 | Hashtable post = new Hashtable(); |
340 | 32 | post.put(TITLE, entry.getTitle()); |
341 | 32 | post.put(PERMALINK, entry.getPermalink()); |
342 | 32 | post.put(TITLE, entry.getTitle()); |
343 | 32 | post.put(DESCRIPTION, entry.getBody()); |
344 | 32 | post.put(DATE_CREATED, entry.getDate()); |
345 | 32 | post.put(PUB_DATE, entry.getDate()); |
346 | 32 | post.put(USER_ID, entry.getAuthor()); |
347 | 32 | post.put(POST_ID, formatPostId(entry.getBlog().getId(), entry.getId())); |
348 | |
|
349 | 32 | Vector categories = new Vector(); |
350 | 32 | Iterator it = entry.getCategories().iterator(); |
351 | 40 | while (it.hasNext()) { |
352 | 8 | Category cat = (Category)it.next(); |
353 | 8 | categories.add(cat.getId()); |
354 | 8 | } |
355 | 32 | post.put(CATEGORIES, categories); |
356 | |
|
357 | |
|
358 | 32 | Vector tags = new Vector(); |
359 | 32 | for (Tag tag : entry.getAllTags()) { |
360 | 0 | tags.add(tag.getName()); |
361 | |
} |
362 | 32 | post.put(TAGS, tags); |
363 | |
|
364 | |
|
365 | 32 | Vector comments = new Vector(); |
366 | 32 | for (Comment comment : entry.getComments()) { |
367 | 0 | comments.add(adaptBlogEntryComment(comment)); |
368 | |
} |
369 | 32 | post.put(COMMENTS, comments); |
370 | |
|
371 | 32 | return post; |
372 | |
} |
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
private Hashtable adaptBlogEntryComment(Comment comment) { |
381 | 0 | Hashtable cmnt = new Hashtable(); |
382 | |
|
383 | 0 | cmnt.put(BODY, comment.getBody()); |
384 | 0 | cmnt.put(AUTHOR, comment.getAuthor()); |
385 | 0 | String email = comment.getEmail(); |
386 | 0 | if (email != null) cmnt.put(EMAIL, email); |
387 | 0 | cmnt.put(DATE, comment.getDate()); |
388 | 0 | cmnt.put(PERMALINK, comment.getPermalink()); |
389 | 0 | String website = comment.getWebsite(); |
390 | 0 | if (website != null) cmnt.put(WEBSITE, website); |
391 | 0 | cmnt.put(IP_ADDRESS, comment.getIpAddress()); |
392 | |
|
393 | 0 | return cmnt; |
394 | |
} |
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
private void populateEntry(BlogEntry entry, Hashtable struct, String username) { |
404 | 36 | assert entry != null; |
405 | 36 | entry.setTitle((String)struct.get(TITLE)); |
406 | 36 | entry.setBody((String)struct.get(DESCRIPTION)); |
407 | 36 | entry.setAuthor(username); |
408 | |
|
409 | 36 | Vector categories = (Vector)struct.get(CATEGORIES); |
410 | 36 | if (categories != null) { |
411 | 40 | for (int i = 0; i < categories.size(); i++) { |
412 | 20 | Category c = entry.getBlog().getCategory((String)categories.get(i)); |
413 | 20 | if (c != null) |
414 | 12 | entry.addCategory(c); |
415 | |
} |
416 | |
} |
417 | |
|
418 | 36 | String taglist = (String)struct.get(TAGS); |
419 | 36 | entry.setTags( taglist ); |
420 | 36 | } |
421 | |
|
422 | |
} |