Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/roller/presentation/xmlrpc/MetaWeblogAPIHandler.java


1   
2   package org.roller.presentation.xmlrpc;
3   
4   import org.apache.commons.logging.Log;
5   import org.apache.commons.logging.LogFactory;
6   import org.apache.xmlrpc.XmlRpcException;
7   import org.roller.model.Roller;
8   import org.roller.model.UserManager;
9   import org.roller.model.WeblogManager;
10  import org.roller.pojos.WeblogCategoryData;
11  import org.roller.pojos.WeblogEntryData;
12  import org.roller.pojos.WebsiteData;
13  import org.roller.presentation.RollerContext;
14  import org.roller.presentation.RollerRequest;
15  import org.roller.util.Utilities;
16  
17  import java.sql.Timestamp;
18  import java.util.Hashtable;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Vector;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  
26  /**
27   * Roller XML-RPC Handler for the MetaWeblog API.
28   *
29   * MetaWeblog API spec can be found at http://www.xmlrpc.com/metaWeblogApi
30   *
31   * @author David M Johnson
32   */
33  public class MetaWeblogAPIHandler extends BloggerAPIHandler
34  {
35      private static Log mLogger = 
36          LogFactory.getFactory().getInstance(MetaWeblogAPIHandler.class);
37  
38      public MetaWeblogAPIHandler()
39      {
40          super();
41      }
42      
43      //------------------------------------------------------------------------
44  
45      /**
46       * Authenticates a user and returns the categories available in the blojsom
47       *
48       * @param blogid Dummy Value for Blojsom
49       * @param userid Login for a MetaWeblog user who has permission to post to the blog
50       * @param password Password for said username
51       * @throws Exception
52       * @return
53       */
54      public Object getCategories(String blogid, String userid, String password)
55                           throws Exception
56      {
57          mLogger.info("getCategories() Called =====[ SUPPORTED ]=====");
58          mLogger.info("     BlogId: " + blogid);
59          mLogger.info("     UserId: " + userid);
60          mLogger.info("   Password: " + password);
61  
62          validate(userid,password);
63                   
64          try
65          {
66              RollerRequest rreq = RollerRequest.getRollerRequest();
67              HttpServletRequest req = rreq.getRequest();
68              String contextUrl = 
69                  RollerContext.getRollerContext(req).getAbsoluteContextUrl(req);
70              Roller roller = rreq.getRoller();
71  
72              Vector result = new Vector();
73              WeblogManager weblogMgr = roller.getWeblogManager();
74              
75              List cats = weblogMgr.getWeblogCategories(userid);  
76              
77              for (Iterator wbcItr = cats.iterator(); wbcItr.hasNext();) {
78          WeblogCategoryData category = (WeblogCategoryData) wbcItr.next();
79          String name = category.getName();
80          String ctx = contextUrl;
81  
82          Hashtable catDetails = new Hashtable(3);
83          catDetails.put("description", name);
84  
85          String catUrl = ctx+"/page/"+userid+"?catname="+name;
86          catUrl = Utilities.stringReplace(catUrl," ","%20");                
87          catDetails.put("htmlUrl", catUrl);
88  
89          String rssUrl = ctx+"/rss/"+userid+"?catname="+name;
90          rssUrl = Utilities.stringReplace(catUrl," ","%20");                
91          catDetails.put("rssUrl",rssUrl);
92  
93          result.add(catDetails);
94        }                 
95              
96              return result;
97          }
98          catch (Exception e)
99          {
100             String msg = "ERROR in MetaWeblogAPIHandler.getCategories";
101             mLogger.error(msg,e);
102             throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
103         }
104     }
105 
106     //------------------------------------------------------------------------
107 
108     /**
109      * Edits a given post. Optionally, will publish the blog after making the edit
110      *
111      * @param postid Unique identifier of the post to be changed
112      * @param userid Login for a MetaWeblog user who has permission to post to the blog
113      * @param password Password for said username
114      * @param struct Contents of the post
115      * @param publish If true, the blog will be published immediately after the post is made
116      * @throws org.apache.xmlrpc.XmlRpcException
117      * @return
118      */
119     public boolean editPost(String postid, String userid, String password, 
120                             Hashtable struct, boolean publish)
121                      throws Exception
122     {
123         mLogger.info("editPost() Called ========[ SUPPORTED ]=====");
124         mLogger.info("     PostId: " + postid);
125         mLogger.info("     UserId: " + userid);
126         mLogger.info("   Password: " + password);
127         mLogger.info("    Publish: " + publish);
128 
129         validate(userid,password);
130         
131         Roller roller = RollerRequest.getRollerRequest().getRoller();
132                  
133         Hashtable postcontent = struct;
134         String description = (String)postcontent.get("description");
135         String title = (String)postcontent.get("title");
136         if (title == null) title = "";
137         
138         String cat = null;
139         if ( postcontent.get("categories") != null )
140         {
141             Vector cats = (Vector)postcontent.get("categories");
142             cat = (String)cats.elementAt(0);
143         }        
144         mLogger.info("      Title: " + title);
145         mLogger.info("   Category: " + cat);
146         
147         try
148         {
149             WeblogManager weblogMgr = roller.getWeblogManager();
150 //            UserManager userMgr = mRoller.getUserManager();
151 
152             Timestamp current = 
153                 new Timestamp(System.currentTimeMillis());
154 
155             WeblogEntryData entry = 
156                 weblogMgr.retrieveWeblogEntry(postid);
157             
158             if ( !title.equals("") ) entry.setTitle(title);
159             entry.setText(description);
160             entry.setUpdateTime(current);                    
161             entry.setPublishEntry(Boolean.valueOf(publish));
162             
163             if ( cat != null )
164             {
165                 // Use first category specified by request
166                 WeblogCategoryData cd = weblogMgr.getWeblogCategory(cat,userid);
167                 entry.setCategory(cd);
168             }
169 
170             weblogMgr.storeWeblogEntry(entry);
171             flushPageCache(userid);
172             
173             return true;
174         }
175         catch (Exception e)
176         {
177             String msg = "ERROR in MetaWeblogAPIHandler.editPost";
178             mLogger.error(msg,e);
179             throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
180         }                
181     }
182 
183     //------------------------------------------------------------------------
184 
185     /**
186      * Makes a new post to a designated blog. Optionally, will publish the blog after making the post
187      *
188      * @param blogid Unique identifier of the blog the post will be added to
189      * @param userid Login for a MetaWeblog user who has permission to post to the blog
190      * @param password Password for said username
191      * @param struct Contents of the post
192      * @param publish If true, the blog will be published immediately after the post is made
193      * @throws org.apache.xmlrpc.XmlRpcException
194      * @return
195      */
196     public String newPost(String blogid, String userid, String password, 
197                           Hashtable struct, boolean publish)
198                    throws Exception
199     {
200         mLogger.info("newPost() Called ===========[ SUPPORTED ]=====");
201         mLogger.info("     BlogId: " + blogid);
202         mLogger.info("     UserId: " + userid);
203         mLogger.info("   Password: " + password);
204         mLogger.info("    Publish: " + publish);
205 
206         validate(userid,password);
207                  
208         Hashtable postcontent = struct;
209         String description = (String)postcontent.get("description");
210         String title = (String)postcontent.get("title");
211         if (title == null) title = "";
212         
213         String cat = null;
214         if ( postcontent.get("categories") != null )
215         {
216             Vector cats = (Vector)postcontent.get("categories");
217             cat = (String)cats.elementAt(0);
218         }        
219         mLogger.info("      Title: " + title);
220         mLogger.info("   Category: " + cat);
221             
222         try
223         {
224             Roller roller = RollerRequest.getRollerRequest().getRoller();
225             WeblogManager weblogMgr = roller.getWeblogManager();
226             UserManager userMgr = roller.getUserManager();
227 
228             WebsiteData website = userMgr.getWebsite(userid);
229             Timestamp current = new Timestamp(System.currentTimeMillis());
230 
231             WeblogEntryData entry = new WeblogEntryData();
232             entry.setTitle(title);
233             entry.setText(description);
234             entry.setPubTime(current);
235             entry.setUpdateTime(current);
236             entry.setWebsite(website);
237             entry.setPublishEntry(Boolean.valueOf(publish));
238             
239             if ( cat != null )
240             {
241                 // Use first category specified by request
242                 WeblogCategoryData cd = weblogMgr.getWeblogCategory(cat,userid);
243                 entry.setCategory(cd);
244             }
245             else
246             {
247                 // Use Blogger API category from user's weblog config
248                 entry.setCategory(
249                     weblogMgr.retrieveWeblogCategory(
250                         website.getBloggerCategoryId()) );
251             }            
252 
253             weblogMgr.storeWeblogEntry(entry);
254             flushPageCache(userid);
255                        
256             RollerRequest rreq = RollerRequest.getRollerRequest();
257             HttpServletRequest req = rreq.getRequest();
258                 
259             String blogUrl = Utilities.escapeHTML( 
260                 RollerContext.getRollerContext(req).getAbsoluteContextUrl(req)
261                 + "/page/" + userid);
262            
263       RollerXmlRpcClient.sendWeblogsPing(
264         blogUrl, entry.getWebsite().getName());
265 
266             return entry.getId();
267         }
268         catch (Exception e)
269         {
270             String msg = "ERROR in MetaWeblogAPIHandler.newPost";
271             mLogger.error(msg,e);
272             throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
273         }
274     }
275 
276     //------------------------------------------------------------------------
277 
278     /**
279      *
280      * @param postid
281      * @param userid
282      * @param password
283      * @return
284      * @throws Exception
285      */
286     public Object getPost(String postid, String userid, String password)
287                    throws Exception
288     {
289         mLogger.info("getPost() Called =========[ SUPPORTED ]=====");
290         mLogger.info("     PostId: " + postid);
291         mLogger.info("     UserId: " + userid);
292         mLogger.info("   Password: " + password);
293 
294         validate(userid,password);
295                  
296         try
297         {
298             Roller roller = RollerRequest.getRollerRequest().getRoller();
299             WeblogManager weblogMgr = roller.getWeblogManager();
300             WeblogEntryData entry = weblogMgr.retrieveWeblogEntry(postid);
301 
302             Hashtable result = new Hashtable();
303             
304             Vector cats = new Vector();
305             cats.addElement(entry.getCategory().getName());
306             
307             result.put("categories", cats);            
308             result.put("dateCreated", entry.getPubTime());            
309             result.put("postid", entry.getId());
310             result.put("title", entry.getTitle());
311             result.put("description", entry.getText());
312             result.put("userid", userid);  
313 
314             return result;
315         }
316         catch (Exception e)
317         {
318             String msg = "ERROR in MetaWeblogAPIHandler.getPost";
319             mLogger.error(msg,e);
320             throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
321         }
322     }
323 
324     //------------------------------------------------------------------------
325 
326     /**
327      *
328      * @param blogid
329      * @param userid
330      * @param password
331      * @param struct
332      * @return
333      * @throws Exception
334      */
335     public Object newMediaObject(String blogid, String userid, String password, 
336                                  Object struct) throws Exception
337     {
338         mLogger.info("newMediaObject() Called =[ UNSUPPORTED ]=====");
339         mLogger.info("     BlogId: " + blogid);
340         mLogger.info("     UserId: " + userid);
341         mLogger.info("   Password: " + password);
342 
343         throw new XmlRpcException(UNSUPPORTED_EXCEPTION, 
344                                   UNSUPPORTED_EXCEPTION_MSG);
345     }
346     
347     /**
348      * Get a list of recent posts for a category
349      *
350      * @param blogid Unique identifier of the blog the post will be added to
351      * @param userid Login for a Blogger user who has permission to post to the blog
352      * @param password Password for said username
353      * @param numposts Number of Posts to Retrieve
354      * @throws XmlRpcException
355      * @return
356      */
357     public Object getRecentPosts(String blogid, String userid, 
358                                  String password, int numposts)
359                           throws Exception
360     {
361         return getRecentPosts("",blogid,userid,password,numposts);
362     }    
363 }