001: // DAVManager.java
002: // $Id: DAVManager.java,v 1.1 2000/10/11 13:20:08 bmahe Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.www.protocol.webdav;
006:
007: import java.util.Hashtable;
008: import java.util.Properties;
009:
010: import org.w3c.www.mime.MimeHeaderHolder;
011: import org.w3c.www.mime.MimeParser;
012: import org.w3c.www.mime.MimeParserFactory;
013:
014: import org.w3c.www.protocol.http.HttpManager;
015: import org.w3c.www.protocol.http.HttpException;
016:
017: class DAVReplyFactory implements MimeParserFactory {
018:
019: public MimeHeaderHolder createHeaderHolder(MimeParser parser) {
020: return new DAVReply(parser);
021: }
022:
023: }
024:
025: /**
026: * @version $Revision: 1.1 $
027: * @author Benoît Mahé (bmahe@w3.org)
028: */
029: public class DAVManager extends HttpManager {
030:
031: private static Hashtable davmanagers = new Hashtable();
032:
033: protected static HttpManager getNewInstance() {
034: return new DAVManager();
035: }
036:
037: /**
038: * Get an instance of the WEBDAV manager.
039: * This method returns an actual instance of the WEBDAV manager. It may
040: * return different managers, if it decides to distribute the load on
041: * different managers (avoid the HttpManager being a bottleneck).
042: * @return An application wide instance of the WEBDAV manager.
043: */
044:
045: public static synchronized DAVManager getDAVManager(Properties p) {
046: return (DAVManager) getManager(DAVManager.class, p);
047: }
048:
049: public static DAVManager getDAVManager() {
050: return getDAVManager(System.getProperties());
051: }
052:
053: MimeParserFactory factory = null;
054:
055: public MimeParserFactory getReplyFactory() {
056: if (factory == null) {
057: factory = new DAVReplyFactory();
058: }
059: return factory;
060: }
061:
062: /**
063: * Create a new default outgoing request.
064: * This method should <em>always</em> be used to create outgoing requests.
065: * It will initialize the request with appropriate default values for
066: * the various headers, and make sure that the request is enhanced by
067: * the registered request filters.
068: * @return An instance of DAVRequest, suitable to be launched.
069: */
070:
071: public DAVRequest createDAVRequest() {
072: return (DAVRequest) createRequest();
073: }
074:
075: /**
076: * Run the given request, in synchronous mode.
077: * This method will launch the given request, and block the calling thread
078: * until the response headers are available.
079: * @param request The request to run.
080: * @return An instance of Reply, containing all the reply
081: * informations.
082: * @exception HttpException If something failed during request processing.
083: */
084:
085: public DAVReply runDAVRequest(DAVRequest request)
086: throws HttpException {
087: return (DAVReply) runRequest(request);
088: }
089:
090: public DAVManager() {
091: super ();
092: this .template = new DAVRequest(this );
093: }
094:
095: public static void main(String args[]) {
096: try {
097: DAVManager manager = DAVManager.getDAVManager();
098: manager
099: .setGlobalHeader("User-Agent",
100: "Jigsaw/2.1.2 WEBDAV");
101: manager.setGlobalHeader("Accept", "*/*;q=1.0");
102: manager.setGlobalHeader("Accept-Encoding", "gzip");
103: org.w3c.www.protocol.http.PropRequestFilter filter = new org.w3c.www.protocol.http.cookies.CookieFilter();
104: filter.initialize(manager);
105: org.w3c.www.protocol.http.PropRequestFilter pdebug = new org.w3c.www.protocol.http.DebugFilter();
106: pdebug.initialize(manager);
107: DAVRequest request = manager.createDAVRequest();
108: request.setURL(new java.net.URL(args[0]));
109: request.setMethod("GET");
110: DAVReply reply = manager.runDAVRequest(request);
111: //Display some infos:
112: System.out.println("last-modified: "
113: + reply.getLastModified());
114: System.out.println("length : "
115: + reply.getContentLength());
116: // Display the returned body:
117: java.io.InputStream in = reply.getInputStream();
118: byte buf[] = new byte[4096];
119: int cnt = 0;
120: while ((cnt = in.read(buf)) > 0)
121: System.out.print(new String(buf, 0, cnt));
122: System.out.println("-");
123: in.close();
124: manager.sync();
125: } catch (Exception ex) {
126: ex.printStackTrace();
127: }
128: System.exit(1);
129:
130: }
131:
132: }
|