1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.servlet.HttpHeaders;
21  import com.liferay.portal.kernel.util.ContentTypes;
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.Http;
25  import com.liferay.portal.kernel.util.StringBundler;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.util.SystemProperties;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.UnsupportedEncodingException;
34  
35  import java.net.InetAddress;
36  import java.net.URL;
37  import java.net.URLConnection;
38  import java.net.URLDecoder;
39  import java.net.URLEncoder;
40  
41  import java.util.ArrayList;
42  import java.util.Date;
43  import java.util.LinkedHashMap;
44  import java.util.List;
45  import java.util.Map;
46  import java.util.StringTokenizer;
47  import java.util.regex.Pattern;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.RenderRequest;
51  
52  import javax.servlet.http.Cookie;
53  import javax.servlet.http.HttpServletRequest;
54  
55  import org.apache.commons.httpclient.Credentials;
56  import org.apache.commons.httpclient.Header;
57  import org.apache.commons.httpclient.HostConfiguration;
58  import org.apache.commons.httpclient.HttpClient;
59  import org.apache.commons.httpclient.HttpMethod;
60  import org.apache.commons.httpclient.HttpState;
61  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
62  import org.apache.commons.httpclient.NTCredentials;
63  import org.apache.commons.httpclient.NameValuePair;
64  import org.apache.commons.httpclient.URI;
65  import org.apache.commons.httpclient.UsernamePasswordCredentials;
66  import org.apache.commons.httpclient.auth.AuthPolicy;
67  import org.apache.commons.httpclient.auth.AuthScope;
68  import org.apache.commons.httpclient.cookie.CookiePolicy;
69  import org.apache.commons.httpclient.methods.DeleteMethod;
70  import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
71  import org.apache.commons.httpclient.methods.GetMethod;
72  import org.apache.commons.httpclient.methods.HeadMethod;
73  import org.apache.commons.httpclient.methods.PostMethod;
74  import org.apache.commons.httpclient.methods.PutMethod;
75  import org.apache.commons.httpclient.methods.RequestEntity;
76  import org.apache.commons.httpclient.methods.StringRequestEntity;
77  import org.apache.commons.httpclient.params.HttpClientParams;
78  import org.apache.commons.httpclient.params.HttpConnectionParams;
79  
80  /**
81   * <a href="HttpImpl.java.html"><b><i>View Source</i></b></a>
82   *
83   * @author Brian Wing Shun Chan
84   */
85  public class HttpImpl implements Http {
86  
87      public HttpImpl() {
88  
89          // Mimic behavior found in
90          // http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html
91  
92          if (Validator.isNotNull(_NON_PROXY_HOSTS)) {
93              String nonProxyHostsRegEx = _NON_PROXY_HOSTS;
94  
95              nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll(
96                  "\\.", "\\\\.");
97              nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll(
98                  "\\*", ".*?");
99              nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll(
100                 "\\|", ")|(");
101 
102             nonProxyHostsRegEx = "(" + nonProxyHostsRegEx + ")";
103 
104             _nonProxyHostsPattern = Pattern.compile(nonProxyHostsRegEx);
105         }
106 
107         MultiThreadedHttpConnectionManager httpConnectionManager =
108             new MultiThreadedHttpConnectionManager();
109 
110         HttpConnectionParams params = httpConnectionManager.getParams();
111 
112         params.setParameter(
113             "maxConnectionsPerHost", new Integer(_MAX_CONNECTIONS_PER_HOST));
114         params.setParameter(
115             "maxTotalConnections", new Integer(_MAX_TOTAL_CONNECTIONS));
116         params.setConnectionTimeout(_TIMEOUT);
117         params.setSoTimeout(_TIMEOUT);
118 
119         _client.setHttpConnectionManager(httpConnectionManager);
120         _proxyClient.setHttpConnectionManager(httpConnectionManager);
121 
122         if (hasProxyConfig() && Validator.isNotNull(_PROXY_USERNAME)) {
123             if (_PROXY_AUTH_TYPE.equals("username-password")) {
124                 _proxyCredentials = new UsernamePasswordCredentials(
125                     _PROXY_USERNAME, _PROXY_PASSWORD);
126             }
127             else if (_PROXY_AUTH_TYPE.equals("ntlm")) {
128                 _proxyCredentials = new NTCredentials(
129                     _PROXY_USERNAME, _PROXY_PASSWORD, _PROXY_NTLM_HOST,
130                     _PROXY_NTLM_DOMAIN);
131 
132                 List<String> authPrefs = new ArrayList<String>();
133 
134                 authPrefs.add(AuthPolicy.NTLM);
135                 authPrefs.add(AuthPolicy.BASIC);
136                 authPrefs.add(AuthPolicy.DIGEST);
137 
138                 _proxyClient.getParams().setParameter(
139                     AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
140             }
141         }
142     }
143 
144     public String addParameter(String url, String name, boolean value) {
145         return addParameter(url, name, String.valueOf(value));
146     }
147 
148     public String addParameter(String url, String name, double value) {
149         return addParameter(url, name, String.valueOf(value));
150     }
151 
152     public String addParameter(String url, String name, int value) {
153         return addParameter(url, name, String.valueOf(value));
154     }
155 
156     public String addParameter(String url, String name, long value) {
157         return addParameter(url, name, String.valueOf(value));
158     }
159 
160     public String addParameter(String url, String name, short value) {
161         return addParameter(url, name, String.valueOf(value));
162     }
163 
164     public String addParameter(String url, String name, String value) {
165         if (url == null) {
166             return null;
167         }
168 
169         String anchor = StringPool.BLANK;
170 
171         int pos = url.indexOf(StringPool.POUND);
172 
173         if (pos != -1) {
174             anchor = url.substring(pos);
175             url = url.substring(0, pos);
176         }
177 
178         if (url.indexOf(StringPool.QUESTION) == -1) {
179             url += StringPool.QUESTION;
180         }
181 
182         if (!url.endsWith(StringPool.QUESTION) &&
183             !url.endsWith(StringPool.AMPERSAND)) {
184 
185             url += StringPool.AMPERSAND;
186         }
187 
188         return url + name + StringPool.EQUAL + encodeURL(value) + anchor;
189     }
190 
191     public String decodeURL(String url) {
192         return decodeURL(url, false);
193     }
194 
195     public String decodeURL(String url, boolean unescapeSpace) {
196         if (url == null) {
197             return null;
198         }
199 
200         try {
201             url = URLDecoder.decode(url, StringPool.UTF8);
202 
203             if (unescapeSpace) {
204                 url = StringUtil.replace(url, "%20", StringPool.PLUS);
205             }
206 
207             return url;
208         }
209         catch (UnsupportedEncodingException uee) {
210             _log.error(uee, uee);
211 
212             return StringPool.BLANK;
213         }
214     }
215 
216     public void destroy() {
217         MultiThreadedHttpConnectionManager.shutdownAll();
218     }
219 
220     public String encodeURL(String url) {
221         return encodeURL(url, false);
222     }
223 
224     public String encodeURL(String url, boolean escapeSpaces) {
225         if (url == null) {
226             return null;
227         }
228 
229         try {
230             url = URLEncoder.encode(url, StringPool.UTF8);
231 
232             if (escapeSpaces) {
233                 url = StringUtil.replace(url, StringPool.PLUS, "%20");
234             }
235 
236             return url;
237         }
238         catch (UnsupportedEncodingException uee) {
239             _log.error(uee, uee);
240 
241             return StringPool.BLANK;
242         }
243     }
244 
245     public HttpClient getClient(HostConfiguration hostConfig) {
246         if (isProxyHost(hostConfig.getHost())) {
247             return _proxyClient;
248         }
249         else {
250             return _client;
251         }
252     }
253 
254     public String getCompleteURL(HttpServletRequest request) {
255         StringBuffer sb = request.getRequestURL();
256 
257         if (sb == null) {
258             sb = new StringBuffer();
259         }
260 
261         if (request.getQueryString() != null) {
262             sb.append(StringPool.QUESTION);
263             sb.append(request.getQueryString());
264         }
265 
266         String completeURL = sb.toString();
267 
268         if (_log.isWarnEnabled()) {
269             if (completeURL.contains("?&")) {
270                 _log.warn("Invalid url " + completeURL);
271             }
272         }
273 
274         return completeURL;
275     }
276 
277     public Cookie[] getCookies() {
278         return _cookies.get();
279     }
280 
281     public String getDomain(String url) {
282         url = removeProtocol(url);
283 
284         int pos = url.indexOf(StringPool.SLASH);
285 
286         if (pos != -1) {
287             return url.substring(0, pos);
288         }
289         else {
290             return url;
291         }
292     }
293 
294     public HostConfiguration getHostConfig(String location) throws IOException {
295         if (_log.isDebugEnabled()) {
296             _log.debug("Location is " + location);
297         }
298 
299         HostConfiguration hostConfig = new HostConfiguration();
300 
301         hostConfig.setHost(new URI(location, false));
302 
303         if (isProxyHost(hostConfig.getHost())) {
304             hostConfig.setProxy(_PROXY_HOST, _PROXY_PORT);
305         }
306 
307         return hostConfig;
308     }
309 
310     public String getIpAddress(String url) {
311         try {
312             URL urlObj = new URL(url);
313 
314             InetAddress address = InetAddress.getByName(urlObj.getHost());
315 
316             return address.getHostAddress();
317         }
318         catch (Exception e) {
319             return url;
320         }
321     }
322 
323     public String getParameter(String url, String name) {
324         return getParameter(url, name, true);
325     }
326 
327     public String getParameter(String url, String name, boolean escaped) {
328         if (Validator.isNull(url) || Validator.isNull(name)) {
329             return StringPool.BLANK;
330         }
331 
332         String[] parts = StringUtil.split(url, StringPool.QUESTION);
333 
334         if (parts.length == 2) {
335             String[] params = null;
336 
337             if (escaped) {
338                 params = StringUtil.split(parts[1], "&amp;");
339             }
340             else {
341                 params = StringUtil.split(parts[1], StringPool.AMPERSAND);
342             }
343 
344             for (int i = 0; i < params.length; i++) {
345                 String[] kvp = StringUtil.split(params[i], StringPool.EQUAL);
346 
347                 if ((kvp.length == 2) && kvp[0].equals(name)) {
348                     return kvp[1];
349                 }
350             }
351         }
352 
353         return StringPool.BLANK;
354     }
355 
356     public Map<String, String[]> getParameterMap(String queryString) {
357         return parameterMapFromString(queryString);
358     }
359 
360     public String getProtocol(ActionRequest actionRequest) {
361         return getProtocol(actionRequest.isSecure());
362     }
363 
364     public String getProtocol(boolean secure) {
365         if (!secure) {
366             return Http.HTTP;
367         }
368         else {
369             return Http.HTTPS;
370         }
371     }
372 
373     public String getProtocol(HttpServletRequest request) {
374         return getProtocol(request.isSecure());
375     }
376 
377     public String getProtocol(RenderRequest renderRequest) {
378         return getProtocol(renderRequest.isSecure());
379     }
380 
381     public String getProtocol(String url) {
382         int pos = url.indexOf(Http.PROTOCOL_DELIMITER);
383 
384         if (pos != -1) {
385             return url.substring(0, pos);
386         }
387         else {
388             return Http.HTTP;
389         }
390     }
391 
392     public String getQueryString(String url) {
393         if (Validator.isNull(url)) {
394             return url;
395         }
396 
397         int pos = url.indexOf(StringPool.QUESTION);
398 
399         if (pos == -1) {
400             return StringPool.BLANK;
401         }
402         else {
403             return url.substring(pos + 1, url.length());
404         }
405     }
406 
407     public String getRequestURL(HttpServletRequest request) {
408         return request.getRequestURL().toString();
409     }
410 
411     public boolean hasDomain(String url) {
412         return Validator.isNotNull(getDomain(url));
413     }
414 
415     public boolean hasProtocol(String url) {
416         int pos = url.indexOf(Http.PROTOCOL_DELIMITER);
417 
418         if (pos != -1) {
419             return true;
420         }
421         else {
422             return false;
423         }
424     }
425 
426     public boolean hasProxyConfig() {
427         if (Validator.isNotNull(_PROXY_HOST) && (_PROXY_PORT > 0)) {
428             return true;
429         }
430         else {
431             return false;
432         }
433     }
434 
435     public boolean isNonProxyHost(String host) {
436         if (_nonProxyHostsPattern == null ||
437             _nonProxyHostsPattern.matcher(host).matches()) {
438 
439             return true;
440         }
441         else {
442             return false;
443         }
444     }
445 
446     public boolean isProxyHost(String host) {
447         if (hasProxyConfig() && !isNonProxyHost(host)) {
448             return true;
449         }
450         else {
451             return false;
452         }
453     }
454 
455     public Map<String, String[]> parameterMapFromString(String queryString) {
456         Map<String, String[]> parameterMap =
457             new LinkedHashMap<String, String[]>();
458 
459         if (Validator.isNull(queryString)) {
460             return parameterMap;
461         }
462 
463         Map<String, List<String>> tempParameterMap =
464             new LinkedHashMap<String, List<String>>();
465 
466         StringTokenizer st = new StringTokenizer(
467             queryString, StringPool.AMPERSAND);
468 
469         while (st.hasMoreTokens()) {
470             String token = st.nextToken();
471 
472             if (Validator.isNotNull(token)) {
473                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
474 
475                 String key = kvp[0];
476 
477                 String value = StringPool.BLANK;
478 
479                 if (kvp.length > 1) {
480                     value = kvp[1];
481                 }
482 
483                 List<String> values = tempParameterMap.get(key);
484 
485                 if (values == null) {
486                     values = new ArrayList<String>();
487 
488                     tempParameterMap.put(key, values);
489                 }
490 
491                 values.add(value);
492             }
493         }
494 
495         for (Map.Entry<String, List<String>> entry :
496                 tempParameterMap.entrySet()) {
497 
498             String key = entry.getKey();
499             List<String> values = entry.getValue();
500 
501             parameterMap.put(key, values.toArray(new String[values.size()]));
502         }
503 
504         return parameterMap;
505     }
506 
507     public String parameterMapToString(Map<String, String[]> parameterMap) {
508         return parameterMapToString(parameterMap, true);
509     }
510 
511     public String parameterMapToString(
512         Map<String, String[]> parameterMap, boolean addQuestion) {
513 
514         StringBundler sb = new StringBundler();
515 
516         if (parameterMap.size() > 0) {
517             if (addQuestion) {
518                 sb.append(StringPool.QUESTION);
519             }
520 
521             for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
522                 String name = entry.getKey();
523                 String[] values = entry.getValue();
524 
525                 for (String value : values) {
526                     sb.append(name);
527                     sb.append(StringPool.EQUAL);
528                     sb.append(encodeURL(value));
529                     sb.append(StringPool.AMPERSAND);
530                 }
531             }
532 
533             if (sb.index() > 1) {
534                 sb.setIndex(sb.index() - 1);
535             }
536         }
537 
538         return sb.toString();
539     }
540 
541     public String protocolize(String url, ActionRequest actionRequest) {
542         return protocolize(url, actionRequest.isSecure());
543     }
544 
545     public String protocolize(String url, boolean secure) {
546         if (secure) {
547             if (url.startsWith(Http.HTTP_WITH_SLASH)) {
548                 return StringUtil.replace(
549                     url, Http.HTTP_WITH_SLASH, Http.HTTPS_WITH_SLASH);
550             }
551         }
552         else {
553             if (url.startsWith(Http.HTTPS_WITH_SLASH)) {
554                 return StringUtil.replace(
555                     url, Http.HTTPS_WITH_SLASH, Http.HTTP_WITH_SLASH);
556             }
557         }
558 
559         return url;
560     }
561 
562     public String protocolize(String url, HttpServletRequest request) {
563         return protocolize(url, request.isSecure());
564     }
565 
566     public String protocolize(String url, RenderRequest renderRequest) {
567         return protocolize(url, renderRequest.isSecure());
568     }
569 
570     public String removeDomain(String url) {
571         url = removeProtocol(url);
572 
573         int pos = url.indexOf(StringPool.SLASH);
574 
575         if (pos > 0) {
576             return url.substring(pos);
577         }
578         else {
579             return url;
580         }
581     }
582 
583     public String removeParameter(String url, String name) {
584         int pos = url.indexOf(StringPool.QUESTION);
585 
586         if (pos == -1) {
587             return url;
588         }
589 
590         String anchor = StringPool.BLANK;
591 
592         int anchorPos = url.indexOf(StringPool.POUND);
593 
594         if (anchorPos != -1) {
595             anchor = url.substring(anchorPos);
596             url = url.substring(0, anchorPos);
597         }
598 
599         StringBundler sb = new StringBundler();
600 
601         sb.append(url.substring(0, pos + 1));
602 
603         StringTokenizer st = new StringTokenizer(
604             url.substring(pos + 1, url.length()), StringPool.AMPERSAND);
605 
606         while (st.hasMoreTokens()) {
607             String token = st.nextToken();
608 
609             if (Validator.isNotNull(token)) {
610                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
611 
612                 String key = kvp[0];
613 
614                 String value = StringPool.BLANK;
615 
616                 if (kvp.length > 1) {
617                     value = kvp[1];
618                 }
619 
620                 if (!key.equals(name)) {
621                     sb.append(key);
622                     sb.append(StringPool.EQUAL);
623                     sb.append(value);
624                     sb.append(StringPool.AMPERSAND);
625                 }
626             }
627         }
628 
629         url = StringUtil.replace(
630             sb.toString(), StringPool.AMPERSAND + StringPool.AMPERSAND,
631             StringPool.AMPERSAND);
632 
633         if (url.endsWith(StringPool.AMPERSAND)) {
634             url = url.substring(0, url.length() - 1);
635         }
636 
637         if (url.endsWith(StringPool.QUESTION)) {
638             url = url.substring(0, url.length() - 1);
639         }
640 
641         return url + anchor;
642     }
643 
644     public String removeProtocol(String url) {
645         if (url.startsWith(Http.HTTP_WITH_SLASH)) {
646             return url.substring(Http.HTTP_WITH_SLASH.length() , url.length());
647         }
648         else if (url.startsWith(Http.HTTPS_WITH_SLASH)) {
649             return url.substring(Http.HTTPS_WITH_SLASH.length() , url.length());
650         }
651         else {
652             return url;
653         }
654     }
655 
656     public String setParameter(String url, String name, boolean value) {
657         return setParameter(url, name, String.valueOf(value));
658     }
659 
660     public String setParameter(String url, String name, double value) {
661         return setParameter(url, name, String.valueOf(value));
662     }
663 
664     public String setParameter(String url, String name, int value) {
665         return setParameter(url, name, String.valueOf(value));
666     }
667 
668     public String setParameter(String url, String name, long value) {
669         return setParameter(url, name, String.valueOf(value));
670     }
671 
672     public String setParameter(String url, String name, short value) {
673         return setParameter(url, name, String.valueOf(value));
674     }
675 
676     public String setParameter(String url, String name, String value) {
677         if (url == null) {
678             return null;
679         }
680 
681         url = removeParameter(url, name);
682 
683         return addParameter(url, name, value);
684     }
685 
686     public byte[] URLtoByteArray(Http.Options options) throws IOException {
687         return URLtoByteArray(
688             options.getLocation(), options.getMethod(), options.getHeaders(),
689             options.getCookies(), options.getAuth(), options.getBody(),
690             options.getParts(), options.getResponse(),
691             options.isFollowRedirects());
692     }
693 
694     public byte[] URLtoByteArray(String location) throws IOException {
695         Http.Options options = new Http.Options();
696 
697         options.setLocation(location);
698 
699         return URLtoByteArray(options);
700     }
701 
702     public byte[] URLtoByteArray(String location, boolean post)
703         throws IOException {
704 
705         Http.Options options = new Http.Options();
706 
707         options.setLocation(location);
708         options.setPost(post);
709 
710         return URLtoByteArray(options);
711     }
712 
713     public String URLtoString(Http.Options options) throws IOException {
714         return new String(URLtoByteArray(options));
715     }
716 
717     public String URLtoString(String location) throws IOException {
718         return new String(URLtoByteArray(location));
719     }
720 
721     public String URLtoString(String location, boolean post)
722         throws IOException {
723 
724         return new String(URLtoByteArray(location, post));
725     }
726 
727     /**
728      * This method only uses the default Commons HttpClient implementation when
729      * the URL object represents a HTTP resource. The URL object could also
730      * represent a file or some JNDI resource. In that case, the default Java
731      * implementation is used.
732      *
733      * @return A string representation of the resource referenced by the URL
734      *         object
735      */
736     public String URLtoString(URL url) throws IOException {
737         String xml = null;
738 
739         if (url != null) {
740             String protocol = url.getProtocol().toLowerCase();
741 
742             if (protocol.startsWith(Http.HTTP) ||
743                 protocol.startsWith(Http.HTTPS)) {
744 
745                 return URLtoString(url.toString());
746             }
747 
748             URLConnection con = url.openConnection();
749 
750             InputStream is = con.getInputStream();
751 
752             UnsyncByteArrayOutputStream ubaos =
753                 new UnsyncByteArrayOutputStream();
754             byte[] bytes = new byte[512];
755 
756             for (int i = is.read(bytes, 0, 512); i != -1;
757                     i = is.read(bytes, 0, 512)) {
758 
759                 ubaos.write(bytes, 0, i);
760             }
761 
762             xml = new String(ubaos.unsafeGetByteArray(), 0, ubaos.size());
763 
764             is.close();
765             ubaos.close();
766         }
767 
768         return xml;
769     }
770 
771     protected void proxifyState(HttpState state, HostConfiguration hostConfig) {
772         Credentials proxyCredentials = _proxyCredentials;
773 
774         String host = hostConfig.getHost();
775 
776         if (isProxyHost(host) && (proxyCredentials != null)) {
777             AuthScope scope = new AuthScope(_PROXY_HOST, _PROXY_PORT, null);
778 
779             state.setProxyCredentials(scope, proxyCredentials);
780         }
781     }
782 
783     protected org.apache.commons.httpclient.Cookie toCommonsCookie(
784         Cookie cookie) {
785 
786         org.apache.commons.httpclient.Cookie commonsCookie =
787             new org.apache.commons.httpclient.Cookie(
788             cookie.getDomain(), cookie.getName(), cookie.getValue(),
789             cookie.getPath(), cookie.getMaxAge(), cookie.getSecure());
790 
791         commonsCookie.setVersion(cookie.getVersion());
792 
793         return commonsCookie;
794     }
795 
796     protected org.apache.commons.httpclient.Cookie[] toCommonsCookies(
797         Cookie[] cookies) {
798 
799         if (cookies == null) {
800             return null;
801         }
802 
803         org.apache.commons.httpclient.Cookie[] commonCookies =
804             new org.apache.commons.httpclient.Cookie[cookies.length];
805 
806         for (int i = 0; i < cookies.length; i++) {
807             commonCookies[i] = toCommonsCookie(cookies[i]);
808         }
809 
810         return commonCookies;
811     }
812 
813     protected Cookie toServletCookie(
814         org.apache.commons.httpclient.Cookie commonsCookie) {
815 
816         Cookie cookie = new Cookie(
817             commonsCookie.getName(), commonsCookie.getValue());
818 
819         cookie.setDomain(commonsCookie.getDomain());
820 
821         Date expiryDate = commonsCookie.getExpiryDate();
822 
823         if (expiryDate != null) {
824             int maxAge =
825                 (int)(expiryDate.getTime() - System.currentTimeMillis());
826 
827             maxAge = maxAge / 1000;
828 
829             if (maxAge > -1) {
830                 cookie.setMaxAge(maxAge);
831             }
832         }
833 
834         cookie.setPath(commonsCookie.getPath());
835         cookie.setSecure(commonsCookie.getSecure());
836         cookie.setVersion(commonsCookie.getVersion());
837 
838         return cookie;
839     }
840 
841     protected Cookie[] toServletCookies(
842         org.apache.commons.httpclient.Cookie[] commonsCookies) {
843 
844         if (commonsCookies == null) {
845             return null;
846         }
847 
848         Cookie[] cookies = new Cookie[commonsCookies.length];
849 
850         for (int i = 0; i < commonsCookies.length; i++) {
851             cookies[i] = toServletCookie(commonsCookies[i]);
852         }
853 
854         return cookies;
855     }
856 
857     protected byte[] URLtoByteArray(
858             String location, Http.Method method, Map<String, String> headers,
859             Cookie[] cookies, Http.Auth auth, Http.Body body, Map<String,
860             String> parts, Http.Response response, boolean followRedirects)
861         throws IOException {
862 
863         byte[] bytes = null;
864 
865         HttpMethod httpMethod = null;
866         HttpState httpState = null;
867 
868         try {
869             _cookies.set(null);
870 
871             if (location == null) {
872                 return bytes;
873             }
874             else if (!location.startsWith(Http.HTTP_WITH_SLASH) &&
875                      !location.startsWith(Http.HTTPS_WITH_SLASH)) {
876 
877                 location = Http.HTTP_WITH_SLASH + location;
878             }
879 
880             HostConfiguration hostConfig = getHostConfig(location);
881 
882             HttpClient httpClient = getClient(hostConfig);
883 
884             if ((method == Http.Method.POST) ||
885                 (method == Http.Method.PUT)) {
886 
887                 if (method == Http.Method.POST) {
888                     httpMethod = new PostMethod(location);
889                 }
890                 else {
891                     httpMethod = new PutMethod(location);
892                 }
893 
894                 if (body != null) {
895                     RequestEntity requestEntity = new StringRequestEntity(
896                         body.getContent(), body.getContentType(),
897                         body.getCharset());
898 
899                     EntityEnclosingMethod entityEnclosingMethod =
900                         (EntityEnclosingMethod)httpMethod;
901 
902                     entityEnclosingMethod.setRequestEntity(requestEntity);
903                 }
904                 else if ((parts != null) && (parts.size() > 0) &&
905                          (method == Http.Method.POST)) {
906 
907                     List<NameValuePair> nvpList =
908                         new ArrayList<NameValuePair>();
909 
910                     for (Map.Entry<String, String> entry : parts.entrySet()) {
911                         String key = entry.getKey();
912                         String value = entry.getValue();
913 
914                         if (value != null) {
915                             nvpList.add(new NameValuePair(key, value));
916                         }
917                     }
918 
919                     NameValuePair[] nvpArray = nvpList.toArray(
920                         new NameValuePair[nvpList.size()]);
921 
922                     PostMethod postMethod = (PostMethod)httpMethod;
923 
924                     postMethod.setRequestBody(nvpArray);
925                 }
926             }
927             else if (method == Http.Method.DELETE) {
928                 httpMethod = new DeleteMethod(location);
929             }
930             else if (method == Http.Method.HEAD) {
931                 httpMethod = new HeadMethod(location);
932             }
933             else {
934                 httpMethod = new GetMethod(location);
935             }
936 
937             if (headers != null) {
938                 for (Map.Entry<String, String> header : headers.entrySet()) {
939                     httpMethod.addRequestHeader(
940                         header.getKey(), header.getValue());
941                 }
942             }
943 
944             if ((method == Http.Method.POST) || (method == Http.Method.PUT) &&
945                 (body != null)) {
946             }
947             else if (!_hasRequestHeader(httpMethod, HttpHeaders.CONTENT_TYPE)) {
948                 httpMethod.addRequestHeader(
949                     HttpHeaders.CONTENT_TYPE,
950                     ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED);
951             }
952 
953             if (!_hasRequestHeader(httpMethod, HttpHeaders.USER_AGENT)) {
954                 httpMethod.addRequestHeader(
955                     HttpHeaders.USER_AGENT, _DEFAULT_USER_AGENT);
956             }
957 
958             httpMethod.getParams().setIntParameter(
959                 HttpClientParams.SO_TIMEOUT, 0);
960 
961             httpState = new HttpState();
962 
963             if ((cookies != null) && (cookies.length > 0)) {
964                 org.apache.commons.httpclient.Cookie[] commonsCookies =
965                     toCommonsCookies(cookies);
966 
967                 httpState.addCookies(commonsCookies);
968 
969                 httpMethod.getParams().setCookiePolicy(
970                     CookiePolicy.BROWSER_COMPATIBILITY);
971             }
972 
973             if (auth != null) {
974                 httpMethod.setDoAuthentication(true);
975 
976                 httpState.setCredentials(
977                     new AuthScope(
978                         auth.getHost(), auth.getPort(), auth.getRealm()),
979                     new UsernamePasswordCredentials(
980                         auth.getUsername(), auth.getPassword()));
981             }
982 
983             proxifyState(httpState, hostConfig);
984 
985             httpClient.executeMethod(hostConfig, httpMethod, httpState);
986 
987             Header locationHeader = httpMethod.getResponseHeader("location");
988 
989             if ((locationHeader != null) && !locationHeader.equals(location)) {
990                 String redirect = locationHeader.getValue();
991 
992                 if (followRedirects) {
993                     return URLtoByteArray(
994                         redirect, Http.Method.GET, headers,
995                         cookies, auth, body, parts, response, followRedirects);
996                 }
997                 else {
998                     response.setRedirect(redirect);
999                 }
1000            }
1001
1002            InputStream is = httpMethod.getResponseBodyAsStream();
1003
1004            if (is != null) {
1005                Header contentLength = httpMethod.getResponseHeader(
1006                    HttpHeaders.CONTENT_LENGTH);
1007
1008                if (contentLength != null) {
1009                    response.setContentLength(
1010                        GetterUtil.getInteger(contentLength.getValue()));
1011                }
1012
1013                Header contentType = httpMethod.getResponseHeader(
1014                    HttpHeaders.CONTENT_TYPE);
1015
1016                if (contentType != null) {
1017                    response.setContentType(contentType.getValue());
1018                }
1019
1020                bytes = FileUtil.getBytes(is);
1021
1022                is.close();
1023            }
1024
1025            for (Header header : httpMethod.getResponseHeaders()) {
1026                response.addHeader(header.getName(), header.getValue());
1027            }
1028
1029            return bytes;
1030        }
1031        finally {
1032            try {
1033                if (httpState != null) {
1034                    _cookies.set(toServletCookies(httpState.getCookies()));
1035                }
1036            }
1037            catch (Exception e) {
1038                _log.error(e, e);
1039            }
1040
1041            try {
1042                if (httpMethod != null) {
1043                    httpMethod.releaseConnection();
1044                }
1045            }
1046            catch (Exception e) {
1047                _log.error(e, e);
1048            }
1049        }
1050    }
1051
1052    private boolean _hasRequestHeader(HttpMethod httpMethod, String name) {
1053        if (httpMethod.getRequestHeaders(name).length == 0) {
1054            return false;
1055        }
1056        else {
1057            return true;
1058        }
1059    }
1060
1061    private static final String _DEFAULT_USER_AGENT =
1062        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
1063
1064    private static final int _MAX_CONNECTIONS_PER_HOST = GetterUtil.getInteger(
1065        PropsUtil.get(HttpImpl.class.getName() + ".max.connections.per.host"),
1066        2);
1067
1068    private static final int _MAX_TOTAL_CONNECTIONS = GetterUtil.getInteger(
1069        PropsUtil.get(HttpImpl.class.getName() + ".max.total.connections"),
1070        20);
1071
1072    private static final String _NON_PROXY_HOSTS =
1073        SystemProperties.get("http.nonProxyHosts");
1074
1075    private static final String _PROXY_AUTH_TYPE = GetterUtil.getString(
1076        PropsUtil.get(HttpImpl.class.getName() + ".proxy.auth.type"));
1077
1078    private static final String _PROXY_HOST = GetterUtil.getString(
1079        SystemProperties.get("http.proxyHost"));
1080
1081    private static final String _PROXY_NTLM_DOMAIN = GetterUtil.getString(
1082        PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.domain"));
1083
1084    private static final String _PROXY_NTLM_HOST = GetterUtil.getString(
1085        PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.host"));
1086
1087    private static final String _PROXY_PASSWORD = GetterUtil.getString(
1088        PropsUtil.get(HttpImpl.class.getName() + ".proxy.password"));
1089
1090    private static final int _PROXY_PORT = GetterUtil.getInteger(
1091        SystemProperties.get("http.proxyPort"));
1092
1093    private static final String _PROXY_USERNAME = GetterUtil.getString(
1094        PropsUtil.get(HttpImpl.class.getName() + ".proxy.username"));
1095
1096    private static final int _TIMEOUT = GetterUtil.getInteger(
1097        PropsUtil.get(HttpImpl.class.getName() + ".timeout"), 5000);
1098
1099    private static Log _log = LogFactoryUtil.getLog(HttpImpl.class);
1100
1101    private static ThreadLocal<Cookie[]> _cookies = new ThreadLocal<Cookie[]>();
1102
1103    private HttpClient _client = new HttpClient();
1104    private Pattern _nonProxyHostsPattern;
1105    private HttpClient _proxyClient = new HttpClient();
1106    private Credentials _proxyCredentials;
1107
1108}