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.authorization.AuthorizationException; 37 import org.blojsom.blog.Category; 38 import org.blojsom.blog.Entry; 39 import org.blojsom.blog.Trackback; 40 import org.blojsom.fetcher.FetcherException; 41 import org.blojsom.plugin.trackback.TrackbackPlugin; 42 import org.blojsom.util.BlojsomUtils; 43 44 import java.util.Hashtable ; 45 import java.util.Vector ; 46 47 54 public class MovableTypeAPIHandler extends APIHandler { 55 56 private Log _logger = LogFactory.getLog(MovableTypeAPIHandler.class); 57 58 private static final String MEMBER_DATECREATED = "dateCreated"; 59 private static final String MEMBER_USERID = "userid"; 60 private static final String MEMBER_POSTID = "postid"; 61 private static final String MEMBER_TITLE = "title"; 62 private static final String MEMBER_CATEGORYID = "categoryId"; 63 private static final String MEMBER_CATEGORYNAME = "categoryName"; 64 private static final String MEMBER_ISPRIMARY = "isPrimary"; 65 private static final String MEMBER_KEY = "key"; 66 private static final String MEMBER_LABEL = "label"; 67 private static final String MEMBER_PING_TITLE = "pingTitle"; 68 private static final String MEMBER_PING_URL = "pingURL"; 69 private static final String MEMBER_PING_IP = "pingIP"; 70 71 private static final String API_PREFIX = "mt"; 72 73 private static final String MOVABLETYPE_API_PERMISSION = "post_via_movabletype_api_permission"; 74 75 78 public MovableTypeAPIHandler() { 79 } 80 81 86 public String getName() { 87 return API_PREFIX; 88 } 89 90 100 public Object getRecentPostTitles(String blogID, String username, String password, int numberOfPosts) throws Exception { 101 _logger.debug("getRecentPostTitles() Called ===========[ SUPPORTED ]====="); 102 _logger.debug(" BlogId: " + blogID); 103 _logger.debug(" UserId: " + username); 104 _logger.debug(" Password: *********"); 105 _logger.debug(" Numposts: " + numberOfPosts); 106 107 Vector recentPosts = new Vector (); 108 109 try { 110 _authorizationProvider.authorize(_blog, null, username, password); 111 checkXMLRPCPermission(username, MOVABLETYPE_API_PERMISSION); 112 113 Entry[] entries; 114 try { 115 entries = _fetcher.loadEntriesForCategory(_blog, Integer.valueOf(blogID), new Integer (numberOfPosts)); 116 } catch (FetcherException e) { 117 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 118 } catch (NumberFormatException e) { 119 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG); 120 } 121 122 if (entries != null && entries.length > 0) { 123 for (int x = 0; x < entries.length; x++) { 124 Entry entry = entries[x]; 125 Hashtable entrystruct = new Hashtable (); 126 127 entrystruct.put(MEMBER_TITLE, entry.getTitle()); 128 entrystruct.put(MEMBER_USERID, entry.getAuthor()); 129 entrystruct.put(MEMBER_DATECREATED, entry.getDate()); 130 entrystruct.put(MEMBER_POSTID, Integer.toString(entry.getId().intValue())); 131 132 recentPosts.add(entrystruct); 133 } 134 } 135 136 return recentPosts; 137 } catch (AuthorizationException e) { 138 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 139 } 140 } 141 142 151 public Object getCategoryList(String blogID, String username, String password) throws Exception { 152 _logger.debug("getCategories() Called =====[ SUPPORTED ]====="); 153 _logger.debug(" BlogId: " + blogID); 154 _logger.debug(" UserId: " + username); 155 _logger.debug(" Password: *********"); 156 157 try { 158 _authorizationProvider.authorize(_blog, null, username, password); 159 checkXMLRPCPermission(username, MOVABLETYPE_API_PERMISSION); 160 161 Vector result; 162 163 Category[] categories = _fetcher.loadAllCategories(_blog); 164 165 if (categories != null) { 166 result = new Vector (categories.length); 167 168 for (int x = 0; x < categories.length; x++) { 169 Hashtable catlist = new Hashtable (3); 170 Category category = categories[x]; 171 172 String description; 173 if (!BlojsomUtils.checkNullOrBlank(category.getDescription())) { 174 description = category.getDescription(); 175 } else { 176 description = category.getName(); 177 } 178 179 catlist.put(MEMBER_CATEGORYID, Integer.toString(category.getId().intValue())); 180 catlist.put(MEMBER_CATEGORYNAME, description); 181 182 result.add(catlist); 183 } 184 } else { 185 throw new XmlRpcException(NOBLOGS_EXCEPTION, NOBLOGS_EXCEPTION_MSG); 186 } 187 188 return result; 189 } catch (AuthorizationException e) { 190 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 191 } 192 } 193 194 203 public Object getPostCategories(String postID, String username, String password) throws Exception { 204 _logger.debug("getPost() Called =========[ SUPPORTED ]====="); 205 _logger.debug(" PostId: " + postID); 206 _logger.debug(" UserId: " + username); 207 _logger.debug(" Password: *********"); 208 209 Vector result = new Vector (); 210 211 try { 212 _authorizationProvider.authorize(_blog, null, username, password); 213 checkXMLRPCPermission(username, MOVABLETYPE_API_PERMISSION); 214 215 Integer postIDForEntry; 216 try { 217 postIDForEntry = Integer.valueOf(postID); 218 } catch (NumberFormatException e) { 219 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 220 } 221 222 try { 223 Entry entry = _fetcher.loadEntry(_blog, postIDForEntry); 224 225 Hashtable categoryContent = new Hashtable (); 226 227 String description; 228 if (!BlojsomUtils.checkNullOrBlank(entry.getBlogCategory().getDescription())) { 229 description = entry.getBlogCategory().getDescription(); 230 } else { 231 description = entry.getBlogCategory().getName(); 232 } 233 234 categoryContent.put(MEMBER_CATEGORYID, entry.getBlogCategoryId()); 235 categoryContent.put(MEMBER_CATEGORYNAME, description); 236 categoryContent.put(MEMBER_ISPRIMARY, Boolean.TRUE); 237 238 result.add(categoryContent); 239 } catch (FetcherException e) { 240 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 241 } 242 243 return result; 244 } catch (AuthorizationException e) { 245 throw new XmlRpcException(AUTHORIZATION_EXCEPTION, AUTHORIZATION_EXCEPTION_MSG); 246 } 247 } 248 249 259 public boolean setPostCategories(String postID, String username, String password, Vector categories) throws Exception { 260 throw new XmlRpcException(UNSUPPORTED_EXCEPTION, UNSUPPORTED_EXCEPTION_MSG); 261 } 262 263 269 public Object supportedMethods() throws Exception { 270 Vector result = new Vector (); 271 272 result.add("blogger.newPost"); 273 result.add("blogger.editPost"); 274 result.add("blogger.getPost"); 275 result.add("blogger.deletePost"); 276 result.add("blogger.getRecentPosts"); 277 result.add("blogger.getUsersBlogs"); 278 result.add("blogger.getUserInfo"); 279 result.add("metaWeblog.getUsersBlogs"); 280 result.add("metaWeblog.getCategories"); 281 result.add("metaWeblog.newPost"); 282 result.add("metaWeblog.editPost"); 283 result.add("metaWeblog.getPost"); 284 result.add("metaWeblog.deletePost"); 285 result.add("metaWeblog.getRecentPosts"); 286 result.add("metaWeblog.newMediaObject"); 287 result.add("mt.getRecentPostTitles"); 288 result.add("mt.getCategoryList"); 289 result.add("mt.getPostCategories"); 290 result.add("mt.supportedMethods"); 291 result.add("mt.supportedTextFilters"); 292 result.add("mt.getTrackbackPings"); 293 294 return result; 295 } 296 297 305 public Object supportedTextFilters() throws Exception { 306 return new Vector (); 308 } 309 310 319 public Object getTrackbackPings(String postID) throws Exception { 320 _logger.debug("getTrackbackPings() Called =========[ SUPPORTED ]====="); 321 _logger.debug(" PostId: " + postID); 322 323 Vector trackbackPings = new Vector (); 324 325 Integer postIDForEntry; 326 try { 327 postIDForEntry = Integer.valueOf(postID); 328 } catch (NumberFormatException e) { 329 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 330 } 331 332 try { 333 Entry entry = _fetcher.loadEntry(_blog, postIDForEntry); 334 Trackback[] trackbacks = entry.getTrackbacksAsArray(); 335 336 for (int i = 0; i < trackbacks.length; i++) { 337 Hashtable trackbackInformation = new Hashtable (3); 338 339 trackbackInformation.put(MEMBER_PING_TITLE, trackbacks[i].getTitle()); 340 trackbackInformation.put(MEMBER_PING_URL, trackbacks[i].getUrl()); 341 if (BlojsomUtils.checkMapForKey(trackbacks[i].getMetaData(), TrackbackPlugin.BLOJSOM_TRACKBACK_PLUGIN_METADATA_IP)) 342 { 343 trackbackInformation.put(MEMBER_PING_IP, trackbacks[i].getMetaData().get(TrackbackPlugin.BLOJSOM_TRACKBACK_PLUGIN_METADATA_IP)); 344 } else { 345 trackbackInformation.put(MEMBER_PING_IP, ""); 346 } 347 348 trackbackPings.add(trackbackInformation); 349 } 350 351 return trackbackPings; 352 } catch (FetcherException e) { 353 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG); 354 } 355 } 356 357 366 public boolean publishPost(String postID, String username, String password) throws Exception { 367 throw new XmlRpcException(UNSUPPORTED_EXCEPTION, UNSUPPORTED_EXCEPTION_MSG); 368 } 369 370 } 371 | Popular Tags |