| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NewMediaObjectTask |
|
| 1.2222222222222223;1.222 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2011, Simon Brown | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * - Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * - Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in | |
| 13 | * the documentation and/or other materials provided with the | |
| 14 | * distribution. | |
| 15 | * | |
| 16 | * - Neither the name of Pebble nor the names of its contributors may | |
| 17 | * be used to endorse or promote products derived from this software | |
| 18 | * without specific prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 | * POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | package net.sourceforge.pebble.ant.metaweblog; | |
| 33 | ||
| 34 | import java.io.File; | |
| 35 | import java.io.FileInputStream; | |
| 36 | import java.io.InputStream; | |
| 37 | import java.util.Hashtable; | |
| 38 | import java.util.Vector; | |
| 39 | ||
| 40 | import org.apache.tools.ant.BuildException; | |
| 41 | import org.apache.tools.ant.Task; | |
| 42 | import org.apache.xmlrpc.XmlRpcClient; | |
| 43 | ||
| 44 | /** | |
| 45 | * Ant task to post a file to a blog via the MetaWeblog API. | |
| 46 | * | |
| 47 | * @author Simon Brown | |
| 48 | */ | |
| 49 | 0 | public class NewMediaObjectTask extends Task { |
| 50 | ||
| 51 | private String blogid; | |
| 52 | private String username; | |
| 53 | private String password; | |
| 54 | private String src; | |
| 55 | private String dest; | |
| 56 | private String url; | |
| 57 | 0 | private String handler = "metaWeblog"; |
| 58 | ||
| 59 | /** | |
| 60 | * Performs the work of this task. | |
| 61 | * | |
| 62 | * @throws org.apache.tools.ant.BuildException if something goes wrong | |
| 63 | */ | |
| 64 | public void execute() throws BuildException { | |
| 65 | ||
| 66 | try { | |
| 67 | 0 | System.out.println("Calling " + handler + ".newMediaObject at " + url); |
| 68 | 0 | System.out.println(" blogid=" + blogid); |
| 69 | 0 | System.out.println(" username=" + username); |
| 70 | 0 | System.out.println(" password=********"); |
| 71 | 0 | System.out.println(" name=" + dest); |
| 72 | 0 | System.out.println(" type=" + ""); |
| 73 | ||
| 74 | 0 | XmlRpcClient xmlrpc = new XmlRpcClient(url); |
| 75 | 0 | Vector params = new Vector(); |
| 76 | 0 | params.add(blogid); |
| 77 | 0 | params.add(username); |
| 78 | 0 | params.add(password); |
| 79 | ||
| 80 | 0 | Hashtable struct = new Hashtable(); |
| 81 | 0 | struct.put("name", dest); |
| 82 | 0 | struct.put("type", ""); |
| 83 | 0 | struct.put("bits", readFile()); |
| 84 | ||
| 85 | 0 | params.add(struct); |
| 86 | ||
| 87 | 0 | struct = (Hashtable)xmlrpc.execute(handler + ".newMediaObject", params); |
| 88 | ||
| 89 | 0 | System.out.println("URL for media object is " + struct.get("url")); |
| 90 | ||
| 91 | 0 | } catch (Exception e) { |
| 92 | 0 | throw new BuildException(e); |
| 93 | 0 | } |
| 94 | 0 | } |
| 95 | ||
| 96 | private byte[] readFile() throws Exception { | |
| 97 | byte bytes[]; | |
| 98 | 0 | File file = new File(src); |
| 99 | 0 | InputStream in = new FileInputStream(src); |
| 100 | 0 | bytes = new byte[(int)file.length()]; |
| 101 | 0 | in.read(bytes); |
| 102 | 0 | in.close(); |
| 103 | ||
| 104 | 0 | return bytes; |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * Sets the blog id. | |
| 109 | * | |
| 110 | * @param blogid a String | |
| 111 | */ | |
| 112 | public void setBlogid(String blogid) { | |
| 113 | 0 | this.blogid = blogid; |
| 114 | 0 | } |
| 115 | ||
| 116 | /** | |
| 117 | * Sets the XML-RPC username. | |
| 118 | * | |
| 119 | * @param username a String | |
| 120 | */ | |
| 121 | public void setUsername(String username) { | |
| 122 | 0 | this.username = username; |
| 123 | 0 | } |
| 124 | ||
| 125 | /** | |
| 126 | * Sets the XML-RPC password. | |
| 127 | * | |
| 128 | * @param password a String | |
| 129 | */ | |
| 130 | public void setPassword(String password) { | |
| 131 | 0 | this.password = password; |
| 132 | 0 | } |
| 133 | ||
| 134 | /** | |
| 135 | * Sets the URL of the XML-RPC call. | |
| 136 | * | |
| 137 | * @param url the URL as a String | |
| 138 | */ | |
| 139 | public void setUrl(String url) { | |
| 140 | 0 | this.url = url; |
| 141 | 0 | } |
| 142 | ||
| 143 | /** | |
| 144 | * Sets the name of the XML-RPC Blogger API handler (e.g. "blogger"). | |
| 145 | * | |
| 146 | * @param handler the name of the handler as a String | |
| 147 | */ | |
| 148 | public void setHandler(String handler) { | |
| 149 | 0 | this.handler = handler; |
| 150 | 0 | } |
| 151 | ||
| 152 | /** | |
| 153 | * Sets the source file. | |
| 154 | * | |
| 155 | * @param src the source as a String | |
| 156 | */ | |
| 157 | public void setSrc(String src) { | |
| 158 | 0 | this.src = src; |
| 159 | 0 | } |
| 160 | ||
| 161 | /** | |
| 162 | * Sets the destination file. | |
| 163 | * | |
| 164 | * @param dest the destination as a String | |
| 165 | */ | |
| 166 | public void setDest(String dest) { | |
| 167 | 0 | this.dest = dest; |
| 168 | 0 | } |
| 169 | ||
| 170 | } |