Improved Templates i.e.
[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 net.sourceforge.phpeclipse.wiki.internal.Configuration;
14
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;
19
20 public class MetaWeblog {
21
22   XmlRpcClient xmlrpc;
23
24   Configuration config;
25
26   public MetaWeblog(Configuration conf) throws MalformedURLException {
27     config = conf;
28     xmlrpc = new XmlRpcClient(config.getURL());
29   }
30
31   public String newPost(Configuration config, IFile file, String title, StringBuffer htmlBuffer) {
32     return newPost(config, file, title, htmlBuffer);
33   }
34
35   public String newPost(IFile file, String title, StringBuffer htmlBuffer, boolean publish) {
36     try {
37       //      XmlRpcClient xmlrpc = new XmlRpcClient(config.getUrl());
38
39       Hashtable message = new Hashtable();
40       message.put("title", "title");
41       message.put("description", htmlBuffer.toString());
42
43       Vector params = new Vector();
44       String guid = null;
45
46       params.add(config.getId());
47       params.add(config.getUser());
48       params.add(config.getPassword());
49       params.add(message);
50       if (publish) {
51         params.add(Boolean.TRUE); //publish=true
52       } else {
53         params.add(Boolean.FALSE); //publish=false
54       }
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();
61     }
62     return null;
63   }
64
65   /**
66    * Publish (or delete) the attachement.
67    * 
68    * @param snipId
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
72    *          location.
73    * @param delete
74    *          true if the attachment is in fact to be deleted
75    * @return the attachment info
76    * @throws TransferFilesException
77    */
78   public void publishAttachement(String snipId, String attachementFilename, boolean delete) throws TransferFilesException {
79     Vector params = new Vector();
80
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);
86     try {
87       if (attf.length() > 2 * 1000 * 1000) {
88         throw new TransferFilesException("File should not be close to 2MB. Currently it is " + attf.length() + " bytes.");
89       }
90       byte[] data;
91       if (delete) {
92         data = new byte[0];
93       } else {
94         data = new byte[(int) attf.length()];
95         InputStream in = new FileInputStream(attf);
96         int total = 0;
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);
101         }
102       }
103       message.put("bits", data);
104       message.put("name", attf.getName());
105       int index = attachementFilename.lastIndexOf('.');
106       if (index != (-1)) {
107         message.put("key", attachementFilename.substring(index+1, attachementFilename.length()).toLowerCase());
108       } else {
109         // assume png as default
110         message.put("key", "png");
111       }
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);
117     }
118     params.add(message);
119     String attURL;
120     try {
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);
127     }
128     //    return att.setUrl(attURL);
129   }
130
131   /**
132    * delete the entry from the blog server
133    * 
134    * @param entry
135    * @return
136    */
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);
145
146     Boolean result;
147     try {
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);
153     }
154
155     return result.booleanValue();
156   }
157
158   /**
159    * 
160    * @param entry
161    *          the entry that requires updating
162    * @return true if successful.
163    */
164   boolean updateEntry(BlogEntry entry, String title, StringBuffer htmlBuffer) throws TransferFilesException {
165     Vector params = new Vector(5);
166     Boolean result = null;
167
168     params.add(entry.getGuid());
169     params.add(config.getUser());
170     params.add(config.getPassword());
171
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()));
176     params.add(message);
177
178     params.add(Boolean.TRUE);
179
180     try {
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);
186     }
187     return (result == null) ? false : result.booleanValue();
188
189   }
190
191 }