Coverage Report - net.sourceforge.pebble.webservice.PebbleAPIHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
PebbleAPIHandler
0%
0/47
0%
0/8
3
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 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
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 package net.sourceforge.pebble.webservice;
 33  
 
 34  
 import net.sourceforge.pebble.domain.Blog;
 35  
 import net.sourceforge.pebble.domain.BlogEntry;
 36  
 import net.sourceforge.pebble.domain.Category;
 37  
 import net.sourceforge.pebble.domain.Tag;
 38  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 39  
 import net.sourceforge.pebble.Constants;
 40  
 import org.apache.commons.logging.Log;
 41  
 import org.apache.commons.logging.LogFactory;
 42  
 import org.apache.xmlrpc.XmlRpcException;
 43  
 
 44  
 import java.util.Collection;
 45  
 import java.util.Hashtable;
 46  
 import java.util.Iterator;
 47  
 import java.util.Vector;
 48  
 
 49  
 /**
 50  
  * A handler for the Pebble API (accessed via XML-RPC).
 51  
  *
 52  
  * @author    Simon Brown
 53  
  */
 54  0
 public class PebbleAPIHandler extends AbstractAPIHandler {
 55  
 
 56  
   public static final String ID = "id";
 57  
   public static final String UUID = "uuid";
 58  
   public static final String DATE = "date";
 59  
   public static final String AUTHOR = "author";
 60  
   public static final String TITLE = "title";
 61  
   public static final String SUBTITLE = "subtitle";
 62  
   public static final String EXCERPT = "excerpt";
 63  
   public static final String BODY = "body";
 64  
   public static final String PERMALINK = "permalink";
 65  
   public static final String CATEGORIES = "categories";
 66  
   public static final String TAGS = "tags";
 67  
   public static final String ATTACHMENT = "attachment";
 68  
   public static final String ATTACHMENT_URL = "attachment.url";
 69  
   public static final String ATTACHMENT_SIZE = "attachment.size";
 70  
   public static final String ATTACHMENT_TYPE = "attachment.type";
 71  
 
 72  
   /** the log used by this class */
 73  0
   private static Log log = LogFactory.getLog(PebbleAPIHandler.class);
 74  
 
 75  
   /**
 76  
    * Gets a list of the recent blog entries.
 77  
    *
 78  
    * @param blogid    the ID of the blog (ignored)
 79  
    * @param username  the username used for logging in via XML-RPC
 80  
    * @param password  the password used for logging in via XML-RPC
 81  
    * @param numberOfPosts   the number of posts to get
 82  
    * @return  a Vector of Hashtables (an array of structs) representing blog entries
 83  
    * @throws org.apache.xmlrpc.XmlRpcException    if something goes wrong, including an authentication error
 84  
    */
 85  
   public Vector getRecentBlogEntries(String blogid, String username, String password, int numberOfPosts) throws XmlRpcException {
 86  0
     log.debug("pebble.getRecentBlogEntries(" +
 87  
         blogid + ", " +
 88  
         username + ", " +
 89  
         "********)");
 90  
 
 91  0
     Blog blog = getBlogWithBlogId(blogid);
 92  0
     authenticate(blog, username, password);
 93  
 
 94  0
     Vector posts = new Vector();
 95  0
     Collection coll = blog.getRecentPublishedBlogEntries(numberOfPosts);
 96  0
     Iterator it = coll.iterator();
 97  
     BlogEntry entry;
 98  0
     while (it.hasNext()) {
 99  0
       entry = (BlogEntry)it.next();
 100  0
       posts.add(adaptBlogEntry(entry));
 101  
     }
 102  
 
 103  0
     return posts;
 104  
   }
 105  
 
 106  
   /**
 107  
    * Helper method to adapt a blog entry into an XML-RPC compatible struct.
 108  
    *
 109  
    * @param entry   the BlogEntry to adapt
 110  
    * @return  a Hashtable representing the major properties of the entry
 111  
    */
 112  
   private Hashtable adaptBlogEntry(BlogEntry entry) {
 113  
     // first apply decorators - we don't want to go out naked :-)
 114  0
     ContentDecoratorContext context = new ContentDecoratorContext();
 115  0
     context.setView(ContentDecoratorContext.DETAIL_VIEW);
 116  0
     context.setMedia(ContentDecoratorContext.XML_RPC);
 117  0
     entry.getBlog().getContentDecoratorChain().decorate(context, entry);
 118  
 
 119  0
     Hashtable post = new Hashtable();
 120  0
     post.put(TITLE, entry.getTitle());
 121  0
     post.put(SUBTITLE, entry.getSubtitle());
 122  0
     post.put(PERMALINK, entry.getPermalink());
 123  0
     post.put(EXCERPT, entry.getExcerpt());
 124  0
     post.put(BODY, entry.getBody());
 125  0
     post.put(DATE, entry.getDate());
 126  0
     post.put(AUTHOR, entry.getAuthor());
 127  0
     post.put(ID, entry.getId());
 128  0
     post.put(UUID, formatPostId(entry.getBlog().getId(), entry.getId()));
 129  
 
 130  0
     Vector categories = new Vector();
 131  0
     Iterator it = entry.getCategories().iterator();
 132  0
     while (it.hasNext()) {
 133  0
       Category cat = (Category)it.next();
 134  0
       categories.add(cat.getId());
 135  0
     }
 136  0
     post.put(CATEGORIES, categories);
 137  
 
 138  0
     Vector tags = new Vector();
 139  0
     it = entry.getTagsAsList().iterator();
 140  0
     while (it.hasNext()) {
 141  0
       Tag tag = (Tag)it.next();
 142  0
       tags.add(tag.getName());
 143  0
     }
 144  0
     post.put(TAGS, tags);
 145  
 
 146  0
     if (entry.getAttachment() != null) {
 147  0
       Hashtable attachment = new Hashtable();
 148  0
       attachment.put(ATTACHMENT_URL, entry.getAttachment().getUrl());
 149  0
       attachment.put(ATTACHMENT_SIZE, entry.getAttachment().getSize());
 150  0
       attachment.put(ATTACHMENT_TYPE, entry.getAttachment().getType());
 151  
 
 152  0
       post.put(ATTACHMENT, attachment);
 153  
     }
 154  
 
 155  0
     return post;
 156  
   }
 157  
 
 158  
 }