001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.extension.xmlrpc.handler;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.xmlrpc.XmlRpcException;
035: import org.blojsom.BlojsomException;
036: import org.blojsom.authorization.AuthorizationException;
037: import org.blojsom.blog.Blog;
038: import org.blojsom.blog.Category;
039: import org.blojsom.blog.Entry;
040: import org.blojsom.fetcher.FetcherException;
041: import org.blojsom.plugin.admin.event.EntryAddedEvent;
042: import org.blojsom.plugin.admin.event.EntryDeletedEvent;
043: import org.blojsom.plugin.admin.event.EntryUpdatedEvent;
044: import org.blojsom.util.BlojsomMetaDataConstants;
045: import org.blojsom.util.BlojsomUtils;
046: import org.blojsom.util.BlojsomConstants;
047:
048: import java.io.BufferedOutputStream;
049: import java.io.File;
050: import java.io.FileOutputStream;
051: import java.io.IOException;
052: import java.util.Date;
053: import java.util.HashMap;
054: import java.util.Hashtable;
055: import java.util.Vector;
056:
057: /**
058: * MetaWeblogAPIHandler
059: *
060: * @author David Czarnecki
061: * @since blojsom 3.0
062: * @version $Id: MetaWeblogAPIHandler.java,v 1.7 2007/01/17 02:35:07 czarneckid Exp $
063: */
064: public class MetaWeblogAPIHandler extends APIHandler {
065:
066: private Log _logger = LogFactory.getLog(MetaWeblogAPIHandler.class);
067:
068: private static final String METAWEBLOG_ACCEPTED_TYPES_IP = "blojsom-extension-metaweblog-accepted-types";
069:
070: /**
071: * Blogger API "blogid" key
072: */
073: private static final String MEMBER_BLOGID = "blogid";
074:
075: /**
076: * Blogger API "blogName" key
077: */
078: private static final String MEMBER_BLOGNAME = "blogName";
079:
080: /**
081: * MetaWeblog API "description" key
082: */
083: private static final String MEMBER_DESCRIPTION = "description";
084:
085: /**
086: * MetaWeblog API "htmlUrl" key
087: */
088: private static final String MEMBER_HTML_URL = "htmlUrl";
089:
090: /**
091: * MetaWeblog API "rssUrl" key
092: */
093: private static final String MEMBER_RSS_URL = "rssUrl";
094:
095: /**
096: * MetaWeblog API "title" key
097: */
098: private static final String MEMBER_TITLE = "title";
099:
100: /**
101: * MetaWeblog API "link" key
102: */
103: private static final String MEMBER_LINK = "link";
104:
105: /**
106: * MetaWeblog API "name" key
107: */
108: private static final String MEMBER_NAME = "name";
109:
110: /**
111: * MetaWeblog API "type" key
112: */
113: private static final String MEMBER_TYPE = "type";
114:
115: /**
116: * MetaWeblog API "bits" key
117: */
118: private static final String MEMBER_BITS = "bits";
119:
120: /**
121: * MetaWeblog API "permaLink" key
122: */
123: private static final String MEMBER_PERMALINK = "permaLink";
124:
125: /**
126: * MetaWeblog API "dateCreated" key
127: */
128: private static final String MEMBER_DATE_CREATED = "dateCreated";
129:
130: /**
131: * MetaWeblog API "categories" key
132: */
133: private static final String MEMBER_CATEGORIES = "categories";
134:
135: /**
136: * MetaWeblog API "postid" key
137: */
138: private static final String MEMBER_POSTID = "postid";
139:
140: /**
141: * MetaWeblog API "url" key
142: */
143: private static final String MEMBER_URL = "url";
144:
145: private static final String METAWEBLOG_API_PERMISSION = "post_via_metaweblog_api_permission";
146:
147: private static final String API_PREFIX = "metaWeblog";
148:
149: private String _uploadDirectory;
150: private HashMap _acceptedMimeTypes;
151: private String _staticURLPrefix;
152:
153: /**
154: * Create a new instance of the MetaWeblog API handler
155: */
156: public MetaWeblogAPIHandler() {
157: }
158:
159: /**
160: * Retrieve the API handler name
161: *
162: * @return API handler name "metaWeblog"
163: */
164: public String getName() {
165: return API_PREFIX;
166: }
167:
168: /**
169: * Set the {@link Blog}
170: *
171: * @param blog {@link Blog}
172: */
173: public void setBlog(Blog blog) {
174: super .setBlog(blog);
175:
176: _uploadDirectory = _servletConfig
177: .getServletContext()
178: .getRealPath(
179: _properties
180: .getProperty(
181: BlojsomConstants.RESOURCES_DIRECTORY_IP,
182: BlojsomConstants.DEFAULT_RESOURCES_DIRECTORY));
183: if (BlojsomUtils.checkNullOrBlank(_uploadDirectory)) {
184: if (_logger.isErrorEnabled()) {
185: _logger
186: .error("Unable to obtain path to resources directory");
187: }
188: }
189:
190: if (!_uploadDirectory.endsWith("/")) {
191: _uploadDirectory += "/";
192: }
193:
194: _acceptedMimeTypes = new HashMap(3);
195: String acceptedMimeTypes = _blog
196: .getProperty(METAWEBLOG_ACCEPTED_TYPES_IP);
197: if (acceptedMimeTypes != null && !"".equals(acceptedMimeTypes)) {
198: String[] types = BlojsomUtils
199: .parseCommaList(acceptedMimeTypes);
200: for (int i = 0; i < types.length; i++) {
201: String type = types[i];
202: type = type.toLowerCase();
203: _acceptedMimeTypes.put(type, type);
204: }
205: }
206:
207: _staticURLPrefix = _properties.getProperty(
208: BlojsomConstants.RESOURCES_DIRECTORY_IP,
209: BlojsomConstants.DEFAULT_RESOURCES_DIRECTORY);
210: if (!_staticURLPrefix.endsWith("/")) {
211: _staticURLPrefix += "/";
212: }
213: }
214:
215: /**
216: * Returns information on all the blogs a given user is a member of
217: *
218: * @param appkey Unique identifier/passcode of the application sending the post
219: * @param userid Login for a Blogger user who has permission to post to the blog
220: * @param password Password for said username
221: * @return Blog category list
222: * @throws XmlRpcException If there are no categories or the user was not authenticated correctly
223: */
224: public Object getUsersBlogs(String appkey, String userid,
225: String password) throws Exception {
226: _logger.debug("getUsersBlogs() Called ===[ SUPPORTED ]=======");
227: _logger.debug(" Appkey: " + appkey);
228: _logger.debug(" UserId: " + userid);
229: _logger.debug(" Password: *********");
230:
231: try {
232: _authorizationProvider.authorize(_blog, null, userid,
233: password);
234: checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION);
235:
236: Vector result = new Vector();
237:
238: Category[] categories = _fetcher.loadAllCategories(_blog);
239:
240: if (categories != null) {
241: for (int x = 0; x < categories.length; x++) {
242: Hashtable _bloglist = new Hashtable(3);
243: Category category = categories[x];
244:
245: String description;
246: if (!BlojsomUtils.checkNullOrBlank(category
247: .getDescription())) {
248: description = category.getDescription();
249: } else {
250: description = category.getName();
251: }
252:
253: _bloglist.put(MEMBER_URL, _blog.getBlogURL()
254: + category.getName());
255: _bloglist.put(MEMBER_BLOGID, Integer
256: .toString(category.getId().intValue()));
257: _bloglist.put(MEMBER_BLOGNAME, description);
258:
259: result.add(_bloglist);
260: }
261: } else {
262: throw new XmlRpcException(NOBLOGS_EXCEPTION,
263: NOBLOGS_EXCEPTION_MSG);
264: }
265:
266: return result;
267: } catch (BlojsomException e) {
268: throw new XmlRpcException(AUTHORIZATION_EXCEPTION,
269: AUTHORIZATION_EXCEPTION_MSG);
270: }
271: }
272:
273: /**
274: * Authenticates a user and returns the categories available in the blojsom
275: *
276: * @param blogid Dummy Value for Blojsom
277: * @param userid Login for a MetaWeblog user who has permission to post to the blog
278: * @param password Password for said username
279: * @return Blog category list
280: * @throws XmlRpcException If there are no categories or the user was not authenticated correctly
281: */
282: public Object getCategories(String blogid, String userid,
283: String password) throws Exception {
284: _logger.debug("getCategories() Called =====[ SUPPORTED ]=====");
285: _logger.debug(" BlogId: " + blogid);
286: _logger.debug(" UserId: " + userid);
287: _logger.debug(" Password: *********");
288:
289: try {
290: _authorizationProvider.authorize(_blog, null, userid,
291: password);
292: checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION);
293:
294: Hashtable result;
295:
296: Category[] categories = _fetcher.loadAllCategories(_blog);
297:
298: if (categories != null) {
299: result = new Hashtable(categories.length);
300:
301: for (int x = 0; x < categories.length; x++) {
302: Hashtable catlist = new Hashtable(3);
303: Category category = categories[x];
304:
305: String description;
306: if (!BlojsomUtils.checkNullOrBlank(category
307: .getDescription())) {
308: description = category.getDescription();
309: } else {
310: description = category.getName();
311: }
312:
313: catlist.put(MEMBER_DESCRIPTION, description);
314: catlist.put(MEMBER_HTML_URL, _blog.getBlogURL()
315: + category.getName());
316: catlist.put(MEMBER_RSS_URL, _blog.getBlogURL()
317: + category.getName() + "?flavor=rss2");
318:
319: result.put(Integer.toString(category.getId()
320: .intValue()), catlist);
321: }
322: } else {
323: throw new XmlRpcException(NOBLOGS_EXCEPTION,
324: NOBLOGS_EXCEPTION_MSG);
325: }
326:
327: return result;
328: } catch (AuthorizationException e) {
329: throw new XmlRpcException(AUTHORIZATION_EXCEPTION,
330: AUTHORIZATION_EXCEPTION_MSG);
331: }
332: }
333:
334: /**
335: * Makes a new post to a designated blog. Optionally, will publish the blog after making the post
336: *
337: * @param blogid Unique identifier of the blog the post will be added to
338: * @param userid Login for a MetaWeblog user who has permission to post to the blog
339: * @param password Password for said username
340: * @param struct Contents of the post
341: * @param publish If true, the blog will be published immediately after the post is made
342: * @return Post ID of the added entry
343: * @throws XmlRpcException If the user was not authenticated correctly or if there was an I/O exception
344: */
345: public String newPost(String blogid, String userid,
346: String password, Hashtable struct, boolean publish)
347: throws Exception {
348: _logger.debug("newPost() Called ===========[ SUPPORTED ]=====");
349: _logger.debug(" BlogId: " + blogid);
350: _logger.debug(" UserId: " + userid);
351: _logger.debug(" Password: *********");
352: _logger.debug(" Publish: " + publish);
353:
354: if (struct.containsKey(MEMBER_CATEGORIES)) {
355: Vector categories = (Vector) struct.get(MEMBER_CATEGORIES);
356: if (categories.size() > 0) {
357: blogid = (String) categories.get(0);
358: }
359: }
360:
361: try {
362: _authorizationProvider.authorize(_blog, null, userid,
363: password);
364: checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION);
365:
366: Integer blogID;
367: try {
368: blogID = Integer.valueOf(blogid);
369: } catch (NumberFormatException e) {
370: throw new XmlRpcException(UNKNOWN_EXCEPTION,
371: UNKNOWN_EXCEPTION_MSG);
372: }
373:
374: String result;
375:
376: Hashtable postcontent;
377: postcontent = struct;
378:
379: String title = (String) postcontent.get(MEMBER_TITLE);
380: String description = (String) postcontent
381: .get(MEMBER_DESCRIPTION);
382: Date dateCreated = (Date) postcontent
383: .get(MEMBER_DATE_CREATED);
384:
385: try {
386: Category category = _fetcher
387: .loadCategory(_blog, blogID);
388: Entry entry = _fetcher.newEntry();
389:
390: entry.setBlogId(_blog.getId());
391: entry.setBlogCategoryId(category.getId());
392: if (dateCreated == null) {
393: entry.setDate(new Date());
394: } else {
395: entry.setDate(dateCreated);
396: }
397: entry.setModifiedDate(entry.getDate());
398: entry.setTitle(title);
399: entry.setDescription(description);
400: entry.setAuthor(userid);
401:
402: if (publish) {
403: entry
404: .setStatus(BlojsomMetaDataConstants.PUBLISHED_STATUS);
405: } else {
406: entry
407: .setStatus(BlojsomMetaDataConstants.DRAFT_STATUS);
408: }
409:
410: _fetcher.saveEntry(_blog, entry);
411:
412: result = Integer.toString(entry.getId().intValue());
413:
414: // Send out an add blog entry event
415: _eventBroadcaster.broadcastEvent(new EntryAddedEvent(
416: this , new Date(), entry, _blog));
417: } catch (FetcherException e) {
418: if (_logger.isErrorEnabled()) {
419: _logger.error(e);
420: }
421:
422: throw new XmlRpcException(UNKNOWN_EXCEPTION,
423: UNKNOWN_EXCEPTION_MSG);
424: }
425:
426: return result;
427: } catch (AuthorizationException e) {
428: throw new XmlRpcException(AUTHORIZATION_EXCEPTION,
429: AUTHORIZATION_EXCEPTION_MSG);
430: }
431: }
432:
433: /**
434: * Edits a given post. Optionally, will publish the blog after making the edit
435: *
436: * @param postid Unique identifier of the post to be changed
437: * @param userid Login for a MetaWeblog user who has permission to post to the blog
438: * @param password Password for said username
439: * @param struct Contents of the post
440: * @param publish If true, the blog will be published immediately after the post is made
441: * @return <code>true</code> if the entry was edited, <code>false</code> otherwise
442: * @throws XmlRpcException If the user was not authenticated correctly, if there was an I/O exception,
443: * or if the entry permalink ID is invalid
444: */
445: public boolean editPost(String postid, String userid,
446: String password, Hashtable struct, boolean publish)
447: throws Exception {
448: _logger.debug("editPost() Called ========[ SUPPORTED ]=====");
449: _logger.debug(" PostId: " + postid);
450: _logger.debug(" UserId: " + userid);
451: _logger.debug(" Password: *********");
452: _logger.debug(" Publish: " + publish);
453:
454: try {
455: _authorizationProvider.authorize(_blog, null, userid,
456: password);
457: checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION);
458:
459: boolean result;
460:
461: Integer postID;
462: try {
463: postID = Integer.valueOf(BlojsomUtils
464: .removeSlashes(postid));
465: } catch (NumberFormatException e) {
466: throw new XmlRpcException(INVALID_POSTID,
467: INVALID_POSTID_MSG);
468: }
469:
470: try {
471: Entry entryToEdit = _fetcher.loadEntry(_blog, postID);
472:
473: if (!userid.equals(entryToEdit.getAuthor())) {
474: checkXMLRPCPermission(userid,
475: ALL_XMLRPC_EDIT_PERMISSION);
476: }
477:
478: Hashtable postcontent;
479: postcontent = struct;
480:
481: String title = (String) postcontent.get(MEMBER_TITLE);
482: String description = (String) postcontent
483: .get(MEMBER_DESCRIPTION);
484: Date dateCreated = (Date) postcontent
485: .get(MEMBER_DATE_CREATED);
486:
487: entryToEdit.setTitle(title);
488: entryToEdit.setDescription(description);
489: if (dateCreated != null) {
490: entryToEdit.setDate(dateCreated);
491: entryToEdit.setModifiedDate(dateCreated);
492: } else {
493: entryToEdit.setModifiedDate(new Date());
494: }
495:
496: if (publish) {
497: entryToEdit
498: .setStatus(BlojsomMetaDataConstants.PUBLISHED_STATUS);
499: } else {
500: entryToEdit
501: .setStatus(BlojsomMetaDataConstants.DRAFT_STATUS);
502: }
503:
504: _fetcher.saveEntry(_blog, entryToEdit);
505:
506: result = true;
507:
508: _eventBroadcaster.broadcastEvent(new EntryUpdatedEvent(
509: this , new Date(), entryToEdit, _blog));
510: } catch (FetcherException e) {
511: if (_logger.isErrorEnabled()) {
512: _logger.error(e);
513: }
514:
515: throw new XmlRpcException(UNKNOWN_EXCEPTION,
516: UNKNOWN_EXCEPTION_MSG);
517: }
518:
519: return result;
520: } catch (AuthorizationException e) {
521: throw new XmlRpcException(AUTHORIZATION_EXCEPTION,
522: AUTHORIZATION_EXCEPTION_MSG);
523: }
524: }
525:
526: /**
527: * Retrieves a given post from the blog
528: *
529: * @param postid Unique identifier of the post to be changed
530: * @param userid Login for a MetaWeblog user who has permission to post to the blog
531: * @param password Password for said username
532: * @return Structure containing the minimal attributes for the MetaWeblog API getPost() method: title, link, and description
533: * @throws XmlRpcException If the user was not authenticated correctly, if there was an I/O exception,
534: * or if the entry permalink ID is invalid
535: */
536: public Object getPost(String postid, String userid, String password)
537: throws Exception {
538: _logger.debug("getPost() Called =========[ SUPPORTED ]=====");
539: _logger.debug(" PostId: " + postid);
540: _logger.debug(" UserId: " + userid);
541: _logger.debug(" Password: *********");
542:
543: try {
544: _authorizationProvider.authorize(_blog, null, userid,
545: password);
546: checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION);
547:
548: Integer postID;
549: try {
550: postID = Integer.valueOf(postid);
551: } catch (NumberFormatException e) {
552: throw new XmlRpcException(INVALID_POSTID,
553: INVALID_POSTID_MSG);
554: }
555:
556: try {
557: Entry entry = _fetcher.loadEntry(_blog, postID);
558:
559: Hashtable postcontent = new Hashtable();
560: postcontent.put(MEMBER_TITLE, entry.getTitle());
561: postcontent.put(MEMBER_LINK, _blog.getBlogURL()
562: + entry.getCategory() + entry.getPostSlug());
563: postcontent.put(MEMBER_DESCRIPTION, entry
564: .getDescription());
565: postcontent.put(MEMBER_DATE_CREATED, entry.getDate());
566: postcontent.put(MEMBER_PERMALINK, _blog.getBlogURL()
567: + entry.getCategory() + entry.getPostSlug());
568: postcontent.put(MEMBER_POSTID, Integer.toString(entry
569: .getId().intValue()));
570:
571: Vector postCategories = new Vector(1);
572: postCategories.add(Integer.toString(entry
573: .getBlogCategoryId().intValue()));
574: postcontent.put(MEMBER_CATEGORIES, postCategories);
575:
576: return postcontent;
577: } catch (FetcherException e) {
578: if (_logger.isErrorEnabled()) {
579: _logger.error(e);
580: }
581:
582: throw new XmlRpcException(INVALID_POSTID,
583: INVALID_POSTID_MSG);
584: }
585: } catch (AuthorizationException e) {
586: throw new XmlRpcException(AUTHORIZATION_EXCEPTION,
587: AUTHORIZATION_EXCEPTION_MSG);
588: }
589: }
590:
591: /**
592: * Delete a Post
593: *
594: * @param appkey Unique identifier/passcode of the application sending the post
595: * @param postid Unique identifier of the post to be changed
596: * @param userid Login for a Blogger user who has permission to post to the blog
597: * @param password Password for said username
598: * @param publish Ignored
599: * @return <code>true</code> if the entry was delete, <code>false</code> otherwise
600: * @throws XmlRpcException
601: */
602: public boolean deletePost(String appkey, String postid,
603: String userid, String password, boolean publish)
604: throws Exception {
605: _logger.debug("deletePost() Called =====[ SUPPORTED ]=====");
606: _logger.debug(" Appkey: " + appkey);
607: _logger.debug(" PostId: " + postid);
608: _logger.debug(" UserId: " + userid);
609: _logger.debug(" Password: *********");
610:
611: boolean result;
612:
613: try {
614: _authorizationProvider.authorize(_blog, null, userid,
615: password);
616: checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION);
617:
618: Integer postID;
619: try {
620: postID = Integer.valueOf(BlojsomUtils
621: .removeSlashes(postid));
622: } catch (NumberFormatException e) {
623: throw new XmlRpcException(INVALID_POSTID,
624: INVALID_POSTID_MSG);
625: }
626:
627: try {
628: Entry entryToDelete = _fetcher.loadEntry(_blog, postID);
629: _fetcher.deleteEntry(_blog, entryToDelete);
630:
631: result = true;
632: _eventBroadcaster.broadcastEvent(new EntryDeletedEvent(
633: this , new Date(), entryToDelete, _blog));
634: } catch (FetcherException e) {
635: if (_logger.isErrorEnabled()) {
636: _logger.error(e);
637: }
638:
639: throw new XmlRpcException(UNKNOWN_EXCEPTION,
640: UNKNOWN_EXCEPTION_MSG);
641: }
642: } catch (AuthorizationException e) {
643: throw new XmlRpcException(AUTHORIZATION_EXCEPTION,
644: AUTHORIZATION_EXCEPTION_MSG);
645: }
646:
647: return result;
648: }
649:
650: /**
651: * Retrieves a set of recent posts to the blog
652: *
653: * @param blogid Unique identifier of the blog the post will be added to
654: * @param userid Login for a MetaWeblog user who has permission to post to the blog
655: * @param password Password for said username
656: * @param numberOfPosts Number of posts to be retrieved from the blog
657: * @return Array of structures containing the minimal attributes for the MetaWeblog API getPost() method: title, link, and description
658: * @throws Exception If the user was not authenticated correctly
659: */
660: public Object getRecentPosts(String blogid, String userid,
661: String password, int numberOfPosts) throws Exception {
662: _logger
663: .debug("getRecentPosts() Called =========[ SUPPORTED ]=====");
664: _logger.debug(" BlogId: " + blogid);
665: _logger.debug(" UserId: " + userid);
666: _logger.debug(" Password: *********");
667:
668: Vector recentPosts = new Vector();
669:
670: try {
671: _authorizationProvider.authorize(_blog, null, userid,
672: password);
673: checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION);
674:
675: Entry[] entries;
676: try {
677: entries = _fetcher.loadEntriesForCategory(_blog,
678: Integer.valueOf(blogid), new Integer(
679: numberOfPosts));
680: } catch (FetcherException e) {
681: throw new XmlRpcException(UNKNOWN_EXCEPTION,
682: UNKNOWN_EXCEPTION_MSG);
683: } catch (NumberFormatException e) {
684: throw new XmlRpcException(UNKNOWN_EXCEPTION,
685: UNKNOWN_EXCEPTION_MSG);
686: }
687:
688: if (entries != null && entries.length > 0) {
689: for (int x = 0; x < entries.length; x++) {
690: Entry entry = entries[x];
691: Hashtable entrystruct = new Hashtable();
692:
693: entrystruct.put(MEMBER_TITLE, entry.getTitle());
694: entrystruct
695: .put(MEMBER_LINK, _blog.getBlogURL()
696: + entry.getCategory()
697: + entry.getPostSlug());
698: entrystruct.put(MEMBER_DESCRIPTION, entry
699: .getDescription());
700: entrystruct.put(MEMBER_DATE_CREATED, entry
701: .getDate());
702: entrystruct
703: .put(MEMBER_PERMALINK, _blog.getBlogURL()
704: + entry.getCategory()
705: + entry.getPostSlug());
706: entrystruct.put(MEMBER_POSTID, Integer
707: .toString(entry.getId().intValue()));
708:
709: Vector postCategories = new Vector(1);
710: postCategories.add(Integer.toString(entry
711: .getBlogCategoryId().intValue()));
712: entrystruct.put(MEMBER_CATEGORIES, postCategories);
713:
714: recentPosts.add(entrystruct);
715: }
716: }
717:
718: return recentPosts;
719: } catch (AuthorizationException e) {
720: throw new XmlRpcException(AUTHORIZATION_EXCEPTION,
721: AUTHORIZATION_EXCEPTION_MSG);
722: }
723: }
724:
725: /**
726: * Uploads an object to the blog to a specified directory
727: *
728: * @param blogid Unique identifier of the blog the post will be added to
729: * @param userid Login for a MetaWeblog user who has permission to post to the blog
730: * @param password Password for said username
731: * @param struct Upload structure defined by the MetaWeblog API
732: * @return Structure containing a link to the uploaded media object
733: * @throws XmlRpcException If the user was not authenticated correctly, if there was an I/O exception,
734: * or if the MIME type of the upload object is not accepted
735: */
736: public Object newMediaObject(String blogid, String userid,
737: String password, Hashtable struct) throws Exception {
738: _logger.debug("newMediaObject() Called =[ SUPPORTED ]=====");
739: _logger.debug(" BlogId: " + blogid);
740: _logger.debug(" UserId: " + userid);
741: _logger.debug(" Password: *********");
742:
743: try {
744: _authorizationProvider.authorize(_blog, null, userid,
745: password);
746: checkXMLRPCPermission(userid, METAWEBLOG_API_PERMISSION);
747:
748: String name = (String) struct.get(MEMBER_NAME);
749: name = BlojsomUtils.getFilenameFromPath(name);
750: _logger.debug("newMediaObject name: " + name);
751: String type = (String) struct.get(MEMBER_TYPE);
752: _logger.debug("newMediaObject type: " + type);
753: byte[] bits = (byte[]) struct.get(MEMBER_BITS);
754:
755: File uploadDirectory = new File(_uploadDirectory);
756: if (!uploadDirectory.exists()) {
757: if (_logger.isErrorEnabled()) {
758: _logger.error("Upload directory does not exist: "
759: + uploadDirectory.toString());
760: }
761:
762: throw new XmlRpcException(UNKNOWN_EXCEPTION,
763: "Upload directory does not exist: "
764: + uploadDirectory.toString());
765: }
766:
767: if (_acceptedMimeTypes.containsKey(type.toLowerCase())) {
768: try {
769: File uploadDirectoryForUser = new File(
770: uploadDirectory, _blog.getBlogId());
771: if (!uploadDirectoryForUser.exists()) {
772: if (!uploadDirectoryForUser.mkdir()) {
773: if (_logger.isErrorEnabled()) {
774: _logger
775: .error("Could not create upload directory for user: "
776: + uploadDirectoryForUser
777: .toString());
778: }
779:
780: throw new XmlRpcException(
781: UNKNOWN_EXCEPTION,
782: "Could not create upload directory for user: "
783: + _blog.getBlogId());
784: }
785: }
786:
787: BufferedOutputStream bos = new BufferedOutputStream(
788: new FileOutputStream(new File(
789: uploadDirectoryForUser, name)));
790: bos.write(bits);
791: bos.close();
792:
793: Hashtable returnStruct = new Hashtable(1);
794: String mediaURL = _blog.getBlogBaseURL()
795: + _staticURLPrefix + _blog.getBlogId()
796: + "/" + name;
797: returnStruct.put(MEMBER_URL, mediaURL);
798:
799: return returnStruct;
800: } catch (IOException e) {
801: if (_logger.isErrorEnabled()) {
802: _logger.error(e);
803: }
804:
805: throw new XmlRpcException(UNKNOWN_EXCEPTION,
806: UNKNOWN_EXCEPTION_MSG);
807: }
808: } else {
809: throw new XmlRpcException(UNKNOWN_EXCEPTION,
810: "MIME type not accepted. Received MIME type: "
811: + type);
812: }
813: } catch (BlojsomException e) {
814: throw new XmlRpcException(AUTHORIZATION_EXCEPTION,
815: AUTHORIZATION_EXCEPTION_MSG);
816: }
817: }
818:
819: /**
820: * Edits the main or archive index template of a given blog (NOT IMPLEMENTED)
821: *
822: * @param appkey Unique identifier/passcode of the application sending the post
823: * @param blogid Unique identifier of the blog the post will be added to
824: * @param userid Login for a Blogger user who has permission to post to the blog
825: * @param password Password for said username
826: * @param template The text for the new template (usually mostly HTML). Must contain opening and closing <Blogger> tags, since they're needed to publish
827: * @param templateType Determines which of the blog's templates will be returned. Currently, either "main" or "archiveIndex"
828: * @return
829: * @throws XmlRpcException
830: */
831: public boolean setTemplate(String appkey, String blogid,
832: String userid, String password, String template,
833: String templateType) throws Exception {
834: _logger.debug("setTemplate() Called =====[ UNSUPPORTED ]=====");
835: _logger.debug(" Appkey: " + appkey);
836: _logger.debug(" BlogId: " + blogid);
837: _logger.debug(" UserId: " + userid);
838: _logger.debug(" Password: *********");
839: _logger.debug(" Template: " + template);
840: _logger.debug(" Type: " + templateType);
841:
842: throw new XmlRpcException(UNSUPPORTED_EXCEPTION,
843: UNSUPPORTED_EXCEPTION_MSG);
844: }
845:
846: /**
847: * Returns the main or archive index template of a given blog (NOT IMPLEMENTED)
848: *
849: * @param appkey Unique identifier/passcode of the application sending the post
850: * @param blogid Unique identifier of the blog the post will be added to
851: * @param userid Login for a Blogger user who has permission to post to the blog
852: * @param password Password for said username
853: * @param templateType Determines which of the blog's templates will be returned. Currently, either "main" or "archiveIndex"
854: * @return
855: * @throws XmlRpcException
856: */
857: public String getTemplate(String appkey, String blogid,
858: String userid, String password, String templateType)
859: throws Exception {
860: _logger.debug("getTemplate() Called =====[ UNSUPPORTED ]=====");
861: _logger.debug(" Appkey: " + appkey);
862: _logger.debug(" BlogId: " + blogid);
863: _logger.debug(" UserId: " + userid);
864: _logger.debug(" Password: *********");
865: _logger.debug(" Type: " + templateType);
866:
867: throw new XmlRpcException(UNSUPPORTED_EXCEPTION,
868: UNSUPPORTED_EXCEPTION_MSG);
869: }
870: }
|