KickJava   Java API By Example, From Geeks To Geeks.

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


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.applet.*;
22 import java.io.*;
23 import java.net.*;
24 import java.util.*;
25
26 import HTTPClient.*;
27 import HTTPClient.URI;
28
29 /**
30  * Extending the HTTPConnection from the HTTPClient this class
31  * simply provides the execute method to take in and translate AbstractWebDAVMethods.
32  *
33  * @author Matthew Large
34  * @version $Revision: 1.1 $
35  *
36  */

37 public class WebDAVConnection extends HTTPConnection {
38     
39     private static int TIMEOUT = 90000; /* 90 seconds in milliseconds */
40     
41     /**
42      * WebDAV namespace.
43      */

44     public static final String JavaDoc WEBDAV_NAMESPACE = "DAV:";
45     
46     /**
47      * Virtual file system that created this connection.
48      */

49     private WebDAVFileSystem m_vfs = null;
50     
51     public WebDAVConnection(WebDAVFileSystem vfs, Applet arg0) throws ProtocolNotSuppException {
52         super(arg0);
53         this.m_vfs = vfs;
54         this.setup();
55     }
56
57     public WebDAVConnection(WebDAVFileSystem vfs, String JavaDoc arg0) {
58         super(arg0);
59         this.m_vfs = vfs;
60         this.setup();
61     }
62
63     public WebDAVConnection(WebDAVFileSystem vfs, String JavaDoc arg0, int arg1) {
64         super(arg0, arg1);
65         this.m_vfs = vfs;
66         this.setup();
67     }
68
69     public WebDAVConnection(WebDAVFileSystem vfs, String JavaDoc arg0, String JavaDoc arg1, int arg2)
70         throws ProtocolNotSuppException {
71         super(arg0, arg1, arg2);
72         this.m_vfs = vfs;
73         this.setup();
74     }
75
76     public WebDAVConnection(WebDAVFileSystem vfs,
77         String JavaDoc arg0,
78         String JavaDoc arg1,
79         int arg2,
80         InetAddress arg3,
81         int arg4)
82         throws ProtocolNotSuppException {
83         super(arg0, arg1, arg2, arg3, arg4);
84         this.m_vfs = vfs;
85         this.setup();
86     }
87
88     public WebDAVConnection(WebDAVFileSystem vfs, URL arg0) throws ProtocolNotSuppException {
89         super(arg0);
90         this.m_vfs = vfs;
91         this.setup();
92     }
93
94     /**
95      * @param arg0
96      * @throws HTTPClient.ProtocolNotSuppException
97      */

98     public WebDAVConnection(WebDAVFileSystem vfs, URI arg0) throws ProtocolNotSuppException {
99         super(arg0);
100         this.m_vfs = vfs;
101         this.setup();
102     }
103     
104     public void setup() {
105         WebDAVConnection.setDefaultTimeout(TIMEOUT);
106     }
107
108     public static void main(String JavaDoc[] args) {
109
110     }
111     
112     /**
113      * This method accepts an AbstractWebDAVMethod and uses it to call the ExtensionMethod
114      * on the underlying HTTPConnection. Then it uses the HTTPResponse returned from
115      * the HTTPConnection and constructs a WebDAVResponse object.
116      *
117      * @param method AbstractWebDAVMethod, all the information to be sent to the server
118      * @return WebDAVResponse, all the information sent back from the server
119      * @throws IOException
120      * @throws ModuleException
121      */

122     public WebDAVResponse execute(AbstractWebDAVMethod method) throws IOException, ModuleException {
123         
124         WebDAVResponse davResponse = null;
125         
126         try {
127             HTTPResponse response = this.ExtensionMethod(method.getName(),
128                                                             URLEncode(method.getURL()),
129                                                             method.getData(),
130             method.getAllHeaders());
131             davResponse = new WebDAVResponse( response );
132             davResponse.setURL( method.getURL() );
133         } catch(SocketException se) {
134                 HTTPResponse response = this.ExtensionMethod(method.getName(),
135                                                                 URLEncode(method.getURL()),
136                                                                 method.getData(),
137                 method.getAllHeaders());
138                 davResponse = new WebDAVResponse( response );
139                 davResponse.setURL( method.getURL() );
140         } catch(IOException io) {
141                 HTTPResponse response = this.ExtensionMethod(method.getName(),
142                                                                 URLEncode(method.getURL()),
143                                                                 method.getData(),
144                 method.getAllHeaders());
145                 davResponse = new WebDAVResponse( response );
146                 davResponse.setURL( method.getURL() );
147         }
148
149         return davResponse;
150     }
151     
152     /**
153      * Used for encoding URLs, importantly encoding only the segments and not the '/'.
154      *
155      * @param sURL URL String to encode
156      * @return Encoded URL String
157      */

158     public static String JavaDoc URLEncode(String JavaDoc sURL) {
159         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
160         
161         StringTokenizer sTok = new StringTokenizer(sURL, " /:", true);
162         while(sTok.hasMoreTokens()) {
163             try {
164                 String JavaDoc sToken = sTok.nextToken();
165                 if( !sToken.equals(":") && !sToken.equals("/") && !sToken.equals(" ") ) {
166                     sBuff.append( URLEncoder.encode(sToken, "UTF-8") );
167                 } else if(sToken.equals(" ")) {
168                     sBuff.append("%20");
169                 } else if(sToken.equals(":")) {
170                     sBuff.append(":");
171                 } else {
172                     sBuff.append(sToken);
173                 }
174             } catch (UnsupportedEncodingException e) {
175                 e.printStackTrace();
176             }
177             
178         }
179
180         return sBuff.toString();
181     }
182
183     /**
184      * Used for decoding URLs, importantly decoding only the segments and not the '/'.
185      *
186      * @param sURL URL String to decode
187      * @return Decoded URL String
188      */

189     public static String JavaDoc URLDencode(String JavaDoc sURL) {
190         sURL.replaceAll("%20", " ");
191         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
192         
193         StringTokenizer sTok = new StringTokenizer(sURL, "/", true);
194         while(sTok.hasMoreTokens()) {
195             try {
196                 String JavaDoc sToken = sTok.nextToken();
197                 if( !sToken.equals("/") ) {
198                     sBuff.append( URLDecoder.decode(sToken, "UTF-8") );
199                 } else {
200                     sBuff.append(sToken);
201                 }
202             } catch (UnsupportedEncodingException e) {
203                 e.printStackTrace();
204             }
205             
206         }
207
208         return sBuff.toString();
209     }
210 }
211
Popular Tags