1 18 19 package org.apache.roller.webservices.xmlrpc; 20 21 import java.io.ByteArrayInputStream ; 22 import java.sql.Timestamp ; 23 import java.util.Date ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Vector ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 31 import org.apache.commons.lang.StringUtils; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.roller.RollerException; 35 import org.apache.roller.config.RollerRuntimeConfig; 36 import org.apache.roller.model.FileManager; 37 import org.apache.roller.model.Roller; 38 import org.apache.roller.model.RollerFactory; 39 import org.apache.roller.model.WeblogManager; 40 import org.apache.roller.pojos.UserData; 41 import org.apache.roller.pojos.WeblogCategoryData; 42 import org.apache.roller.pojos.WeblogEntryData; 43 import org.apache.roller.pojos.WebsiteData; 44 import org.apache.roller.ui.core.RollerContext; 45 import org.apache.roller.util.RollerMessages; 46 import org.apache.roller.util.URLUtilities; 47 import org.apache.roller.util.Utilities; 48 import org.apache.struts.util.RequestUtils; 49 import org.apache.xmlrpc.XmlRpcException; 50 51 58 public class MetaWeblogAPIHandler extends BloggerAPIHandler { 59 60 static final long serialVersionUID = -1364456614935668629L; 61 62 private static Log mLogger = LogFactory.getLog(MetaWeblogAPIHandler.class); 63 64 public MetaWeblogAPIHandler() { 65 super(); 66 } 67 68 69 78 public Object getCategories(String blogid, String userid, String password) 79 throws Exception { 80 81 mLogger.debug("getCategories() Called =====[ SUPPORTED ]====="); 82 mLogger.debug(" BlogId: " + blogid); 83 mLogger.debug(" UserId: " + userid); 84 85 WebsiteData website = validate(blogid, userid,password); 86 Roller roller = RollerFactory.getRoller(); 87 try { 88 Hashtable result = new Hashtable (); 89 WeblogManager weblogMgr = roller.getWeblogManager(); 90 List cats = weblogMgr.getWeblogCategories(website, false); 91 for (Iterator wbcItr = cats.iterator(); wbcItr.hasNext();) { 92 WeblogCategoryData category = (WeblogCategoryData) wbcItr.next(); 93 result.put(category.getPath(), 94 createCategoryStruct(category, userid)); 95 } 96 return result; 97 } catch (Exception e) { 98 String msg = "ERROR in MetaWeblogAPIHandler.getCategories"; 99 mLogger.error(msg,e); 100 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 101 } 102 } 103 104 105 116 public boolean editPost(String postid, String userid, String password, 117 Hashtable struct, int publish) throws Exception { 118 119 return editPost(postid, userid, password, struct, publish > 0); 120 } 121 122 123 public boolean editPost(String postid, String userid, String password, 124 Hashtable struct, boolean publish) throws Exception { 125 126 mLogger.debug("editPost() Called ========[ SUPPORTED ]====="); 127 mLogger.debug(" PostId: " + postid); 128 mLogger.debug(" UserId: " + userid); 129 mLogger.debug(" Publish: " + publish); 130 131 Roller roller = RollerFactory.getRoller(); 132 WeblogManager weblogMgr = roller.getWeblogManager(); 133 WeblogEntryData entry = weblogMgr.getWeblogEntry(postid); 134 135 validate(entry.getWebsite().getHandle(), userid,password); 136 137 Hashtable postcontent = struct; 138 String description = (String )postcontent.get("description"); 139 String title = (String )postcontent.get("title"); 140 if (title == null) title = ""; 141 142 Date dateCreated = (Date )postcontent.get("dateCreated"); 143 if (dateCreated == null) dateCreated = (Date )postcontent.get("pubDate"); 144 145 String cat = null; 146 if ( postcontent.get("categories") != null ) { 147 Vector cats = (Vector )postcontent.get("categories"); 148 cat = (String )cats.elementAt(0); 149 } 150 mLogger.debug(" Title: " + title); 151 mLogger.debug(" Category: " + cat); 152 153 try { 154 155 Timestamp current = 156 new Timestamp (System.currentTimeMillis()); 157 158 if ( !title.equals("") ) entry.setTitle(title); 159 entry.setText(description); 160 entry.setUpdateTime(current); 161 if (Boolean.valueOf(publish).booleanValue()) { 162 entry.setStatus(WeblogEntryData.PUBLISHED); 163 } else { 164 entry.setStatus(WeblogEntryData.DRAFT); 165 } 166 if (dateCreated != null) { 167 entry.setPubTime(new Timestamp (dateCreated.getTime())); 168 } 169 170 if ( cat != null ) { 171 WeblogCategoryData cd = 173 weblogMgr.getWeblogCategoryByPath(entry.getWebsite(), cat); 174 entry.setCategory(cd); 175 } 176 177 weblogMgr.saveWeblogEntry(entry); 179 roller.flush(); 180 181 flushPageCache(entry.getWebsite()); 183 184 Thread.sleep(1000); 187 188 return true; 189 } catch (Exception e) { 190 String msg = "ERROR in MetaWeblogAPIHandler.editPost"; 191 mLogger.error(msg,e); 192 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 193 } 194 } 195 196 197 208 public String newPost(String blogid, String userid, String password, 209 Hashtable struct, int publish) throws Exception { 210 211 return newPost(blogid, userid, password, struct, publish > 0); 212 } 213 214 215 public String newPost(String blogid, String userid, String password, 216 Hashtable struct, boolean publish) throws Exception { 217 218 mLogger.debug("newPost() Called ===========[ SUPPORTED ]====="); 219 mLogger.debug(" BlogId: " + blogid); 220 mLogger.debug(" UserId: " + userid); 221 mLogger.debug(" Publish: " + publish); 222 223 WebsiteData website = validate(blogid, userid, password); 224 225 Hashtable postcontent = struct; 226 String description = (String )postcontent.get("description"); 227 String title = (String )postcontent.get("title"); 228 if (StringUtils.isEmpty(title) && StringUtils.isEmpty(description)) { 229 throw new XmlRpcException( 230 BLOGGERAPI_INCOMPLETE_POST, "Must specify title or description"); 231 } 232 if (StringUtils.isEmpty(title)) { 233 title = Utilities.truncateNicely(description, 15, 15, "..."); 234 } 235 236 Date dateCreated = (Date )postcontent.get("dateCreated"); 237 if (dateCreated == null) dateCreated = (Date )postcontent.get("pubDate"); 238 if (dateCreated == null) dateCreated = new Date (); 239 mLogger.debug(" Title: " + title); 240 241 try { 242 Roller roller = RollerFactory.getRoller(); 243 WeblogManager weblogMgr = roller.getWeblogManager(); 244 UserData user = roller.getUserManager().getUserByUserName(userid); 245 Timestamp current = 246 new Timestamp (System.currentTimeMillis()); 247 248 WeblogEntryData entry = new WeblogEntryData(); 249 entry.setTitle(title); 250 entry.setText(description); 251 entry.setPubTime(new Timestamp (dateCreated.getTime())); 252 entry.setUpdateTime(current); 253 entry.setWebsite(website); 254 entry.setCreator(user); 255 if (Boolean.valueOf(publish).booleanValue()) { 256 entry.setStatus(WeblogEntryData.PUBLISHED); 257 } else { 258 entry.setStatus(WeblogEntryData.DRAFT); 259 } 260 261 WeblogCategoryData rollerCat = null; 264 if ( postcontent.get("categories") != null ) { 265 Vector cats = (Vector )postcontent.get("categories"); 266 if (cats != null && cats.size() > 0) { 267 for (int i=0; i<cats.size(); i++) { 268 String cat = (String )cats.get(i); 269 rollerCat = weblogMgr.getWeblogCategoryByPath(website, cat); 270 if (rollerCat != null) { 271 entry.setCategory(rollerCat); 272 break; 273 } 274 } 275 } 276 } 277 if (rollerCat == null) { 278 entry.setCategory(website.getBloggerCategory()); 280 } 281 282 weblogMgr.saveWeblogEntry(entry); 284 roller.flush(); 285 286 flushPageCache(entry.getWebsite()); 288 289 Thread.sleep(1000); 292 293 return entry.getId(); 294 } catch (Exception e) { 295 String msg = "ERROR in MetaWeblogAPIHandler.newPost"; 296 mLogger.error(msg,e); 297 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 298 } 299 } 300 301 302 310 public Object getPost(String postid, String userid, String password) 311 throws Exception { 312 313 mLogger.debug("getPost() Called =========[ SUPPORTED ]====="); 314 mLogger.debug(" PostId: " + postid); 315 mLogger.debug(" UserId: " + userid); 316 317 Roller roller = RollerFactory.getRoller(); 318 WeblogManager weblogMgr = roller.getWeblogManager(); 319 WeblogEntryData entry = weblogMgr.getWeblogEntry(postid); 320 321 validate(entry.getWebsite().getHandle(), userid,password); 322 323 try { 324 return createPostStruct(entry, userid); 325 } catch (Exception e) { 326 String msg = "ERROR in MetaWeblogAPIHandler.getPost"; 327 mLogger.error(msg,e); 328 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 329 } 330 } 331 332 333 338 public Object newMediaObject(String blogid, String userid, String password, 339 Hashtable struct) throws Exception { 340 341 mLogger.debug("newMediaObject() Called =[ SUPPORTED ]====="); 342 mLogger.debug(" BlogId: " + blogid); 343 mLogger.debug(" UserId: " + userid); 344 mLogger.debug(" Password: *********"); 345 346 WebsiteData website = validate(blogid, userid, password); 347 try { 348 String name = (String ) struct.get("name"); 349 name = name.replaceAll("/","_"); 350 String type = (String ) struct.get("type"); 351 mLogger.debug("newMediaObject name: " + name); 352 mLogger.debug("newMediaObject type: " + type); 353 354 byte[] bits = (byte[]) struct.get("bits"); 355 356 Roller roller = RollerFactory.getRoller(); 357 FileManager fmgr = roller.getFileManager(); 358 RollerMessages msgs = new RollerMessages(); 359 360 if (fmgr.canSave(website.getHandle(), name, type, bits.length, msgs)) { 362 fmgr.saveFile(website.getHandle(), name, type, bits.length, new ByteArrayInputStream (bits)); 364 365 String uploadPath = RollerFactory.getRoller().getFileManager().getUploadUrl(); 367 uploadPath += "/" + website.getHandle() + "/" + name; 368 String fileLink = URLUtilities.getWeblogResourceURL(website, name, true); 369 370 Hashtable returnStruct = new Hashtable (1); 371 returnStruct.put("url", fileLink); 372 return returnStruct; 373 } 374 throw new XmlRpcException(UPLOAD_DENIED_EXCEPTION, 375 "File upload denied because:" + msgs.toString()); 376 } catch (RollerException e) { 377 String msg = "ERROR in MetaWeblogAPIHandler.newMediaObject"; 378 mLogger.error(msg,e); 379 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 380 } 381 } 382 383 384 394 public Object getRecentPosts(String blogid, String userid, String password, 395 int numposts) throws Exception { 396 397 mLogger.debug("getRecentPosts() Called ===========[ SUPPORTED ]====="); 398 mLogger.debug(" BlogId: " + blogid); 399 mLogger.debug(" UserId: " + userid); 400 mLogger.debug(" Number: " + numposts); 401 402 WebsiteData website = validate(blogid, userid,password); 403 404 try { 405 Vector results = new Vector (); 406 407 Roller roller = RollerFactory.getRoller(); 408 WeblogManager weblogMgr = roller.getWeblogManager(); 409 if (website != null) { 410 List entries = weblogMgr.getWeblogEntries( 411 website, null, 413 null, null, null, null, "updateTime", null, 0, numposts); 419 420 Iterator iter = entries.iterator(); 421 while (iter.hasNext()) { 422 WeblogEntryData entry = (WeblogEntryData)iter.next(); 423 results.addElement(createPostStruct(entry, userid)); 424 } 425 } 426 return results; 427 428 } catch (Exception e) { 429 String msg = "ERROR in MetaWeblogAPIHandler.getRecentPosts"; 430 mLogger.error(msg,e); 431 throw new XmlRpcException(UNKNOWN_EXCEPTION, msg); 432 } 433 } 434 435 436 private Hashtable createPostStruct(WeblogEntryData entry, String userid) { 437 438 String permalink = 439 RollerRuntimeConfig.getAbsoluteContextURL() + entry.getPermaLink(); 440 441 Hashtable struct = new Hashtable (); 442 struct.put("title", entry.getTitle()); 443 if (entry.getLink() != null) { 444 struct.put("link", Utilities.escapeHTML(entry.getLink())); 445 } 446 struct.put("description", entry.getText()); 447 if (entry.getPubTime() != null) { 448 struct.put("pubDate", entry.getPubTime()); 449 struct.put("dateCreated", entry.getPubTime()); 450 } 451 struct.put("guid", Utilities.escapeHTML(permalink)); 452 struct.put("permaLink", Utilities.escapeHTML(permalink)); 453 struct.put("postid", entry.getId()); 454 struct.put("userid", userid); 455 456 Vector catArray = new Vector (); 457 catArray.addElement(entry.getCategory().getPath()); 458 struct.put("categories", catArray); 459 460 return struct; 461 } 462 463 464 private Hashtable createCategoryStruct(WeblogCategoryData category, String userid) { 465 466 String contextUrl = RollerRuntimeConfig.getAbsoluteContextURL(); 467 468 Hashtable struct = new Hashtable (); 469 struct.put("description", category.getPath()); 470 471 String catUrl = contextUrl+"/page/"+userid+"?catname="+category.getPath(); 472 catUrl = StringUtils.replace(catUrl," ","%20"); 473 struct.put("htmlUrl", catUrl); 474 475 String rssUrl = contextUrl+"/rss/"+userid+"?catname="+category.getPath(); 476 rssUrl = StringUtils.replace(catUrl," ","%20"); 477 struct.put("rssUrl",rssUrl); 478 479 return struct; 480 } 481 482 } 483 | Popular Tags |