KickJava   Java API By Example, From Geeks To Geeks.

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


1 // vi: set ts=3 sw=3:
2
/*
3  * $Header: /home/cvs/jakarta-slide/webdavclient/ant/src/java/org/apache/webdav/ant/taskdefs/Proppatch.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 java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
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 /**
44  * WebDAV task for editing resource properties.
45  *
46  * <p>TODO: Howto set Properties with XML values?
47  */

48 public class Proppatch extends WebdavMatchingTask
49 {
50    private String JavaDoc locktoken = null;
51    private List JavaDoc toSet = new ArrayList JavaDoc();
52    private List JavaDoc toRemove = new ArrayList JavaDoc();
53    private int count = 0;
54    
55    public void execute() throws BuildException {
56       validate();
57       try {
58          if (!getFileSets().hasNext()) {
59             // delete the resource given by url
60
log(getUrl().getURI(), Project.MSG_INFO);
61             proppatch(getUrl(), getUrl().getURI());
62          } else {
63             log("at: " + getUrl(), ifVerbose());
64             // URL must be a collection
65
if (!getUrl().getPath().endsWith("/")) {
66                getUrl().setPath(getUrl().getPath() + "/");
67             }
68             for(Iterator JavaDoc 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 JavaDoc e) {
78          throw Utils.makeBuildException("Can't proppatch!", e);
79       }
80    }
81    
82    protected void validate() {
83       super.validate();
84       for(Iterator JavaDoc 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 JavaDoc 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 JavaDoc logName)
99       throws IOException JavaDoc, 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 JavaDoc 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 JavaDoc 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             // ok
128
break;
129          case WebdavStatus.SC_MULTI_STATUS:
130             for(Enumeration JavaDoc 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 JavaDoc, HttpException
149    {
150       CollectionScanner scanner =
151          fileSet.getCollectionScanner(getProject(), getHttpClient(), getUrl());
152       HttpURL baseUrl = scanner.getBaseURL();
153      
154       String JavaDoc[] 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 JavaDoc[] 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 JavaDoc 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 JavaDoc name;
185       String JavaDoc namespace;
186       String JavaDoc abbrev;
187       String JavaDoc value;
188       StringBuffer JavaDoc 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 JavaDoc name) {
196          this.name = name;
197       }
198       public void setNamespace(String JavaDoc namespace) {
199          this.namespace = namespace;
200       }
201       public void setNamespaceprefix(String JavaDoc pfx) {
202          this.abbrev = pfx;
203       }
204       public void setValue(String JavaDoc value) {
205          this.value = value;
206       }
207       public void addText(String JavaDoc 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 JavaDoc();
212          this.text.append(getProject().replaceProperties(text));
213       }
214       String JavaDoc 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 JavaDoc name;
226       String JavaDoc namespace;
227       String JavaDoc abbrev;
228       Remove() {
229          this.name = null;
230          this.name = Constants.DAV;
231          this.abbrev = null;
232       }
233       public void setName(String JavaDoc name) {
234          this.name = name;
235       }
236       public void setNamespace(String JavaDoc namespace) {
237          this.namespace = namespace;
238       }
239       public void setNamespaceprefix(String JavaDoc pfx) {
240          this.abbrev = pfx;
241       }
242    }
243 }
244
Popular Tags