1 25 26 package org.snipsnap.xmlrpc; 27 28 import org.apache.xmlrpc.XmlRpcException; 29 import org.radeox.util.logging.Logger; 30 import org.snipsnap.app.Application; 31 import org.snipsnap.container.Components; 32 import org.snipsnap.net.FileUploadServlet; 33 import org.snipsnap.semanticweb.rss.BlogFeeder; 34 import org.snipsnap.snip.Blog; 35 import org.snipsnap.snip.Snip; 36 import org.snipsnap.snip.SnipSpace; 37 import org.snipsnap.snip.SnipSpaceFactory; 38 import org.snipsnap.snip.attachment.Attachment; 39 import org.snipsnap.snip.attachment.storage.AttachmentStorage; 40 import org.snipsnap.user.AuthenticationService; 41 import org.snipsnap.user.Permissions; 42 import org.snipsnap.user.Security; 43 import org.snipsnap.user.User; 44 45 import java.io.File ; 46 import java.io.IOException ; 47 import java.io.OutputStream ; 48 import java.util.*; 49 import org.snipsnap.snip.BlogKit; 50 import org.snipsnap.snip.attachment.Attachments; 51 import org.snipsnap.feeder.Feeder; 52 53 62 63 public class MetaWeblogHandler extends XmlRpcSupport implements MetaWeblogAPI { 64 public String API_PREFIX = "metaWeblog"; 65 66 public MetaWeblogHandler(AuthenticationService authenticationService) { 67 this.authenticationService = authenticationService; 68 } 69 70 public String getName() { 71 return API_PREFIX; 72 } 73 74 78 public String newPost(String appkey, 79 String blogid, 80 String username, 81 String password, 82 String body, 83 boolean publish) throws Exception { 84 Hashtable content = new Hashtable(1); 85 content.put("description", body); 86 return newPost(blogid, username, password, content, publish); 87 } 88 89 public boolean editPost(String appkey, 90 String postId, 91 String username, 92 String password, 93 String body, 94 boolean publish) throws Exception { 95 Hashtable content = new Hashtable(1); 96 content.put("description", body); 97 return editPost(postId, username, password, content, publish); 98 } 99 100 public Vector getUsersBlogs(String appkey, String username, String password) throws Exception { 101 User user = authenticate(username, password); 102 103 Hashtable blog = new Hashtable(3); 104 blog.put("url", Application.get().getConfiguration().getUrl()); 105 blog.put("blogid", "0"); 106 blog.put("blogName", Application.get().getConfiguration().getName()); 107 Vector vector = new Vector(1); 108 vector.add(blog); 109 return vector; 110 } 111 112 public Hashtable getPost(String appkeyIgnored, 113 String postId, 114 String username, 115 String password) throws Exception { 116 return getPost(postId, username, password); 117 } 118 119 public boolean deletePost(String appkey, 120 String postId, 121 String username, 122 String password, 123 boolean publish) throws Exception { 124 User user = authenticate(username, password); 125 Snip snip = SnipSpaceFactory.getInstance().load(postId); 126 SnipSpaceFactory.getInstance().remove(snip); 127 return true; 128 } 129 130 public Vector getRecentPosts(String ignoredAppkey, 131 String postId, 132 String username, 133 String password, 134 int numberOfPosts) throws Exception { 135 return getRecentPosts(postId, username, password, numberOfPosts); 136 } 137 138 139 140 144 145 public String newPost(String blogid, String username, String password, Hashtable postcontent, boolean publish) throws Exception { 146 User user = authenticate(username, password); 147 148 Blog blog = SnipSpaceFactory.getInstance().getBlog(blogid); 149 150 String title = (String ) postcontent.get("title"); 151 String content = (String ) postcontent.get("description"); 152 153 Snip snip = null; 154 if (null == title) { 155 snip = blog.post(content); 156 } else { 157 snip = blog.post(content, title); 158 } 159 160 return snip.getName(); 161 } 162 163 public Hashtable getPost(String postId, 164 String username, 165 String password) throws XmlRpcException { 166 User user = authenticate(username, password); 167 Snip snip = SnipSpaceFactory.getInstance().load(postId); 168 Hashtable post = new Hashtable(); 169 post.put("content", snip.getXMLContent()); 170 post.put("userid", snip.getOUser()); post.put("postid", snip.getName()); post.put("dateCreated", snip.getCTime()); 174 post.put("pubDate", snip.getCTime()); 175 post.put("title", snip.getTitle()); post.put("link", (snip.getLink() == null) ? "" : snip.getLink()); 178 post.put("description", snip.getContent()); 179 post.put("content", snip.getXMLContent()); post.put("guid", snip.getName()); 181 182 try { 183 post.put("comments", snip.getComments().getPostUrl()); 184 } catch (IOException e) { 185 } 189 190 return post; 191 } 192 193 public boolean editPost(String postId, String username, String password, Hashtable content, boolean publish) throws Exception { 194 User user = authenticate(username, password); 195 SnipSpace space = SnipSpaceFactory.getInstance(); 196 Snip snip = space.load(postId); 197 String title = (String ) content.get("title"); 198 String body = (String ) content.get("description"); 199 snip.setContent(BlogKit.getContent(title, body)); 201 space.systemStore(snip); 202 return true; 203 } 204 205 206 public Vector getRecentPosts(String blogid, 207 String username, 208 String password, 209 int numberOfPosts) throws Exception { 210 User user = authenticate(username, password); 211 212 Feeder feeder = new BlogFeeder(blogid); 213 List children = feeder.getFeed(); 214 215 Vector posts = new Vector(children.size()); 216 for (Iterator i = children.iterator(); i.hasNext() && numberOfPosts-- > 0;) { 217 Snip snip = (Snip) i.next(); 218 Hashtable data = new Hashtable(); 219 data.put("userid", snip.getCUser()); data.put("title", snip.getTitle()); data.put("link", (snip.getLink() == null) ? "" : snip.getLink()); data.put("dateCreated", snip.getCTime()); 227 data.put("pubDate", snip.getCTime()); 228 data.put("description", snip.getContent()); 229 data.put("content", snip.getXMLContent()); data.put("guid", snip.getName()); 232 try { 233 data.put("comments", snip.getComments().getPostUrl()); 234 } catch (IOException e) { 235 } 239 posts.add(data); 240 } 241 return posts; 242 } 243 244 public Hashtable newMediaObject(String blogid, 246 String username, 247 String password, 248 Hashtable content) throws Exception { 249 User user = authenticate(username, password); 250 System.out.println("new media object."); 251 252 byte[] data = (byte[]) content.get("bits"); 253 String fileName = (String ) content.get("name"); 254 String mime = (String ) content.get("key"); 255 String snipId = (String ) content.get("postid"); 256 System.out.println("the postid is: "+snipId); 257 258 SnipSpace space = SnipSpaceFactory.getInstance(); 259 Snip snip = space.load(snipId); 260 261 AttachmentStorage attachmentStorage = (AttachmentStorage) Components.getComponent(AttachmentStorage.class); 262 263 if (!Security.checkPermission(Permissions.ATTACH_TO_SNIP, user, snip)) { 264 throw new XmlRpcException(0, "Do not have write access to this snip"); 265 } 268 269 try { 271 if (data.length == 0) { Attachments attachments = snip.getAttachments(); 273 Attachment attachment = attachments.getAttachment(fileName); 274 if (null != attachment) { 275 attachmentStorage.delete(attachment); 276 attachments.removeAttachment(attachment); 277 } 278 SnipSpaceFactory.getInstance().store(snip); 280 Hashtable result = new Hashtable(1); 281 result.put("url", ""); 282 return result; 283 } 284 fileName = FileUploadServlet.getCanonicalFileName(fileName); 285 if (snipId != null && snipId.length() > 0 && 286 mime != null && mime.length() > 0) { 287 File relativeFileLocation = new File (snip.getName(), fileName); 288 Attachment attachment = 289 new Attachment(relativeFileLocation.getName(), 290 mime, 0, new Date(), 291 relativeFileLocation.getPath()); 292 OutputStream out = attachmentStorage.getOutputStream(attachment); 293 out.write(data); 294 out.flush(); 295 out.close(); 296 attachment.setSize(data.length); 298 snip.getAttachments().addAttachment(attachment); 299 300 SnipSpaceFactory.getInstance().store(snip); 301 Hashtable result = new Hashtable(1); 302 result.put("url", attachment.getLocation()); return result; 305 306 } 307 308 } catch (IOException e) { 309 throw new XmlRpcException(0, "Error writing file to disk: " + e); 311 } 312 return null; 313 } 314 315 316 } 317 | Popular Tags |