KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > ant > taskdefs > WebdavTask


1 // vi: set ts=3 sw=3:
2
/*
3  * $Header: /home/cvs/jakarta-slide/webdavclient/ant/src/java/org/apache/webdav/ant/taskdefs/WebdavTask.java,v 1.4.2.1 2004/08/15 13:01:15 luetzkendorf Exp $
4  * $Revision: 1.4.2.1 $
5  * $Date: 2004/08/15 13:01:15 $
6  * ========================================================================
7  * Copyright 2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ========================================================================
21  */

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 /**
35  * Base class of all WebDAV tasks. Provides common attributes and the creation
36  * of the HttpClient.
37  *
38  * <p>TODO enable proxies?
39  */

40 public abstract class WebdavTask extends Task {
41    private HttpURL url = null;
42    private String JavaDoc userid = "";
43    private String JavaDoc 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    /**
72     * Sets the username for authentication at the WebDAV server.
73     * @param userid
74     */

75    public void setUserid(String JavaDoc userid) {
76       this.userid = userid;
77    }
78    protected String JavaDoc getUserid() {
79       return this.userid;
80    }
81    /**
82     * Sets the password for authentication at the WebDAV server.
83     * @param password
84     */

85    public void setPassword(String JavaDoc password) {
86       this.password = password;
87    }
88    protected String JavaDoc getPassword() {
89       return this.password;
90    }
91    /**
92     * Set the base URL.
93     * @param url URL for the request.
94     */

95    public void setUrl(String JavaDoc url) {
96        try {
97           this.url = Utils.createHttpURL(url);
98           // remove double slashes in url like /DAV/files//document.txt
99
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 (HttpURL)this.url.clone(); // does not work, clone returns an URL
110
return this.url;
111    }
112    
113    public void setVerbose(boolean v) {
114       this.verbose = v;
115    }
116    /**
117     * Returns the INFO message level if the verbose attribute is set.
118     * @return Project.MSG_VERBOSE or Project.MSG_INFO
119     */

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 JavaDoc removeDoubleSlashes(String JavaDoc path) {
137       if (path.indexOf("//") == -1) return path;
138       
139       StringBuffer JavaDoc r = new StringBuffer JavaDoc(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