| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NewPostTask |
|
| 1.6666666666666667;1.667 |
| 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.blogger; | |
| 33 | ||
| 34 | import java.util.Vector; | |
| 35 | ||
| 36 | import org.apache.tools.ant.BuildException; | |
| 37 | import org.apache.tools.ant.Task; | |
| 38 | import org.apache.xmlrpc.XmlRpcClient; | |
| 39 | ||
| 40 | /** | |
| 41 | * Ant task to post to a blog via the Blogger API. | |
| 42 | * | |
| 43 | * @author Simon Brown | |
| 44 | */ | |
| 45 | 0 | public class NewPostTask extends Task { |
| 46 | ||
| 47 | private static final String APP_KEY = "Pebble Blogger API Ant Task"; | |
| 48 | ||
| 49 | private String blogid; | |
| 50 | private String username; | |
| 51 | private String password; | |
| 52 | private String title; | |
| 53 | private String content; | |
| 54 | private String category; | |
| 55 | private String url; | |
| 56 | 0 | private String handler = "blogger"; |
| 57 | ||
| 58 | /** | |
| 59 | * Performs the work of this task. | |
| 60 | * | |
| 61 | * @throws BuildException if something goes wrong | |
| 62 | */ | |
| 63 | public void execute() throws BuildException { | |
| 64 | ||
| 65 | // create the content | |
| 66 | // - a <title> tag containing the title | |
| 67 | // - a <category> tag containing a comma separated list of categories | |
| 68 | // - the blog entry content | |
| 69 | 0 | StringBuffer buf = new StringBuffer(); |
| 70 | 0 | if (title != null && title.length() > 0) { |
| 71 | 0 | buf.append("<title>"); |
| 72 | 0 | buf.append(title); |
| 73 | 0 | buf.append("</title>"); |
| 74 | } | |
| 75 | 0 | if (category != null && category.length() > 0) { |
| 76 | 0 | buf.append("<category>"); |
| 77 | 0 | buf.append(category); |
| 78 | 0 | buf.append("</category>"); |
| 79 | } | |
| 80 | 0 | buf.append(content); |
| 81 | ||
| 82 | try { | |
| 83 | 0 | System.out.println("Calling " + handler + ".newPost at " + url); |
| 84 | 0 | System.out.println(" appkey=" + APP_KEY); |
| 85 | 0 | System.out.println(" blogid=" + blogid); |
| 86 | 0 | System.out.println(" username=" + username); |
| 87 | 0 | System.out.println(" password=********"); |
| 88 | 0 | System.out.println(" content=" + buf.toString()); |
| 89 | 0 | System.out.println(" publish=true"); |
| 90 | ||
| 91 | 0 | XmlRpcClient xmlrpc = new XmlRpcClient(url); |
| 92 | 0 | Vector params = new Vector(); |
| 93 | 0 | params.add(APP_KEY); |
| 94 | 0 | params.add(blogid); |
| 95 | 0 | params.add(username); |
| 96 | 0 | params.add(password); |
| 97 | 0 | params.add(buf.toString()); |
| 98 | 0 | params.add(Boolean.TRUE); |
| 99 | 0 | Object postId = xmlrpc.execute(handler + ".newPost", params); |
| 100 | ||
| 101 | 0 | System.out.println("New post id is " + postId); |
| 102 | ||
| 103 | 0 | } catch (Exception e) { |
| 104 | 0 | throw new BuildException(e); |
| 105 | 0 | } |
| 106 | 0 | } |
| 107 | ||
| 108 | /** | |
| 109 | * Sets the blog id. | |
| 110 | * | |
| 111 | * @param blogid a String | |
| 112 | */ | |
| 113 | public void setBlogid(String blogid) { | |
| 114 | 0 | this.blogid = blogid; |
| 115 | 0 | } |
| 116 | ||
| 117 | /** | |
| 118 | * Sets the XML-RPC username. | |
| 119 | * | |
| 120 | * @param username a String | |
| 121 | */ | |
| 122 | public void setUsername(String username) { | |
| 123 | 0 | this.username = username; |
| 124 | 0 | } |
| 125 | ||
| 126 | /** | |
| 127 | * Sets the XML-RPC password. | |
| 128 | * | |
| 129 | * @param password a String | |
| 130 | */ | |
| 131 | public void setPassword(String password) { | |
| 132 | 0 | this.password = password; |
| 133 | 0 | } |
| 134 | ||
| 135 | /** | |
| 136 | * Sets the title of the blog entry. | |
| 137 | * | |
| 138 | * @param title the blog entry title | |
| 139 | */ | |
| 140 | public void setTitle(String title) { | |
| 141 | 0 | this.title = title; |
| 142 | 0 | } |
| 143 | ||
| 144 | /** | |
| 145 | * Sets the blog entry content. | |
| 146 | * | |
| 147 | * @param content the blog entry content | |
| 148 | */ | |
| 149 | public void setContent(String content) { | |
| 150 | 0 | this.content = content; |
| 151 | 0 | } |
| 152 | ||
| 153 | /** | |
| 154 | * Sets the category to which the blog entry should be added to. | |
| 155 | * | |
| 156 | * @param category the category ID as a String | |
| 157 | */ | |
| 158 | public void setCategory(String category) { | |
| 159 | 0 | this.category = category; |
| 160 | 0 | } |
| 161 | ||
| 162 | /** | |
| 163 | * Sets the URL of the XML-RPC call. | |
| 164 | * | |
| 165 | * @param url the URL as a String | |
| 166 | */ | |
| 167 | public void setUrl(String url) { | |
| 168 | 0 | this.url = url; |
| 169 | 0 | } |
| 170 | ||
| 171 | /** | |
| 172 | * Sets the name of the XML-RPC Blogger API handler (e.g. "blogger"). | |
| 173 | * | |
| 174 | * @param handler the name of the handler as a String | |
| 175 | */ | |
| 176 | public void setHandler(String handler) { | |
| 177 | 0 | this.handler = handler; |
| 178 | 0 | } |
| 179 | ||
| 180 | } |