KickJava   Java API By Example, From Geeks To Geeks.

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


1 // vi: set ts=3 sw=3:
2
/*
3  * $Header: /home/cvs/jakarta-slide/webdavclient/ant/src/java/org/apache/webdav/ant/taskdefs/Delete.java,v 1.5.2.2 2004/08/22 10:36:47 luetzkendorf Exp $
4  * $Revision: 1.5.2.2 $
5  * $Date: 2004/08/22 10:36:47 $
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 java.io.IOException JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.commons.httpclient.HttpException;
28 import org.apache.commons.httpclient.HttpURL;
29
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Project;
32 import org.apache.webdav.ant.CollectionScanner;
33 import org.apache.webdav.ant.Utils;
34 import org.apache.webdav.ant.WebdavFileSet;
35 import org.apache.webdav.lib.methods.DeleteMethod;
36 import org.apache.webdav.lib.util.WebdavStatus;
37
38 /**
39  * WebDAV task for deleting resources and collections.
40  *
41  * @see <a HREF="../doc-files/tasks.htm#davdelete">Task documentation</a>
42  */

43 public class Delete extends WebdavMatchingTask
44 {
45    private String JavaDoc locktoken = null;
46    private int count = 0;
47    
48    public void execute() throws BuildException {
49       try {
50          if (!getFileSets().hasNext()) {
51             // delete the resource given by url
52
log("Deleting: " + getUrl(), Project.MSG_INFO);
53             delete(getUrl(), getUrl().getURI());
54          } else {
55             // delete all resources in file sets
56
log("Deleting at: " + getUrl(), ifVerbose());
57             // URL must be a collection
58
if (!getUrl().getPath().endsWith("/")) {
59                getUrl().setPath(getUrl().getPath() + "/");
60             }
61             for(Iterator JavaDoc i = getFileSets(); i.hasNext(); ) {
62                deleteFileset((WebdavFileSet)i.next());
63             }
64             log("Deleted " + this.count
65                   + (this.count == 1 ? " resource" : " resources")
66                   + " from " + getUrl(),
67                   this.count > 0
68                      ? Project.MSG_INFO
69                      : ifVerbose());
70          }
71       }
72       catch (IOException JavaDoc e) {
73          throw Utils.makeBuildException("Can't delete!", e);
74       }
75    }
76    
77    
78    private void delete(HttpURL url, String JavaDoc logName)
79       throws IOException JavaDoc, HttpException
80    {
81       validate();
82       log("Deleting " + logName, ifVerbose());
83       DeleteMethod delete = new DeleteMethod(url.getURI());
84       delete.setFollowRedirects(true);
85       if (this.locktoken != null) {
86          Utils.generateIfHeader(delete, this.locktoken);
87       }
88       int status = getHttpClient().executeMethod(delete);
89       
90       switch (status) {
91          case WebdavStatus.SC_OK:
92          case WebdavStatus.SC_NO_CONTENT:
93          case WebdavStatus.SC_NOT_FOUND:
94             // ok
95
this.count++;
96             break;
97          default:
98             HttpException ex = new HttpException();
99             ex.setReasonCode(status);
100             throw ex;
101       }
102    }
103    
104    private void deleteFileset(WebdavFileSet fileSet)
105       throws IOException JavaDoc, HttpException
106    {
107       CollectionScanner scanner =
108          fileSet.getCollectionScanner(getProject(), getHttpClient(), getUrl());
109       HttpURL baseUrl = scanner.getBaseURL();
110      
111       String JavaDoc[] files = scanner.getIncludedFiles();
112       for (int i = 0; i < files.length; i++) {
113          HttpURL url = Utils.createHttpURL(baseUrl, files[i]);
114          delete(url, files[i]);
115       }
116       String JavaDoc[] colls = scanner.getIncludedDirectories();
117       for (int i = 0; i < colls.length; i++) {
118          HttpURL url = Utils.createHttpURL(baseUrl, colls[i]);
119          delete(url, colls[i]);
120       }
121    }
122    
123    public void setLocktoken(String JavaDoc token) {
124       this.locktoken = token;
125       if (!this.locktoken.startsWith("opaquelocktoken:")) {
126          throw new BuildException("Invalid locktoken: " + token);
127       }
128    }
129 }
130
Popular Tags