1 18 19 package org.apache.roller.webservices.xmlrpc; 20 21 import java.sql.Timestamp ; 22 import java.util.ArrayList ; 23 import java.util.Date ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.StringTokenizer ; 29 import java.util.Vector ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 33 import org.apache.commons.lang.StringUtils; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.roller.RollerException; 37 import org.apache.roller.config.RollerRuntimeConfig; 38 import org.apache.roller.model.Roller; 39 import org.apache.roller.model.RollerFactory; 40 import org.apache.roller.model.UserManager; 41 import org.apache.roller.model.WeblogManager; 42 import org.apache.roller.pojos.UserData; 43 import org.apache.roller.pojos.WeblogEntryData; 44 import org.apache.roller.pojos.WeblogTemplate; 45 import org.apache.roller.pojos.WebsiteData; 46 import org.apache.roller.util.Utilities; 47 import org.apache.xmlrpc.XmlRpcException; 48 49 57 public class BloggerAPIHandler extends BaseAPIHandler { 58 59 static final long serialVersionUID = 2398898776655115019L; 60 61 private static Log mLogger = LogFactory.getLog(BloggerAPIHandler.class); 62 63 public BloggerAPIHandler() { 64 super(); 65 } 66 67 68 79 public boolean deletePost(String appkey, String postid, String userid, 80 String password, boolean publish) throws Exception { 81 82 mLogger.debug("deletePost() Called =====[ SUPPORTED ]====="); 83 mLogger.debug(" Appkey: " + appkey); 84 mLogger.debug(" PostId: " + postid); 85 mLogger.debug(" UserId: " + userid); 86 87 Roller roller = RollerFactory.getRoller(); 88 WeblogManager weblogMgr = roller.getWeblogManager(); 89 WeblogEntryData entry = weblogMgr.getWeblogEntry(postid); 90 91 validate(entry.getWebsite().getHandle(), userid, password); 92 93 try { 94 weblogMgr.removeWeblogEntry(entry); 96 roller.flush(); 97 98 flushPageCache(entry.getWebsite()); 100 } catch (Exception e) { 101 String msg = "ERROR in blogger.deletePost: "+e.getClass().getName(); 102 mLogger.error(msg,e); 103 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 104 } 105 106 return true; 107 } 108 109 110 123 public boolean setTemplate(String appkey, String blogid, String userid, 124 String password, String templateData, 125 String templateType) throws Exception { 126 127 mLogger.debug("setTemplate() Called =====[ SUPPORTED ]====="); 128 mLogger.debug(" Appkey: " + appkey); 129 mLogger.debug(" BlogId: " + blogid); 130 mLogger.debug(" UserId: " + userid); 131 mLogger.debug(" Template: " + templateData); 132 mLogger.debug(" Type: " + templateType); 133 134 validate(blogid, userid, password); 135 136 if (! templateType.equals("main")) { 137 throw new XmlRpcException( 138 UNKNOWN_EXCEPTION, "Roller only supports main template"); 139 } 140 141 try { 142 Roller roller = RollerFactory.getRoller(); 143 UserManager userMgr = roller.getUserManager(); 144 145 WeblogTemplate page = userMgr.getPage(templateType); 146 page.setContents(templateData); 147 userMgr.savePage(page); 148 flushPageCache(page.getWebsite()); 149 150 return true; 151 } catch (RollerException e) { 152 String msg = "ERROR in BlooggerAPIHander.setTemplate"; 153 mLogger.error(msg,e); 154 throw new XmlRpcException(UNKNOWN_EXCEPTION,msg); 155 } 156 } 157 158 159 170 public String getTemplate(String appkey, String blogid, String userid, 171 String password, String templateType) 172 throws Exception { 173 174 mLogger.debug("getTemplate() Called =====[ SUPPORTED ]====="); 175 mLogger.debug(" Appkey: " + appkey); 176 mLogger.debug(" BlogId: " + blogid); 177 mLogger.debug(" UserId: " + userid); 178 mLogger.debug(" Type: " + templateType); 179 180 validate(blogid, userid,password); 181 182 try { 183 Roller roller = RollerFactory.getRoller(); 184 UserManager userMgr = roller.getUserManager(); 185 WeblogTemplate page = userMgr.getPage(templateType); 186 187 if ( null == page ) { 188 throw new XmlRpcException(UNKNOWN_EXCEPTION,"Template not found"); 189 } else { 190 return page.getContents(); 191 } 192 } catch (Exception e) { 193 String msg = "ERROR in BlooggerAPIHander.getTemplate"; 194 mLogger.error(msg,e); 195 throw new XmlRpcException(UNKNOWN_EXCEPTION,msg); 196 } 197 } 198 199 200 209 public Object getUserInfo(String appkey, String userid, String password) 210 throws Exception { 211 212 mLogger.debug("getUserInfo() Called =====[ SUPPORTED ]====="); 213 mLogger.debug(" Appkey: " + appkey); 214 mLogger.debug(" UserId: " + userid); 215 216 validateUser(userid, password); 217 218 try { 219 Roller roller = RollerFactory.getRoller(); 220 UserManager userMgr = roller.getUserManager(); 221 UserData user = userMgr.getUserByUserName(userid); 222 223 String firstname = "", lastname = ""; 225 StringTokenizer toker = new StringTokenizer (user.getFullName()); 226 227 if (toker.hasMoreTokens()) { 228 firstname = toker.nextToken(); 229 } 230 231 while (toker.hasMoreTokens()) { 232 if ( !lastname.equals("") ) { 233 lastname += " "; 234 } 235 lastname += toker.nextToken(); 236 } 237 238 Hashtable result = new Hashtable (); 240 result.put("nickname", user.getUserName()); 241 result.put("userid", user.getUserName()); 242 result.put("email", ""); 243 result.put("lastname", lastname); 244 result.put("firstname", firstname); 245 246 return result; 247 } catch (RollerException e) { 248 String msg = "ERROR in BlooggerAPIHander.getInfo"; 249 mLogger.error(msg,e); 250 throw new XmlRpcException(UNKNOWN_EXCEPTION,msg); 251 } 252 } 253 254 255 264 public Object getUsersBlogs(String appkey, String userid, String password) 265 throws Exception { 266 267 mLogger.debug("getUsersBlogs() Called ===[ SUPPORTED ]======="); 268 mLogger.debug(" Appkey: " + appkey); 269 mLogger.debug(" UserId: " + userid); 270 271 Vector result = new Vector (); 272 if (validateUser(userid, password)) { 273 try { 274 String contextUrl = RollerRuntimeConfig.getAbsoluteContextURL(); 275 276 UserManager umgr = RollerFactory.getRoller().getUserManager(); 277 UserData user = umgr.getUserByUserName(userid); 278 List websites = umgr.getWebsites(user, Boolean.TRUE, null, null, null, 0, -1); 280 Iterator iter = websites.iterator(); 281 while (iter.hasNext()) { 282 WebsiteData website = (WebsiteData)iter.next(); 283 Hashtable blog = new Hashtable (3); 284 blog.put("url", website.getURL()); 285 blog.put("blogid", website.getHandle()); 286 blog.put("blogName", website.getName()); 287 result.add(blog); 288 } 289 } catch (Exception e) { 290 String msg = "ERROR in BlooggerAPIHander.getUsersBlogs"; 291 mLogger.error(msg,e); 292 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 293 } 294 } 295 return result; 296 } 297 298 299 311 public boolean editPost(String appkey, String postid, String userid, 312 String password, String content, boolean publish) 313 throws Exception { 314 315 mLogger.debug("editPost() Called ========[ SUPPORTED ]====="); 316 mLogger.debug(" Appkey: " + appkey); 317 mLogger.debug(" PostId: " + postid); 318 mLogger.debug(" UserId: " + userid); 319 mLogger.debug(" Publish: " + publish); 320 mLogger.debug(" Content:\n " + content); 321 322 if (validateUser(userid, password)) { 323 try { 324 Timestamp current = new Timestamp (System.currentTimeMillis()); 325 326 Roller roller = RollerFactory.getRoller(); 327 WeblogManager weblogMgr = roller.getWeblogManager(); 328 WeblogEntryData entry = weblogMgr.getWeblogEntry(postid); 329 entry.setText(content); 330 entry.setUpdateTime(current); 331 if (Boolean.valueOf(publish).booleanValue()) { 332 entry.setStatus(WeblogEntryData.PUBLISHED); 333 } else { 334 entry.setStatus(WeblogEntryData.DRAFT); 335 } 336 337 weblogMgr.saveWeblogEntry(entry); 339 roller.flush(); 340 341 flushPageCache(entry.getWebsite()); 343 344 return true; 345 } catch (Exception e) { 346 String msg = "ERROR in BlooggerAPIHander.editPost"; 347 mLogger.error(msg,e); 348 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 349 } 350 } 351 return false; 352 } 353 354 355 367 public String newPost(String appkey, String blogid, String userid, 368 String password, String content, boolean publish) 369 throws Exception { 370 371 mLogger.debug("newPost() Called ===========[ SUPPORTED ]====="); 372 mLogger.debug(" Appkey: " + appkey); 373 mLogger.debug(" BlogId: " + blogid); 374 mLogger.debug(" UserId: " + userid); 375 mLogger.debug(" Publish: " + publish); 376 mLogger.debug(" Content:\n " + content); 377 378 WebsiteData website = validate(blogid, userid, password); 379 380 String title = ""; 382 383 if (content.indexOf("<title>") != -1) { 384 title = 385 content.substring(content.indexOf("<title>") + 7, 386 content.indexOf("</title>")); 387 content = StringUtils.replace(content, "<title>"+title+"</title>", ""); 388 } 389 if (StringUtils.isEmpty(title)) { 390 title = Utilities.truncateNicely(content, 15, 15, "..."); 391 } 392 393 try { 394 Roller roller = RollerFactory.getRoller(); 395 WeblogManager weblogMgr = roller.getWeblogManager(); 396 397 Timestamp current = new Timestamp (System.currentTimeMillis()); 398 399 WeblogEntryData entry = new WeblogEntryData(); 400 entry.setTitle(title); 401 entry.setText(content); 402 entry.setPubTime(current); 403 entry.setUpdateTime(current); 404 UserData user = roller.getUserManager().getUserByUserName(userid); 405 entry.setCreator(user); 406 entry.setWebsite(website); 407 entry.setCategory(website.getBloggerCategory()); 408 if (Boolean.valueOf(publish).booleanValue()) { 409 entry.setStatus(WeblogEntryData.PUBLISHED); 410 } else { 411 entry.setStatus(WeblogEntryData.DRAFT); 412 } 413 414 weblogMgr.saveWeblogEntry(entry); 416 roller.flush(); 417 418 flushPageCache(entry.getWebsite()); 420 421 return entry.getId(); 422 } catch (Exception e) { 423 String msg = "ERROR in BlooggerAPIHander.newPost"; 424 mLogger.error(msg,e); 425 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 426 } 427 } 428 429 430 443 public Object getRecentPosts(String appkey, String blogid, String userid, 444 String password, int numposts) 445 throws Exception { 446 447 mLogger.debug("getRecentPosts() Called ===========[ SUPPORTED ]====="); 448 mLogger.debug(" Appkey: " + appkey); 449 mLogger.debug(" BlogId: " + blogid); 450 mLogger.debug(" UserId: " + userid); 451 mLogger.debug(" Number: " + numposts); 452 453 WebsiteData website = validate(blogid, userid,password); 454 455 try { 456 Vector results = new Vector (); 457 458 Roller roller = RollerFactory.getRoller(); 459 WeblogManager weblogMgr = roller.getWeblogManager(); 460 if (website != null) { 461 Map entries = weblogMgr.getWeblogEntryObjectMap( 462 website, null, new Date (), null, null, null, 0, -1); 467 468 Iterator iter = entries.values().iterator(); 469 while (iter.hasNext()) { 470 ArrayList list = (ArrayList ) iter.next(); 471 Iterator i = list.iterator(); 472 while (i.hasNext()) { 473 WeblogEntryData entry = (WeblogEntryData) i.next(); 474 Hashtable result = new Hashtable (); 475 if (entry.getPubTime() != null) { 476 result.put("dateCreated", entry.getPubTime()); 477 } 478 result.put("userid", userid); 479 result.put("postid", entry.getId()); 480 result.put("content", entry.getText()); 481 results.add(result); 482 } 483 } 484 } 485 return results; 486 } catch (Exception e) { 487 String msg = "ERROR in BlooggerAPIHander.getRecentPosts"; 488 mLogger.error(msg,e); 489 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 490 } 491 } 492 493 } 494 | Popular Tags |