1 package net.sourceforge.phpeclipse.wiki.blog;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.MalformedURLException;
10 import java.util.Hashtable;
11 import java.util.Vector;
13 import org.apache.xmlrpc.XmlRpcClient;
14 import org.apache.xmlrpc.XmlRpcException;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.jface.dialogs.MessageDialog;
18 public class MetaWeblog {
24 public MetaWeblog(Configuration conf) throws MalformedURLException {
26 xmlrpc = new XmlRpcClient(config.getUrl());
29 public String newPost(Configuration config, IFile file, String title, StringBuffer htmlBuffer) {
30 return newPost(config, file, title, htmlBuffer);
33 public String newPost(IFile file, String title, StringBuffer htmlBuffer, boolean publish) {
35 // XmlRpcClient xmlrpc = new XmlRpcClient(config.getUrl());
37 Hashtable message = new Hashtable();
38 message.put("title", "title");
39 message.put("description", htmlBuffer.toString());
41 Vector params = new Vector();
44 params.add(config.getBlogID());
45 params.add(config.getUser());
46 params.add(config.getPassword());
49 params.add(Boolean.TRUE); //publish=true
51 params.add(Boolean.FALSE); //publish=false
53 return (String) xmlrpc.execute("metaWeblog.newPost", params);
54 } catch (MalformedURLException e) {
55 MessageDialog.openError(null, "MalformedURLException: ", e.toString());
56 } catch (Exception e) {
57 MessageDialog.openError(null, "Exception: ", e.toString());
58 // e.printStackTrace();
64 * Publish (or delete) the attachement.
67 * the id of the snip to which the attachment belongs
68 * @param attachementFilename
69 * The blog attachement. If the publication is successful this will be updated to contain a reference to the publication
72 * true if the attachment is in fact to be deleted
73 * @return the attachment info
74 * @throws TransferFilesException
76 public void publishAttachement(String snipId, String attachementFilename, boolean delete) throws TransferFilesException {
77 Vector params = new Vector();
79 params.add(config.getBlogID());
80 params.add(config.getUser());
81 params.add(config.getPassword());
82 Hashtable message = new Hashtable();
83 File attf = new File(attachementFilename);
85 if (attf.length() > 2 * 1000 * 1000) {
86 throw new TransferFilesException("File should not be close to 2MB. Currently it is " + attf.length() + " bytes.");
92 data = new byte[(int) attf.length()];
93 InputStream in = new FileInputStream(attf);
95 for (int read = 0; total + read < data.length && (read = in.read(data, read, data.length - read)) >= 0; total += read) {
96 } //read file into data
97 if (total != data.length) {
98 throw new TransferFilesException("Could not read all of " + attf);
101 message.put("bits", data);
102 message.put("name", attf.getName());
103 int index = attachementFilename.lastIndexOf('.');
105 message.put("key", attachementFilename.substring(index+1, attachementFilename.length()).toLowerCase());
107 // assume png as default
108 message.put("key", "png");
110 message.put("postid", snipId); //required for snipsnap.
111 } catch (FileNotFoundException e) {
112 throw new TransferFilesException("Could not find image " + attf, e);
113 } catch (IOException e) {
114 throw new TransferFilesException("Could not read data from file " + attf, e);
119 Hashtable res = (Hashtable) xmlrpc.execute("metaWeblog.newMediaObject", params);
120 attURL = (String) res.get("url");
121 } catch (XmlRpcException e) {
122 throw new TransferFilesException("problem in communication with server", e);
123 } catch (IOException e) {
124 throw new TransferFilesException("IO problem trying to communicate with server", e);
126 // return att.setUrl(attURL);
130 * delete the entry from the blog server
135 boolean deleteEntry(BlogEntry entry) throws TransferFilesException {
136 Vector params = new Vector(5);
137 // params.add(props.getProperty("blogid", "none")); //Should be the appkey, but what use is that?
138 params.add(config.getBlogID());
139 params.add(entry.getGuid());
140 params.add(config.getUser());
141 params.add(config.getPassword());
142 params.add(Boolean.TRUE);
146 result = (Boolean) xmlrpc.execute("blogger.deletePost", params);
147 } catch (XmlRpcException e) {
148 throw new TransferFilesException("problem in communication with server", e);
149 } catch (IOException e) {
150 throw new TransferFilesException("IO problem trying to communicate with server", e);
153 return result.booleanValue();
159 * the entry that requires updating
160 * @return true if successful.
162 boolean updateEntry(BlogEntry entry, String title, StringBuffer htmlBuffer) throws TransferFilesException {
163 Vector params = new Vector(5);
164 Boolean result = null;
166 params.add(entry.getGuid());
167 params.add(config.getUser());
168 params.add(config.getPassword());
170 Hashtable message = new Hashtable(5);
171 message.put("title", title);
172 message.put("description", htmlBuffer.toString());
173 message.put("dateCreated", new Date(entry.getTime()));
176 params.add(Boolean.TRUE);
179 result = (Boolean) xmlrpc.execute("metaWeblog.editPost", params);
180 } catch (XmlRpcException e) {
181 throw new TransferFilesException("problem in communication with server", e);
182 } catch (IOException e) {
183 throw new TransferFilesException("IO problem trying to communicate with server", e);
185 return (result == null) ? false : result.booleanValue();