KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > curl > HttpConnection


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Nam Nguyen
28  */

29
30 package com.caucho.quercus.lib.curl;
31
32 import com.caucho.quercus.QuercusModuleException;
33
34 import java.io.Closeable JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38 import java.net.*;
39
40 /**
41  * Represents a HttpURLConnection wrapper.
42  */

43 public class HttpConnection
44   implements Closeable JavaDoc
45 {
46   private HttpURLConnection _conn;
47
48   private URL _URL;
49   private String JavaDoc _username;
50   private String JavaDoc _password;
51
52   private URL _proxyURL;
53   private String JavaDoc _proxyUsername;
54   private String JavaDoc _proxyPassword;
55   private String JavaDoc _proxyType;
56
57   private int _responseCode;
58   private boolean _hadSentAuthorization = false;
59   private boolean _hadSentProxyAuthorization = false;
60
61   private String JavaDoc _authorization;
62   private String JavaDoc _proxyAuthorization;
63
64   public HttpConnection(URL url,
65                               String JavaDoc username,
66                               String JavaDoc password)
67     throws IOException JavaDoc
68   {
69     _URL = url;
70     _username = username;
71     _password = password;
72
73     init();
74   }
75
76   public HttpConnection(URL url,
77                               String JavaDoc username,
78                               String JavaDoc password,
79                               URL proxyURL,
80                               String JavaDoc proxyUsername,
81                               String JavaDoc proxyPassword,
82                               String JavaDoc proxyType)
83     throws IOException JavaDoc
84   {
85     _URL = url;
86     _proxyURL = proxyURL;
87     _proxyType = proxyType;
88
89     _username = username;
90     _password = password;
91     _proxyUsername = proxyUsername;
92     _proxyPassword = proxyPassword;
93
94     init();
95   }
96
97   public void setConnectTimeout(int time)
98   {
99     _conn.setConnectTimeout(time);
100   }
101
102   public void setDoOutput(boolean doOutput)
103   {
104     _conn.setDoOutput(doOutput);
105   }
106
107   public void setInstanceFollowRedirects(boolean isToFollowRedirects)
108   {
109     _conn.setInstanceFollowRedirects(isToFollowRedirects);
110   }
111
112   public void setReadTimeout(int time)
113   {
114     _conn.setReadTimeout(time);
115   }
116
117   public void setRequestMethod(String JavaDoc method)
118     throws ProtocolException
119   {
120     _conn.setRequestMethod(method);
121   }
122
123   public void setRequestProperty(String JavaDoc key, String JavaDoc value)
124   {
125     _conn.setRequestProperty(key, value);
126   }
127
128   private void init()
129     throws IOException JavaDoc
130   {
131     Proxy proxy = Proxy.NO_PROXY;
132
133     if (_proxyURL != null) {
134       InetSocketAddress address
135           = new InetSocketAddress(_proxyURL.getHost(), _proxyURL.getPort());
136
137       proxy = new Proxy(Proxy.Type.valueOf(_proxyType), address);
138     }
139
140     _conn = (HttpURLConnection)_URL.openConnection(proxy);
141   }
142
143   /**
144    * Handles the authentication for this connection.
145    */

146   public void authenticate()
147     throws ConnectException, ProtocolException, SocketTimeoutException,
148             IOException JavaDoc
149   {
150     Proxy proxy = Proxy.NO_PROXY;
151
152     if (_proxyURL != null) {
153       InetSocketAddress address
154           = new InetSocketAddress(_proxyURL.getHost(), _proxyURL.getPort());
155
156       proxy = new Proxy(Proxy.Type.valueOf(_proxyType), address);
157     }
158
159     HttpURLConnection headConn = (HttpURLConnection)_URL.openConnection(proxy);
160     headConn.setRequestMethod("HEAD");
161
162     if (_proxyAuthorization != null)
163       headConn.setRequestProperty("Proxy-Authorization", _proxyAuthorization);
164
165     if (_authorization != null)
166       headConn.setRequestProperty("Authorization", _authorization);
167
168     headConn.connect();
169
170     int responseCode = headConn.getResponseCode();
171
172     if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH
173         && _proxyAuthorization == null)
174     {
175       String JavaDoc header = headConn.getHeaderField("Proxy-Authenticate");
176
177       _proxyAuthorization = getAuthorization(_URL,
178                                             _conn.getRequestMethod(),
179                                             header,
180                                             "Proxy-Authorization",
181                                             _proxyUsername,
182                                             _proxyPassword);
183       authenticate();
184     }
185     else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED
186         && _authorization == null)
187     {
188       String JavaDoc header = headConn.getHeaderField("WWW-Authenticate");
189
190       _authorization = getAuthorization(_URL,
191                                        _conn.getRequestMethod(),
192                                        header,
193                                        "Authorization",
194                                        _username,
195                                        _password);
196       authenticate();
197     }
198
199     headConn.disconnect();
200   }
201
202   /**
203    * Connects to the server.
204    */

205   public void connect()
206     throws ConnectException, ProtocolException, SocketTimeoutException,
207             IOException JavaDoc
208   {
209     if (_username != null || _proxyUsername != null)
210       authenticate();
211
212     if (_proxyAuthorization != null)
213       _conn.setRequestProperty("Proxy-Authorization", _proxyAuthorization);
214     if (_authorization != null)
215       _conn.setRequestProperty("Authorization", _authorization);
216
217     _conn.connect();
218   }
219
220   /**
221    * Returns the authorization response.
222    */

223   private final String JavaDoc getAuthorization(URL url,
224                                         String JavaDoc requestMethod,
225                                         String JavaDoc header,
226                                         String JavaDoc clientField,
227                                         String JavaDoc username,
228                                         String JavaDoc password)
229     throws ConnectException, SocketTimeoutException, IOException JavaDoc
230   {
231     if (username == null || password == null)
232       return "";
233
234     String JavaDoc uri = url.getFile();
235     if (uri.length() == 0)
236       uri = "/";
237
238     String JavaDoc auth = Authentication.getAuthorization(username,
239                                                   password,
240                                                   requestMethod,
241                                                   uri,
242                                                   header);
243
244     return auth;
245   }
246
247   public int getContentLength()
248   {
249     return _conn.getContentLength();
250   }
251
252   public InputStream JavaDoc getErrorStream()
253   {
254     return _conn.getErrorStream();
255   }
256
257   public String JavaDoc getHeaderField(String JavaDoc key)
258   {
259     return _conn.getHeaderField(key);
260   }
261
262   public String JavaDoc getHeaderField(int i)
263   {
264     return _conn.getHeaderField(i);
265   }
266
267   public String JavaDoc getHeaderFieldKey(int i)
268   {
269     return _conn.getHeaderFieldKey(i);
270   }
271
272   public InputStream JavaDoc getInputStream()
273     throws IOException JavaDoc
274   {
275     return _conn.getInputStream();
276   }
277
278   public OutputStream JavaDoc getOutputStream()
279     throws IOException JavaDoc
280   {
281     return _conn.getOutputStream();
282   }
283
284   public int getResponseCode()
285   {
286     try {
287       return _conn.getResponseCode();
288     }
289     catch (IOException JavaDoc e) {
290       throw new QuercusModuleException(e);
291     }
292   }
293
294   public void disconnect()
295   {
296     close();
297   }
298
299   public void close()
300   {
301     if (_conn != null)
302       _conn.disconnect();
303   }
304 }
305
Popular Tags