1 22 package org.apache.webdav.ant.taskdefs; 23 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.commons.httpclient.HttpException; 31 import org.apache.commons.httpclient.HttpURL; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 import org.apache.webdav.ant.CollectionScanner; 36 import org.apache.webdav.ant.Utils; 37 import org.apache.webdav.ant.WebdavFileSet; 38 import org.apache.webdav.lib.Constants; 39 import org.apache.webdav.lib.ResponseEntity; 40 import org.apache.webdav.lib.methods.PropPatchMethod; 41 import org.apache.webdav.lib.util.WebdavStatus; 42 43 48 public class Proppatch extends WebdavMatchingTask 49 { 50 private String locktoken = null; 51 private List toSet = new ArrayList (); 52 private List toRemove = new ArrayList (); 53 private int count = 0; 54 55 public void execute() throws BuildException { 56 validate(); 57 try { 58 if (!getFileSets().hasNext()) { 59 log(getUrl().getURI(), Project.MSG_INFO); 61 proppatch(getUrl(), getUrl().getURI()); 62 } else { 63 log("at: " + getUrl(), ifVerbose()); 64 if (!getUrl().getPath().endsWith("/")) { 66 getUrl().setPath(getUrl().getPath() + "/"); 67 } 68 for(Iterator i = getFileSets(); i.hasNext(); ) { 69 proppatch((WebdavFileSet)i.next()); 70 } 71 log("Properties set on " + this.count 72 + (this.count == 1 ? " resource" : " resources") 73 + " at " + getUrl(), 74 this.count > 0 ? Project.MSG_INFO : ifVerbose()); 75 } 76 } 77 catch (IOException e) { 78 throw Utils.makeBuildException("Can't proppatch!", e); 79 } 80 } 81 82 protected void validate() { 83 super.validate(); 84 for(Iterator i = this.toSet.iterator(); i.hasNext();) { 85 Set a = (Set)i.next(); 86 if (a.name == null) { 87 throw new BuildException("Add must have name attribute."); 88 } 89 } 90 for(Iterator i = this.toRemove.iterator(); i.hasNext();) { 91 Remove r = (Remove)i.next(); 92 if (r.name == null) { 93 throw new BuildException("Remove must have name attribute."); 94 } 95 } 96 } 97 98 protected void proppatch(HttpURL url, String logName) 99 throws IOException , HttpException 100 { 101 log(logName, ifVerbose()); 102 PropPatchMethod propPatch = new PropPatchMethod(url.getURI()); 103 if (this.locktoken != null) { 104 Utils.generateIfHeader(propPatch, this.locktoken); 105 } 106 107 int c = 1; 108 for(Iterator i = toRemove.iterator(); i.hasNext(); ) { 109 Remove r = (Remove)i.next(); 110 propPatch.addPropertyToRemove(r.name, 111 r.abbrev != null ? r.abbrev : "NS"+(c++), 112 r.namespace); 113 } 114 for(Iterator i = toSet.iterator(); i.hasNext(); ) { 115 Set a = (Set)i.next(); 116 propPatch.addPropertyToSet(a.name, 117 a.getValue(), 118 a.abbrev != null ? a.abbrev : "NS"+(c++), 119 a.namespace); 120 } 121 122 int status = getHttpClient().executeMethod(propPatch); 123 count++; 124 125 switch (status) { 126 case WebdavStatus.SC_OK: 127 break; 129 case WebdavStatus.SC_MULTI_STATUS: 130 for(Enumeration e = propPatch.getResponses(); e.hasMoreElements();) { 131 ResponseEntity response = (ResponseEntity)e.nextElement(); 132 133 if (response.getStatusCode() > 400) { 134 throw Utils.makeBuildException("Error while PROPPATCH", 135 propPatch.getResponses()); 136 } 137 } 138 break; 139 140 default: 141 HttpException ex = new HttpException(); 142 ex.setReasonCode(status); 143 throw ex; 144 } 145 } 146 147 protected void proppatch(WebdavFileSet fileSet) 148 throws IOException , HttpException 149 { 150 CollectionScanner scanner = 151 fileSet.getCollectionScanner(getProject(), getHttpClient(), getUrl()); 152 HttpURL baseUrl = scanner.getBaseURL(); 153 154 String [] files = scanner.getIncludedFiles(); 155 for (int i = 0; i < files.length; i++) { 156 HttpURL url = Utils.createHttpURL(baseUrl, files[i]); 157 proppatch(url, files[i]); 158 } 159 String [] colls = scanner.getIncludedDirectories(); 160 for (int i = 0; i < colls.length; i++) { 161 HttpURL url = Utils.createHttpURL(baseUrl, colls[i]); 162 proppatch(url, colls[i]); 163 } 164 } 165 166 public void setLocktoken(String token) { 167 this.locktoken = token; 168 if (!this.locktoken.startsWith("opaquelocktoken:")) { 169 throw new BuildException("Invalid locktoken: " + token); 170 } 171 } 172 public Set createSet() { 173 Set add = new Set(); 174 this.toSet.add(add); 175 return add; 176 } 177 public Remove createRemove() { 178 Remove remove = new Remove(); 179 this.toRemove.add(remove); 180 return remove; 181 } 182 183 public class Set { 184 String name; 185 String namespace; 186 String abbrev; 187 String value; 188 StringBuffer text = null; 189 Set() { 190 this.name = null; 191 this.namespace = Constants.DAV; 192 this.abbrev = null; 193 this.value = null; 194 } 195 public void setName(String name) { 196 this.name = name; 197 } 198 public void setNamespace(String namespace) { 199 this.namespace = namespace; 200 } 201 public void setNamespaceprefix(String pfx) { 202 this.abbrev = pfx; 203 } 204 public void setValue(String value) { 205 this.value = value; 206 } 207 public void addText(String text) { 208 if (this.value != null) { 209 throw new BuildException("Only one of nested text or value attribute is allowed."); 210 } 211 if (this.text == null) this.text = new StringBuffer (); 212 this.text.append(getProject().replaceProperties(text)); 213 } 214 String getValue() { 215 if (this.value != null) { 216 return this.value; 217 } 218 if (this.text != null) { 219 return this.text.toString(); 220 } 221 throw new BuildException("Either one of nested text or value attribute must be set."); 222 } 223 } 224 public static class Remove { 225 String name; 226 String namespace; 227 String abbrev; 228 Remove() { 229 this.name = null; 230 this.name = Constants.DAV; 231 this.abbrev = null; 232 } 233 public void setName(String name) { 234 this.name = name; 235 } 236 public void setNamespace(String namespace) { 237 this.namespace = namespace; 238 } 239 public void setNamespaceprefix(String pfx) { 240 this.abbrev = pfx; 241 } 242 } 243 } 244 | Popular Tags |