KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
50 import java.io.File JavaDoc;
51 import java.io.FileOutputStream JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.util.Date JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.Hashtable JavaDoc;
56 import java.util.Vector JavaDoc;
57
58 /**
59  * MetaWeblogAPIHandler
60  *
61  * @author David Czarnecki
62  * @since blojsom 3.0
63  * @version $Id: MetaWeblogAPIHandler.java,v 1.5 2006/09/26 02:55:20 czarneckid Exp $
64  */

65 public class MetaWeblogAPIHandler extends APIHandler {
66
67     private Log _logger = LogFactory.getLog(MetaWeblogAPIHandler.class);
68
69     private static final String JavaDoc METAWEBLOG_ACCEPTED_TYPES_IP = "blojsom-extension-metaweblog-accepted-types";
70
71     /**
72      * Blogger API "blogid" key
73      */

74     private static final String JavaDoc MEMBER_BLOGID = "blogid";
75
76     /**
77      * Blogger API "blogName" key
78      */

79     private static final String JavaDoc MEMBER_BLOGNAME = "blogName";
80
81     /**
82      * MetaWeblog API "description" key
83      */

84     private static final String JavaDoc MEMBER_DESCRIPTION = "description";
85
86     /**
87      * MetaWeblog API "htmlUrl" key
88      */

89     private static final String JavaDoc MEMBER_HTML_URL = "htmlUrl";
90
91     /**
92      * MetaWeblog API "rssUrl" key
93      */

94     private static final String JavaDoc MEMBER_RSS_URL = "rssUrl";
95
96     /**
97      * MetaWeblog API "title" key
98      */

99     private static final String JavaDoc MEMBER_TITLE = "title";
100
101     /**
102      * MetaWeblog API "link" key
103      */

104     private static final String JavaDoc MEMBER_LINK = "link";
105
106     /**
107      * MetaWeblog API "name" key
108      */

109     private static final String JavaDoc MEMBER_NAME = "name";
110
111     /**
112      * MetaWeblog API "type" key
113      */

114     private static final String JavaDoc MEMBER_TYPE = "type";
115
116     /**
117      * MetaWeblog API "bits" key
118      */

119     private static final String JavaDoc MEMBER_BITS = "bits";
120
121     /**
122      * MetaWeblog API "permaLink" key
123      */

124     private static final String JavaDoc MEMBER_PERMALINK = "permaLink";
125
126     /**
127      * MetaWeblog API "dateCreated" key
128      */

129     private static final String JavaDoc MEMBER_DATE_CREATED = "dateCreated";
130
131     /**
132      * MetaWeblog API "categories" key
133      */

134     private static final String JavaDoc MEMBER_CATEGORIES = "categories";
135
136     /**
137      * MetaWeblog API "postid" key
138      */

139     private static final String JavaDoc MEMBER_POSTID = "postid";
140
141     /**
142      * MetaWeblog API "url" key
143      */

144     private static final String JavaDoc MEMBER_URL = "url";
145
146     private static final String JavaDoc METAWEBLOG_API_PERMISSION = "post_via_metaweblog_api_permission";
147
148     private static final String JavaDoc API_PREFIX = "metaWeblog";
149
150     private String JavaDoc _uploadDirectory;
151     private HashMap JavaDoc _acceptedMimeTypes;
152     private String JavaDoc _staticURLPrefix;
153
154     /**
155      * Create a new instance of the MetaWeblog API handler
156      */

157     public MetaWeblogAPIHandler() {
158     }
159
160     /**
161      * Retrieve the API handler name
162      *
163      * @return API handler name "metaWeblog"
164      */

165     public String JavaDoc getName() {
166         return API_PREFIX;
167     }
168
169     /**
170      * Set the {@link Blog}
171      *
172      * @param blog {@link Blog}
173      */

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 JavaDoc(3);
189         String JavaDoc acceptedMimeTypes = _blog.getProperty(METAWEBLOG_ACCEPTED_TYPES_IP);
190         if (acceptedMimeTypes != null && !"".equals(acceptedMimeTypes)) {
191             String JavaDoc[] types = BlojsomUtils.parseCommaList(acceptedMimeTypes);
192             for (int i = 0; i < types.length; i++) {
193                 String JavaDoc 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     /**
206      * Returns information on all the blogs a given user is a member of
207      *
208      * @param appkey Unique identifier/passcode of the application sending the post
209      * @param userid Login for a Blogger user who has permission to post to the blog
210      * @param password Password for said username
211      * @return Blog category list
212      * @throws XmlRpcException If there are no categories or the user was not authenticated correctly
213      */

214     public Object JavaDoc getUsersBlogs(String JavaDoc appkey, String JavaDoc userid, String JavaDoc password) throws Exception JavaDoc {
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 JavaDoc result = new Vector JavaDoc();
225
226             Category[] categories = _fetcher.loadAllCategories(_blog);
227
228             if (categories != null) {
229                 for (int x = 0; x < categories.length; x++) {
230                     Hashtable JavaDoc _bloglist = new Hashtable JavaDoc(3);
231                     Category category = categories[x];
232
233                     String JavaDoc 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     /**
257      * Authenticates a user and returns the categories available in the blojsom
258      *
259      * @param blogid Dummy Value for Blojsom
260      * @param userid Login for a MetaWeblog user who has permission to post to the blog
261      * @param password Password for said username
262      * @return Blog category list
263      * @throws XmlRpcException If there are no categories or the user was not authenticated correctly
264      */

265     public Object JavaDoc getCategories(String JavaDoc blogid, String JavaDoc userid, String JavaDoc password) throws Exception JavaDoc {
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 JavaDoc result;
276
277             Category[] categories = _fetcher.loadAllCategories(_blog);
278
279             if (categories != null) {
280                 result = new Hashtable JavaDoc(categories.length);
281
282                 for (int x = 0; x < categories.length; x++) {
283                     Hashtable JavaDoc catlist = new Hashtable JavaDoc(3);
284                     Category category = categories[x];
285
286                     String JavaDoc 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     /**
310      * Makes a new post to a designated blog. Optionally, will publish the blog after making the post
311      *
312      * @param blogid Unique identifier of the blog the post will be added to
313      * @param userid Login for a MetaWeblog user who has permission to post to the blog
314      * @param password Password for said username
315      * @param struct Contents of the post
316      * @param publish If true, the blog will be published immediately after the post is made
317      * @return Post ID of the added entry
318      * @throws XmlRpcException If the user was not authenticated correctly or if there was an I/O exception
319      */

320     public String JavaDoc newPost(String JavaDoc blogid, String JavaDoc userid, String JavaDoc password, Hashtable JavaDoc struct, boolean publish) throws Exception JavaDoc {
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 JavaDoc categories = (Vector JavaDoc) struct.get(MEMBER_CATEGORIES);
329             if (categories.size() > 0) {
330                 blogid = (String JavaDoc) categories.get(0);
331             }
332         }
333
334         try {
335             _authorizationProvider.authorize(_blog, null, userid, password);
336             checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION);
337
338             Integer JavaDoc blogID;
339             try {
340                 blogID = Integer.valueOf(blogid);
341             } catch (NumberFormatException JavaDoc e) {
342                 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG);
343             }
344
345             String JavaDoc result;
346
347             Hashtable JavaDoc postcontent;
348             postcontent = struct;
349
350             String JavaDoc title = (String JavaDoc) postcontent.get(MEMBER_TITLE);
351             String JavaDoc description = (String JavaDoc) postcontent.get(MEMBER_DESCRIPTION);
352             Date JavaDoc dateCreated = (Date JavaDoc) 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 JavaDoc());
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                 // Send out an add blog entry event
381
_eventBroadcaster.broadcastEvent(new EntryAddedEvent(this, new Date JavaDoc(), 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     /**
397      * Edits a given post. Optionally, will publish the blog after making the edit
398      *
399      * @param postid Unique identifier of the post to be changed
400      * @param userid Login for a MetaWeblog user who has permission to post to the blog
401      * @param password Password for said username
402      * @param struct Contents of the post
403      * @param publish If true, the blog will be published immediately after the post is made
404      * @return <code>true</code> if the entry was edited, <code>false</code> otherwise
405      * @throws XmlRpcException If the user was not authenticated correctly, if there was an I/O exception,
406      * or if the entry permalink ID is invalid
407      */

408     public boolean editPost(String JavaDoc postid, String JavaDoc userid, String JavaDoc password, Hashtable JavaDoc struct, boolean publish) throws Exception JavaDoc {
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 JavaDoc postID;
422             try {
423                 postID = Integer.valueOf(BlojsomUtils.removeSlashes(postid));
424             } catch (NumberFormatException JavaDoc e) {
425                 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG);
426             }
427
428             try {
429                 Entry entryToEdit = _fetcher.loadEntry(_blog, postID);
430
431                 Hashtable JavaDoc postcontent;
432                 postcontent = struct;
433
434                 String JavaDoc title = (String JavaDoc) postcontent.get(MEMBER_TITLE);
435                 String JavaDoc description = (String JavaDoc) postcontent.get(MEMBER_DESCRIPTION);
436                 Date JavaDoc dateCreated = (Date JavaDoc) 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 JavaDoc());
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 JavaDoc(), 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     /**
473      * Retrieves a given post from the blog
474      *
475      * @param postid Unique identifier of the post to be changed
476      * @param userid Login for a MetaWeblog user who has permission to post to the blog
477      * @param password Password for said username
478      * @return Structure containing the minimal attributes for the MetaWeblog API getPost() method: title, link, and description
479      * @throws XmlRpcException If the user was not authenticated correctly, if there was an I/O exception,
480      * or if the entry permalink ID is invalid
481      */

482     public Object JavaDoc getPost(String JavaDoc postid, String JavaDoc userid, String JavaDoc password) throws Exception JavaDoc {
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 JavaDoc postID;
493             try {
494                 postID = Integer.valueOf(postid);
495             } catch (NumberFormatException JavaDoc e) {
496                 throw new XmlRpcException(INVALID_POSTID, INVALID_POSTID_MSG);
497             }
498
499             try {
500                 Entry entry = _fetcher.loadEntry(_blog, postID);
501
502                 Hashtable JavaDoc postcontent = new Hashtable JavaDoc();
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 JavaDoc postCategories = new Vector JavaDoc(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     /**
528      * Delete a Post
529      *
530      * @param appkey Unique identifier/passcode of the application sending the post
531      * @param postid Unique identifier of the post to be changed
532      * @param userid Login for a Blogger user who has permission to post to the blog
533      * @param password Password for said username
534      * @param publish Ignored
535      * @return <code>true</code> if the entry was delete, <code>false</code> otherwise
536      * @throws XmlRpcException
537      */

538     public boolean deletePost(String JavaDoc appkey, String JavaDoc postid, String JavaDoc userid, String JavaDoc password, boolean publish) throws Exception JavaDoc {
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 JavaDoc postID;
552             try {
553                 postID = Integer.valueOf(BlojsomUtils.removeSlashes(postid));
554             } catch (NumberFormatException JavaDoc 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 JavaDoc(), 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     /**
579      * Retrieves a set of recent posts to the blog
580      *
581      * @param blogid Unique identifier of the blog the post will be added to
582      * @param userid Login for a MetaWeblog user who has permission to post to the blog
583      * @param password Password for said username
584      * @param numberOfPosts Number of posts to be retrieved from the blog
585      * @return Array of structures containing the minimal attributes for the MetaWeblog API getPost() method: title, link, and description
586      * @throws Exception If the user was not authenticated correctly
587      */

588     public Object JavaDoc getRecentPosts(String JavaDoc blogid, String JavaDoc userid, String JavaDoc password, int numberOfPosts) throws Exception JavaDoc {
589         _logger.debug("getRecentPosts() Called =========[ SUPPORTED ]=====");
590         _logger.debug(" BlogId: " + blogid);
591         _logger.debug(" UserId: " + userid);
592         _logger.debug(" Password: *********");
593
594         Vector JavaDoc recentPosts = new Vector JavaDoc();
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 JavaDoc(numberOfPosts));
603             } catch (FetcherException e) {
604                 throw new XmlRpcException(UNKNOWN_EXCEPTION, UNKNOWN_EXCEPTION_MSG);
605             } catch (NumberFormatException JavaDoc 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 JavaDoc entrystruct = new Hashtable JavaDoc();
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 JavaDoc postCategories = new Vector JavaDoc(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     /**
636      * Uploads an object to the blog to a specified directory
637      *
638      * @param blogid Unique identifier of the blog the post will be added to
639      * @param userid Login for a MetaWeblog user who has permission to post to the blog
640      * @param password Password for said username
641      * @param struct Upload structure defined by the MetaWeblog API
642      * @return Structure containing a link to the uploaded media object
643      * @throws XmlRpcException If the user was not authenticated correctly, if there was an I/O exception,
644      * or if the MIME type of the upload object is not accepted
645      */

646     public Object JavaDoc newMediaObject(String JavaDoc blogid, String JavaDoc userid, String JavaDoc password, Hashtable JavaDoc struct) throws Exception JavaDoc {
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 JavaDoc name = (String JavaDoc) struct.get(MEMBER_NAME);
657             name = BlojsomUtils.getFilenameFromPath(name);
658             _logger.debug("newMediaObject name: " + name);
659             String JavaDoc type = (String JavaDoc) struct.get(MEMBER_TYPE);
660             _logger.debug("newMediaObject type: " + type);
661             byte[] bits = (byte[]) struct.get(MEMBER_BITS);
662
663             File JavaDoc uploadDirectory = new File JavaDoc(_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 JavaDoc uploadDirectoryForUser = new File JavaDoc(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 JavaDoc bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(new File JavaDoc(uploadDirectoryForUser, name)));
686                     bos.write(bits);
687                     bos.close();
688
689                     Hashtable JavaDoc returnStruct = new Hashtable JavaDoc(1);
690                     String JavaDoc mediaURL = _blog.getBlogBaseURL() + _staticURLPrefix + _blog.getBlogId() + "/" + name;
691                     returnStruct.put(MEMBER_URL, mediaURL);
692
693                     return returnStruct;
694                 } catch (IOException JavaDoc 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     /**
710      * Edits the main or archive index template of a given blog (NOT IMPLEMENTED)
711      *
712      * @param appkey Unique identifier/passcode of the application sending the post
713      * @param blogid Unique identifier of the blog the post will be added to
714      * @param userid Login for a Blogger user who has permission to post to the blog
715      * @param password Password for said username
716      * @param template The text for the new template (usually mostly HTML). Must contain opening and closing <Blogger> tags, since they're needed to publish
717      * @param templateType Determines which of the blog's templates will be returned. Currently, either "main" or "archiveIndex"
718      * @return
719      * @throws XmlRpcException
720      */

721     public boolean setTemplate(String JavaDoc appkey, String JavaDoc blogid, String JavaDoc userid, String JavaDoc password, String JavaDoc template, String JavaDoc templateType) throws Exception JavaDoc {
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     /**
734      * Returns the main or archive index template of a given blog (NOT IMPLEMENTED)
735      *
736      * @param appkey Unique identifier/passcode of the application sending the post
737      * @param blogid Unique identifier of the blog the post will be added to
738      * @param userid Login for a Blogger user who has permission to post to the blog
739      * @param password Password for said username
740      * @param templateType Determines which of the blog's templates will be returned. Currently, either "main" or "archiveIndex"
741      * @return
742      * @throws XmlRpcException
743      */

744     public String JavaDoc getTemplate(String JavaDoc appkey, String JavaDoc blogid, String JavaDoc userid, String JavaDoc password, String JavaDoc templateType) throws Exception JavaDoc {
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