KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > WebdavSession


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/WebdavSession.java,v 1.7 2004/07/30 13:20:48 ib Exp $
3  * $Revision: 1.7 $
4  * $Date: 2004/07/30 13:20:48 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib;
25
26 import java.io.IOException JavaDoc;
27 import org.apache.commons.httpclient.Credentials;
28 import org.apache.commons.httpclient.HostConfiguration;
29 import org.apache.commons.httpclient.HttpClient;
30 import org.apache.commons.httpclient.HttpState;
31 import org.apache.commons.httpclient.UsernamePasswordCredentials;
32 import org.apache.commons.httpclient.HttpURL;
33
34 /**
35  * This WebdavSession class is for the session management of WebDAV clients.
36  * This class saves and restores the requested client.
37  *
38  * Although this class is thread safe, it should only be accessed by one
39  * concurrent thread, since the underlying protocol, HTTP, is not multiplexed.
40  * If simultaneous operations are needed, it is recommended to create
41  * additional threads, each having its own associated WebDAV client.
42  *
43  * Clients that use persistent connections SHOULD limit the number of
44  * simultaneous connections that they maintain to a given server. A
45  * single-user client SHOULD NOT maintain more than 2 connections with
46  * any server or proxy. A proxy SHOULD use up to 2*N connections to
47  * another server or proxy, where N is the number of simultaneously
48  * active users. These guidelines are intended to improve HTTP response
49  * times and avoid congestion.
50  *
51  */

52 public abstract class WebdavSession {
53
54
55     // ------------------------------------------------------- Constructors
56

57
58     /**
59      * Default constructor.
60      */

61     public WebdavSession() {
62         super();
63     }
64
65
66     // ---------------------------------------------------- Instance Variables
67

68
69     /**
70      * The Http client instance.
71      */

72     protected HttpClient client;
73
74     /**
75      * Credentials to use for authentication
76      */

77     protected Credentials hostCredentials = null;
78
79     /**
80      * The hostname to use for the proxy, if any
81      */

82     protected String JavaDoc proxyHost = null;
83
84     /**
85      * Port number to use for proxy, if any
86      */

87     protected int proxyPort = -1;
88
89     /**
90      * Credentials to use for an authenticating proxy
91      */

92     protected Credentials proxyCredentials = null;
93
94
95     /**
96      * Debug level.
97      */

98     protected int debug = 0;
99
100
101     // ------------------------------------------------------------- Properties
102

103
104     /**
105      * Set debug level.
106      */

107     public void setDebug(int debug) {
108         this.debug = debug;
109     }
110
111
112     // ------------------------------------------------------ Public methods
113

114
115     /**
116      * Get a <code>HttpClient</code> instance.
117      * This method returns a new client instance for the first time.
118      * And it is saved util it's closed or reset.
119      *
120      * @param httpURL The http URL to connect. only used the authority part.
121      * @return An instance of <code>HttpClient</code>.
122      * @exception IOException
123      */

124     public HttpClient getSessionInstance(HttpURL httpURL)
125         throws IOException JavaDoc {
126
127         return getSessionInstance(httpURL, false);
128     }
129
130
131     /**
132      * Get a <code>HttpClient</code> instance.
133      * This method returns a new client instance, when reset is true.
134      *
135      * @param httpURL The http URL to connect. only used the authority part.
136      * @param reset The reset flag to represent whether the saved information
137      * is used or not.
138      * @return An instance of <code>HttpClient</code>.
139      * @exception IOException
140      */

141     public HttpClient getSessionInstance(HttpURL httpURL, boolean reset)
142         throws IOException JavaDoc {
143
144         if (reset || client == null) {
145             client = new HttpClient();
146             // Set a state which allows lock tracking
147
client.setState(new WebdavState());
148             HostConfiguration hostConfig = client.getHostConfiguration();
149             hostConfig.setHost(httpURL);
150             if (proxyHost != null && proxyPort > 0)
151                 hostConfig.setProxy(proxyHost, proxyPort);
152
153             if (hostCredentials == null) {
154                 String JavaDoc userName = httpURL.getUser();
155                 if (userName != null && userName.length() > 0) {
156                     hostCredentials =
157                         new UsernamePasswordCredentials(userName,
158                                                         httpURL.getPassword());
159                 }
160             }
161
162             if (hostCredentials != null) {
163                 HttpState clientState = client.getState();
164                 clientState.setCredentials(null, httpURL.getHost(),
165                                            hostCredentials);
166                 clientState.setAuthenticationPreemptive(true);
167             }
168
169             if (proxyCredentials != null) {
170                 client.getState().setProxyCredentials(null, proxyHost,
171                                                       proxyCredentials);
172             }
173         }
174
175         return client;
176     }
177
178     /**
179      * Set credentials for authentication.
180      *
181      * @param credentials The credentials to use for authentication.
182      */

183     public void setCredentials(Credentials credentials) {
184         hostCredentials = credentials;
185     }
186
187     /** Set proxy info, to use proxying.
188      */

189     public void setProxy(String JavaDoc host, int port)
190     {
191         this.proxyHost = host;
192         this.proxyPort = port;
193     }
194
195     /**
196      * Set credentials for authenticating against a proxy.
197      *
198      * @param credentials The credentials to use for authentication.
199      */

200     public void setProxyCredentials(Credentials credentials) {
201         proxyCredentials = credentials;
202     }
203
204     /**
205      * Close an session and delete the connection information.
206      *
207      * @exception IOException Error in closing socket.
208      */

209     public void closeSession()
210         throws IOException JavaDoc {
211         if (client != null) {
212             client.getHttpConnectionManager().getConnection(
213                 client.getHostConfiguration()).close();
214             client = null;
215         }
216     }
217
218
219     /**
220      * Close an session and delete the connection information.
221      *
222      * @param client The HttpClient instance.
223      * @exception IOException Error in closing socket.
224      * @deprecated Replaced by closeSession()
225      */

226     public synchronized void closeSession(HttpClient client)
227         throws IOException JavaDoc {
228         closeSession();
229     }
230
231
232     /**
233      * Progressing by the progress event.
234      *
235      * @param pe The progress event.
236      */

237     /*
238     public void progressing(ProgressEvent pe) {
239         if (debug > 3)
240             System.err.println("[EVENT/WebdavSession] " +
241                                "action:" + pe.getAction() +
242                                ((pe.getResourceName() == null) ? "" :
243                                ", resource:" + pe.getResourceName()) +
244                                ", soMany:" + pe.getSoMany() +
245                                ", remained:" + pe.getRemainedSize() +
246                                ", total:" + pe.getTotalSize());
247         getProgressUtil().fireProgress(pe);
248     }
249     */

250
251
252     /**
253      * Get the utility of this progress event and listener.
254      *
255      * @return ProgressUtil
256      */

257     /*
258     public ProgressUtil getProgressUtil() {
259         return sessionProgress;
260     }
261     */

262 }
263
Popular Tags