1 17 18 19 package org.apache.catalina.ant; 20 21 22 import java.io.BufferedOutputStream ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.net.HttpURLConnection ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 import org.apache.catalina.util.Base64; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Project; 31 32 33 42 43 public abstract class AbstractCatalinaTask extends BaseRedirectorHelperTask { 44 45 46 48 49 52 private static String CHARSET = "utf-8"; 53 54 55 57 58 61 protected String charset = "ISO-8859-1"; 62 63 public String getCharset() { 64 return (this.charset); 65 } 66 67 public void setCharset(String charset) { 68 this.charset = charset; 69 } 70 71 72 75 protected String password = null; 76 77 public String getPassword() { 78 return (this.password); 79 } 80 81 public void setPassword(String password) { 82 this.password = password; 83 } 84 85 86 89 protected String url = "http://localhost:8080/manager"; 90 91 public String getUrl() { 92 return (this.url); 93 } 94 95 public void setUrl(String url) { 96 this.url = url; 97 } 98 99 100 103 protected String username = null; 104 105 public String getUsername() { 106 return (this.username); 107 } 108 109 public void setUsername(String username) { 110 this.username = username; 111 } 112 113 114 116 117 124 public void execute() throws BuildException { 125 126 if ((username == null) || (password == null) || (url == null)) { 127 throw new BuildException 128 ("Must specify all of 'username', 'password', and 'url'"); 129 } 130 131 } 132 133 134 136 137 144 public void execute(String command) throws BuildException { 145 146 execute(command, null, null, -1); 147 148 } 149 150 151 163 public void execute(String command, InputStream istream, 164 String contentType, int contentLength) 165 throws BuildException { 166 167 URLConnection conn = null; 168 InputStreamReader reader = null; 169 try { 170 171 conn = (new URL (url + command)).openConnection(); 173 HttpURLConnection hconn = (HttpURLConnection ) conn; 174 175 hconn.setAllowUserInteraction(false); 177 hconn.setDoInput(true); 178 hconn.setUseCaches(false); 179 if (istream != null) { 180 hconn.setDoOutput(true); 181 hconn.setRequestMethod("PUT"); 182 if (contentType != null) { 183 hconn.setRequestProperty("Content-Type", contentType); 184 } 185 if (contentLength >= 0) { 186 hconn.setRequestProperty("Content-Length", 187 "" + contentLength); 188 } 189 } else { 190 hconn.setDoOutput(false); 191 hconn.setRequestMethod("GET"); 192 } 193 hconn.setRequestProperty("User-Agent", 194 "Catalina-Ant-Task/1.0"); 195 196 String input = username + ":" + password; 198 String output = new String (Base64.encode(input.getBytes())); 199 hconn.setRequestProperty("Authorization", 200 "Basic " + output); 201 202 hconn.connect(); 204 205 if (istream != null) { 207 BufferedOutputStream ostream = 208 new BufferedOutputStream (hconn.getOutputStream(), 1024); 209 byte buffer[] = new byte[1024]; 210 while (true) { 211 int n = istream.read(buffer); 212 if (n < 0) { 213 break; 214 } 215 ostream.write(buffer, 0, n); 216 } 217 ostream.flush(); 218 ostream.close(); 219 istream.close(); 220 } 221 222 reader = new InputStreamReader (hconn.getInputStream(), CHARSET); 224 StringBuffer buff = new StringBuffer (); 225 String error = null; 226 int msgPriority = Project.MSG_INFO; 227 boolean first = true; 228 while (true) { 229 int ch = reader.read(); 230 if (ch < 0) { 231 break; 232 } else if ((ch == '\r') || (ch == '\n')) { 233 if (buff.length() > 0) { 237 String line = buff.toString(); 238 buff.setLength(0); 239 if (first) { 240 if (!line.startsWith("OK -")) { 241 error = line; 242 msgPriority = Project.MSG_ERR; 243 } 244 first = false; 245 } 246 handleOutput(line, msgPriority); 247 } 248 } else { 249 buff.append((char) ch); 250 } 251 } 252 if (buff.length() > 0) { 253 handleOutput(buff.toString(), msgPriority); 254 } 255 if (error != null && isFailOnError()) { 256 throw new BuildException(error); 259 } 260 } catch (Throwable t) { 261 if (isFailOnError()) { 262 throw new BuildException(t); 263 } else { 264 handleErrorOutput(t.getMessage()); 265 } 266 } finally { 267 closeRedirector(); 268 if (reader != null) { 269 try { 270 reader.close(); 271 } catch (Throwable u) { 272 ; 273 } 274 reader = null; 275 } 276 if (istream != null) { 277 try { 278 istream.close(); 279 } catch (Throwable u) { 280 ; 281 } 282 istream = null; 283 } 284 } 285 286 } 287 288 289 } 290 | Popular Tags |