1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ByteArrayMaker;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.Http;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.util.SystemProperties;
35  
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.io.UnsupportedEncodingException;
39  
40  import java.net.URL;
41  import java.net.URLConnection;
42  import java.net.URLDecoder;
43  import java.net.URLEncoder;
44  
45  import java.util.ArrayList;
46  import java.util.LinkedHashMap;
47  import java.util.List;
48  import java.util.Map;
49  import java.util.StringTokenizer;
50  import java.util.regex.Pattern;
51  
52  import javax.portlet.ActionRequest;
53  import javax.portlet.RenderRequest;
54  
55  import javax.servlet.http.Cookie;
56  import javax.servlet.http.HttpServletRequest;
57  
58  import org.apache.commons.httpclient.Credentials;
59  import org.apache.commons.httpclient.Header;
60  import org.apache.commons.httpclient.HostConfiguration;
61  import org.apache.commons.httpclient.HttpClient;
62  import org.apache.commons.httpclient.HttpMethod;
63  import org.apache.commons.httpclient.HttpState;
64  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
65  import org.apache.commons.httpclient.NTCredentials;
66  import org.apache.commons.httpclient.NameValuePair;
67  import org.apache.commons.httpclient.URI;
68  import org.apache.commons.httpclient.UsernamePasswordCredentials;
69  import org.apache.commons.httpclient.auth.AuthPolicy;
70  import org.apache.commons.httpclient.auth.AuthScope;
71  import org.apache.commons.httpclient.cookie.CookiePolicy;
72  import org.apache.commons.httpclient.methods.GetMethod;
73  import org.apache.commons.httpclient.methods.PostMethod;
74  import org.apache.commons.httpclient.methods.RequestEntity;
75  import org.apache.commons.httpclient.methods.StringRequestEntity;
76  import org.apache.commons.httpclient.params.HttpConnectionParams;
77  
78  /**
79   * <a href="HttpImpl.java.html"><b><i>View Source</i></b></a>
80   *
81   * @author Brian Wing Shun Chan
82   *
83   */
84  public class HttpImpl implements Http {
85  
86      public HttpImpl() {
87  
88          // Mimic behavior found in
89          // http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html
90  
91          if (Validator.isNotNull(_NON_PROXY_HOSTS)) {
92              String nonProxyHostsRegEx = _NON_PROXY_HOSTS;
93  
94              nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll(
95                  "\\.", "\\\\.");
96              nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll(
97                  "\\*", ".*?");
98              nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll(
99                  "\\|", ")|(");
100 
101             nonProxyHostsRegEx = "(" + nonProxyHostsRegEx + ")";
102 
103             _nonProxyHostsPattern = Pattern.compile(nonProxyHostsRegEx);
104         }
105 
106         MultiThreadedHttpConnectionManager connectionManager =
107             new MultiThreadedHttpConnectionManager();
108 
109         HttpConnectionParams params = connectionManager.getParams();
110 
111         params.setParameter(
112             "maxConnectionsPerHost", new Integer(_MAX_CONNECTIONS_PER_HOST));
113         params.setParameter(
114             "maxTotalConnections", new Integer(_MAX_TOTAL_CONNECTIONS));
115         params.setConnectionTimeout(_TIMEOUT);
116         params.setSoTimeout(_TIMEOUT);
117 
118         _client.setHttpConnectionManager(connectionManager);
119         _proxyClient.setHttpConnectionManager(connectionManager);
120 
121         if (hasProxyConfig() && Validator.isNotNull(_PROXY_USERNAME)) {
122             if (_PROXY_AUTH_TYPE.equals("username-password")) {
123                 _proxyCredentials = new UsernamePasswordCredentials(
124                     _PROXY_USERNAME, _PROXY_PASSWORD);
125             }
126             else if (_PROXY_AUTH_TYPE.equals("ntlm")) {
127                 _proxyCredentials = new NTCredentials(
128                     _PROXY_USERNAME, _PROXY_PASSWORD, _PROXY_NTLM_HOST,
129                     _PROXY_NTLM_DOMAIN);
130 
131                 List<String> authPrefs = new ArrayList<String>();
132 
133                 authPrefs.add(AuthPolicy.NTLM);
134                 authPrefs.add(AuthPolicy.BASIC);
135                 authPrefs.add(AuthPolicy.DIGEST);
136 
137                 _proxyClient.getParams().setParameter(
138                     AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
139             }
140         }
141     }
142 
143     public String addParameter(String url, String name, boolean value) {
144         return addParameter(url, name, String.valueOf(value));
145     }
146 
147     public String addParameter(String url, String name, double value) {
148         return addParameter(url, name, String.valueOf(value));
149     }
150 
151     public String addParameter(String url, String name, int value) {
152         return addParameter(url, name, String.valueOf(value));
153     }
154 
155     public String addParameter(String url, String name, long value) {
156         return addParameter(url, name, String.valueOf(value));
157     }
158 
159     public String addParameter(String url, String name, short value) {
160         return addParameter(url, name, String.valueOf(value));
161     }
162 
163     public String addParameter(String url, String name, String value) {
164         if (url == null) {
165             return null;
166         }
167 
168         String anchor = StringPool.BLANK;
169 
170         int pos = url.indexOf(StringPool.POUND);
171 
172         if (pos != -1) {
173             anchor = url.substring(pos);
174             url = url.substring(0, pos);
175         }
176 
177         if (url.indexOf(StringPool.QUESTION) == -1) {
178             url += StringPool.QUESTION;
179         }
180 
181         if (!url.endsWith(StringPool.QUESTION) &&
182             !url.endsWith(StringPool.AMPERSAND)) {
183 
184             url += StringPool.AMPERSAND;
185         }
186 
187         return url + name + StringPool.EQUAL + encodeURL(value) + anchor;
188     }
189 
190     public String decodeURL(String url) {
191         return decodeURL(url, false);
192     }
193 
194     public String decodeURL(String url, boolean unescapeSpace) {
195         if (url == null) {
196             return null;
197         }
198 
199         try {
200             url = URLDecoder.decode(url, StringPool.UTF8);
201 
202             if (unescapeSpace) {
203                 url = StringUtil.replace(url, "%20", StringPool.PLUS);
204             }
205 
206             return url;
207         }
208         catch (UnsupportedEncodingException uee) {
209             _log.error(uee, uee);
210 
211             return StringPool.BLANK;
212         }
213     }
214 
215     public String encodeURL(String url) {
216         return encodeURL(url, false);
217     }
218 
219     public String encodeURL(String url, boolean escapeSpaces) {
220         if (url == null) {
221             return null;
222         }
223 
224         try {
225             url = URLEncoder.encode(url, StringPool.UTF8);
226 
227             if (escapeSpaces) {
228                 url = StringUtil.replace(url, StringPool.PLUS, "%20");
229             }
230 
231             return url;
232         }
233         catch (UnsupportedEncodingException uee) {
234             _log.error(uee, uee);
235 
236             return StringPool.BLANK;
237         }
238     }
239 
240     public HttpClient getClient(HostConfiguration hostConfig) {
241         if (isProxyHost(hostConfig.getHost())) {
242             return _proxyClient;
243         }
244         else {
245             return _client;
246         }
247     }
248 
249     public String getCompleteURL(HttpServletRequest request) {
250         StringBuffer sb = request.getRequestURL();
251 
252         if (sb == null) {
253             sb = new StringBuffer();
254         }
255 
256         if (request.getQueryString() != null) {
257             sb.append(StringPool.QUESTION);
258             sb.append(request.getQueryString());
259         }
260 
261         String completeURL = sb.toString();
262 
263         if (_log.isWarnEnabled()) {
264             if (completeURL.contains("?&")) {
265                 _log.warn("Invalid url " + completeURL);
266             }
267         }
268 
269         return completeURL;
270     }
271 
272     public String getDomain(String url) {
273         url = removeProtocol(url);
274 
275         int pos = url.indexOf(StringPool.SLASH);
276 
277         if (pos != -1) {
278             return url.substring(0, pos);
279         }
280         else {
281             return url;
282         }
283     }
284 
285     public HostConfiguration getHostConfig(String location) throws IOException {
286         if (_log.isDebugEnabled()) {
287             _log.debug("Location is " + location);
288         }
289 
290         HostConfiguration hostConfig = new HostConfiguration();
291 
292         hostConfig.setHost(new URI(location, false));
293 
294         if (isProxyHost(hostConfig.getHost())) {
295             hostConfig.setProxy(_PROXY_HOST, _PROXY_PORT);
296         }
297 
298         return hostConfig;
299     }
300 
301     public String getParameter(String url, String name) {
302         return getParameter(url, name, true);
303     }
304 
305     public String getParameter(String url, String name, boolean escaped) {
306         if (Validator.isNull(url) || Validator.isNull(name)) {
307             return StringPool.BLANK;
308         }
309 
310         String[] parts = StringUtil.split(url, StringPool.QUESTION);
311 
312         if (parts.length == 2) {
313             String[] params = null;
314 
315             if (escaped) {
316                 params = StringUtil.split(parts[1], "&amp;");
317             }
318             else {
319                 params = StringUtil.split(parts[1], StringPool.AMPERSAND);
320             }
321 
322             for (int i = 0; i < params.length; i++) {
323                 String[] kvp = StringUtil.split(params[i], StringPool.EQUAL);
324 
325                 if ((kvp.length == 2) && kvp[0].equals(name)) {
326                     return kvp[1];
327                 }
328             }
329         }
330 
331         return StringPool.BLANK;
332     }
333 
334     public Map<String, String[]> getParameterMap(String queryString) {
335         return parameterMapFromString(queryString);
336     }
337 
338     public String getProtocol(ActionRequest actionRequest) {
339         return getProtocol(actionRequest.isSecure());
340     }
341 
342     public String getProtocol(boolean secure) {
343         if (!secure) {
344             return Http.HTTP;
345         }
346         else {
347             return Http.HTTPS;
348         }
349     }
350 
351     public String getProtocol(HttpServletRequest request) {
352         return getProtocol(request.isSecure());
353     }
354 
355     public String getProtocol(RenderRequest renderRequest) {
356         return getProtocol(renderRequest.isSecure());
357     }
358 
359     public String getProtocol(String url) {
360         int pos = url.indexOf(Http.PROTOCOL_DELIMITER);
361 
362         if (pos != -1) {
363             return url.substring(0, pos);
364         }
365         else {
366             return Http.HTTP;
367         }
368     }
369 
370     public String getQueryString(String url) {
371         if (Validator.isNull(url)) {
372             return url;
373         }
374 
375         int pos = url.indexOf(StringPool.QUESTION);
376 
377         if (pos == -1) {
378             return StringPool.BLANK;
379         }
380         else {
381             return url.substring(pos + 1, url.length());
382         }
383     }
384 
385     public String getRequestURL(HttpServletRequest request) {
386         return request.getRequestURL().toString();
387     }
388 
389     public boolean hasDomain(String url) {
390         return Validator.isNotNull(getDomain(url));
391     }
392 
393     public boolean hasProxyConfig() {
394         if (Validator.isNotNull(_PROXY_HOST) && (_PROXY_PORT > 0)) {
395             return true;
396         }
397         else {
398             return false;
399         }
400     }
401 
402     public boolean isNonProxyHost(String host) {
403         if (_nonProxyHostsPattern == null ||
404             _nonProxyHostsPattern.matcher(host).matches()) {
405 
406             return true;
407         }
408         else {
409             return false;
410         }
411     }
412 
413     public boolean isProxyHost(String host) {
414         if (hasProxyConfig() && !isNonProxyHost(host)) {
415             return true;
416         }
417         else {
418             return false;
419         }
420     }
421 
422     public Map<String, String[]> parameterMapFromString(String queryString) {
423         Map<String, String[]> parameterMap =
424             new LinkedHashMap<String, String[]>();
425 
426         if (Validator.isNull(queryString)) {
427             return parameterMap;
428         }
429 
430         Map<String, List<String>> tempParameterMap =
431             new LinkedHashMap<String, List<String>>();
432 
433         StringTokenizer st = new StringTokenizer(
434             queryString, StringPool.AMPERSAND);
435 
436         while (st.hasMoreTokens()) {
437             String token = st.nextToken();
438 
439             if (Validator.isNotNull(token)) {
440                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
441 
442                 String key = kvp[0];
443 
444                 String value = StringPool.BLANK;
445 
446                 if (kvp.length > 1) {
447                     value = kvp[1];
448                 }
449 
450                 List<String> values = tempParameterMap.get(key);
451 
452                 if (values == null) {
453                     values = new ArrayList<String>();
454 
455                     tempParameterMap.put(key, values);
456                 }
457 
458                 values.add(value);
459             }
460         }
461 
462         for (Map.Entry<String, List<String>> entry :
463                 tempParameterMap.entrySet()) {
464 
465             String key = entry.getKey();
466             List<String> values = entry.getValue();
467 
468             parameterMap.put(key, values.toArray(new String[values.size()]));
469         }
470 
471         return parameterMap;
472     }
473 
474     public String parameterMapToString(Map<String, String[]> parameterMap) {
475         return parameterMapToString(parameterMap, true);
476     }
477 
478     public String parameterMapToString(
479         Map<String, String[]> parameterMap, boolean addQuestion) {
480 
481         StringBuilder sb = new StringBuilder();
482 
483         if (parameterMap.size() > 0) {
484             if (addQuestion) {
485                 sb.append(StringPool.QUESTION);
486             }
487 
488             for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
489                 String name = entry.getKey();
490                 String[] values = entry.getValue();
491 
492                 for (String value : values) {
493                     sb.append(name);
494                     sb.append(StringPool.EQUAL);
495                     sb.append(encodeURL(value));
496                     sb.append(StringPool.AMPERSAND);
497                 }
498             }
499 
500             sb.deleteCharAt(sb.length() - 1);
501         }
502 
503         return sb.toString();
504     }
505 
506     public String protocolize(String url, ActionRequest actionRequest) {
507         return protocolize(url, actionRequest.isSecure());
508     }
509 
510     public String protocolize(String url, boolean secure) {
511         if (secure) {
512             if (url.startsWith(Http.HTTP_WITH_SLASH)) {
513                 return StringUtil.replace(
514                     url, Http.HTTP_WITH_SLASH, Http.HTTPS_WITH_SLASH);
515             }
516         }
517         else {
518             if (url.startsWith(Http.HTTPS_WITH_SLASH)) {
519                 return StringUtil.replace(
520                     url, Http.HTTPS_WITH_SLASH, Http.HTTP_WITH_SLASH);
521             }
522         }
523 
524         return url;
525     }
526 
527     public String protocolize(String url, HttpServletRequest request) {
528         return protocolize(url, request.isSecure());
529     }
530 
531     public String protocolize(String url, RenderRequest renderRequest) {
532         return protocolize(url, renderRequest.isSecure());
533     }
534 
535     public String removeDomain(String url) {
536         url = removeProtocol(url);
537 
538         int pos = url.indexOf(StringPool.SLASH);
539 
540         if (pos > 0) {
541             return url.substring(pos);
542         }
543         else {
544             return url;
545         }
546     }
547 
548     public String removeParameter(String url, String name) {
549         int pos = url.indexOf(StringPool.QUESTION);
550 
551         if (pos == -1) {
552             return url;
553         }
554 
555         String anchor = StringPool.BLANK;
556 
557         int anchorPos = url.indexOf(StringPool.POUND);
558 
559         if (anchorPos != -1) {
560             anchor = url.substring(anchorPos);
561             url = url.substring(0, anchorPos);
562         }
563 
564         StringBuilder sb = new StringBuilder();
565 
566         sb.append(url.substring(0, pos + 1));
567 
568         StringTokenizer st = new StringTokenizer(
569             url.substring(pos + 1, url.length()), StringPool.AMPERSAND);
570 
571         while (st.hasMoreTokens()) {
572             String token = st.nextToken();
573 
574             if (Validator.isNotNull(token)) {
575                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
576 
577                 String key = kvp[0];
578 
579                 String value = StringPool.BLANK;
580 
581                 if (kvp.length > 1) {
582                     value = kvp[1];
583                 }
584 
585                 if (!key.equals(name)) {
586                     sb.append(key);
587                     sb.append(StringPool.EQUAL);
588                     sb.append(value);
589                     sb.append(StringPool.AMPERSAND);
590                 }
591             }
592         }
593 
594         url = StringUtil.replace(
595             sb.toString(), StringPool.AMPERSAND + StringPool.AMPERSAND,
596             StringPool.AMPERSAND);
597 
598         if (url.endsWith(StringPool.AMPERSAND)) {
599             url = url.substring(0, url.length() - 1);
600         }
601 
602         if (url.endsWith(StringPool.QUESTION)) {
603             url = url.substring(0, url.length() - 1);
604         }
605 
606         return url + anchor;
607     }
608 
609     public String removeProtocol(String url) {
610         if (url.startsWith(Http.HTTP_WITH_SLASH)) {
611             return url.substring(Http.HTTP_WITH_SLASH.length() , url.length());
612         }
613         else if (url.startsWith(Http.HTTPS_WITH_SLASH)) {
614             return url.substring(Http.HTTPS_WITH_SLASH.length() , url.length());
615         }
616         else {
617             return url;
618         }
619     }
620 
621     public String setParameter(String url, String name, boolean value) {
622         return setParameter(url, name, String.valueOf(value));
623     }
624 
625     public String setParameter(String url, String name, double value) {
626         return setParameter(url, name, String.valueOf(value));
627     }
628 
629     public String setParameter(String url, String name, int value) {
630         return setParameter(url, name, String.valueOf(value));
631     }
632 
633     public String setParameter(String url, String name, long value) {
634         return setParameter(url, name, String.valueOf(value));
635     }
636 
637     public String setParameter(String url, String name, short value) {
638         return setParameter(url, name, String.valueOf(value));
639     }
640 
641     public String setParameter(String url, String name, String value) {
642         if (url == null) {
643             return null;
644         }
645 
646         url = removeParameter(url, name);
647 
648         return addParameter(url, name, value);
649     }
650 
651     public byte[] URLtoByteArray(String location) throws IOException {
652         return URLtoByteArray(location, null, null, null, null, false);
653     }
654 
655     public byte[] URLtoByteArray(String location, boolean post)
656         throws IOException {
657 
658         return URLtoByteArray(location, null, null, null, null, post);
659     }
660 
661     public byte[] URLtoByteArray(
662             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
663             boolean post)
664         throws IOException {
665 
666         return URLtoByteArray(location, cookies, auth, body, null, post);
667     }
668 
669     public byte[] URLtoByteArray(
670             String location, Cookie[] cookies, Http.Auth auth,
671             Map<String, String> parts, boolean post)
672         throws IOException {
673 
674         return URLtoByteArray(location, cookies, auth, null, parts, post);
675     }
676 
677     public String URLtoString(String location) throws IOException {
678         return new String(URLtoByteArray(location));
679     }
680 
681     public String URLtoString(String location, boolean post)
682         throws IOException {
683 
684         return new String(URLtoByteArray(location, post));
685     }
686 
687     public String URLtoString(
688             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
689             boolean post)
690         throws IOException {
691 
692         return new String(URLtoByteArray(location, cookies, auth, body, post));
693     }
694 
695     public String URLtoString(
696             String location, Cookie[] cookies, Http.Auth auth,
697             Map<String, String> parts, boolean post)
698         throws IOException {
699 
700         return new String(URLtoByteArray(location, cookies, auth, parts, post));
701     }
702 
703     /**
704      * This method only uses the default Commons HttpClient implementation when
705      * the URL object represents a HTTP resource. The URL object could also
706      * represent a file or some JNDI resource. In that case, the default Java
707      * implementation is used.
708      *
709      * @param       url URL object
710      * @return      A string representation of the resource referenced by the
711      *              URL object
712      * @throws      IOException
713      */
714     public String URLtoString(URL url) throws IOException {
715         String xml = null;
716 
717         if (url != null) {
718             String protocol = url.getProtocol().toLowerCase();
719 
720             if (protocol.startsWith(Http.HTTP) ||
721                 protocol.startsWith(Http.HTTPS)) {
722 
723                 return URLtoString(url.toString());
724             }
725 
726             URLConnection con = url.openConnection();
727 
728             InputStream is = con.getInputStream();
729 
730             ByteArrayMaker bam = new ByteArrayMaker();
731             byte[] bytes = new byte[512];
732 
733             for (int i = is.read(bytes, 0, 512); i != -1;
734                     i = is.read(bytes, 0, 512)) {
735 
736                 bam.write(bytes, 0, i);
737             }
738 
739             xml = new String(bam.toByteArray());
740 
741             is.close();
742             bam.close();
743         }
744 
745         return xml;
746     }
747 
748     protected void proxifyState(HttpState state, HostConfiguration hostConfig) {
749         Credentials proxyCredentials = _proxyCredentials;
750 
751         String host = hostConfig.getHost();
752 
753         if (isProxyHost(host) && (proxyCredentials != null)) {
754             AuthScope scope = new AuthScope(_PROXY_HOST, _PROXY_PORT, null);
755 
756             state.setProxyCredentials(scope, proxyCredentials);
757         }
758     }
759 
760     protected byte[] URLtoByteArray(
761             String location, Cookie[] cookies, Http.Auth auth, Http.Body body,
762             Map<String, String> parts, boolean post)
763         throws IOException {
764 
765         byte[] bytes = null;
766 
767         HttpMethod method = null;
768 
769         try {
770             if (location == null) {
771                 return bytes;
772             }
773             else if (!location.startsWith(Http.HTTP_WITH_SLASH) &&
774                      !location.startsWith(Http.HTTPS_WITH_SLASH)) {
775 
776                 location = Http.HTTP_WITH_SLASH + location;
777             }
778 
779             HostConfiguration hostConfig = getHostConfig(location);
780 
781             HttpClient client = getClient(hostConfig);
782 
783             if (post) {
784                 method = new PostMethod(location);
785 
786                 if (body != null) {
787                     RequestEntity requestEntity = new StringRequestEntity(
788                         body.getContent(), body.getContentType(),
789                         body.getCharset());
790 
791                     PostMethod postMethod = (PostMethod)method;
792 
793                     postMethod.setRequestEntity(requestEntity);
794                 }
795                 else if ((parts != null) && (parts.size() > 0)) {
796                     List<NameValuePair> nvpList =
797                         new ArrayList<NameValuePair>();
798 
799                     for (Map.Entry<String, String> entry : parts.entrySet()) {
800                         String key = entry.getKey();
801                         String value = entry.getValue();
802 
803                         if (value != null) {
804                             nvpList.add(new NameValuePair(key, value));
805                         }
806                     }
807 
808                     NameValuePair[] nvpArray = nvpList.toArray(
809                         new NameValuePair[nvpList.size()]);
810 
811                     PostMethod postMethod = (PostMethod)method;
812 
813                     postMethod.setRequestBody(nvpArray);
814                 }
815             }
816             else {
817                 method = new GetMethod(location);
818             }
819 
820             method.addRequestHeader(
821                 "Content-Type", "application/x-www-form-urlencoded");
822 
823             method.addRequestHeader(
824                 "User-agent",
825                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
826 
827             //method.setFollowRedirects(true);
828 
829             HttpState state = new HttpState();
830 
831             if ((cookies != null) && (cookies.length > 0)) {
832                 org.apache.commons.httpclient.Cookie[] commonsCookies =
833                     new org.apache.commons.httpclient.Cookie[0];
834 
835                 for (int i = 0; i < cookies.length; i++) {
836                     Cookie cookie = cookies[i];
837 
838                     commonsCookies[i] =
839                         new org.apache.commons.httpclient.Cookie(
840                             cookie.getDomain(), cookie.getName(),
841                             cookie.getValue(), cookie.getPath(),
842                             cookie.getMaxAge(), cookie.getSecure());
843                 }
844 
845                 state.addCookies(commonsCookies);
846 
847                 method.getParams().setCookiePolicy(
848                     CookiePolicy.BROWSER_COMPATIBILITY);
849             }
850 
851             if (auth != null) {
852                 method.setDoAuthentication(true);
853 
854                 state.setCredentials(
855                     new AuthScope(
856                         auth.getHost(), auth.getPort(), auth.getRealm()),
857                     new UsernamePasswordCredentials(
858                         auth.getUsername(), auth.getPassword()));
859             }
860 
861             proxifyState(state, hostConfig);
862 
863             client.executeMethod(hostConfig, method, state);
864 
865             Header locationHeader = method.getResponseHeader("location");
866 
867             if ((locationHeader != null) && !locationHeader.equals(location)) {
868                 return URLtoByteArray(
869                     locationHeader.getValue(), cookies, auth, body, parts,
870                     post);
871             }
872 
873             InputStream is = method.getResponseBodyAsStream();
874 
875             if (is != null) {
876                 bytes = FileUtil.getBytes(is);
877 
878                 is.close();
879             }
880 
881             return bytes;
882         }
883         finally {
884             try {
885                 if (method != null) {
886                     method.releaseConnection();
887                 }
888             }
889             catch (Exception e) {
890                 _log.error(e, e);
891             }
892         }
893     }
894 
895     private static final int _MAX_CONNECTIONS_PER_HOST = GetterUtil.getInteger(
896         PropsUtil.get(HttpImpl.class.getName() + ".max.connections.per.host"),
897         2);
898 
899     private static final int _MAX_TOTAL_CONNECTIONS = GetterUtil.getInteger(
900         PropsUtil.get(HttpImpl.class.getName() + ".max.total.connections"),
901         20);
902 
903     private static final String _NON_PROXY_HOSTS =
904         SystemProperties.get("http.nonProxyHosts");
905 
906     private static final String _PROXY_AUTH_TYPE = GetterUtil.getString(
907         PropsUtil.get(HttpImpl.class.getName() + ".proxy.auth.type"));
908 
909     private static final String _PROXY_HOST = GetterUtil.getString(
910         SystemProperties.get("http.proxyHost"));
911 
912     private static final String _PROXY_NTLM_DOMAIN = GetterUtil.getString(
913         PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.domain"));
914 
915     private static final String _PROXY_NTLM_HOST = GetterUtil.getString(
916         PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.host"));
917 
918     private static final String _PROXY_PASSWORD = GetterUtil.getString(
919         PropsUtil.get(HttpImpl.class.getName() + ".proxy.password"));
920 
921     private static final int _PROXY_PORT = GetterUtil.getInteger(
922         SystemProperties.get("http.proxyPort"));
923 
924     private static final String _PROXY_USERNAME = GetterUtil.getString(
925         PropsUtil.get(HttpImpl.class.getName() + ".proxy.username"));
926 
927     private static final int _TIMEOUT = GetterUtil.getInteger(
928         PropsUtil.get(HttpImpl.class.getName() + ".timeout"), 5000);
929 
930     private static Log _log = LogFactoryUtil.getLog(HttpImpl.class);
931 
932     private HttpClient _client = new HttpClient();
933     private Pattern _nonProxyHostsPattern;
934     private HttpClient _proxyClient = new HttpClient();
935     private Credentials _proxyCredentials;
936 
937 }