KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > runtime > web > HttpClientManager


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

37 package org.webharvest.runtime.web;
38
39 import java.io.IOException JavaDoc;
40 import java.io.UnsupportedEncodingException JavaDoc;
41 import java.net.*;
42 import java.util.*;
43
44 import org.apache.commons.httpclient.*;
45 import org.apache.commons.httpclient.auth.*;
46 import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
47 import org.apache.commons.httpclient.methods.GetMethod;
48 import org.apache.commons.httpclient.methods.PostMethod;
49 import org.apache.commons.httpclient.params.HttpClientParams;
50 import org.apache.commons.httpclient.protocol.Protocol;
51 import org.webharvest.utils.CommonUtil;
52
53 /**
54  * HTTP client functionality.
55  */

56 public class HttpClientManager {
57
58     public static final String JavaDoc DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1";
59
60     private HttpClient client;
61
62     /**
63      * Constructor.
64      */

65     public HttpClientManager() {
66         client = new HttpClient();
67         
68         HttpClientParams clientParams = new HttpClientParams();
69         clientParams.setBooleanParameter("http.protocol.allow-circular-redirects", true);
70         client.setParams(clientParams);
71         
72         // registers default handling for https
73
Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
74         
75         //client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
76
}
77
78     /**
79      * Defines HTTP proxy for the client with specified host and port
80      * @param hostName
81      * @param hostPort
82      */

83     public void setHttpProxy(String JavaDoc hostName, int hostPort) {
84         client.getHostConfiguration().setProxyHost(new ProxyHost(hostName, hostPort));
85     }
86
87     /**
88      * Defines HTTP proxy for the client with specified host
89      * @param hostName
90      */

91     public void setHttpProxy(String JavaDoc hostName) {
92         client.getHostConfiguration().setProxyHost(new ProxyHost(hostName));
93     }
94
95
96     /**
97      * Defines user credintials for the HTTP proxy server
98      * @param username
99      * @param password
100      */

101     public void setHttpProxyCredentials(String JavaDoc username, String JavaDoc password) {
102         client.getState().setProxyCredentials(
103                 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
104                 new UsernamePasswordCredentials(username, password)
105         );
106     }
107     
108     public HttpResponseWrapper execute(
109             String JavaDoc methodType,
110             String JavaDoc url,
111             String JavaDoc charset,
112             String JavaDoc username,
113             String JavaDoc password,
114             List params,
115             Map headers ) {
116         if ( !url.startsWith("http://") && !url.startsWith("https://") ) {
117             url = "http://" + url;
118         }
119
120         url = CommonUtil.encodeUrl(url, charset);
121         
122         // if username and password are specified, define new credentials for authenticaton
123
if ( username != null && password != null ) {
124             try {
125                 URL urlObj = new URL(url);
126                 client.getState().setCredentials(
127                     new AuthScope(urlObj.getHost(), urlObj.getPort()),
128                     new UsernamePasswordCredentials(username, password)
129                 );
130             } catch (MalformedURLException e) {
131                 e.printStackTrace();
132             }
133         }
134         
135         HttpMethodBase method;
136         if ( "post".equalsIgnoreCase(methodType) ) {
137             method = createPostMethod(url, params);
138         } else {
139             method = createGetMethod(url, params, charset);
140         }
141         
142         identifyAsDefaultBrowser(method);
143
144         // define request headers, if any exist
145
if (headers != null) {
146             Iterator it = headers.keySet().iterator();
147             while (it.hasNext()) {
148                 String JavaDoc headerName = (String JavaDoc) it.next();
149                 String JavaDoc headerValue = (String JavaDoc) headers.get(headerName);
150                 method.addRequestHeader(new Header(headerName, headerValue));
151             }
152         }
153
154         try {
155             int statusCode = client.executeMethod(method);
156             
157             // if there is redirection, try to download redirection page
158
if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) ||
159                 (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) ||
160                 (statusCode == HttpStatus.SC_SEE_OTHER) ||
161                 (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
162                 Header header = method.getResponseHeader("location");
163                 if (header != null) {
164                     String JavaDoc newURI = header.getValue();
165                     if ( newURI != null && !newURI.equals("") ) {
166                         newURI = CommonUtil.fullUrl(url, newURI);
167                         method.releaseConnection();
168                         method = new GetMethod(newURI);
169                         identifyAsDefaultBrowser(method);
170                         client.executeMethod(method);
171                     }
172                 }
173             }
174
175             return new HttpResponseWrapper(method);
176         } catch (IOException JavaDoc e) {
177             throw new org.webharvest.exception.HttpException("IO error during HTTP execution for URL: " + url, e);
178         } finally {
179             method.releaseConnection();
180         }
181     }
182
183     /**
184      * Defines "User-Agent" HTTP header.
185      * @param method
186      */

187     private void identifyAsDefaultBrowser(HttpMethodBase method) {
188         method.addRequestHeader(new Header("User-Agent", DEFAULT_USER_AGENT));
189     }
190
191     private HttpMethodBase createPostMethod(String JavaDoc url, List params) {
192         PostMethod method = new PostMethod(url);
193
194         if (params != null) {
195             NameValuePair[] paramArray = new NameValuePair[params.size()];
196             Iterator it = params.iterator();
197             int index = 0;
198             while (it.hasNext()) {
199                 paramArray[index++] = (NameValuePair) it.next();
200             }
201
202             method.setRequestBody(paramArray);
203         }
204
205         return method;
206     }
207
208     private GetMethod createGetMethod(String JavaDoc url, List params, String JavaDoc charset) {
209         if (params != null) {
210             String JavaDoc urlParams = "";
211             Iterator it = params.iterator();
212             while (it.hasNext()) {
213                 NameValuePair pair = (NameValuePair) it.next();
214                 try {
215                     urlParams += pair.getName() + "=" + URLEncoder.encode(pair.getValue(), charset) + "&";
216                 } catch (UnsupportedEncodingException JavaDoc e) {
217                     throw new org.webharvest.exception.HttpException("Charset " + charset + " is not supported!", e);
218                 }
219             }
220
221             if (url.indexOf("?") < 0) {
222                 url += "?" + urlParams;
223             } else if (url.endsWith("&")) {
224                 url += urlParams;
225             } else {
226                 url += "&" + urlParams;
227             }
228         }
229
230         return new GetMethod(url);
231     }
232
233 }
Popular Tags