KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > client > AbstractWebDAVMethod


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.webdav.client;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import HTTPClient.NVPair;
26
27 /**
28  * This class provides the common functionality amongst all WebDAV
29  * methods.
30  *
31  * @author Matthew Large
32  * @version $Revision: 1.1 $
33  *
34  */

35 public abstract class AbstractWebDAVMethod {
36
37     /**
38      * Name of the method.
39      */

40     private String JavaDoc m_sMethodName = "OPTIONS";
41
42     /**
43      * WebDAV namespace.
44      */

45     public static final String JavaDoc WEBDAV_NAMESPACE =
46         WebDAVConnection.WEBDAV_NAMESPACE;
47
48     /**
49      * Depth.
50      */

51     private String JavaDoc m_sDepth = "0";
52     
53     /**
54      * Depth 0 identifier.
55      */

56     public static final String JavaDoc DEPTH_0 = "0";
57     
58     /**
59      * Depth 1 identifier.
60      */

61     public static final String JavaDoc DEPTH_1 = "1";
62     
63     /**
64      * Depth infinity identifier.
65      */

66     public static final String JavaDoc DEPTH_INFINITY = "infinity";
67     
68     /**
69      * No depth identifier.
70      */

71     public static final String JavaDoc DEPTH_NONE = "-1";
72
73     /**
74      * Destination url.
75      */

76     private String JavaDoc m_sDestination = "";
77     
78     /**
79      * Overwrite.
80      */

81     private String JavaDoc m_sOverwrite = "F";
82     
83     /**
84      * Overwrite true identifier.
85      */

86     public static final String JavaDoc OVERWRITE_T = "T";
87     
88     /**
89      * Overwrite false identifier.
90      */

91     public static final String JavaDoc OVERWRITE_F = "F";
92
93     /**
94      * Map of HTTP header names to HTTP header String values.
95      */

96     private Map JavaDoc m_mHeaders = new HashMap JavaDoc(3);
97
98     /**
99      * URL of request.
100      */

101     private String JavaDoc m_sURL = "";
102
103     /**
104      * Request body.
105      */

106     private byte[] m_bData = null;
107
108     /**
109      * Creates a new WebDAV method.
110      *
111      * @param sMethodName Name of WebDAV method being created
112      * @param sURL URL of resource to execute WebDAV method on
113      */

114     public AbstractWebDAVMethod(String JavaDoc sMethodName, String JavaDoc sURL) {
115         super();
116         this.m_sMethodName = sMethodName;
117         this.addHeader("User-Agent", "Simzuki/1.0");
118         if (sURL != null) {
119             this.m_sURL = sURL;
120         }
121     }
122
123     /**
124      * Returns the WebDAV depth header value.
125      *
126      * @return WebDAV depth header value
127      */

128     public String JavaDoc getDepth() {
129         return m_sDepth;
130     }
131     
132     /**
133      * Returns the WebDAV destination URL.
134      *
135      * @return WebDAV destination URL
136      */

137     public String JavaDoc getDestination() {
138         return m_sDestination;
139     }
140     
141     /**
142      * Sets the WebDAV depth header value.
143      *
144      * @param sDepth WebDAV depth header value
145      */

146     public void setDepth(String JavaDoc sDepth) {
147         m_sDepth = sDepth;
148     }
149     
150     /**
151      * Sets the WebDAV destination URL.
152      *
153      * @param sDestination WebDAV destination value
154      */

155     public void setDestination(String JavaDoc sDestination) {
156         m_sDestination = sDestination;
157     }
158     
159     /**
160      * Returns the WebDAV overwrite header value.
161      *
162      * @return WebDAV overwrite header value
163      */

164     public boolean getOverwrite() {
165         return this.m_sOverwrite.equals("T") ? true : false;
166     }
167     
168     /**
169      * Sets the WebDAV overwrite header value.
170      *
171      * @param bOverwrite WebDAV overwrite header value
172      */

173     public void setOverwrite(boolean bOverwrite) {
174         m_sOverwrite = bOverwrite ? "T" : "F";
175     }
176
177     /**
178      * Adds a header to the request.
179      *
180      * @param sName name for WebDAV Header to add
181      * @param sValue value for WebDAV Header to add
182      */

183     public void addHeader(String JavaDoc sName, String JavaDoc sValue) {
184         if (sName.equals("Depth")) {
185             this.setDepth(sValue);
186         } else if (sName.equals("Destination")) {
187             this.setDestination(sValue);
188         } else if (sName.equals("Overwrite")) {
189             if (sValue.equals("T")) {
190                 this.setOverwrite(true);
191             } else if (sValue.equals("F")) {
192                 this.setOverwrite(false);
193             } else {
194                 this.m_mHeaders.put(sName, sValue);
195             }
196         } else {
197             this.m_mHeaders.put(sName, sValue);
198         }
199     }
200
201     /**
202      * Returns a header value for a given header name.
203      *
204      * @param sName name for WebDAV Header to return
205      * @return value for WebDAV Header to add
206      */

207     public String JavaDoc getHeader(String JavaDoc sName) {
208         if (sName.equals("Depth")) {
209             return this.getDepth();
210         } else if (sName.equals("Destination")) {
211             return this.getDestination();
212         } else if (sName.equals("Overwrite")) {
213             return this.getOverwrite() ? "T" : "F";
214         } else {
215             return (String JavaDoc) this.m_mHeaders.get(sName);
216         }
217     }
218
219     /**
220      * Returns all of the name/value header pairs.
221      *
222      * @return Array of {@link NVPair} objects
223      */

224     protected NVPair[] getAllHeaders() {
225         NVPair[] headers = new NVPair[this.m_mHeaders.size() + 3];
226
227         Iterator JavaDoc itor = this.m_mHeaders.keySet().iterator();
228
229         int nCount = 0;
230         while (itor.hasNext()) {
231             String JavaDoc sName = (String JavaDoc) itor.next();
232             headers[nCount] =
233                 new NVPair(sName, (String JavaDoc) this.m_mHeaders.get(sName));
234             nCount++;
235         }
236
237         if(this.m_sDepth!=AbstractWebDAVMethod.DEPTH_NONE) {
238             headers[nCount] = new NVPair("Depth", this.getHeader("Depth"));
239             nCount++;
240         }
241         if (!this.getDestination().equals("")) {
242             headers[nCount] =
243                 new NVPair(
244                     "Destination",
245                     WebDAVConnection.URLEncode(this.getHeader("Destination")));
246             nCount++;
247         }
248
249         return headers;
250     }
251
252     /**
253      * Returns the URL for the request.
254      *
255      * @return URL for request
256      */

257     public String JavaDoc getURL() {
258         return m_sURL;
259     }
260     
261     /**
262      * Sets the URL for the request.
263      *
264      * @param sURL URL for request
265      */

266     public void setURL(String JavaDoc sURL) {
267         m_sURL = sURL;
268     }
269
270     /**
271      * Returns the body of the request.
272      *
273      * @return Body of request
274      */

275     public byte[] getData() {
276         return m_bData;
277     }
278
279     /**
280      * Sets the body for the request.
281      *
282      * @param bData Body for request
283      */

284     public void setData(byte[] bData) {
285         m_bData = bData;
286     }
287
288     /**
289      * Returns the name of the method.
290      *
291      * @return Name
292      */

293     public String JavaDoc getName() {
294         return this.m_sMethodName;
295     }
296
297 }
298
Popular Tags