1 22 package org.apache.webdav.ant.taskdefs; 23 24 import java.io.IOException ; 25 import java.util.Iterator ; 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 43 public class Delete extends WebdavMatchingTask 44 { 45 private String locktoken = null; 46 private int count = 0; 47 48 public void execute() throws BuildException { 49 try { 50 if (!getFileSets().hasNext()) { 51 log("Deleting: " + getUrl(), Project.MSG_INFO); 53 delete(getUrl(), getUrl().getURI()); 54 } else { 55 log("Deleting at: " + getUrl(), ifVerbose()); 57 if (!getUrl().getPath().endsWith("/")) { 59 getUrl().setPath(getUrl().getPath() + "/"); 60 } 61 for(Iterator 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 e) { 73 throw Utils.makeBuildException("Can't delete!", e); 74 } 75 } 76 77 78 private void delete(HttpURL url, String logName) 79 throws IOException , 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 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 , HttpException 106 { 107 CollectionScanner scanner = 108 fileSet.getCollectionScanner(getProject(), getHttpClient(), getUrl()); 109 HttpURL baseUrl = scanner.getBaseURL(); 110 111 String [] 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 [] 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 token) { 124 this.locktoken = token; 125 if (!this.locktoken.startsWith("opaquelocktoken:")) { 126 throw new BuildException("Invalid locktoken: " + token); 127 } 128 } 129 } 130 | Popular Tags |