001: package com.ibm.webdav.protocol.http;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017: import java.io.*;
018: import java.net.*;
019:
020: /**
021: * A class to represent a WebDAV HTTP connection to a remote object.
022: * WebDAVURLConnection is a subclass of sun.net.www.protocol.http.HttpURLConnection
023: * that overrides methods as required to support the additional WebDAV methods.
024: * HttpURLConnection was not developed to be
025: * subclassed as it contains private data that needs to be available in the
026: * subclasses. So some of the method overrides may seem a little strange. See the
027: * individual methods for details. The changes to HttpURLConnection are to allow
028: * WebDAV methods, and to handle errors differently so that clients can receive
029: * more specific exceptions.
030: *
031: * @author Jim Amsden
032: */
033: public class WebDAVURLConnection extends
034: sun.net.www.protocol.http.HttpURLConnection {
035:
036: private static final String[] methods = { "GET", "POST", "HEAD",
037: "OPTIONS", "PUT",
038: "DELETE", // from HTTP/1.1
039: "MKCOL", "COPY", "MOVE", "PROPFIND", "PROPPATCH", "LOCK",
040: "UNLOCK", "SEARCH" };
041: /* We only have a single static authenticator for now.
042: */
043: private static WebDAVAuthenticator defaultAuth;
044:
045: protected WebDAVURLConnection(URL u, Handler handler)
046: throws IOException {
047: super (u, handler);
048: }
049:
050: public WebDAVURLConnection(URL u, String host, int port)
051: throws IOException {
052: super (u, host, port);
053: }
054:
055: /*
056: * Catch the IOException the superclass raises when it gets a status code
057: * greater than 400 and the file is not some text or HTML file that might
058: * contain additional error information. Without this, the DAV4J client
059: * API doesn't get a chance to convert the status code to a WebDAVException.
060: */
061:
062: public synchronized InputStream getInputStream() throws IOException {
063: InputStream is = null;
064: try {
065: is = super .getInputStream();
066: } catch (IOException exc) {
067: // just ignore it, the DAV4J client API will translate the
068: // status code to the proper WebDAVException
069: }
070: return is;
071: }
072:
073: /*
074: *
075: * Overridden to support additional WebDAV methods.
076: */
077:
078: public synchronized OutputStream getOutputStream()
079: throws IOException {
080: OutputStream os = null;
081: String savedMethod = method;
082: // see if the method supports output
083: if (method.equals("GET") || method.equals("PUT")
084: || method.equals("POST") || method.equals("PROPFIND")
085: || method.equals("PROPPATCH") || method.equals("MKCOL")
086: || method.equals("MOVE") || method.equals("COPY")
087: || method.equals("LOCK")) {
088: // fake the method so the superclass method sets its instance variables
089: method = "PUT";
090: } else {
091: // use any method that doesn't support output, an exception will be
092: // raised by the superclass
093: method = "DELETE";
094: }
095: os = super .getOutputStream();
096: method = savedMethod;
097: return os;
098: }
099:
100: /**
101: * Set the method for the URL request, one of:
102: * <UL>
103: * <LI>GET
104: * <LI>POST
105: * <LI>HEAD
106: * <LI>OPTIONS
107: * <LI>PUT
108: * <LI>DELETE
109: * <LI>PROPFIND
110: * <LI>PROPPATCH
111: * <LI>MKCOL
112: * <LI>MOVE
113: * <LI>COPY
114: * <LI>LOCK
115: * <LI>UNLOCK
116: * <LI>TRACE
117: *
118: * @exception ProtocolException if the method cannot be reset or if
119: * the requested method isn't valid for HTTP.
120: */
121: public void setRequestMethod(String method)
122: throws ProtocolException {
123: if (connected) {
124: throw new ProtocolException(
125: "Cannot reset method once connected");
126: }
127: // prevent clients from specifying invalid methods. This prevents experimenting
128: // with new methods without editing this code, but should be included for
129: // security reasons.
130: for (int i = 0; i < methods.length; i++) {
131: if (methods[i].equals(method)) {
132: this .method = method;
133: return;
134: }
135: }
136: throw new ProtocolException("Invalid WebDAV method: " + method);
137: }
138: }
|