initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / blog / MetaWeblog.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/blog/MetaWeblog.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/blog/MetaWeblog.java
new file mode 100644 (file)
index 0000000..07de445
--- /dev/null
@@ -0,0 +1,189 @@
+package net.sourceforge.phpeclipse.wiki.blog;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.util.Date;
+import java.util.Hashtable;
+import java.util.Vector;
+
+import org.apache.xmlrpc.XmlRpcClient;
+import org.apache.xmlrpc.XmlRpcException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.jface.dialogs.MessageDialog;
+
+public class MetaWeblog {
+
+  XmlRpcClient xmlrpc;
+
+  Configuration config;
+
+  public MetaWeblog(Configuration conf) throws MalformedURLException {
+    config = conf;
+    xmlrpc = new XmlRpcClient(config.getUrl());
+  }
+
+  public String newPost(Configuration config, IFile file, String title, StringBuffer htmlBuffer) {
+    return newPost(config, file, title, htmlBuffer);
+  }
+
+  public String newPost(IFile file, String title, StringBuffer htmlBuffer, boolean publish) {
+    try {
+      //      XmlRpcClient xmlrpc = new XmlRpcClient(config.getUrl());
+
+      Hashtable message = new Hashtable();
+      message.put("title", "title");
+      message.put("description", htmlBuffer.toString());
+
+      Vector params = new Vector();
+      String guid = null;
+
+      params.add(config.getBlogID());
+      params.add(config.getUser());
+      params.add(config.getPassword());
+      params.add(message);
+      if (publish) {
+        params.add(Boolean.TRUE); //publish=true
+      } else {
+        params.add(Boolean.FALSE); //publish=false
+      }
+      return (String) xmlrpc.execute("metaWeblog.newPost", params);
+    } catch (MalformedURLException e) {
+      MessageDialog.openError(null, "MalformedURLException: ", e.toString());
+    } catch (Exception e) {
+      MessageDialog.openError(null, "Exception: ", e.toString());
+      //      e.printStackTrace();
+    }
+    return null;
+  }
+
+  /**
+   * Publish (or delete) the attachement.
+   * 
+   * @param snipId
+   *          the id of the snip to which the attachment belongs
+   * @param attachementFilename
+   *          The blog attachement. If the publication is successful this will be updated to contain a reference to the publication
+   *          location.
+   * @param delete
+   *          true if the attachment is in fact to be deleted
+   * @return the attachment info
+   * @throws TransferFilesException
+   */
+  public void publishAttachement(String snipId, String attachementFilename, boolean delete) throws TransferFilesException {
+    Vector params = new Vector();
+
+    params.add(config.getBlogID());
+    params.add(config.getUser());
+    params.add(config.getPassword());
+    Hashtable message = new Hashtable();
+    File attf = new File(attachementFilename);
+    try {
+      if (attf.length() > 2 * 1000 * 1000) {
+        throw new TransferFilesException("File should not be close to 2MB. Currently it is " + attf.length() + " bytes.");
+      }
+      byte[] data;
+      if (delete) {
+        data = new byte[0];
+      } else {
+        data = new byte[(int) attf.length()];
+        InputStream in = new FileInputStream(attf);
+        int total = 0;
+        for (int read = 0; total + read < data.length && (read = in.read(data, read, data.length - read)) >= 0; total += read) {
+        } //read file into data
+        if (total != data.length) {
+          throw new TransferFilesException("Could not read all of " + attf);
+        }
+      }
+      message.put("bits", data);
+      message.put("name", attf.getName());
+      int index = attachementFilename.lastIndexOf('.');
+      if (index != (-1)) {
+        message.put("key", attachementFilename.substring(index+1, attachementFilename.length()).toLowerCase());
+      } else {
+        // assume png as default
+        message.put("key", "png");
+      }
+      message.put("postid", snipId); //required for snipsnap.
+    } catch (FileNotFoundException e) {
+      throw new TransferFilesException("Could not find image " + attf, e);
+    } catch (IOException e) {
+      throw new TransferFilesException("Could not read data from file " + attf, e);
+    }
+    params.add(message);
+    String attURL;
+    try {
+      Hashtable res = (Hashtable) xmlrpc.execute("metaWeblog.newMediaObject", params);
+      attURL = (String) res.get("url");
+    } catch (XmlRpcException e) {
+      throw new TransferFilesException("problem in communication with server", e);
+    } catch (IOException e) {
+      throw new TransferFilesException("IO problem trying to communicate with server", e);
+    }
+    //    return att.setUrl(attURL);
+  }
+
+  /**
+   * delete the entry from the blog server
+   * 
+   * @param entry
+   * @return
+   */
+  boolean deleteEntry(BlogEntry entry) throws TransferFilesException {
+    Vector params = new Vector(5);
+    //    params.add(props.getProperty("blogid", "none")); //Should be the appkey, but what use is that?
+    params.add(config.getBlogID());
+    params.add(entry.getGuid());
+    params.add(config.getUser());
+    params.add(config.getPassword());
+    params.add(Boolean.TRUE);
+
+    Boolean result;
+    try {
+      result = (Boolean) xmlrpc.execute("blogger.deletePost", params);
+    } catch (XmlRpcException e) {
+      throw new TransferFilesException("problem in communication with server", e);
+    } catch (IOException e) {
+      throw new TransferFilesException("IO problem trying to communicate with server", e);
+    }
+
+    return result.booleanValue();
+  }
+
+  /**
+   * 
+   * @param entry
+   *          the entry that requires updating
+   * @return true if successful.
+   */
+  boolean updateEntry(BlogEntry entry, String title, StringBuffer htmlBuffer) throws TransferFilesException {
+    Vector params = new Vector(5);
+    Boolean result = null;
+
+    params.add(entry.getGuid());
+    params.add(config.getUser());
+    params.add(config.getPassword());
+
+    Hashtable message = new Hashtable(5);
+    message.put("title", title);
+    message.put("description", htmlBuffer.toString());
+    message.put("dateCreated", new Date(entry.getTime()));
+    params.add(message);
+
+    params.add(Boolean.TRUE);
+
+    try {
+      result = (Boolean) xmlrpc.execute("metaWeblog.editPost", params);
+    } catch (XmlRpcException e) {
+      throw new TransferFilesException("problem in communication with server", e);
+    } catch (IOException e) {
+      throw new TransferFilesException("IO problem trying to communicate with server", e);
+    }
+    return (result == null) ? false : result.booleanValue();
+
+  }
+
+}
\ No newline at end of file