001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.webdav.client;
020:
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import HTTPClient.NVPair;
026:
027: /**
028: * This class provides the common functionality amongst all WebDAV
029: * methods.
030: *
031: * @author Matthew Large
032: * @version $Revision: 1.1 $
033: *
034: */
035: public abstract class AbstractWebDAVMethod {
036:
037: /**
038: * Name of the method.
039: */
040: private String m_sMethodName = "OPTIONS";
041:
042: /**
043: * WebDAV namespace.
044: */
045: public static final String WEBDAV_NAMESPACE = WebDAVConnection.WEBDAV_NAMESPACE;
046:
047: /**
048: * Depth.
049: */
050: private String m_sDepth = "0";
051:
052: /**
053: * Depth 0 identifier.
054: */
055: public static final String DEPTH_0 = "0";
056:
057: /**
058: * Depth 1 identifier.
059: */
060: public static final String DEPTH_1 = "1";
061:
062: /**
063: * Depth infinity identifier.
064: */
065: public static final String DEPTH_INFINITY = "infinity";
066:
067: /**
068: * No depth identifier.
069: */
070: public static final String DEPTH_NONE = "-1";
071:
072: /**
073: * Destination url.
074: */
075: private String m_sDestination = "";
076:
077: /**
078: * Overwrite.
079: */
080: private String m_sOverwrite = "F";
081:
082: /**
083: * Overwrite true identifier.
084: */
085: public static final String OVERWRITE_T = "T";
086:
087: /**
088: * Overwrite false identifier.
089: */
090: public static final String OVERWRITE_F = "F";
091:
092: /**
093: * Map of HTTP header names to HTTP header String values.
094: */
095: private Map m_mHeaders = new HashMap(3);
096:
097: /**
098: * URL of request.
099: */
100: private String m_sURL = "";
101:
102: /**
103: * Request body.
104: */
105: private byte[] m_bData = null;
106:
107: /**
108: * Creates a new WebDAV method.
109: *
110: * @param sMethodName Name of WebDAV method being created
111: * @param sURL URL of resource to execute WebDAV method on
112: */
113: public AbstractWebDAVMethod(String sMethodName, String sURL) {
114: super ();
115: this .m_sMethodName = sMethodName;
116: this .addHeader("User-Agent", "Simzuki/1.0");
117: if (sURL != null) {
118: this .m_sURL = sURL;
119: }
120: }
121:
122: /**
123: * Returns the WebDAV depth header value.
124: *
125: * @return WebDAV depth header value
126: */
127: public String getDepth() {
128: return m_sDepth;
129: }
130:
131: /**
132: * Returns the WebDAV destination URL.
133: *
134: * @return WebDAV destination URL
135: */
136: public String getDestination() {
137: return m_sDestination;
138: }
139:
140: /**
141: * Sets the WebDAV depth header value.
142: *
143: * @param sDepth WebDAV depth header value
144: */
145: public void setDepth(String sDepth) {
146: m_sDepth = sDepth;
147: }
148:
149: /**
150: * Sets the WebDAV destination URL.
151: *
152: * @param sDestination WebDAV destination value
153: */
154: public void setDestination(String sDestination) {
155: m_sDestination = sDestination;
156: }
157:
158: /**
159: * Returns the WebDAV overwrite header value.
160: *
161: * @return WebDAV overwrite header value
162: */
163: public boolean getOverwrite() {
164: return this .m_sOverwrite.equals("T") ? true : false;
165: }
166:
167: /**
168: * Sets the WebDAV overwrite header value.
169: *
170: * @param bOverwrite WebDAV overwrite header value
171: */
172: public void setOverwrite(boolean bOverwrite) {
173: m_sOverwrite = bOverwrite ? "T" : "F";
174: }
175:
176: /**
177: * Adds a header to the request.
178: *
179: * @param sName name for WebDAV Header to add
180: * @param sValue value for WebDAV Header to add
181: */
182: public void addHeader(String sName, String sValue) {
183: if (sName.equals("Depth")) {
184: this .setDepth(sValue);
185: } else if (sName.equals("Destination")) {
186: this .setDestination(sValue);
187: } else if (sName.equals("Overwrite")) {
188: if (sValue.equals("T")) {
189: this .setOverwrite(true);
190: } else if (sValue.equals("F")) {
191: this .setOverwrite(false);
192: } else {
193: this .m_mHeaders.put(sName, sValue);
194: }
195: } else {
196: this .m_mHeaders.put(sName, sValue);
197: }
198: }
199:
200: /**
201: * Returns a header value for a given header name.
202: *
203: * @param sName name for WebDAV Header to return
204: * @return value for WebDAV Header to add
205: */
206: public String getHeader(String sName) {
207: if (sName.equals("Depth")) {
208: return this .getDepth();
209: } else if (sName.equals("Destination")) {
210: return this .getDestination();
211: } else if (sName.equals("Overwrite")) {
212: return this .getOverwrite() ? "T" : "F";
213: } else {
214: return (String) this .m_mHeaders.get(sName);
215: }
216: }
217:
218: /**
219: * Returns all of the name/value header pairs.
220: *
221: * @return Array of {@link NVPair} objects
222: */
223: protected NVPair[] getAllHeaders() {
224: NVPair[] headers = new NVPair[this .m_mHeaders.size() + 3];
225:
226: Iterator itor = this .m_mHeaders.keySet().iterator();
227:
228: int nCount = 0;
229: while (itor.hasNext()) {
230: String sName = (String) itor.next();
231: headers[nCount] = new NVPair(sName,
232: (String) this .m_mHeaders.get(sName));
233: nCount++;
234: }
235:
236: if (this .m_sDepth != AbstractWebDAVMethod.DEPTH_NONE) {
237: headers[nCount] = new NVPair("Depth", this
238: .getHeader("Depth"));
239: nCount++;
240: }
241: if (!this .getDestination().equals("")) {
242: headers[nCount] = new NVPair("Destination",
243: WebDAVConnection.URLEncode(this
244: .getHeader("Destination")));
245: nCount++;
246: }
247:
248: return headers;
249: }
250:
251: /**
252: * Returns the URL for the request.
253: *
254: * @return URL for request
255: */
256: public String getURL() {
257: return m_sURL;
258: }
259:
260: /**
261: * Sets the URL for the request.
262: *
263: * @param sURL URL for request
264: */
265: public void setURL(String sURL) {
266: m_sURL = sURL;
267: }
268:
269: /**
270: * Returns the body of the request.
271: *
272: * @return Body of request
273: */
274: public byte[] getData() {
275: return m_bData;
276: }
277:
278: /**
279: * Sets the body for the request.
280: *
281: * @param bData Body for request
282: */
283: public void setData(byte[] bData) {
284: m_bData = bData;
285: }
286:
287: /**
288: * Returns the name of the method.
289: *
290: * @return Name
291: */
292: public String getName() {
293: return this.m_sMethodName;
294: }
295:
296: }
|