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.applet.*;
022: import java.io.*;
023: import java.net.*;
024: import java.util.*;
025:
026: import HTTPClient.*;
027: import HTTPClient.URI;
028:
029: /**
030: * Extending the HTTPConnection from the HTTPClient this class
031: * simply provides the execute method to take in and translate AbstractWebDAVMethods.
032: *
033: * @author Matthew Large
034: * @version $Revision: 1.1 $
035: *
036: */
037: public class WebDAVConnection extends HTTPConnection {
038:
039: private static int TIMEOUT = 90000; /* 90 seconds in milliseconds */
040:
041: /**
042: * WebDAV namespace.
043: */
044: public static final String WEBDAV_NAMESPACE = "DAV:";
045:
046: /**
047: * Virtual file system that created this connection.
048: */
049: private WebDAVFileSystem m_vfs = null;
050:
051: public WebDAVConnection(WebDAVFileSystem vfs, Applet arg0)
052: throws ProtocolNotSuppException {
053: super (arg0);
054: this .m_vfs = vfs;
055: this .setup();
056: }
057:
058: public WebDAVConnection(WebDAVFileSystem vfs, String arg0) {
059: super (arg0);
060: this .m_vfs = vfs;
061: this .setup();
062: }
063:
064: public WebDAVConnection(WebDAVFileSystem vfs, String arg0, int arg1) {
065: super (arg0, arg1);
066: this .m_vfs = vfs;
067: this .setup();
068: }
069:
070: public WebDAVConnection(WebDAVFileSystem vfs, String arg0,
071: String arg1, int arg2) throws ProtocolNotSuppException {
072: super (arg0, arg1, arg2);
073: this .m_vfs = vfs;
074: this .setup();
075: }
076:
077: public WebDAVConnection(WebDAVFileSystem vfs, String arg0,
078: String arg1, int arg2, InetAddress arg3, int arg4)
079: throws ProtocolNotSuppException {
080: super (arg0, arg1, arg2, arg3, arg4);
081: this .m_vfs = vfs;
082: this .setup();
083: }
084:
085: public WebDAVConnection(WebDAVFileSystem vfs, URL arg0)
086: throws ProtocolNotSuppException {
087: super (arg0);
088: this .m_vfs = vfs;
089: this .setup();
090: }
091:
092: /**
093: * @param arg0
094: * @throws HTTPClient.ProtocolNotSuppException
095: */
096: public WebDAVConnection(WebDAVFileSystem vfs, URI arg0)
097: throws ProtocolNotSuppException {
098: super (arg0);
099: this .m_vfs = vfs;
100: this .setup();
101: }
102:
103: public void setup() {
104: WebDAVConnection.setDefaultTimeout(TIMEOUT);
105: }
106:
107: public static void main(String[] args) {
108:
109: }
110:
111: /**
112: * This method accepts an AbstractWebDAVMethod and uses it to call the ExtensionMethod
113: * on the underlying HTTPConnection. Then it uses the HTTPResponse returned from
114: * the HTTPConnection and constructs a WebDAVResponse object.
115: *
116: * @param method AbstractWebDAVMethod, all the information to be sent to the server
117: * @return WebDAVResponse, all the information sent back from the server
118: * @throws IOException
119: * @throws ModuleException
120: */
121: public WebDAVResponse execute(AbstractWebDAVMethod method)
122: throws IOException, ModuleException {
123:
124: WebDAVResponse davResponse = null;
125:
126: try {
127: HTTPResponse response = this .ExtensionMethod(method
128: .getName(), URLEncode(method.getURL()), method
129: .getData(), method.getAllHeaders());
130: davResponse = new WebDAVResponse(response);
131: davResponse.setURL(method.getURL());
132: } catch (SocketException se) {
133: HTTPResponse response = this .ExtensionMethod(method
134: .getName(), URLEncode(method.getURL()), method
135: .getData(), method.getAllHeaders());
136: davResponse = new WebDAVResponse(response);
137: davResponse.setURL(method.getURL());
138: } catch (IOException io) {
139: HTTPResponse response = this .ExtensionMethod(method
140: .getName(), URLEncode(method.getURL()), method
141: .getData(), method.getAllHeaders());
142: davResponse = new WebDAVResponse(response);
143: davResponse.setURL(method.getURL());
144: }
145:
146: return davResponse;
147: }
148:
149: /**
150: * Used for encoding URLs, importantly encoding only the segments and not the '/'.
151: *
152: * @param sURL URL String to encode
153: * @return Encoded URL String
154: */
155: public static String URLEncode(String sURL) {
156: StringBuffer sBuff = new StringBuffer();
157:
158: StringTokenizer sTok = new StringTokenizer(sURL, " /:", true);
159: while (sTok.hasMoreTokens()) {
160: try {
161: String sToken = sTok.nextToken();
162: if (!sToken.equals(":") && !sToken.equals("/")
163: && !sToken.equals(" ")) {
164: sBuff.append(URLEncoder.encode(sToken, "UTF-8"));
165: } else if (sToken.equals(" ")) {
166: sBuff.append("%20");
167: } else if (sToken.equals(":")) {
168: sBuff.append(":");
169: } else {
170: sBuff.append(sToken);
171: }
172: } catch (UnsupportedEncodingException e) {
173: e.printStackTrace();
174: }
175:
176: }
177:
178: return sBuff.toString();
179: }
180:
181: /**
182: * Used for decoding URLs, importantly decoding only the segments and not the '/'.
183: *
184: * @param sURL URL String to decode
185: * @return Decoded URL String
186: */
187: public static String URLDencode(String sURL) {
188: sURL.replaceAll("%20", " ");
189: StringBuffer sBuff = new StringBuffer();
190:
191: StringTokenizer sTok = new StringTokenizer(sURL, "/", true);
192: while (sTok.hasMoreTokens()) {
193: try {
194: String sToken = sTok.nextToken();
195: if (!sToken.equals("/")) {
196: sBuff.append(URLDecoder.decode(sToken, "UTF-8"));
197: } else {
198: sBuff.append(sToken);
199: }
200: } catch (UnsupportedEncodingException e) {
201: e.printStackTrace();
202: }
203:
204: }
205:
206: return sBuff.toString();
207: }
208: }
|