KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > xmlrpc > BloggerAPIHandler


1
2 package org.roller.presentation.xmlrpc;
3
4 import org.apache.commons.lang.StringUtils;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.apache.xmlrpc.XmlRpcException;
8 import org.roller.RollerException;
9 import org.roller.model.Roller;
10 import org.roller.model.UserManager;
11 import org.roller.model.WeblogManager;
12 import org.roller.pojos.PageData;
13 import org.roller.pojos.UserData;
14 import org.roller.pojos.WeblogEntryData;
15 import org.roller.pojos.WebsiteData;
16 import org.roller.presentation.RollerContext;
17 import org.roller.presentation.RollerRequest;
18
19 import java.sql.Timestamp JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30
31 /**
32  * Roller XML-RPC Handler for the Blogger v1 API.
33  *
34  * Blogger API spec can be found at http://plant.blogger.com/api/index.html
35  * See also http://xmlrpc.free-conversant.com/docs/bloggerAPI
36  *
37  * @author David M Johnson
38  */

39 public class BloggerAPIHandler extends BaseAPIHandler
40 {
41     static final long serialVersionUID = 2398898776655115019L;
42     
43     private static Log mLogger =
44         LogFactory.getFactory().getInstance(RollerXMLRPCServlet.class);
45
46     public BloggerAPIHandler()
47     {
48         super();
49     }
50
51     //------------------------------------------------------------------------
52

53     /**
54      * Delete a Post
55      *
56      * @param appkey Unique identifier/passcode of the application sending the post
57      * @param postid Unique identifier of the post to be changed
58      * @param userid Login for a Blogger user who has permission to post to the blog
59      * @param password Password for said username
60      * @param publish Ignored
61      * @throws XmlRpcException
62      * @return
63      */

64     public boolean deletePost(String JavaDoc appkey, String JavaDoc postid, String JavaDoc userid,
65                               String JavaDoc password, boolean publish)
66                        throws Exception JavaDoc
67     {
68         mLogger.info("deletePost() Called =====[ SUPPORTED ]=====");
69         mLogger.info(" Appkey: " + appkey);
70         mLogger.info(" PostId: " + postid);
71         mLogger.info(" UserId: " + userid);
72
73         validate(userid,password);
74
75         Roller roller = RollerRequest.getRollerRequest().getRoller();
76         WeblogManager weblogMgr = roller.getWeblogManager();
77         try
78         {
79             WeblogEntryData entry = weblogMgr.retrieveWeblogEntry(postid);
80             entry.remove();
81             roller.commit();
82             flushPageCache(userid);
83         }
84         catch (Exception JavaDoc e)
85         {
86             String JavaDoc msg = "ERROR in blogger.deletePost: "+e.getClass().getName();
87             mLogger.error(msg,e);
88             e.printStackTrace();
89             throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
90         }
91         return true;
92     }
93
94     //------------------------------------------------------------------------
95

96     /**
97      * Edits the main index template of a given blog. Roller only support
98      * updating the main template, the default template of your weblog.
99      *
100      * @param appkey Unique identifier/passcode of the application sending the post
101      * @param blogid Unique identifier of the blog the post will be added to
102      * @param userid Login for a Blogger user who has permission to post to the blog
103      * @param password Password for said username
104      * @param template The text for the new template (usually mostly HTML).
105      * @param templateType Determines which of the blog's templates is to be set.
106      * @throws XmlRpcException
107      * @return
108      */

109     public boolean setTemplate(String JavaDoc appkey, String JavaDoc blogid, String JavaDoc userid,
110                                String JavaDoc password, String JavaDoc templateData,
111                                String JavaDoc templateType) throws Exception JavaDoc
112     {
113         mLogger.info("setTemplate() Called =====[ SUPPORTED ]=====");
114         mLogger.info(" Appkey: " + appkey);
115         mLogger.info(" BlogId: " + blogid);
116         mLogger.info(" UserId: " + userid);
117         mLogger.info(" Template: " + templateData);
118         mLogger.info(" Type: " + templateType);
119
120         validate(userid,password);
121
122         if (! templateType.equals("main"))
123         {
124             throw new XmlRpcException(
125                 UNKNOWN_EXCEPTION, "Roller only supports main template");
126         }
127
128         try
129         {
130             Roller roller = RollerRequest.getRollerRequest().getRoller();
131             UserManager userMgr = roller.getUserManager();
132
133             PageData page = userMgr.retrievePage(templateType);
134             page.setTemplate(templateData);
135             userMgr.storePage(page);
136             flushPageCache(userid);
137
138             return true;
139         }
140         catch (RollerException e)
141         {
142             String JavaDoc msg = "ERROR in BlooggerAPIHander.setTemplate";
143             mLogger.error(msg,e);
144             throw new XmlRpcException(UNKNOWN_EXCEPTION,msg);
145         }
146     }
147
148     //------------------------------------------------------------------------
149

150     /**
151      * Returns the main or archive index template of a given blog
152      *
153      * @param appkey Unique identifier/passcode of the application sending the post
154      * @param blogid Unique identifier of the blog the post will be added to
155      * @param userid Login for a Blogger user who has permission to post to the blog
156      * @param password Password for said username
157      * @param templateType Determines which of the blog's templates will be returned. Currently, either "main" or "archiveIndex"
158      * @throws XmlRpcException
159      * @return
160      */

161     public String JavaDoc getTemplate(String JavaDoc appkey, String JavaDoc blogid, String JavaDoc userid,
162                               String JavaDoc password, String JavaDoc templateType)
163                        throws Exception JavaDoc
164     {
165         mLogger.info("getTemplate() Called =====[ SUPPORTED ]=====");
166         mLogger.info(" Appkey: " + appkey);
167         mLogger.info(" BlogId: " + blogid);
168         mLogger.info(" UserId: " + userid);
169         mLogger.info(" Type: " + templateType);
170
171         validate(userid,password);
172
173         try
174         {
175             Roller roller = RollerRequest.getRollerRequest().getRoller();
176             UserManager userMgr = roller.getUserManager();
177             PageData page = userMgr.retrievePage(templateType);
178
179             if ( null == page )
180             {
181                 throw new XmlRpcException(UNKNOWN_EXCEPTION,"Template not found");
182             }
183             else
184             {
185                 return page.getTemplate();
186             }
187         }
188         catch (Exception JavaDoc e)
189         {
190             String JavaDoc msg = "ERROR in BlooggerAPIHander.getTemplate";
191             mLogger.error(msg,e);
192             throw new XmlRpcException(UNKNOWN_EXCEPTION,msg);
193         }
194     }
195
196     //------------------------------------------------------------------------
197

198     /**
199      * Authenticates a user and returns basic user info (name, email, userid, etc.)
200      *
201      * @param appkey Unique identifier/passcode of the application sending the post
202      * @param userid Login for a Blogger user who has permission to post to the blog
203      * @param password Password for said username
204      * @throws XmlRpcException
205      * @return
206      */

207     public Object JavaDoc getUserInfo(String JavaDoc appkey, String JavaDoc userid, String JavaDoc password)
208                        throws Exception JavaDoc
209     {
210         mLogger.info("getUserInfo() Called =====[ SUPPORTED ]=====");
211         mLogger.info(" Appkey: " + appkey);
212         mLogger.info(" UserId: " + userid);
213
214         validate(userid,password);
215
216         try
217         {
218             Roller roller = RollerRequest.getRollerRequest().getRoller();
219             UserManager userMgr = roller.getUserManager();
220             UserData user = userMgr.getUser(userid);
221
222             // parses full name into two strings, firstname and lastname
223
String JavaDoc firstname = "", lastname = "";
224             StringTokenizer JavaDoc toker = new StringTokenizer JavaDoc(user.getFullName());
225
226             if (toker.hasMoreTokens())
227             {
228                 firstname = toker.nextToken();
229             }
230
231             while (toker.hasMoreTokens())
232             {
233                 if ( !lastname.equals("") )
234                 {
235                     lastname += " ";
236                 }
237                 lastname += toker.nextToken();
238             }
239
240             RollerRequest rreq = RollerRequest.getRollerRequest();
241             HttpServletRequest JavaDoc req = rreq.getRequest();
242             String JavaDoc contextUrl =
243                 RollerContext.getRollerContext(req).getAbsoluteContextUrl(req);
244             
245             // populates user information to return as a result
246
Hashtable JavaDoc result = new Hashtable JavaDoc();
247             result.put("nickname", user.getUserName());
248             result.put("userid", user.getUserName());
249             result.put("url", contextUrl+"/page/"+userid);
250             result.put("email", "");
251             result.put("lastname", lastname);
252             result.put("firstname", firstname);
253
254             return result;
255         }
256         catch (RollerException e)
257         {
258             String JavaDoc msg = "ERROR in BlooggerAPIHander.getInfo";
259             mLogger.error(msg,e);
260             throw new XmlRpcException(UNKNOWN_EXCEPTION,msg);
261         }
262     }
263
264     //------------------------------------------------------------------------
265

266     /**
267      * Returns information on all the blogs a given user is a member of
268      *
269      * @param appkey Unique identifier/passcode of the application sending the post
270      * @param userid Login for a Blogger user who has permission to post to the blog
271      * @param password Password for said username
272      * @throws XmlRpcException
273      * @return
274      */

275     public Object JavaDoc getUsersBlogs(String JavaDoc appkey, String JavaDoc userid, String JavaDoc password)
276                          throws Exception JavaDoc
277     {
278         mLogger.info("getUsersBlogs() Called ===[ SUPPORTED ]=======");
279         mLogger.info(" Appkey: " + appkey);
280         mLogger.info(" UserId: " + userid);
281
282         WebsiteData website = validate(userid,password);
283
284         try
285         {
286             RollerRequest rreq = RollerRequest.getRollerRequest();
287             HttpServletRequest JavaDoc req = rreq.getRequest();
288             String JavaDoc contextUrl =
289                 RollerContext.getRollerContext(req).getAbsoluteContextUrl(req);
290
291             Hashtable JavaDoc blog = new Hashtable JavaDoc(3);
292             blog.put("url", contextUrl+"/page/"+userid);
293             blog.put("blogid", userid);
294             blog.put("blogName", website.getName());
295
296             Vector JavaDoc result = new Vector JavaDoc();
297             result.add(blog);
298
299             return result;
300         }
301         catch (Exception JavaDoc e)
302         {
303             String JavaDoc msg = "ERROR in BlooggerAPIHander.getUsersBlogs";
304             mLogger.error(msg,e);
305             throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
306         }
307     }
308
309     //------------------------------------------------------------------------
310

311     /**
312      * Edits a given post. Optionally, will publish the blog after making the edit
313      *
314      * @param appkey Unique identifier/passcode of the application sending the post
315      * @param postid Unique identifier of the post to be changed
316      * @param userid Login for a Blogger user who has permission to post to the blog
317      * @param password Password for said username
318      * @param content Contents of the post
319      * @param publish If true, the blog will be published immediately after the post is made
320      * @throws XmlRpcException
321      * @return
322      */

323     public boolean editPost(String JavaDoc appkey, String JavaDoc postid, String JavaDoc userid,
324                             String JavaDoc password, String JavaDoc content, boolean publish)
325                      throws Exception JavaDoc
326     {
327         mLogger.info("editPost() Called ========[ SUPPORTED ]=====");
328         mLogger.info(" Appkey: " + appkey);
329         mLogger.info(" PostId: " + postid);
330         mLogger.info(" UserId: " + userid);
331         mLogger.info(" Publish: " + publish);
332         mLogger.info(" Content:\n " + content);
333
334         validate(userid,password);
335
336         try
337         {
338             Timestamp JavaDoc current = new Timestamp JavaDoc(System.currentTimeMillis());
339
340             Roller roller = RollerRequest.getRollerRequest().getRoller();
341             WeblogManager weblogMgr = roller.getWeblogManager();
342             WeblogEntryData entry = weblogMgr.retrieveWeblogEntry(postid);
343             entry.setText(content);
344             entry.setUpdateTime(current);
345             entry.setPublishEntry(Boolean.valueOf(publish));
346
347             entry.save();
348             roller.commit();
349             flushPageCache(userid);
350             return true;
351         }
352         catch (Exception JavaDoc e)
353         {
354             String JavaDoc msg = "ERROR in BlooggerAPIHander.editPost";
355             mLogger.error(msg,e);
356             throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
357         }
358     }
359
360     //------------------------------------------------------------------------
361

362     /**
363      * Makes a new post to a designated blog. Optionally, will publish the blog after making the post
364      *
365      * @param appkey Unique identifier/passcode of the application sending the post
366      * @param blogid Unique identifier of the blog the post will be added to
367      * @param userid Login for a Blogger user who has permission to post to the blog
368      * @param password Password for said username
369      * @param content Contents of the post
370      * @param publish If true, the blog will be published immediately after the post is made
371      * @throws XmlRpcException
372      * @return
373      */

374     public String JavaDoc newPost(String JavaDoc appkey, String JavaDoc blogid, String JavaDoc userid,
375                           String JavaDoc password, String JavaDoc content, boolean publish)
376                    throws Exception JavaDoc
377     {
378         mLogger.info("newPost() Called ===========[ SUPPORTED ]=====");
379         mLogger.info(" Appkey: " + appkey);
380         mLogger.info(" BlogId: " + blogid);
381         mLogger.info(" UserId: " + userid);
382         mLogger.info(" Publish: " + publish);
383         mLogger.info(" Content:\n " + content);
384
385         WebsiteData website = validate(userid,password);
386
387         // extract the title from the content
388
String JavaDoc title = "";
389
390         if (content.indexOf("<title>") != -1)
391         {
392             title =
393                 content.substring(content.indexOf("<title>") + 7,
394                                   content.indexOf("</title>"));
395             content = StringUtils.replace(content, "<title>"+title+"</title>", "");
396         }
397
398         try
399         {
400             RollerRequest rreq = RollerRequest.getRollerRequest();
401             Roller roller = rreq.getRoller();
402
403             Timestamp JavaDoc current = new Timestamp JavaDoc(System.currentTimeMillis());
404
405             WeblogEntryData entry = new WeblogEntryData();
406             entry.setTitle(title);
407             entry.setText(content);
408             entry.setPubTime(current);
409             entry.setUpdateTime(current);
410             entry.setWebsite(website);
411             entry.setPublishEntry(Boolean.valueOf(publish));
412             entry.setCategory(website.getBloggerCategory());
413
414             entry.save();
415             roller.commit();
416             flushPageCache(userid);
417 /*
418             String blogUrl = Utilities.escapeHTML(
419                 RollerContext.getRollerContext(req).getAbsoluteContextUrl(req)
420                 + "/page/" + userid);
421             RollerXmlRpcClient.sendWeblogsPing(
422                 blogUrl,
423                 entry.getWebsite().getName());
424 */

425             return entry.getId();
426         }
427         catch (Exception JavaDoc e)
428         {
429             String JavaDoc msg = "ERROR in BlooggerAPIHander.newPost";
430             mLogger.error(msg,e);
431             throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
432         }
433     }
434
435     //------------------------------------------------------------------------
436

437     /**
438      * This method was added to the Blogger 1.0 API via an Email from Evan
439      * Williams to the Yahoo Group bloggerDev, see the email message for details -
440      * http://groups.yahoo.com/group/bloggerDev/message/225
441      *
442      * @param appkey Unique identifier/passcode of the application sending the post
443      * @param blogid Unique identifier of the blog the post will be added to
444      * @param userid Login for a Blogger user who has permission to post to the blog
445      * @param password Password for said username
446      * @param numposts Number of Posts to Retrieve
447      * @throws XmlRpcException
448      * @return Vector of Hashtables, each containing dateCreated, userid, postid, content
449      */

450     public Object JavaDoc getRecentPosts(
451         String JavaDoc appkey, String JavaDoc blogid, String JavaDoc userid, String JavaDoc password, int numposts)
452         throws Exception JavaDoc
453     {
454         mLogger.info("getRecentPosts() Called ===========[ SUPPORTED ]=====");
455         mLogger.info(" Appkey: " + appkey);
456         mLogger.info(" BlogId: " + blogid);
457         mLogger.info(" UserId: " + userid);
458         mLogger.info(" Number: " + numposts);
459
460         WebsiteData website = validate(userid,password);
461
462         try
463         {
464             Vector JavaDoc results = new Vector JavaDoc();
465
466             Roller roller = RollerRequest.getRollerRequest().getRoller();
467             WeblogManager weblogMgr = roller.getWeblogManager();
468             if (website != null)
469             {
470                 Map JavaDoc entries = weblogMgr.getWeblogEntryObjectMap(
471                                 website, // userName
472
null, // startDate
473
new Date JavaDoc(), // endDate
474
null, // catName
475
WeblogManager.ALL, // status
476
new Integer JavaDoc(numposts)); // maxEntries
477

478                 Iterator JavaDoc iter = entries.values().iterator();
479                 while (iter.hasNext())
480                 {
481                     ArrayList JavaDoc list = (ArrayList JavaDoc) iter.next();
482                     Iterator JavaDoc i = list.iterator();
483                     while (i.hasNext())
484                     {
485                         WeblogEntryData entry = (WeblogEntryData) i.next();
486                         Hashtable JavaDoc result = new Hashtable JavaDoc();
487                         result.put("dateCreated", entry.getPubTime());
488                         result.put("userid", userid);
489                         result.put("postid", entry.getId());
490                         result.put("content", entry.getText());
491                         results.add(result);
492                     }
493                 }
494             }
495             return results;
496         }
497         catch (Exception JavaDoc e)
498         {
499             String JavaDoc msg = "ERROR in BlooggerAPIHander.getRecentPosts";
500             mLogger.error(msg,e);
501             throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
502         }
503     }
504 }
505
Popular Tags