1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.Task; 24 import org.apache.tools.ant.util.FileUtils; 25 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.PrintStream ; 31 import java.net.HttpURLConnection ; 32 import java.net.URL ; 33 import java.net.URLConnection ; 34 import java.util.Date ; 35 36 46 public class Get extends Task { 47 48 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 49 50 private URL source; private File dest; private boolean verbose = false; 53 private boolean useTimestamp = false; private boolean ignoreErrors = false; 55 private String uname = null; 56 private String pword = null; 57 58 59 60 65 public void execute() throws BuildException { 66 67 int logLevel = Project.MSG_INFO; 69 DownloadProgress progress = null; 70 if (verbose) { 71 progress = new VerboseProgress(System.out); 72 } 73 74 try { 76 doGet(logLevel, progress); 77 } catch (IOException ioe) { 78 log("Error getting " + source + " to " + dest); 79 if (!ignoreErrors) { 80 throw new BuildException(ioe, getLocation()); 81 } 82 } 83 } 84 85 98 public boolean doGet(int logLevel, DownloadProgress progress) 99 throws IOException { 100 if (source == null) { 101 throw new BuildException("src attribute is required", getLocation()); 102 } 103 104 if (dest == null) { 105 throw new BuildException("dest attribute is required", getLocation()); 106 } 107 108 if (dest.exists() && dest.isDirectory()) { 109 throw new BuildException("The specified destination is a directory", 110 getLocation()); 111 } 112 113 if (dest.exists() && !dest.canWrite()) { 114 throw new BuildException("Can't write to " + dest.getAbsolutePath(), 115 getLocation()); 116 } 117 if (progress == null) { 119 progress = new NullProgress(); 120 } 121 log("Getting: " + source, logLevel); 122 log("To: " + dest.getAbsolutePath(), logLevel); 123 124 long timestamp = 0; 126 127 boolean hasTimestamp = false; 128 if (useTimestamp && dest.exists()) { 129 timestamp = dest.lastModified(); 130 if (verbose) { 131 Date t = new Date (timestamp); 132 log("local file date : " + t.toString(), logLevel); 133 } 134 hasTimestamp = true; 135 } 136 137 URLConnection connection = source.openConnection(); 139 if (hasTimestamp) { 142 connection.setIfModifiedSince(timestamp); 143 } 144 if (uname != null || pword != null) { 146 String up = uname + ":" + pword; 147 String encoding; 148 Base64Converter encoder = new Base64Converter(); 152 encoding = encoder.encode(up.getBytes()); 153 connection.setRequestProperty ("Authorization", 154 "Basic " + encoding); 155 } 156 157 connection.connect(); 159 if (connection instanceof HttpURLConnection ) { 161 HttpURLConnection httpConnection 162 = (HttpURLConnection ) connection; 163 long lastModified = httpConnection.getLastModified(); 164 if (httpConnection.getResponseCode() 165 == HttpURLConnection.HTTP_NOT_MODIFIED 166 || (lastModified != 0 && hasTimestamp 167 && timestamp >= lastModified)) { 168 log("Not modified - so not downloaded", logLevel); 173 return false; 174 } 175 if (httpConnection.getResponseCode() 177 == HttpURLConnection.HTTP_UNAUTHORIZED) { 178 String message = "HTTP Authorization failure"; 179 if (ignoreErrors) { 180 log(message, logLevel); 181 return false; 182 } else { 183 throw new BuildException(message); 184 } 185 } 186 187 } 188 189 195 InputStream is = null; 196 for (int i = 0; i < 3; i++) { 197 try { 201 is = connection.getInputStream(); 202 break; 203 } catch (IOException ex) { 204 log("Error opening connection " + ex, logLevel); 205 } 206 } 207 if (is == null) { 208 log("Can't get " + source + " to " + dest, logLevel); 209 if (ignoreErrors) { 210 return false; 211 } 212 throw new BuildException("Can't get " + source + " to " + dest, 213 getLocation()); 214 } 215 216 FileOutputStream fos = new FileOutputStream (dest); 217 progress.beginDownload(); 218 boolean finished = false; 219 try { 220 byte[] buffer = new byte[100 * 1024]; 221 int length; 222 while ((length = is.read(buffer)) >= 0) { 223 fos.write(buffer, 0, length); 224 progress.onTick(); 225 } 226 finished = true; 227 } finally { 228 FileUtils.close(fos); 229 FileUtils.close(is); 230 231 if (!finished) { 235 dest.delete(); 236 } 237 } 238 progress.endDownload(); 239 240 if (useTimestamp) { 244 long remoteTimestamp = connection.getLastModified(); 245 if (verbose) { 246 Date t = new Date (remoteTimestamp); 247 log("last modified = " + t.toString() 248 + ((remoteTimestamp == 0) 249 ? " - using current time instead" 250 : ""), logLevel); 251 } 252 if (remoteTimestamp != 0) { 253 FILE_UTILS.setFileLastModified(dest, remoteTimestamp); 254 } 255 } 256 257 return true; 259 } 260 261 262 267 public void setSrc(URL u) { 268 this.source = u; 269 } 270 271 276 public void setDest(File dest) { 277 this.dest = dest; 278 } 279 280 285 public void setVerbose(boolean v) { 286 verbose = v; 287 } 288 289 294 public void setIgnoreErrors(boolean v) { 295 ignoreErrors = v; 296 } 297 298 316 public void setUseTimestamp(boolean v) { 317 useTimestamp = v; 318 } 319 320 321 326 public void setUsername(String u) { 327 this.uname = u; 328 } 329 330 335 public void setPassword(String p) { 336 this.pword = p; 337 } 338 339 342 protected static class Base64Converter 343 extends org.apache.tools.ant.util.Base64Converter { 344 } 345 346 350 public interface DownloadProgress { 351 354 void beginDownload(); 355 356 360 void onTick(); 361 362 365 void endDownload(); 366 } 367 368 371 public static class NullProgress implements DownloadProgress { 372 373 376 public void beginDownload() { 377 378 } 379 380 384 public void onTick() { 385 } 386 387 390 public void endDownload() { 391 392 } 393 } 394 395 398 public static class VerboseProgress implements DownloadProgress { 399 private int dots = 0; 400 PrintStream out; 402 404 408 public VerboseProgress(PrintStream out) { 409 this.out = out; 410 } 411 412 415 public void beginDownload() { 416 dots = 0; 417 } 418 419 423 public void onTick() { 424 out.print("."); 425 if (dots++ > 50) { 426 out.flush(); 427 dots = 0; 428 } 429 } 430 431 434 public void endDownload() { 435 out.println(); 436 out.flush(); 437 } 438 } 439 440 } 441 | Popular Tags |