1 package net.sourceforge.blogentis.xmlrpc.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.Date;
26 import java.util.Hashtable;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Vector;
30
31 import net.sourceforge.blogentis.om.Blog;
32 import net.sourceforge.blogentis.om.Post;
33 import net.sourceforge.blogentis.om.PostPeer;
34 import net.sourceforge.blogentis.plugins.BlogPluginService;
35 import net.sourceforge.blogentis.plugins.base.IPostEditExtensionPoint;
36 import net.sourceforge.blogentis.slide.SlideMemoryFile;
37 import net.sourceforge.blogentis.slide.SlideService;
38 import net.sourceforge.blogentis.turbine.BlogRunData;
39 import net.sourceforge.blogentis.turbine.BlogRunDataService;
40 import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
41 import net.sourceforge.blogentis.utils.BlogConstants;
42 import net.sourceforge.blogentis.utils.DateUtils;
43 import net.sourceforge.blogentis.utils.LinkFactoryService;
44 import net.sourceforge.blogentis.utils.StringUtils;
45
46 import org.apache.torque.TorqueException;
47 import org.apache.turbine.om.security.User;
48 import org.apache.turbine.services.mimetype.TurbineMimeTypes;
49 import org.apache.xmlrpc.XmlRpcException;
50
51 /***
52 * @author abas
53 */
54 public class MetaWeblogExecutor
55 extends AuthenticationExecutor {
56 protected Hashtable postToHash(Post post)
57 throws XmlRpcException {
58 Hashtable h = new Hashtable();
59 h.put("title", post.getTitle());
60 h.put("userid", post.getAuthorId());
61 h.put("description", post.getFullText());
62 h.put("dateCreated", DateUtils.formatIso8601(post.getPostedTime()));
63 h.put("postid", Integer.toString(post.getPostId()));
64 h.put("permaLink", new AbsoluteLinkURL().permaLink(post).toString());
65 h.put("link", h.get("permaLink"));
66 h.put("mt_excerpt", post.getShortDescription());
67 h.put("mt_text_more", "");
68 h.put("mt_keywords", "");
69 h.put("mt_allow_comments", new Integer(1));
70 h.put("mt_allow_pings", new Integer(1));
71 h.put("mt_convert_breaks", new Integer(1));
72 return h;
73 }
74
75 protected void fillPost(Post p, Hashtable struct)
76 throws XmlRpcException {
77 p.setTitle((String)struct.get("title"));
78 p.setFullText((String)struct.get("description"));
79 p.setShortDescription((String)struct.get("mt_excerpt"));
80
81 if (p.getFullText() == null)
82 throw new XmlRpcException(1, "Empty blog content not allowed.");
83 if (p.getShortDescription() == null)
84 p.setShortDescription("");
85 Object o = struct.get("mt_convert_breaks");
86 if (o != null
87 && (!(o instanceof Integer) || ((Integer)o).intValue() != 0)) {
88 p.setFullText(StringUtils.convertBreaks(p.getFullText()));
89 }
90 }
91
92 protected void publishPost(Blog b, Post p, boolean publishOrNot) {
93 IPostEditExtensionPoint ipep = (IPostEditExtensionPoint)BlogPluginService
94 .locateExtensionPoint(b, IPostEditExtensionPoint.class);
95 BlogRunData data = BlogRunDataService.getCurrentRunData();
96 if (p.isNew())
97 ipep.postNew(data, p);
98 if (p.isModified())
99 ipep.postModified(data, p);
100 if (publishOrNot)
101 ipep.postPublicationStatusChanged(data, p, PostPeer.DRAFT_TYPE);
102 }
103
104 public Hashtable getPost(String postid, String username, String password)
105 throws XmlRpcException {
106 User u = haveUser(username, password);
107 try {
108 Post p = PostPeer.retrieveByPK(Integer.parseInt(postid));
109 isAuthenticatedForBlog(p.getBlog().getName(), u, null);
110 return postToHash(p);
111 } catch (TorqueException e) {
112 throw new XmlRpcException(1, "Could not fetch post");
113 }
114 }
115
116 public Vector getRecentPosts(String blogid, String username,
117 String password, int NumberOfPosts)
118 throws XmlRpcException {
119 User u = haveUser(username, password);
120 Blog blog = isAuthenticatedForBlog(blogid, u, null);
121 List l = getLatestPosts(blog, NumberOfPosts);
122 Vector v = new Vector();
123 for(Iterator i = l.iterator(); i.hasNext();) {
124 v.add(postToHash((Post)i.next()));
125 }
126 return v;
127 }
128
129 public String newPost(String blogid, String username, String password,
130 Hashtable struct, boolean publish)
131 throws XmlRpcException {
132 User u = haveUser(username, password);
133 Blog blog = isAuthenticatedForBlog(blogid, u, BlogConstants.PERM_WRITE_POSTS);
134 try {
135 Post p = new Post();
136 p.setBlogId(blog.getBlogId());
137 setAuthorID(p, u);
138 fillPost(p, struct);
139 if (publish) {
140 p.setPostedTime(new Date());
141 p.save();
142 publishPost(blog, p, publish);
143 } else {
144 p.save();
145 }
146 return String.valueOf(p.getPostId());
147 } catch (Exception e) {
148 throw new XmlRpcException(1, "Could not save the new post.");
149 }
150 }
151
152 public boolean editPost(String postid, String username, String password,
153 Hashtable struct, boolean publish)
154 throws XmlRpcException {
155 try {
156 User u = haveUser(username, password);
157 Post p = PostPeer.retrieveByPK(Integer.parseInt(postid));
158 Blog b = isAuthenticatedForBlog(p.getBlog().getName(), u,
159 BlogConstants.PERM_EDIT_POSTS);
160
161 boolean isPublished = p.getPostedTime() != null;
162 fillPost(p, struct);
163 if (publish && !isPublished) {
164 p.setPostedTime(new Date());
165 p.save();
166 publishPost(b, p, !isPublished);
167 } else {
168 p.save();
169 }
170 return true;
171 } catch (XmlRpcException e1) {
172 throw e1;
173 } catch (Exception e) {
174 throw new XmlRpcException(1, "Could not save the modified post.");
175 }
176 }
177
178 public Hashtable newMediaObject(String blogid, String username,
179 String password, Hashtable struct)
180 throws XmlRpcException {
181
182 try {
183 Hashtable h = new Hashtable();
184
185 User u = haveUser(username, password);
186 Blog blog = isAuthenticatedForBlog(blogid, u, BlogConstants.PERM_WRITE_POSTS);
187 String name = (String)struct.get("name");
188 name.replaceAll("/", "_");
189 byte[] bytes = (byte[])struct.get("bits");
190
191 SlideMemoryFile bf = new SlideMemoryFile(blog, "media/" + name);
192 bf.setContents(bytes);
193 bf.setLastModified(new Date());
194 bf.setMimeType(TurbineMimeTypes.getContentType(bf.getPath()));
195 SlideService.getInstance().saveFile(blog, bf);
196
197 h.put("url", LinkFactoryService.getLink().setBlog(blog)
198 .setPage(bf.getPath()));
199 return h;
200 } catch (XmlRpcException e1) {
201 throw e1;
202 } catch (Exception e) {
203 throw new XmlRpcException(1,
204 "Could not create the given media object.");
205 }
206 }
207 }