1 37 package org.webharvest.runtime.web; 38 39 import java.io.IOException ; 40 import java.io.UnsupportedEncodingException ; 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 56 public class HttpClientManager { 57 58 public static final String 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 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 Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443)); 74 75 } 77 78 83 public void setHttpProxy(String hostName, int hostPort) { 84 client.getHostConfiguration().setProxyHost(new ProxyHost(hostName, hostPort)); 85 } 86 87 91 public void setHttpProxy(String hostName) { 92 client.getHostConfiguration().setProxyHost(new ProxyHost(hostName)); 93 } 94 95 96 101 public void setHttpProxyCredentials(String username, String 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 methodType, 110 String url, 111 String charset, 112 String username, 113 String 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 != 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 if (headers != null) { 146 Iterator it = headers.keySet().iterator(); 147 while (it.hasNext()) { 148 String headerName = (String ) it.next(); 149 String headerValue = (String ) headers.get(headerName); 150 method.addRequestHeader(new Header(headerName, headerValue)); 151 } 152 } 153 154 try { 155 int statusCode = client.executeMethod(method); 156 157 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 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 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 187 private void identifyAsDefaultBrowser(HttpMethodBase method) { 188 method.addRequestHeader(new Header("User-Agent", DEFAULT_USER_AGENT)); 189 } 190 191 private HttpMethodBase createPostMethod(String 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 url, List params, String charset) { 209 if (params != null) { 210 String 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 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 |