initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / blog / MetaWeblog.java
1 package net.sourceforge.phpeclipse.wiki.blog;
2
3 import java.io.File;
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;
9 import java.util.Date;
10 import java.util.Hashtable;
11 import java.util.Vector;
12
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;
17
18 public class MetaWeblog {
19
20   XmlRpcClient xmlrpc;
21
22   Configuration config;
23
24   public MetaWeblog(Configuration conf) throws MalformedURLException {
25     config = conf;
26     xmlrpc = new XmlRpcClient(config.getUrl());
27   }
28
29   public String newPost(Configuration config, IFile file, String title, StringBuffer htmlBuffer) {
30     return newPost(config, file, title, htmlBuffer);
31   }
32
33   public String newPost(IFile file, String title, StringBuffer htmlBuffer, boolean publish) {
34     try {
35       //      XmlRpcClient xmlrpc = new XmlRpcClient(config.getUrl());
36
37       Hashtable message = new Hashtable();
38       message.put("title", "title");
39       message.put("description", htmlBuffer.toString());
40
41       Vector params = new Vector();
42       String guid = null;
43
44       params.add(config.getBlogID());
45       params.add(config.getUser());
46       params.add(config.getPassword());
47       params.add(message);
48       if (publish) {
49         params.add(Boolean.TRUE); //publish=true
50       } else {
51         params.add(Boolean.FALSE); //publish=false
52       }
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();
59     }
60     return null;
61   }
62
63   /**
64    * Publish (or delete) the attachement.
65    * 
66    * @param snipId
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
70    *          location.
71    * @param delete
72    *          true if the attachment is in fact to be deleted
73    * @return the attachment info
74    * @throws TransferFilesException
75    */
76   public void publishAttachement(String snipId, String attachementFilename, boolean delete) throws TransferFilesException {
77     Vector params = new Vector();
78
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);
84     try {
85       if (attf.length() > 2 * 1000 * 1000) {
86         throw new TransferFilesException("File should not be close to 2MB. Currently it is " + attf.length() + " bytes.");
87       }
88       byte[] data;
89       if (delete) {
90         data = new byte[0];
91       } else {
92         data = new byte[(int) attf.length()];
93         InputStream in = new FileInputStream(attf);
94         int total = 0;
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);
99         }
100       }
101       message.put("bits", data);
102       message.put("name", attf.getName());
103       int index = attachementFilename.lastIndexOf('.');
104       if (index != (-1)) {
105         message.put("key", attachementFilename.substring(index+1, attachementFilename.length()).toLowerCase());
106       } else {
107         // assume png as default
108         message.put("key", "png");
109       }
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);
115     }
116     params.add(message);
117     String attURL;
118     try {
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);
125     }
126     //    return att.setUrl(attURL);
127   }
128
129   /**
130    * delete the entry from the blog server
131    * 
132    * @param entry
133    * @return
134    */
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);
143
144     Boolean result;
145     try {
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);
151     }
152
153     return result.booleanValue();
154   }
155
156   /**
157    * 
158    * @param entry
159    *          the entry that requires updating
160    * @return true if successful.
161    */
162   boolean updateEntry(BlogEntry entry, String title, StringBuffer htmlBuffer) throws TransferFilesException {
163     Vector params = new Vector(5);
164     Boolean result = null;
165
166     params.add(entry.getGuid());
167     params.add(config.getUser());
168     params.add(config.getPassword());
169
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()));
174     params.add(message);
175
176     params.add(Boolean.TRUE);
177
178     try {
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);
184     }
185     return (result == null) ? false : result.booleanValue();
186
187   }
188
189 }