KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > protocol > http > WebDAVURLConnection


1 package com.ibm.webdav.protocol.http;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import java.io.*;
18 import java.net.*;
19
20 /**
21  * A class to represent a WebDAV HTTP connection to a remote object.
22  * WebDAVURLConnection is a subclass of sun.net.www.protocol.http.HttpURLConnection
23  * that overrides methods as required to support the additional WebDAV methods.
24  * HttpURLConnection was not developed to be
25  * subclassed as it contains private data that needs to be available in the
26  * subclasses. So some of the method overrides may seem a little strange. See the
27  * individual methods for details. The changes to HttpURLConnection are to allow
28  * WebDAV methods, and to handle errors differently so that clients can receive
29  * more specific exceptions.
30  *
31  * @author Jim Amsden
32  */

33 public class WebDAVURLConnection extends sun.net.www.protocol.http.HttpURLConnection {
34
35     private static final String JavaDoc[] methods = {"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", // from HTTP/1.1
36
"MKCOL", "COPY", "MOVE", "PROPFIND", "PROPPATCH", "LOCK", "UNLOCK","SEARCH"};
37     /* We only have a single static authenticator for now.
38     */

39     private static WebDAVAuthenticator defaultAuth;
40 protected WebDAVURLConnection(URL u, Handler handler) throws IOException {
41     super(u, handler);
42 }
43 public WebDAVURLConnection(URL u, String JavaDoc host, int port) throws IOException {
44     super(u, host, port);
45 }
46 /*
47 * Catch the IOException the superclass raises when it gets a status code
48 * greater than 400 and the file is not some text or HTML file that might
49 * contain additional error information. Without this, the DAV4J client
50 * API doesn't get a chance to convert the status code to a WebDAVException.
51 */

52
53 public synchronized InputStream getInputStream() throws IOException {
54     InputStream is = null;
55     try {
56         is = super.getInputStream();
57     } catch (IOException exc) {
58         // just ignore it, the DAV4J client API will translate the
59
// status code to the proper WebDAVException
60
}
61     return is;
62 }
63 /*
64 *
65 * Overridden to support additional WebDAV methods.
66 */

67
68 public synchronized OutputStream getOutputStream() throws IOException {
69     OutputStream os = null;
70     String JavaDoc savedMethod = method;
71     // see if the method supports output
72
if (method.equals("GET") || method.equals("PUT") || method.equals("POST") ||
73         method.equals("PROPFIND") || method.equals("PROPPATCH") ||
74         method.equals("MKCOL") || method.equals("MOVE") || method.equals("COPY") ||
75         method.equals("LOCK")) {
76         // fake the method so the superclass method sets its instance variables
77
method = "PUT";
78     } else {
79         // use any method that doesn't support output, an exception will be
80
// raised by the superclass
81
method = "DELETE";
82     }
83     os = super.getOutputStream();
84     method = savedMethod;
85     return os;
86 }
87 /**
88  * Set the method for the URL request, one of:
89  * <UL>
90  * <LI>GET
91  * <LI>POST
92  * <LI>HEAD
93  * <LI>OPTIONS
94  * <LI>PUT
95  * <LI>DELETE
96  * <LI>PROPFIND
97  * <LI>PROPPATCH
98  * <LI>MKCOL
99  * <LI>MOVE
100  * <LI>COPY
101  * <LI>LOCK
102  * <LI>UNLOCK
103  * <LI>TRACE
104  *
105  * @exception ProtocolException if the method cannot be reset or if
106  * the requested method isn't valid for HTTP.
107  */

108 public void setRequestMethod(String JavaDoc method) throws ProtocolException {
109     if (connected) {
110         throw new ProtocolException("Cannot reset method once connected");
111     }
112     // prevent clients from specifying invalid methods. This prevents experimenting
113
// with new methods without editing this code, but should be included for
114
// security reasons.
115
for (int i = 0; i < methods.length; i++) {
116         if (methods[i].equals(method)) {
117             this.method = method;
118             return;
119         }
120     }
121     throw new ProtocolException("Invalid WebDAV method: " + method);
122 }
123 }
124
Popular Tags