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 ; 20 import java.util.ArrayList ; 21 import java.util.Date ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.StringTokenizer ; 26 import java.util.Vector ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 30 31 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 53 64 public boolean deletePost(String appkey, String postid, String userid, 65 String password, boolean publish) 66 throws Exception 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 e) 85 { 86 String 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 96 109 public boolean setTemplate(String appkey, String blogid, String userid, 110 String password, String templateData, 111 String templateType) throws Exception 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 msg = "ERROR in BlooggerAPIHander.setTemplate"; 143 mLogger.error(msg,e); 144 throw new XmlRpcException(UNKNOWN_EXCEPTION,msg); 145 } 146 } 147 148 150 161 public String getTemplate(String appkey, String blogid, String userid, 162 String password, String templateType) 163 throws Exception 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 e) 189 { 190 String msg = "ERROR in BlooggerAPIHander.getTemplate"; 191 mLogger.error(msg,e); 192 throw new XmlRpcException(UNKNOWN_EXCEPTION,msg); 193 } 194 } 195 196 198 207 public Object getUserInfo(String appkey, String userid, String password) 208 throws Exception 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 String firstname = "", lastname = ""; 224 StringTokenizer toker = new StringTokenizer (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 req = rreq.getRequest(); 242 String contextUrl = 243 RollerContext.getRollerContext(req).getAbsoluteContextUrl(req); 244 245 Hashtable result = new Hashtable (); 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 msg = "ERROR in BlooggerAPIHander.getInfo"; 259 mLogger.error(msg,e); 260 throw new XmlRpcException(UNKNOWN_EXCEPTION,msg); 261 } 262 } 263 264 266 275 public Object getUsersBlogs(String appkey, String userid, String password) 276 throws Exception 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 req = rreq.getRequest(); 288 String contextUrl = 289 RollerContext.getRollerContext(req).getAbsoluteContextUrl(req); 290 291 Hashtable blog = new Hashtable (3); 292 blog.put("url", contextUrl+"/page/"+userid); 293 blog.put("blogid", userid); 294 blog.put("blogName", website.getName()); 295 296 Vector result = new Vector (); 297 result.add(blog); 298 299 return result; 300 } 301 catch (Exception e) 302 { 303 String msg = "ERROR in BlooggerAPIHander.getUsersBlogs"; 304 mLogger.error(msg,e); 305 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 306 } 307 } 308 309 311 323 public boolean editPost(String appkey, String postid, String userid, 324 String password, String content, boolean publish) 325 throws Exception 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 current = new Timestamp (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 e) 353 { 354 String msg = "ERROR in BlooggerAPIHander.editPost"; 355 mLogger.error(msg,e); 356 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 357 } 358 } 359 360 362 374 public String newPost(String appkey, String blogid, String userid, 375 String password, String content, boolean publish) 376 throws Exception 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 String 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 current = new Timestamp (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 425 return entry.getId(); 426 } 427 catch (Exception e) 428 { 429 String msg = "ERROR in BlooggerAPIHander.newPost"; 430 mLogger.error(msg,e); 431 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 432 } 433 } 434 435 437 450 public Object getRecentPosts( 451 String appkey, String blogid, String userid, String password, int numposts) 452 throws Exception 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 results = new Vector (); 465 466 Roller roller = RollerRequest.getRollerRequest().getRoller(); 467 WeblogManager weblogMgr = roller.getWeblogManager(); 468 if (website != null) 469 { 470 Map entries = weblogMgr.getWeblogEntryObjectMap( 471 website, null, new Date (), null, WeblogManager.ALL, new Integer (numposts)); 478 Iterator iter = entries.values().iterator(); 479 while (iter.hasNext()) 480 { 481 ArrayList list = (ArrayList ) iter.next(); 482 Iterator i = list.iterator(); 483 while (i.hasNext()) 484 { 485 WeblogEntryData entry = (WeblogEntryData) i.next(); 486 Hashtable result = new Hashtable (); 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 e) 498 { 499 String msg = "ERROR in BlooggerAPIHander.getRecentPosts"; 500 mLogger.error(msg,e); 501 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 502 } 503 } 504 } 505 | Popular Tags |