File Source: pebbleapihandler.java
/*
P/P * Method: net.sourceforge.pebble.webservice.PebbleAPIHandler__static_init
*
* Postconditions:
* init'ed(log)
*/
1 /*
2 * Copyright (c) 2003-2006, 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;
/*
P/P * Method: void net.sourceforge.pebble.webservice.PebbleAPIHandler()
*/
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 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 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 */
/*
P/P * Method: Vector getRecentBlogEntries(String, String, String, int)
*
* Preconditions:
* log != null
* this.authenticationManager != null
*
* Presumptions:
* java.util.Iterator:next(...)@99 != null
* net.sourceforge.pebble.domain.Blog:getRecentPublishedBlogEntries(...)@95 != null
*
* Postconditions:
* return_value == &new Vector(getRecentBlogEntries#2)
* new Vector(getRecentBlogEntries#2) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@98: {1}, {0}
*/
85 public Vector getRecentBlogEntries(String blogid, String username, String password, int numberOfPosts) throws XmlRpcException {
86 log.debug("pebble.getRecentBlogEntries(" +
87 blogid + ", " +
88 username + ", " +
89 "********)");
90
91 Blog blog = getBlogWithBlogId(blogid);
92 authenticate(blog, username, password);
93
94 Vector posts = new Vector();
95 Collection coll = blog.getRecentPublishedBlogEntries(numberOfPosts);
96 Iterator it = coll.iterator();
97 BlogEntry entry;
98 while (it.hasNext()) {
99 entry = (BlogEntry)it.next();
100 posts.add(adaptBlogEntry(entry));
101 }
102
103 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 */
/*
P/P * Method: Hashtable adaptBlogEntry(BlogEntry)
*
* Preconditions:
* entry != null
*
* Presumptions:
* java.util.Iterator:next(...)@133 != null
* java.util.Iterator:next(...)@141 != null
* net.sourceforge.pebble.domain.Blog:getContentDecoratorChain(...)@117 != null
* net.sourceforge.pebble.domain.BlogEntry:getAttachment(...)@148 != null
* net.sourceforge.pebble.domain.BlogEntry:getAttachment(...)@149 != null
* ...
*
* Postconditions:
* return_value == &new Hashtable(adaptBlogEntry#2)
* new Hashtable(adaptBlogEntry#2) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@132: {1}, {0}
* java.util.Iterator:hasNext(...)@140: {1}, {0}
* net.sourceforge.pebble.domain.BlogEntry:getAttachment(...)@146: Addr_Set{null}, Inverse{null}
*/
112 private Hashtable adaptBlogEntry(BlogEntry entry) {
113 // first apply decorators - we don't want to go out naked :-)
114 ContentDecoratorContext context = new ContentDecoratorContext();
115 context.setView(ContentDecoratorContext.DETAIL_VIEW);
116 context.setMedia(ContentDecoratorContext.XML_RPC);
117 entry.getBlog().getContentDecoratorChain().decorate(context, entry);
118
119 Hashtable post = new Hashtable();
120 post.put(TITLE, entry.getTitle());
121 post.put(SUBTITLE, entry.getSubtitle());
122 post.put(PERMALINK, entry.getPermalink());
123 post.put(EXCERPT, entry.getExcerpt());
124 post.put(BODY, entry.getBody());
125 post.put(DATE, entry.getDate());
126 post.put(AUTHOR, entry.getAuthor());
127 post.put(ID, entry.getId());
128 post.put(UUID, formatPostId(entry.getBlog().getId(), entry.getId()));
129
130 Vector categories = new Vector();
131 Iterator it = entry.getCategories().iterator();
132 while (it.hasNext()) {
133 Category cat = (Category)it.next();
134 categories.add(cat.getId());
135 }
136 post.put(CATEGORIES, categories);
137
138 Vector tags = new Vector();
139 it = entry.getTagsAsList().iterator();
140 while (it.hasNext()) {
141 Tag tag = (Tag)it.next();
142 tags.add(tag.getName());
143 }
144 post.put(TAGS, tags);
145
146 if (entry.getAttachment() != null) {
147 Hashtable attachment = new Hashtable();
148 attachment.put(ATTACHMENT_URL, entry.getAttachment().getUrl());
149 attachment.put(ATTACHMENT_SIZE, entry.getAttachment().getSize());
150 attachment.put(ATTACHMENT_TYPE, entry.getAttachment().getType());
151
152 post.put(ATTACHMENT, attachment);
153 }
154
155 return post;
156 }
157
158 }
SofCheck Inspector Build Version : 2.22510
| pebbleapihandler.java |
2010-Jun-25 19:40:32 |
| pebbleapihandler.class |
2010-Jul-19 20:23:38 |