KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > extension > xmlrpc > handler > MovableTypeAPIHandler


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

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 JavaDoc;
45 import java.util.Vector JavaDoc;
46
47 /**
48  * MovableType API handler
49  *
50  * @author David Czarnecki
51  * @since blojsom 3.0
52  * @version $Id: MovableTypeAPIHandler.java,v 1.2 2006/04/27 20:03:00 czarneckid Exp $
53  */

54 public class MovableTypeAPIHandler extends APIHandler {
55
56     private Log _logger = LogFactory.getLog(MovableTypeAPIHandler.class);
57
58     private static final String JavaDoc MEMBER_DATECREATED = "dateCreated";
59     private static final String JavaDoc MEMBER_USERID = "userid";
60     private static final String JavaDoc MEMBER_POSTID = "postid";
61     private static final String JavaDoc MEMBER_TITLE = "title";
62     private static final String JavaDoc MEMBER_CATEGORYID = "categoryId";
63     private static final String JavaDoc MEMBER_CATEGORYNAME = "categoryName";
64     private static final String JavaDoc MEMBER_ISPRIMARY = "isPrimary";
65     private static final String JavaDoc MEMBER_KEY = "key";
66     private static final String JavaDoc MEMBER_LABEL = "label";
67     private static final String JavaDoc MEMBER_PING_TITLE = "pingTitle";
68     private static final String JavaDoc MEMBER_PING_URL = "pingURL";
69     private static final String JavaDoc MEMBER_PING_IP = "pingIP";
70
71     private static final String JavaDoc API_PREFIX = "mt";
72
73     private static final String JavaDoc MOVABLETYPE_API_PERMISSION = "post_via_movabletype_api_permission";
74
75     /**
76      * Construct a new <a HREF="http://www.movabletype.org/docs/mtmanual_programmatic.html">MovableType API</a> handler
77      */

78     public MovableTypeAPIHandler() {
79     }
80
81     /**
82      * Gets the name of API Handler. Used to bind to XML-RPC
83      *
84      * @return The API Name (ie: blogger)
85      */

86     public String JavaDoc getName() {
87         return API_PREFIX;
88     }
89
90     /**
91      * Returns a bandwidth-friendly list of the most recent posts in the system.
92      *
93      * @param blogID Blog ID
94      * @param username Username
95      * @param password Password
96      * @param numberOfPosts Number of titles to retrieve
97      * @return Bandwidth-friendly list of the most recent posts in the system
98      * @throws Exception If there is an error retrieving post titles
99      */

100     public Object JavaDoc getRecentPostTitles(String JavaDoc blogID, String JavaDoc username, String JavaDoc password, int numberOfPosts) throws Exception JavaDoc {
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 JavaDoc recentPosts = new Vector JavaDoc();
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 JavaDoc(numberOfPosts));
116             } catch (FetcherException e) {
117                 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG);
118             } catch (NumberFormatException JavaDoc 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 JavaDoc entrystruct = new Hashtable JavaDoc();
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     /**
143      * Returns a list of all categories defined in the weblog.
144      *
145      * @param blogID Blog ID
146      * @param username Username
147      * @param password Password
148      * @return List of all categories defined in the weblog
149      * @throws Exception If there is an error getting the category list
150      */

151     public Object JavaDoc getCategoryList(String JavaDoc blogID, String JavaDoc username, String JavaDoc password) throws Exception JavaDoc {
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 JavaDoc result;
162
163             Category[] categories = _fetcher.loadAllCategories(_blog);
164
165             if (categories != null) {
166                 result = new Vector JavaDoc(categories.length);
167
168                 for (int x = 0; x < categories.length; x++) {
169                     Hashtable JavaDoc catlist = new Hashtable JavaDoc(3);
170                     Category category = categories[x];
171
172                     String JavaDoc 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     /**
195      * Returns a list of all categories to which the post is assigned. Since we only support
196      * single categories at the moment, just return a single structure.
197      *
198      * @param postID Post ID
199      * @param username Username
200      * @param password Password
201      * @return An array of structs containing String categoryName, String categoryId, and boolean isPrimary
202      */

203     public Object JavaDoc getPostCategories(String JavaDoc postID, String JavaDoc username, String JavaDoc password) throws Exception JavaDoc {
204         _logger.debug("getPost() Called =========[ SUPPORTED ]=====");
205         _logger.debug(" PostId: " + postID);
206         _logger.debug(" UserId: " + username);
207         _logger.debug(" Password: *********");
208
209         Vector JavaDoc result = new Vector JavaDoc();
210
211         try {
212             _authorizationProvider.authorize(_blog, null, username, password);
213             checkXMLRPCPermission(username, MOVABLETYPE_API_PERMISSION);
214
215             Integer JavaDoc postIDForEntry;
216             try {
217                 postIDForEntry = Integer.valueOf(postID);
218             } catch (NumberFormatException JavaDoc e) {
219                 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG);
220             }
221
222             try {
223                 Entry entry = _fetcher.loadEntry(_blog, postIDForEntry);
224
225                 Hashtable JavaDoc categoryContent = new Hashtable JavaDoc();
226
227                 String JavaDoc 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     /**
250      * Sets the categories for a post.
251      *
252      * @param postID Post ID
253      * @param username Username
254      * @param password Password
255      * @param categories Array of structs containing String categoryId and boolean isPrimary
256      * @return <code>true</code> if categories set for a post
257      * @throws Exception If there is an error setting the categories for a post
258      */

259     public boolean setPostCategories(String JavaDoc postID, String JavaDoc username, String JavaDoc password, Vector JavaDoc categories) throws Exception JavaDoc {
260         throw new XmlRpcException(UNSUPPORTED_EXCEPTION, UNSUPPORTED_EXCEPTION_MSG);
261     }
262
263     /**
264      * Retrieve information about the XML-RPC methods supported by the server.
265      *
266      * @return Array of method names supported by the server
267      * @throws Exception If there is an error retrieving the list of supported XML-RPC methods.
268      */

269     public Object JavaDoc supportedMethods() throws Exception JavaDoc {
270         Vector JavaDoc result = new Vector JavaDoc();
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     /**
298      * Retrieve information about the text formatting plugins supported by the server.
299      *
300      * @return An array of structs containing String key and String label. key is the
301      * unique string identifying a text formatting plugin, and label is the readable
302      * description to be displayed to a user
303      * @throws Exception If there is an error retrieving the list of plugins
304      */

305     public Object JavaDoc supportedTextFilters() throws Exception JavaDoc {
306         // Return an empty list as we need to figure out a way to determine supported formatting plugins
307
return new Vector JavaDoc();
308     }
309
310     /**
311      * Retrieve the list of TrackBack pings posted to a particular entry
312      *
313      * @param postID Post ID
314      * @return An array of structs containing String pingTitle (the title of the entry sent
315      * in the ping), String pingURL (the URL of the entry), and String pingIP (the IP address
316      * of the host that sent the ping)
317      * @throws Exception If there is an error retrieving trackbacks for an entry
318      */

319     public Object JavaDoc getTrackbackPings(String JavaDoc postID) throws Exception JavaDoc {
320         _logger.debug("getTrackbackPings() Called =========[ SUPPORTED ]=====");
321         _logger.debug(" PostId: " + postID);
322
323         Vector JavaDoc trackbackPings = new Vector JavaDoc();
324
325         Integer JavaDoc postIDForEntry;
326         try {
327             postIDForEntry = Integer.valueOf(postID);
328         } catch (NumberFormatException JavaDoc 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 JavaDoc trackbackInformation = new Hashtable JavaDoc(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     /**
358      * Publish (rebuild) all of the static files related to an entry from your weblog. Equivalent to saving an entry in the system (but without the ping)
359      *
360      * @param postID Post ID
361      * @param username Username
362      * @param password Password
363      * @return <code>true</code> if post published
364      * @throws Exception If there is an error publishing the post
365      */

366     public boolean publishPost(String JavaDoc postID, String JavaDoc username, String JavaDoc password) throws Exception JavaDoc {
367         throw new XmlRpcException(UNSUPPORTED_EXCEPTION, UNSUPPORTED_EXCEPTION_MSG);
368     }
369
370 }
371
Popular Tags