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.Category; 39 import org.blojsom.blog.Entry; 40 import org.blojsom.fetcher.FetcherException; 41 import org.blojsom.plugin.admin.event.EntryAddedEvent; 42 import org.blojsom.plugin.admin.event.EntryDeletedEvent; 43 import org.blojsom.plugin.admin.event.EntryUpdatedEvent; 44 import org.blojsom.util.BlojsomConstants; 45 import org.blojsom.util.BlojsomUtils; 46 import org.blojsom.util.BlojsomMetaDataConstants; 47 48 import java.util.*; 49 50 57 public class BloggerAPIHandler extends APIHandler { 58 59 private Log _logger = LogFactory.getLog(BloggerAPIHandler.class); 60 61 64 private static final String MEMBER_URL = "url"; 65 66 69 private static final String MEMBER_BLOGID = "blogid"; 70 71 74 private static final String MEMBER_POSTID = "postid"; 75 76 79 private static final String MEMBER_BLOGNAME = "blogName"; 80 81 84 private static final String MEMBER_TITLE = "title"; 85 86 89 private static final String MEMBER_CONTENT = "content"; 90 91 94 private static final String MEMBER_DATECREATED = "dateCreated"; 95 96 99 private static final String MEMBER_AUTHORNAME = "authorName"; 100 101 104 private static final String MEMBER_AUTHOREMAIL = "authorEmail"; 105 106 109 private static final String MEMBER_NICKNAME = "nickname"; 110 111 114 private static final String MEMBER_USERID = "userid"; 115 116 119 private static final String MEMBER_EMAIL = "email"; 120 121 124 private static final String MEMBER_FIRSTNAME = "firstname"; 125 126 129 private static final String MEMBER_LASTNAME = "lastname"; 130 131 private static final String TITLE_TAG_START = "<title>"; 132 private static final String TITLE_TAG_END = "</title>"; 133 134 private static final String API_PREFIX = "blogger"; 135 136 private static final String BLOGGER_API_PERMISSION = "post_via_blogger_api_permission"; 137 138 141 public BloggerAPIHandler() { 142 } 143 144 149 public String getName() { 150 return API_PREFIX; 151 } 152 153 163 public Object getUserInfo(String appkey, String userid, String password) throws Exception { 164 _logger.debug("getUserInfo() Called =====[ SUPPORTED ]======="); 165 _logger.debug(" Appkey: " + appkey); 166 _logger.debug(" UserId: " + userid); 167 _logger.debug(" Password: *********"); 168 169 try { 170 _authorizationProvider.authorize(_blog, null, userid, password); 171 checkXMLRPCPermission(userid, BLOGGER_API_PERMISSION); 172 173 Hashtable userinfo = new Hashtable(); 174 userinfo.put(MEMBER_EMAIL, _blog.getBlogOwnerEmail()); 175 userinfo.put(MEMBER_NICKNAME, userid); 176 userinfo.put(MEMBER_USERID, "1"); 177 userinfo.put(MEMBER_URL, _blog.getBlogURL()); 178 179 String _ownerName = _blog.getBlogOwner(); 180 int _split = _ownerName.indexOf(" "); 181 if (_split > 0) { 182 userinfo.put(MEMBER_FIRSTNAME, _ownerName.substring(0, _split)); 183 userinfo.put(MEMBER_LASTNAME, _ownerName.substring(_split + 1)); 184 } else { 185 userinfo.put(MEMBER_FIRSTNAME, "blojsom"); 186 userinfo.put(MEMBER_LASTNAME, _ownerName); 187 } 188 189 return userinfo; 190 191 } catch (BlojsomException e) { 192 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 193 } 194 } 195 196 205 public Object getUsersBlogs(String appkey, String userid, String password) throws Exception { 206 _logger.debug("getUsersBlogs() Called ===[ SUPPORTED ]======="); 207 _logger.debug(" Appkey: " + appkey); 208 _logger.debug(" UserId: " + userid); 209 _logger.debug(" Password: *********"); 210 211 try { 212 _authorizationProvider.authorize(_blog, null, userid, password); 213 checkXMLRPCPermission(userid, BLOGGER_API_PERMISSION); 214 215 Vector result = new Vector(); 216 217 Category[] categories = _fetcher.loadAllCategories(_blog); 218 219 if (categories != null) { 220 for (int x = 0; x < categories.length; x++) { 221 Hashtable _bloglist = new Hashtable(3); 222 Category category = categories[x]; 223 224 String description; 225 if (!BlojsomUtils.checkNullOrBlank(category.getDescription())) { 226 description = category.getDescription(); 227 } else { 228 description = category.getName(); 229 } 230 231 _bloglist.put(MEMBER_URL, _blog.getBlogURL() + category.getName()); 232 _bloglist.put(MEMBER_BLOGID, Integer.toString(category.getId().intValue())); 233 _bloglist.put(MEMBER_BLOGNAME, description); 234 235 result.add(_bloglist); 236 } 237 } else { 238 throw new XmlRpcException(NOBLOGS_EXCEPTION, NOBLOGS_EXCEPTION_MSG); 239 } 240 241 return result; 242 } catch (BlojsomException e) { 243 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 244 } 245 } 246 247 253 private String findTitleInContent(String content) { 254 String titleFromContent = null; 255 int titleTagStartIndex = content.indexOf(TITLE_TAG_START); 256 257 if (titleTagStartIndex != -1) { 258 int titleTagEndIndex = content.indexOf(TITLE_TAG_END); 259 260 if (titleTagEndIndex != -1 && (titleTagEndIndex > titleTagStartIndex)) { 261 titleFromContent = content.substring(titleTagStartIndex + TITLE_TAG_START.length(), titleTagEndIndex); 262 } 263 } 264 265 return titleFromContent; 266 } 267 268 280 public String newPost(String appkey, String blogid, String userid, String password, String content, boolean publish) throws Exception { 281 _logger.debug("newPost() Called ===========[ SUPPORTED ]====="); 282 _logger.debug(" Appkey: " + appkey); 283 _logger.debug(" BlogId: " + blogid); 284 _logger.debug(" UserId: " + userid); 285 _logger.debug(" Password: *********"); 286 _logger.debug(" Publish: " + publish); 287 _logger.debug(" Content:\n " + content); 288 289 try { 290 _authorizationProvider.authorize(_blog, null, userid, password); 291 checkXMLRPCPermission(userid, BLOGGER_API_PERMISSION); 292 293 String result; 294 String title = findTitleInContent(content); 295 296 Integer blogID; 297 try { 298 blogID = Integer.valueOf(blogid); 299 } catch (NumberFormatException e) { 300 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 301 } 302 303 try { 304 Category category = _fetcher.loadCategory(_blog, blogID); 305 Entry entry = _fetcher.newEntry(); 306 307 if (title != null) { 308 content = BlojsomUtils.replace(content, TITLE_TAG_START + title + TITLE_TAG_END, ""); 309 entry.setTitle(title); 310 } 311 312 entry.setBlogId(_blog.getId()); 313 entry.setBlogCategoryId(category.getId()); 314 entry.setDate(new Date()); 315 entry.setModifiedDate(entry.getDate()); 316 entry.setTitle(title); 317 entry.setDescription(content); 318 entry.setAuthor(userid); 319 320 if (publish) { 321 entry.setStatus(BlojsomMetaDataConstants.PUBLISHED_STATUS); 322 } else { 323 entry.setStatus(BlojsomMetaDataConstants.DRAFT_STATUS); 324 } 325 326 _fetcher.saveEntry(_blog, entry); 327 328 result = Integer.toString(entry.getId().intValue()); 329 330 _eventBroadcaster.broadcastEvent(new EntryAddedEvent(this, new Date(), entry, _blog)); 332 } catch (FetcherException e) { 333 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 334 } 335 336 return result; 337 } catch (BlojsomException e) { 338 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 339 } 340 } 341 342 355 public boolean editPost(String appkey, String postid, String userid, String password, String content, boolean publish) throws Exception { 356 _logger.debug("editPost() Called ========[ SUPPORTED ]====="); 357 _logger.debug(" Appkey: " + appkey); 358 _logger.debug(" PostId: " + postid); 359 _logger.debug(" UserId: " + userid); 360 _logger.debug(" Password: *********"); 361 _logger.debug(" Publish: " + publish); 362 _logger.debug(" Content:\n " + content); 363 364 try { 365 _authorizationProvider.authorize(_blog, null, userid, password); 366 checkXMLRPCPermission(userid, BLOGGER_API_PERMISSION); 367 368 boolean result; 369 370 Integer postID; 371 try { 372 postID = Integer.valueOf(postid); 373 } catch (NumberFormatException e) { 374 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 375 } 376 377 try { 378 Entry entryToEdit = _fetcher.loadEntry(_blog, postID); 379 380 String title = findTitleInContent(content); 381 if (title != null) { 382 content = BlojsomUtils.replace(content, TITLE_TAG_START + title + TITLE_TAG_END, ""); 383 entryToEdit.setTitle(title); 384 } else { 385 entryToEdit.setTitle(""); 386 } 387 entryToEdit.setDescription(content); 388 entryToEdit.setModifiedDate(new Date()); 389 390 if (publish) { 391 entryToEdit.setStatus(BlojsomMetaDataConstants.PUBLISHED_STATUS); 392 } else { 393 entryToEdit.setStatus(BlojsomMetaDataConstants.DRAFT_STATUS); 394 } 395 396 _fetcher.saveEntry(_blog, entryToEdit); 397 398 result = true; 399 400 _eventBroadcaster.broadcastEvent(new EntryUpdatedEvent(this, new Date(), entryToEdit, _blog)); 401 } catch (FetcherException e) { 402 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 403 } 404 405 return result; 406 } catch (AuthorizationException e) { 407 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 408 } 409 } 410 411 421 public Object getPost(String appkey, String postid, String userid, String password) throws Exception { 422 _logger.debug("getPost() Called ===========[ SUPPORTED ]====="); 423 _logger.debug(" Appkey: " + appkey); 424 _logger.debug(" PostId: " + postid); 425 _logger.debug(" UserId: " + userid); 426 _logger.debug(" Password: *********"); 427 428 try { 429 _authorizationProvider.authorize(_blog, null, userid, password); 430 checkXMLRPCPermission(userid, BLOGGER_API_PERMISSION); 431 432 Integer postID; 433 try { 434 postID = Integer.valueOf(postid); 435 } catch (NumberFormatException e) { 436 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 437 } 438 439 try { 440 Entry entry = _fetcher.loadEntry(_blog, postID); 441 442 Hashtable entrystruct = new Hashtable(); 443 entrystruct.put(MEMBER_POSTID, Integer.toString(entry.getId().intValue())); 444 entrystruct.put(MEMBER_BLOGID, Integer.toString(entry.getBlogCategory().getId().intValue())); 445 entrystruct.put(MEMBER_TITLE, entry.getTitle()); 446 entrystruct.put(MEMBER_URL, _blog.getBlogURL() + entry.getCategory() + entry.getPostSlug()); 447 entrystruct.put(MEMBER_CONTENT, entry.getTitle() + BlojsomConstants.LINE_SEPARATOR + entry.getDescription()); 448 entrystruct.put(MEMBER_DATECREATED, entry.getDate()); 449 entrystruct.put(MEMBER_AUTHORNAME, _blog.getBlogOwner()); 450 entrystruct.put(MEMBER_AUTHOREMAIL, _blog.getBlogOwnerEmail()); 451 452 return entrystruct; 453 } catch (FetcherException e) { 454 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 455 } 456 } catch (AuthorizationException e) { 457 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 458 } 459 } 460 461 472 public boolean deletePost(String appkey, String postid, String userid, String password, boolean publish) throws Exception { 473 _logger.debug("deletePost() Called =====[ SUPPORTED ]====="); 474 _logger.debug(" Appkey: " + appkey); 475 _logger.debug(" PostId: " + postid); 476 _logger.debug(" UserId: " + userid); 477 _logger.debug(" Password: *********"); 478 479 boolean result; 480 481 try { 482 _authorizationProvider.authorize(_blog, null, userid, password); 483 checkXMLRPCPermission(userid, BLOGGER_API_PERMISSION); 484 485 Integer postID; 486 try { 487 postID = Integer.valueOf(postid); 488 } catch (NumberFormatException e) { 489 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 490 } 491 492 try { 493 Entry entryToDelete = _fetcher.loadEntry(_blog, postID); 494 _fetcher.deleteEntry(_blog, entryToDelete); 495 496 result = true; 497 _eventBroadcaster.broadcastEvent(new EntryDeletedEvent(this, new Date(), entryToDelete, _blog)); 498 } catch (FetcherException e) { 499 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 500 } 501 } catch (AuthorizationException e) { 502 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 503 } 504 505 return result; 506 } 507 508 519 public Object getRecentPosts(String appkey, String blogid, String userid, String password, int numposts) throws Exception { 520 _logger.debug("getRecentPosts() Called ===========[ SUPPORTED ]====="); 521 _logger.debug(" Appkey: " + appkey); 522 _logger.debug(" BlogId: " + blogid); 523 _logger.debug(" UserId: " + userid); 524 _logger.debug(" Password: *********"); 525 _logger.debug(" Numposts: " + numposts); 526 527 Vector recentPosts = new Vector(); 528 529 try { 530 _authorizationProvider.authorize(_blog, null, userid, password); 531 checkXMLRPCPermission(userid, BLOGGER_API_PERMISSION); 532 533 Entry[] entries; 534 try { 535 entries = _fetcher.loadEntriesForCategory(_blog, Integer.valueOf(blogid), new Integer (numposts)); 536 } catch (FetcherException e) { 537 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 538 } catch (NumberFormatException e) { 539 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 540 } 541 542 if (entries != null && entries.length > 0) { 543 for (int x = 0; x < entries.length; x++) { 544 Entry entry = entries[x]; 545 Hashtable entrystruct = new Hashtable(); 546 entrystruct.put(MEMBER_POSTID, Integer.toString(entry.getId().intValue())); 547 entrystruct.put(MEMBER_BLOGID, Integer.toString(entry.getBlogCategory().getId().intValue())); 548 entrystruct.put(MEMBER_TITLE, entry.getTitle()); 549 entrystruct.put(MEMBER_URL, _blog.getBlogURL() + entry.getCategory() + entry.getPostSlug()); 550 entrystruct.put(MEMBER_CONTENT, entry.getTitle() + BlojsomConstants.LINE_SEPARATOR + entry.getDescription()); 551 entrystruct.put(MEMBER_DATECREATED, entry.getDate()); 552 entrystruct.put(MEMBER_AUTHORNAME, _blog.getBlogOwner()); 553 entrystruct.put(MEMBER_AUTHOREMAIL, _blog.getBlogOwnerEmail()); 554 recentPosts.add(entrystruct); 555 } 556 } 557 558 return recentPosts; 559 } catch (AuthorizationException e) { 560 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 561 } 562 } 563 564 576 public boolean setTemplate(String appkey, String blogid, String userid, String password, String template, String templateType) throws Exception { 577 _logger.debug("setTemplate() Called =====[ UNSUPPORTED ]====="); 578 _logger.debug(" Appkey: " + appkey); 579 _logger.debug(" BlogId: " + blogid); 580 _logger.debug(" UserId: " + userid); 581 _logger.debug(" Password: *********"); 582 _logger.debug(" Template: " + template); 583 _logger.debug(" Type: " + templateType); 584 585 throw new XmlRpcException(UNSUPPORTED_EXCEPTION, UNSUPPORTED_EXCEPTION_MSG); 586 } 587 588 599 public String getTemplate(String appkey, String blogid, String userid, String password, String templateType) throws Exception { 600 _logger.debug("getTemplate() Called =====[ UNSUPPORTED ]====="); 601 _logger.debug(" Appkey: " + appkey); 602 _logger.debug(" BlogId: " + blogid); 603 _logger.debug(" UserId: " + userid); 604 _logger.debug(" Password: *********"); 605 _logger.debug(" Type: " + templateType); 606 607 throw new XmlRpcException(UNSUPPORTED_EXCEPTION, UNSUPPORTED_EXCEPTION_MSG); 608 } 609 } 610 | Popular Tags |