| 1 22 package org.apache.webdav.ant.taskdefs; 23 24 import org.apache.commons.httpclient.HttpClient; 25 import org.apache.commons.httpclient.HttpURL; 26 import org.apache.commons.httpclient.URIException; 27 import org.apache.commons.httpclient.UsernamePasswordCredentials; 28 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.Task; 32 import org.apache.webdav.ant.Utils; 33 34 40 public abstract class WebdavTask extends Task { 41 private HttpURL url = null; 42 private String userid = ""; 43 private String password = ""; 44 private HttpClient httpClient = null; 45 private boolean verbose = false; 46 47 48 protected HttpClient getHttpClient() { 49 try { 50 if (this.httpClient == null) { 51 this.httpClient = new HttpClient(); 52 if (this.userid.length() > 0) { 53 this.httpClient.getState().setCredentials( 54 null, 55 this.url.getHost(), 56 new UsernamePasswordCredentials(this.userid, this.password)); 57 } 58 } 59 return this.httpClient; 60 } catch (URIException e) { 61 throw new BuildException("Can't create HttpClient.", e); 62 } 63 } 64 65 protected void validate() { 66 if (this.url == null) { 67 throw new BuildException("Required attribute url missing!"); 68 } 69 } 70 71 75 public void setUserid(String userid) { 76 this.userid = userid; 77 } 78 protected String getUserid() { 79 return this.userid; 80 } 81 85 public void setPassword(String password) { 86 this.password = password; 87 } 88 protected String getPassword() { 89 return this.password; 90 } 91 95 public void setUrl(String url) { 96 try { 97 this.url = Utils.createHttpURL(url); 98 this.url.setPath(removeDoubleSlashes(this.url.getPath())); 100 } catch (URIException e) { 101 throw new BuildException("Invalid uri!", e); 102 } 103 } 104 105 protected void setUrl(HttpURL url) { 106 this.url = url; 107 } 108 protected HttpURL getUrl() { 109 return this.url; 111 } 112 113 public void setVerbose(boolean v) { 114 this.verbose = v; 115 } 116 120 protected int ifVerbose() { 121 return this.verbose ? Project.MSG_INFO : Project.MSG_VERBOSE; 122 } 123 124 public static HttpURL assureCollectionUrl(HttpURL url) 125 throws URIException 126 { 127 if (url.getPath().endsWith("/")) { 128 return url; 129 } else { 130 HttpURL coll = Utils.createHttpURL(url, ""); 131 coll.setPath(url.getPath() + "/"); 132 return coll; 133 } 134 } 135 136 static String removeDoubleSlashes(String path) { 137 if (path.indexOf("//") == -1) return path; 138 139 StringBuffer r = new StringBuffer (path.length()); 140 for(int i = 0, l = path.length(); i < l; i++) { 141 if (path.charAt(i) == '/') { 142 if (!(i > 0 && path.charAt(i-1) == '/')) { 143 r.append('/'); 144 } 145 } else { 146 r.append(path.charAt(i)); 147 } 148 } 149 return r.toString(); 150 } 151 } 152 | Popular Tags |