1 18 19 package org.apache.tools.ant.taskdefs.condition; 20 21 import java.net.HttpURLConnection ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.net.URLConnection ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.ProjectComponent; 28 29 35 public class Http extends ProjectComponent implements Condition { 36 private static final int ERROR_BEGINS = 400; 37 private String spec = null; 38 39 43 public void setUrl(String url) { 44 spec = url; 45 } 46 47 private int errorsBeginAt = ERROR_BEGINS; 48 49 54 public void setErrorsBeginAt(int errorsBeginAt) { 55 this.errorsBeginAt = errorsBeginAt; 56 } 57 58 62 public boolean eval() throws BuildException { 63 if (spec == null) { 64 throw new BuildException("No url specified in http condition"); 65 } 66 log("Checking for " + spec, Project.MSG_VERBOSE); 67 try { 68 URL url = new URL (spec); 69 try { 70 URLConnection conn = url.openConnection(); 71 if (conn instanceof HttpURLConnection ) { 72 HttpURLConnection http = (HttpURLConnection ) conn; 73 int code = http.getResponseCode(); 74 log("Result code for " + spec + " was " + code, 75 Project.MSG_VERBOSE); 76 if (code > 0 && code < errorsBeginAt) { 77 return true; 78 } 79 return false; 80 } 81 } catch (java.io.IOException e) { 82 return false; 83 } 84 } catch (MalformedURLException e) { 85 throw new BuildException("Badly formed URL: " + spec, e); 86 } 87 return true; 88 } 89 } 90 | Popular Tags |