1 31 package org.blojsom.extension.xmlrpc.handler; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.xmlrpc.XmlRpcException; 36 import org.blojsom.BlojsomException; 37 import org.blojsom.authorization.AuthorizationException; 38 import org.blojsom.blog.Blog; 39 import org.blojsom.blog.Category; 40 import org.blojsom.blog.Entry; 41 import org.blojsom.fetcher.FetcherException; 42 import org.blojsom.plugin.admin.event.EntryAddedEvent; 43 import org.blojsom.plugin.admin.event.EntryDeletedEvent; 44 import org.blojsom.plugin.admin.event.EntryUpdatedEvent; 45 import org.blojsom.util.BlojsomMetaDataConstants; 46 import org.blojsom.util.BlojsomUtils; 47 import org.blojsom.util.BlojsomConstants; 48 49 import java.io.BufferedOutputStream ; 50 import java.io.File ; 51 import java.io.FileOutputStream ; 52 import java.io.IOException ; 53 import java.util.Date ; 54 import java.util.HashMap ; 55 import java.util.Hashtable ; 56 import java.util.Vector ; 57 58 65 public class MetaWeblogAPIHandler extends APIHandler { 66 67 private Log _logger = LogFactory.getLog(MetaWeblogAPIHandler.class); 68 69 private static final String METAWEBLOG_ACCEPTED_TYPES_IP = "blojsom-extension-metaweblog-accepted-types"; 70 71 74 private static final String MEMBER_BLOGID = "blogid"; 75 76 79 private static final String MEMBER_BLOGNAME = "blogName"; 80 81 84 private static final String MEMBER_DESCRIPTION = "description"; 85 86 89 private static final String MEMBER_HTML_URL = "htmlUrl"; 90 91 94 private static final String MEMBER_RSS_URL = "rssUrl"; 95 96 99 private static final String MEMBER_TITLE = "title"; 100 101 104 private static final String MEMBER_LINK = "link"; 105 106 109 private static final String MEMBER_NAME = "name"; 110 111 114 private static final String MEMBER_TYPE = "type"; 115 116 119 private static final String MEMBER_BITS = "bits"; 120 121 124 private static final String MEMBER_PERMALINK = "permaLink"; 125 126 129 private static final String MEMBER_DATE_CREATED = "dateCreated"; 130 131 134 private static final String MEMBER_CATEGORIES = "categories"; 135 136 139 private static final String MEMBER_POSTID = "postid"; 140 141 144 private static final String MEMBER_URL = "url"; 145 146 private static final String METAWEBLOG_API_PERMISSION = "post_via_metaweblog_api_permission"; 147 148 private static final String API_PREFIX = "metaWeblog"; 149 150 private String _uploadDirectory; 151 private HashMap _acceptedMimeTypes; 152 private String _staticURLPrefix; 153 154 157 public MetaWeblogAPIHandler() { 158 } 159 160 165 public String getName() { 166 return API_PREFIX; 167 } 168 169 174 public void setBlog(Blog blog) { 175 super.setBlog(blog); 176 177 _uploadDirectory = _servletConfig.getServletContext().getRealPath(_properties.getProperty(BlojsomConstants.RESOURCES_DIRECTORY_IP, BlojsomConstants.DEFAULT_RESOURCES_DIRECTORY)); 178 if (BlojsomUtils.checkNullOrBlank(_uploadDirectory)) { 179 if (_logger.isErrorEnabled()) { 180 _logger.error("Unable to obtain path to resources directory"); 181 } 182 } 183 184 if (!_uploadDirectory.endsWith("/")) { 185 _uploadDirectory += "/"; 186 } 187 188 _acceptedMimeTypes = new HashMap (3); 189 String acceptedMimeTypes = _blog.getProperty(METAWEBLOG_ACCEPTED_TYPES_IP); 190 if (acceptedMimeTypes != null && !"".equals(acceptedMimeTypes)) { 191 String [] types = BlojsomUtils.parseCommaList(acceptedMimeTypes); 192 for (int i = 0; i < types.length; i++) { 193 String type = types[i]; 194 type = type.toLowerCase(); 195 _acceptedMimeTypes.put(type, type); 196 } 197 } 198 199 _staticURLPrefix = _properties.getProperty(BlojsomConstants.RESOURCES_DIRECTORY_IP, BlojsomConstants.DEFAULT_RESOURCES_DIRECTORY); 200 if (!_staticURLPrefix.endsWith("/")) { 201 _staticURLPrefix += "/"; 202 } 203 } 204 205 214 public Object getUsersBlogs(String appkey, String userid, String password) throws Exception { 215 _logger.debug("getUsersBlogs() Called ===[ SUPPORTED ]======="); 216 _logger.debug(" Appkey: " + appkey); 217 _logger.debug(" UserId: " + userid); 218 _logger.debug(" Password: *********"); 219 220 try { 221 _authorizationProvider.authorize(_blog, null, userid, password); 222 checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION); 223 224 Vector result = new Vector (); 225 226 Category[] categories = _fetcher.loadAllCategories(_blog); 227 228 if (categories != null) { 229 for (int x = 0; x < categories.length; x++) { 230 Hashtable _bloglist = new Hashtable (3); 231 Category category = categories[x]; 232 233 String description; 234 if (!BlojsomUtils.checkNullOrBlank(category.getDescription())) { 235 description = category.getDescription(); 236 } else { 237 description = category.getName(); 238 } 239 240 _bloglist.put(MEMBER_URL, _blog.getBlogURL() + category.getName()); 241 _bloglist.put(MEMBER_BLOGID, Integer.toString(category.getId().intValue())); 242 _bloglist.put(MEMBER_BLOGNAME, description); 243 244 result.add(_bloglist); 245 } 246 } else { 247 throw new XmlRpcException(NOBLOGS_EXCEPTION, NOBLOGS_EXCEPTION_MSG); 248 } 249 250 return result; 251 } catch (BlojsomException e) { 252 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 253 } 254 } 255 256 265 public Object getCategories(String blogid, String userid, String password) throws Exception { 266 _logger.debug("getCategories() Called =====[ SUPPORTED ]====="); 267 _logger.debug(" BlogId: " + blogid); 268 _logger.debug(" UserId: " + userid); 269 _logger.debug(" Password: *********"); 270 271 try { 272 _authorizationProvider.authorize(_blog, null, userid, password); 273 checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION); 274 275 Hashtable result; 276 277 Category[] categories = _fetcher.loadAllCategories(_blog); 278 279 if (categories != null) { 280 result = new Hashtable (categories.length); 281 282 for (int x = 0; x < categories.length; x++) { 283 Hashtable catlist = new Hashtable (3); 284 Category category = categories[x]; 285 286 String description; 287 if (!BlojsomUtils.checkNullOrBlank(category.getDescription())) { 288 description = category.getDescription(); 289 } else { 290 description = category.getName(); 291 } 292 293 catlist.put(MEMBER_DESCRIPTION, description); 294 catlist.put(MEMBER_HTML_URL, _blog.getBlogURL() + category.getName()); 295 catlist.put(MEMBER_RSS_URL, _blog.getBlogURL() + category.getName() + "?flavor=rss2"); 296 297 result.put(Integer.toString(category.getId().intValue()), catlist); 298 } 299 } else { 300 throw new XmlRpcException(NOBLOGS_EXCEPTION, NOBLOGS_EXCEPTION_MSG); 301 } 302 303 return result; 304 } catch (AuthorizationException e) { 305 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 306 } 307 } 308 309 320 public String newPost(String blogid, String userid, String password, Hashtable struct, boolean publish) throws Exception { 321 _logger.debug("newPost() Called ===========[ SUPPORTED ]====="); 322 _logger.debug(" BlogId: " + blogid); 323 _logger.debug(" UserId: " + userid); 324 _logger.debug(" Password: *********"); 325 _logger.debug(" Publish: " + publish); 326 327 if (struct.containsKey(MEMBER_CATEGORIES)) { 328 Vector categories = (Vector ) struct.get(MEMBER_CATEGORIES); 329 if (categories.size() > 0) { 330 blogid = (String ) categories.get(0); 331 } 332 } 333 334 try { 335 _authorizationProvider.authorize(_blog, null, userid, password); 336 checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION); 337 338 Integer blogID; 339 try { 340 blogID = Integer.valueOf(blogid); 341 } catch (NumberFormatException e) { 342 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 343 } 344 345 String result; 346 347 Hashtable postcontent; 348 postcontent = struct; 349 350 String title = (String ) postcontent.get(MEMBER_TITLE); 351 String description = (String ) postcontent.get(MEMBER_DESCRIPTION); 352 Date dateCreated = (Date ) postcontent.get(MEMBER_DATE_CREATED); 353 354 try { 355 Category category = _fetcher.loadCategory(_blog, blogID); 356 Entry entry = _fetcher.newEntry(); 357 358 entry.setBlogId(_blog.getId()); 359 entry.setBlogCategoryId(category.getId()); 360 if (dateCreated == null) { 361 entry.setDate(new Date ()); 362 } else { 363 entry.setDate(dateCreated); 364 } 365 entry.setModifiedDate(entry.getDate()); 366 entry.setTitle(title); 367 entry.setDescription(description); 368 entry.setAuthor(userid); 369 370 if (publish) { 371 entry.setStatus(BlojsomMetaDataConstants.PUBLISHED_STATUS); 372 } else { 373 entry.setStatus(BlojsomMetaDataConstants.DRAFT_STATUS); 374 } 375 376 _fetcher.saveEntry(_blog, entry); 377 378 result = Integer.toString(entry.getId().intValue()); 379 380 _eventBroadcaster.broadcastEvent(new EntryAddedEvent(this, new Date (), entry, _blog)); 382 } catch (FetcherException e) { 383 if (_logger.isErrorEnabled()) { 384 _logger.error(e); 385 } 386 387 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 388 } 389 390 return result; 391 } catch (AuthorizationException e) { 392 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 393 } 394 } 395 396 408 public boolean editPost(String postid, String userid, String password, Hashtable struct, boolean publish) throws Exception { 409 _logger.debug("editPost() Called ========[ SUPPORTED ]====="); 410 _logger.debug(" PostId: " + postid); 411 _logger.debug(" UserId: " + userid); 412 _logger.debug(" Password: *********"); 413 _logger.debug(" Publish: " + publish); 414 415 try { 416 _authorizationProvider.authorize(_blog, null, userid, password); 417 checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION); 418 419 boolean result; 420 421 Integer postID; 422 try { 423 postID = Integer.valueOf(BlojsomUtils.removeSlashes(postid)); 424 } catch (NumberFormatException e) { 425 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 426 } 427 428 try { 429 Entry entryToEdit = _fetcher.loadEntry(_blog, postID); 430 431 Hashtable postcontent; 432 postcontent = struct; 433 434 String title = (String ) postcontent.get(MEMBER_TITLE); 435 String description = (String ) postcontent.get(MEMBER_DESCRIPTION); 436 Date dateCreated = (Date ) postcontent.get(MEMBER_DATE_CREATED); 437 438 entryToEdit.setTitle(title); 439 entryToEdit.setDescription(description); 440 if (dateCreated != null) { 441 entryToEdit.setDate(dateCreated); 442 entryToEdit.setModifiedDate(dateCreated); 443 } else { 444 entryToEdit.setModifiedDate(new Date ()); 445 } 446 447 if (publish) { 448 entryToEdit.setStatus(BlojsomMetaDataConstants.PUBLISHED_STATUS); 449 } else { 450 entryToEdit.setStatus(BlojsomMetaDataConstants.DRAFT_STATUS); 451 } 452 453 _fetcher.saveEntry(_blog, entryToEdit); 454 455 result = true; 456 457 _eventBroadcaster.broadcastEvent(new EntryUpdatedEvent(this, new Date (), entryToEdit, _blog)); 458 } catch (FetcherException e) { 459 if (_logger.isErrorEnabled()) { 460 _logger.error(e); 461 } 462 463 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 464 } 465 466 return result; 467 } catch (AuthorizationException e) { 468 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 469 } 470 } 471 472 482 public Object getPost(String postid, String userid, String password) throws Exception { 483 _logger.debug("getPost() Called =========[ SUPPORTED ]====="); 484 _logger.debug(" PostId: " + postid); 485 _logger.debug(" UserId: " + userid); 486 _logger.debug(" Password: *********"); 487 488 try { 489 _authorizationProvider.authorize(_blog, null, userid, password); 490 checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION); 491 492 Integer postID; 493 try { 494 postID = Integer.valueOf(postid); 495 } catch (NumberFormatException e) { 496 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 497 } 498 499 try { 500 Entry entry = _fetcher.loadEntry(_blog, postID); 501 502 Hashtable postcontent = new Hashtable (); 503 postcontent.put(MEMBER_TITLE, entry.getTitle()); 504 postcontent.put(MEMBER_LINK, _blog.getBlogURL() + entry.getCategory() + entry.getPostSlug()); 505 postcontent.put(MEMBER_DESCRIPTION, entry.getDescription()); 506 postcontent.put(MEMBER_DATE_CREATED, entry.getDate()); 507 postcontent.put(MEMBER_PERMALINK, _blog.getBlogURL() + entry.getCategory() + entry.getPostSlug()); 508 postcontent.put(MEMBER_POSTID, Integer.toString(entry.getId().intValue())); 509 510 Vector postCategories = new Vector (1); 511 postCategories.add(Integer.toString(entry.getBlogCategoryId().intValue())); 512 postcontent.put(MEMBER_CATEGORIES, postCategories); 513 514 return postcontent; 515 } catch (FetcherException e) { 516 if (_logger.isErrorEnabled()) { 517 _logger.error(e); 518 } 519 520 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 521 } 522 } catch (AuthorizationException e) { 523 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 524 } 525 } 526 527 538 public boolean deletePost(String appkey, String postid, String userid, String password, boolean publish) throws Exception { 539 _logger.debug("deletePost() Called =====[ SUPPORTED ]====="); 540 _logger.debug(" Appkey: " + appkey); 541 _logger.debug(" PostId: " + postid); 542 _logger.debug(" UserId: " + userid); 543 _logger.debug(" Password: *********"); 544 545 boolean result; 546 547 try { 548 _authorizationProvider.authorize(_blog, null, userid, password); 549 checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION); 550 551 Integer postID; 552 try { 553 postID = Integer.valueOf(BlojsomUtils.removeSlashes(postid)); 554 } catch (NumberFormatException e) { 555 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 556 } 557 558 try { 559 Entry entryToDelete = _fetcher.loadEntry(_blog, postID); 560 _fetcher.deleteEntry(_blog, entryToDelete); 561 562 result = true; 563 _eventBroadcaster.broadcastEvent(new EntryDeletedEvent(this, new Date (), entryToDelete, _blog)); 564 } catch (FetcherException e) { 565 if (_logger.isErrorEnabled()) { 566 _logger.error(e); 567 } 568 569 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 570 } 571 } catch (AuthorizationException e) { 572 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 573 } 574 575 return result; 576 } 577 578 588 public Object getRecentPosts(String blogid, String userid, String password, int numberOfPosts) throws Exception { 589 _logger.debug("getRecentPosts() Called =========[ SUPPORTED ]====="); 590 _logger.debug(" BlogId: " + blogid); 591 _logger.debug(" UserId: " + userid); 592 _logger.debug(" Password: *********"); 593 594 Vector recentPosts = new Vector (); 595 596 try { 597 _authorizationProvider.authorize(_blog, null, userid, password); 598 checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION); 599 600 Entry[] entries; 601 try { 602 entries = _fetcher.loadEntriesForCategory(_blog, Integer.valueOf(blogid), new Integer (numberOfPosts)); 603 } catch (FetcherException e) { 604 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 605 } catch (NumberFormatException e) { 606 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 607 } 608 609 if (entries != null && entries.length > 0) { 610 for (int x = 0; x < entries.length; x++) { 611 Entry entry = entries[x]; 612 Hashtable entrystruct = new Hashtable (); 613 614 entrystruct.put(MEMBER_TITLE, entry.getTitle()); 615 entrystruct.put(MEMBER_LINK, _blog.getBlogURL() + entry.getCategory() + entry.getPostSlug()); 616 entrystruct.put(MEMBER_DESCRIPTION, entry.getDescription()); 617 entrystruct.put(MEMBER_DATE_CREATED, entry.getDate()); 618 entrystruct.put(MEMBER_PERMALINK, _blog.getBlogURL() + entry.getCategory() + entry.getPostSlug()); 619 entrystruct.put(MEMBER_POSTID, Integer.toString(entry.getId().intValue())); 620 621 Vector postCategories = new Vector (1); 622 postCategories.add(Integer.toString(entry.getBlogCategoryId().intValue())); 623 entrystruct.put(MEMBER_CATEGORIES, postCategories); 624 625 recentPosts.add(entrystruct); 626 } 627 } 628 629 return recentPosts; 630 } catch (AuthorizationException e) { 631 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 632 } 633 } 634 635 646 public Object newMediaObject(String blogid, String userid, String password, Hashtable struct) throws Exception { 647 _logger.debug("newMediaObject() Called =[ SUPPORTED ]====="); 648 _logger.debug(" BlogId: " + blogid); 649 _logger.debug(" UserId: " + userid); 650 _logger.debug(" Password: *********"); 651 652 try { 653 _authorizationProvider.authorize(_blog, null, userid, password); 654 checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION); 655 656 String name = (String ) struct.get(MEMBER_NAME); 657 name = BlojsomUtils.getFilenameFromPath(name); 658 _logger.debug("newMediaObject name: " + name); 659 String type = (String ) struct.get(MEMBER_TYPE); 660 _logger.debug("newMediaObject type: " + type); 661 byte[] bits = (byte[]) struct.get(MEMBER_BITS); 662 663 File uploadDirectory = new File (_uploadDirectory); 664 if (!uploadDirectory.exists()) { 665 if (_logger.isErrorEnabled()) { 666 _logger.error("Upload directory does not exist: " + uploadDirectory.toString()); 667 } 668 669 throw new XmlRpcException(UNKNOWN_EXCEPTION, "Upload directory does not exist: " + uploadDirectory.toString()); 670 } 671 672 if (_acceptedMimeTypes.containsKey(type.toLowerCase())) { 673 try { 674 File uploadDirectoryForUser = new File (uploadDirectory, _blog.getBlogId()); 675 if (!uploadDirectoryForUser.exists()) { 676 if (!uploadDirectoryForUser.mkdir()) { 677 if (_logger.isErrorEnabled()) { 678 _logger.error("Could not create upload directory for user: " + uploadDirectoryForUser.toString()); 679 } 680 681 throw new XmlRpcException(UNKNOWN_EXCEPTION, "Could not create upload directory for user: " + _blog.getBlogId()); 682 } 683 } 684 685 BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (new File (uploadDirectoryForUser, name))); 686 bos.write(bits); 687 bos.close(); 688 689 Hashtable returnStruct = new Hashtable (1); 690 String mediaURL = _blog.getBlogBaseURL() + _staticURLPrefix + _blog.getBlogId() + "/" + name; 691 returnStruct.put(MEMBER_URL, mediaURL); 692 693 return returnStruct; 694 } catch (IOException e) { 695 if (_logger.isErrorEnabled()) { 696 _logger.error(e); 697 } 698 699 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 700 } 701 } else { 702 throw new XmlRpcException(UNKNOWN_EXCEPTION, "MIME type not accepted. Received MIME type: " + type); 703 } 704 } catch (BlojsomException e) { 705 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 706 } 707 } 708 709 721 public boolean setTemplate(String appkey, String blogid, String userid, String password, String template, String templateType) throws Exception { 722 _logger.debug("setTemplate() Called =====[ UNSUPPORTED ]====="); 723 _logger.debug(" Appkey: " + appkey); 724 _logger.debug(" BlogId: " + blogid); 725 _logger.debug(" UserId: " + userid); 726 _logger.debug(" Password: *********"); 727 _logger.debug(" Template: " + template); 728 _logger.debug(" Type: " + templateType); 729 730 throw new XmlRpcException(UNSUPPORTED_EXCEPTION, UNSUPPORTED_EXCEPTION_MSG); 731 } 732 733 744 public String getTemplate(String appkey, String blogid, String userid, String password, String templateType) throws Exception { 745 _logger.debug("getTemplate() Called =====[ UNSUPPORTED ]====="); 746 _logger.debug(" Appkey: " + appkey); 747 _logger.debug(" BlogId: " + blogid); 748 _logger.debug(" UserId: " + userid); 749 _logger.debug(" Password: *********"); 750 _logger.debug(" Type: " + templateType); 751 752 throw new XmlRpcException(UNSUPPORTED_EXCEPTION, UNSUPPORTED_EXCEPTION_MSG); 753 } 754 } 755 | Popular Tags |