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 net.sourceforge.phpeclipse.wiki.internal.Configuration;
15 import org.apache.xmlrpc.XmlRpcClient;
16 import org.apache.xmlrpc.XmlRpcException;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.jface.dialogs.MessageDialog;
20 public class MetaWeblog {
26 public MetaWeblog(Configuration conf) throws MalformedURLException {
28 xmlrpc = new XmlRpcClient(config.getURL());
31 public String newPost(Configuration config, IFile file, String title, StringBuffer htmlBuffer) {
32 return newPost(config, file, title, htmlBuffer);
35 public String newPost(IFile file, String title, StringBuffer htmlBuffer, boolean publish) {
37 // XmlRpcClient xmlrpc = new XmlRpcClient(config.getUrl());
39 Hashtable message = new Hashtable();
40 message.put("title", "title");
41 message.put("description", htmlBuffer.toString());
43 Vector params = new Vector();
46 params.add(config.getId());
47 params.add(config.getUser());
48 params.add(config.getPassword());
51 params.add(Boolean.TRUE); //publish=true
53 params.add(Boolean.FALSE); //publish=false
55 return (String) xmlrpc.execute("metaWeblog.newPost", params);
56 } catch (MalformedURLException e) {
57 MessageDialog.openError(null, "MalformedURLException: ", e.toString());
58 } catch (Exception e) {
59 MessageDialog.openError(null, "Exception: ", e.toString());
60 // e.printStackTrace();
66 * Publish (or delete) the attachement.
69 * the id of the snip to which the attachment belongs
70 * @param attachementFilename
71 * The blog attachement. If the publication is successful this will be updated to contain a reference to the publication
74 * true if the attachment is in fact to be deleted
75 * @return the attachment info
76 * @throws TransferFilesException
78 public void publishAttachement(String snipId, String attachementFilename, boolean delete) throws TransferFilesException {
79 Vector params = new Vector();
81 params.add(config.getId());
82 params.add(config.getUser());
83 params.add(config.getPassword());
84 Hashtable message = new Hashtable();
85 File attf = new File(attachementFilename);
87 if (attf.length() > 2 * 1000 * 1000) {
88 throw new TransferFilesException("File should not be close to 2MB. Currently it is " + attf.length() + " bytes.");
94 data = new byte[(int) attf.length()];
95 InputStream in = new FileInputStream(attf);
97 for (int read = 0; total + read < data.length && (read = in.read(data, read, data.length - read)) >= 0; total += read) {
98 } //read file into data
99 if (total != data.length) {
100 throw new TransferFilesException("Could not read all of " + attf);
103 message.put("bits", data);
104 message.put("name", attf.getName());
105 int index = attachementFilename.lastIndexOf('.');
107 message.put("key", attachementFilename.substring(index+1, attachementFilename.length()).toLowerCase());
109 // assume png as default
110 message.put("key", "png");
112 message.put("postid", snipId); //required for snipsnap.
113 } catch (FileNotFoundException e) {
114 throw new TransferFilesException("Could not find image " + attf, e);
115 } catch (IOException e) {
116 throw new TransferFilesException("Could not read data from file " + attf, e);
121 Hashtable res = (Hashtable) xmlrpc.execute("metaWeblog.newMediaObject", params);
122 attURL = (String) res.get("url");
123 } catch (XmlRpcException e) {
124 throw new TransferFilesException("problem in communication with server", e);
125 } catch (IOException e) {
126 throw new TransferFilesException("IO problem trying to communicate with server", e);
128 // return att.setUrl(attURL);
132 * delete the entry from the blog server
137 boolean deleteEntry(BlogEntry entry) throws TransferFilesException {
138 Vector params = new Vector(5);
139 // params.add(props.getProperty("blogid", "none")); //Should be the appkey, but what use is that?
140 params.add(config.getId());
141 params.add(entry.getGuid());
142 params.add(config.getUser());
143 params.add(config.getPassword());
144 params.add(Boolean.TRUE);
148 result = (Boolean) xmlrpc.execute("blogger.deletePost", params);
149 } catch (XmlRpcException e) {
150 throw new TransferFilesException("problem in communication with server", e);
151 } catch (IOException e) {
152 throw new TransferFilesException("IO problem trying to communicate with server", e);
155 return result.booleanValue();
161 * the entry that requires updating
162 * @return true if successful.
164 boolean updateEntry(BlogEntry entry, String title, StringBuffer htmlBuffer) throws TransferFilesException {
165 Vector params = new Vector(5);
166 Boolean result = null;
168 params.add(entry.getGuid());
169 params.add(config.getUser());
170 params.add(config.getPassword());
172 Hashtable message = new Hashtable(5);
173 message.put("title", title);
174 message.put("description", htmlBuffer.toString());
175 message.put("dateCreated", new Date(entry.getTime()));
178 params.add(Boolean.TRUE);
181 result = (Boolean) xmlrpc.execute("metaWeblog.editPost", params);
182 } catch (XmlRpcException e) {
183 throw new TransferFilesException("problem in communication with server", e);
184 } catch (IOException e) {
185 throw new TransferFilesException("IO problem trying to communicate with server", e);
187 return (result == null) ? false : result.booleanValue();