1   /**
2    * Copyright (c) 2000-2008 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.util.ByteArrayMaker;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.Http;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.util.SystemProperties;
33  
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.io.UnsupportedEncodingException;
37  
38  import java.net.URL;
39  import java.net.URLConnection;
40  import java.net.URLDecoder;
41  import java.net.URLEncoder;
42  
43  import java.util.ArrayList;
44  import java.util.LinkedHashMap;
45  import java.util.List;
46  import java.util.Map;
47  import java.util.StringTokenizer;
48  import java.util.regex.Pattern;
49  
50  import javax.portlet.ActionRequest;
51  import javax.portlet.RenderRequest;
52  
53  import javax.servlet.http.Cookie;
54  import javax.servlet.http.HttpServletRequest;
55  
56  import org.apache.commons.httpclient.Credentials;
57  import org.apache.commons.httpclient.Header;
58  import org.apache.commons.httpclient.HostConfiguration;
59  import org.apache.commons.httpclient.HttpClient;
60  import org.apache.commons.httpclient.HttpMethod;
61  import org.apache.commons.httpclient.HttpState;
62  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
63  import org.apache.commons.httpclient.NTCredentials;
64  import org.apache.commons.httpclient.NameValuePair;
65  import org.apache.commons.httpclient.URI;
66  import org.apache.commons.httpclient.UsernamePasswordCredentials;
67  import org.apache.commons.httpclient.auth.AuthPolicy;
68  import org.apache.commons.httpclient.auth.AuthScope;
69  import org.apache.commons.httpclient.cookie.CookiePolicy;
70  import org.apache.commons.httpclient.methods.GetMethod;
71  import org.apache.commons.httpclient.methods.PostMethod;
72  import org.apache.commons.httpclient.methods.RequestEntity;
73  import org.apache.commons.httpclient.methods.StringRequestEntity;
74  import org.apache.commons.httpclient.params.HttpConnectionParams;
75  import org.apache.commons.logging.Log;
76  import org.apache.commons.logging.LogFactory;
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         if (url.indexOf(StringPool.QUESTION) == -1) {
169             url += StringPool.QUESTION;
170         }
171 
172         if (!url.endsWith(StringPool.QUESTION) &&
173             !url.endsWith(StringPool.AMPERSAND)) {
174 
175             url += StringPool.AMPERSAND;
176         }
177 
178         return url + name + StringPool.EQUAL + encodeURL(value);
179     }
180 
181     public String decodeURL(String url) {
182         return decodeURL(url, false);
183     }
184 
185     public String decodeURL(String url, boolean unescapeSpace) {
186         if (url == null) {
187             return null;
188         }
189 
190         try {
191             url = URLDecoder.decode(url, StringPool.UTF8);
192 
193             if (unescapeSpace) {
194                 url = StringUtil.replace(url, "%20", StringPool.PLUS);
195             }
196 
197             return url;
198         }
199         catch (UnsupportedEncodingException uee) {
200             _log.error(uee, uee);
201 
202             return StringPool.BLANK;
203         }
204     }
205 
206     public String encodeURL(String url) {
207         return encodeURL(url, false);
208     }
209 
210     public String encodeURL(String url, boolean escapeSpaces) {
211         if (url == null) {
212             return null;
213         }
214 
215         try {
216             url = URLEncoder.encode(url, StringPool.UTF8);
217 
218             if (escapeSpaces) {
219                 url = StringUtil.replace(url, StringPool.PLUS, "%20");
220             }
221 
222             return url;
223         }
224         catch (UnsupportedEncodingException uee) {
225             _log.error(uee, uee);
226 
227             return StringPool.BLANK;
228         }
229     }
230 
231     public HttpClient getClient(HostConfiguration hostConfig) {
232         if (isProxyHost(hostConfig.getHost())) {
233             return _proxyClient;
234         }
235         else {
236             return _client;
237         }
238     }
239 
240     public String getCompleteURL(HttpServletRequest request) {
241         StringBuffer completeURL = request.getRequestURL();
242 
243         if (completeURL == null) {
244             completeURL = new StringBuffer();
245         }
246 
247         if (request.getQueryString() != null) {
248             completeURL.append(StringPool.QUESTION);
249             completeURL.append(request.getQueryString());
250         }
251 
252         return completeURL.toString();
253     }
254 
255     public String getDomain(String url) {
256         url = removeProtocol(url);
257 
258         int pos = url.indexOf(StringPool.SLASH);
259 
260         if (pos != -1) {
261             return url.substring(0, pos);
262         }
263         else {
264             return url;
265         }
266     }
267 
268     public HostConfiguration getHostConfig(String location) throws IOException {
269         if (_log.isDebugEnabled()) {
270             _log.debug("Location is " + location);
271         }
272 
273         HostConfiguration hostConfig = new HostConfiguration();
274 
275         hostConfig.setHost(new URI(location, false));
276 
277         if (isProxyHost(hostConfig.getHost())) {
278             hostConfig.setProxy(_PROXY_HOST, _PROXY_PORT);
279         }
280 
281         return hostConfig;
282     }
283 
284     public String getParameter(String url, String name) {
285         return getParameter(url, name, true);
286     }
287 
288     public String getParameter(String url, String name, boolean escaped) {
289         if (Validator.isNull(url) || Validator.isNull(name)) {
290             return StringPool.BLANK;
291         }
292 
293         String[] parts = StringUtil.split(url, StringPool.QUESTION);
294 
295         if (parts.length == 2) {
296             String[] params = null;
297 
298             if (escaped) {
299                 params = StringUtil.split(parts[1], "&amp;");
300             }
301             else {
302                 params = StringUtil.split(parts[1], StringPool.AMPERSAND);
303             }
304 
305             for (int i = 0; i < params.length; i++) {
306                 String[] kvp = StringUtil.split(params[i], StringPool.EQUAL);
307 
308                 if ((kvp.length == 2) && kvp[0].equals(name)) {
309                     return kvp[1];
310                 }
311             }
312         }
313 
314         return StringPool.BLANK;
315     }
316 
317     public Map<String, String[]> getParameterMap(String queryString) {
318         return parameterMapFromString(queryString);
319     }
320 
321     public String getProtocol(boolean secure) {
322         if (!secure) {
323             return Http.HTTP;
324         }
325         else {
326             return Http.HTTPS;
327         }
328     }
329 
330     public String getProtocol(String url) {
331         int pos = url.indexOf(Http.PROTOCOL_DELIMITER);
332 
333         if (pos != -1) {
334             return url.substring(0, pos);
335         }
336         else {
337             return Http.HTTP;
338         }
339     }
340 
341     public String getProtocol(HttpServletRequest request) {
342         return getProtocol(request.isSecure());
343     }
344 
345     public String getProtocol(ActionRequest actionRequest) {
346         return getProtocol(actionRequest.isSecure());
347     }
348 
349     public String getProtocol(RenderRequest renderRequest) {
350         return getProtocol(renderRequest.isSecure());
351     }
352 
353     public String getQueryString(String url) {
354         if (Validator.isNull(url)) {
355             return url;
356         }
357 
358         int pos = url.indexOf(StringPool.QUESTION);
359 
360         if (pos == -1) {
361             return StringPool.BLANK;
362         }
363         else {
364             return url.substring(pos + 1, url.length());
365         }
366     }
367 
368     public String getRequestURL(HttpServletRequest request) {
369         return request.getRequestURL().toString();
370     }
371 
372     public boolean hasDomain(String url) {
373         return Validator.isNotNull(getDomain(url));
374     }
375 
376     public boolean hasProxyConfig() {
377         if (Validator.isNotNull(_PROXY_HOST) && (_PROXY_PORT > 0)) {
378             return true;
379         }
380         else {
381             return false;
382         }
383     }
384 
385     public boolean isNonProxyHost(String host) {
386         if (_nonProxyHostsPattern == null ||
387             _nonProxyHostsPattern.matcher(host).matches()) {
388 
389             return true;
390         }
391         else {
392             return false;
393         }
394     }
395 
396     public boolean isProxyHost(String host) {
397         if (hasProxyConfig() && !isNonProxyHost(host)) {
398             return true;
399         }
400         else {
401             return false;
402         }
403     }
404 
405     public Map<String, String[]> parameterMapFromString(String queryString) {
406         Map<String, String[]> parameterMap =
407             new LinkedHashMap<String, String[]>();
408 
409         if (Validator.isNull(queryString)) {
410             return parameterMap;
411         }
412 
413         Map<String, List<String>> tempParameterMap =
414             new LinkedHashMap<String, List<String>>();
415 
416         StringTokenizer st = new StringTokenizer(
417             queryString, StringPool.AMPERSAND);
418 
419         while (st.hasMoreTokens()) {
420             String token = st.nextToken();
421 
422             if (Validator.isNotNull(token)) {
423                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
424 
425                 String key = kvp[0];
426 
427                 String value = StringPool.BLANK;
428 
429                 if (kvp.length > 1) {
430                     value = kvp[1];
431                 }
432 
433                 List<String> values = tempParameterMap.get(key);
434 
435                 if (values == null) {
436                     values = new ArrayList<String>();
437 
438                     tempParameterMap.put(key, values);
439                 }
440 
441                 values.add(value);
442             }
443         }
444 
445         for (Map.Entry<String, List<String>> entry :
446                 tempParameterMap.entrySet()) {
447 
448             String key = entry.getKey();
449             List<String> values = entry.getValue();
450 
451             parameterMap.put(key, values.toArray(new String[values.size()]));
452         }
453 
454         return parameterMap;
455     }
456 
457     public String parameterMapToString(Map<String, String[]> parameterMap) {
458         return parameterMapToString(parameterMap, true);
459     }
460 
461     public String parameterMapToString(
462         Map<String, String[]> parameterMap, boolean addQuestion) {
463 
464         StringBuilder sb = new StringBuilder();
465 
466         if (parameterMap.size() > 0) {
467             if (addQuestion) {
468                 sb.append(StringPool.QUESTION);
469             }
470 
471             for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
472                 String name = entry.getKey();
473                 String[] values = entry.getValue();
474 
475                 for (String value : values) {
476                     sb.append(name);
477                     sb.append(StringPool.EQUAL);
478                     sb.append(encodeURL(value));
479                     sb.append(StringPool.AMPERSAND);
480                 }
481             }
482 
483             sb.deleteCharAt(sb.length() - 1);
484         }
485 
486         return sb.toString();
487     }
488 
489     public String protocolize(String url, boolean secure) {
490         if (secure) {
491             if (url.startsWith(Http.HTTP_WITH_SLASH)) {
492                 return StringUtil.replace(
493                     url, Http.HTTP_WITH_SLASH, Http.HTTPS_WITH_SLASH);
494             }
495         }
496         else {
497             if (url.startsWith(Http.HTTPS_WITH_SLASH)) {
498                 return StringUtil.replace(
499                     url, Http.HTTPS_WITH_SLASH, Http.HTTP_WITH_SLASH);
500             }
501         }
502 
503         return url;
504     }
505 
506     public String protocolize(String url, HttpServletRequest request) {
507         return protocolize(url, request.isSecure());
508     }
509 
510     public String protocolize(String url, ActionRequest actionRequest) {
511         return protocolize(url, actionRequest.isSecure());
512     }
513 
514     public String protocolize(String url, RenderRequest renderRequest) {
515         return protocolize(url, renderRequest.isSecure());
516     }
517 
518     public String removeDomain(String url) {
519         url = removeProtocol(url);
520 
521         int pos = url.indexOf(StringPool.SLASH);
522 
523         if (pos > 0) {
524             return url.substring(pos);
525         }
526         else {
527             return url;
528         }
529     }
530 
531     public String removeParameter(String url, String name) {
532         int pos = url.indexOf(StringPool.QUESTION);
533 
534         if (pos == -1) {
535             return url;
536         }
537 
538         StringBuilder sb = new StringBuilder();
539 
540         sb.append(url.substring(0, pos + 1));
541 
542         StringTokenizer st = new StringTokenizer(
543             url.substring(pos + 1, url.length()), StringPool.AMPERSAND);
544 
545         while (st.hasMoreTokens()) {
546             String token = st.nextToken();
547 
548             if (Validator.isNotNull(token)) {
549                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
550 
551                 String key = kvp[0];
552 
553                 String value = StringPool.BLANK;
554 
555                 if (kvp.length > 1) {
556                     value = kvp[1];
557                 }
558 
559                 if (!key.equals(name)) {
560                     sb.append(key);
561                     sb.append(StringPool.EQUAL);
562                     sb.append(value);
563                     sb.append(StringPool.AMPERSAND);
564                 }
565             }
566         }
567 
568         url = StringUtil.replace(
569             sb.toString(), StringPool.AMPERSAND + StringPool.AMPERSAND,
570             StringPool.AMPERSAND);
571 
572         return url;
573     }
574 
575     public String removeProtocol(String url) {
576         if (url.startsWith(Http.HTTP_WITH_SLASH)) {
577             return url.substring(Http.HTTP_WITH_SLASH.length() , url.length());
578         }
579         else if (url.startsWith(Http.HTTPS_WITH_SLASH)) {
580             return url.substring(Http.HTTPS_WITH_SLASH.length() , url.length());
581         }
582         else {
583             return url;
584         }
585     }
586 
587     public void submit(String location) throws IOException {
588         submit(location, null);
589     }
590 
591     public void submit(String location, Cookie[] cookies) throws IOException {
592         submit(location, cookies, false);
593     }
594 
595     public void submit(String location, boolean post) throws IOException {
596         submit(location, null, post);
597     }
598 
599     public void submit(String location, Cookie[] cookies, boolean post)
600         throws IOException {
601 
602         URLtoByteArray(location, cookies, post);
603     }
604 
605     public void submit(
606             String location, Cookie[] cookies, Http.Body body, boolean post)
607         throws IOException {
608 
609         URLtoByteArray(location, cookies, body, post);
610     }
611 
612     public void submit(
613             String location, Cookie[] cookies, Map<String, String> parts,
614             boolean post)
615         throws IOException {
616 
617         URLtoByteArray(location, cookies, parts, post);
618     }
619 
620     public byte[] URLtoByteArray(String location) throws IOException {
621         return URLtoByteArray(location, null);
622     }
623 
624     public byte[] URLtoByteArray(String location, Cookie[] cookies)
625         throws IOException {
626 
627         return URLtoByteArray(location, cookies, false);
628     }
629 
630     public byte[] URLtoByteArray(String location, boolean post)
631         throws IOException {
632 
633         return URLtoByteArray(location, null, post);
634     }
635 
636     public byte[] URLtoByteArray(
637             String location, Cookie[] cookies, boolean post)
638         throws IOException {
639 
640         return URLtoByteArray(location, cookies, null, null, post);
641     }
642 
643     public byte[] URLtoByteArray(
644             String location, Cookie[] cookies, Http.Body body, boolean post)
645         throws IOException {
646 
647         return URLtoByteArray(location, cookies, body, null, post);
648     }
649 
650     public byte[] URLtoByteArray(
651             String location, Cookie[] cookies, Map<String, String> parts,
652             boolean post)
653         throws IOException {
654 
655         return URLtoByteArray(location, cookies, null, parts, post);
656     }
657 
658     public String URLtoString(String location) throws IOException {
659         return URLtoString(location, null);
660     }
661 
662     public String URLtoString(String location, Cookie[] cookies)
663         throws IOException {
664 
665         return URLtoString(location, cookies, false);
666     }
667 
668     public String URLtoString(String location, boolean post)
669         throws IOException {
670 
671         return URLtoString(location, null, post);
672     }
673 
674     public String URLtoString(String location, Cookie[] cookies, boolean post)
675         throws IOException {
676 
677         return new String(URLtoByteArray(location, cookies, post));
678     }
679 
680     public String URLtoString(
681             String location, Cookie[] cookies, Http.Body body, boolean post)
682         throws IOException {
683 
684         return new String(URLtoByteArray(location, cookies, body, post));
685     }
686 
687     public String URLtoString(
688             String location, Cookie[] cookies, Map<String, String> parts,
689             boolean post)
690         throws IOException {
691 
692         return new String(URLtoByteArray(location, cookies, parts, post));
693     }
694 
695     public String URLtoString(
696             String location, String host, int port, String realm,
697             String username, String password)
698         throws IOException {
699 
700         HostConfiguration hostConfig = getHostConfig(location);
701 
702         HttpClient client = getClient(hostConfig);
703 
704         GetMethod getMethod = new GetMethod(location);
705 
706         getMethod.setDoAuthentication(true);
707 
708         HttpState state = new HttpState();
709 
710         state.setCredentials(
711             new AuthScope(host, port, realm),
712             new UsernamePasswordCredentials(username, password));
713 
714         proxifyState(state, hostConfig);
715 
716         try {
717             client.executeMethod(hostConfig, getMethod, state);
718 
719             return getMethod.getResponseBodyAsString();
720         }
721         finally {
722             getMethod.releaseConnection();
723         }
724     }
725 
726     /**
727      * This method only uses the default Commons HttpClient implementation when
728      * the URL object represents a HTTP resource. The URL object could also
729      * represent a file or some JNDI resource. In that case, the default Java
730      * implementation is used.
731      *
732      * @param       url URL object
733      * @return      A string representation of the resource referenced by the
734      *              URL object
735      * @throws      IOException
736      */
737     public String URLtoString(URL url) throws IOException {
738         String xml = null;
739 
740         if (url != null) {
741             String protocol = url.getProtocol().toLowerCase();
742 
743             if (protocol.startsWith(Http.HTTP) ||
744                 protocol.startsWith(Http.HTTPS)) {
745 
746                 return URLtoString(url.toString());
747             }
748 
749             URLConnection con = url.openConnection();
750 
751             InputStream is = con.getInputStream();
752 
753             ByteArrayMaker bam = new ByteArrayMaker();
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                 bam.write(bytes, 0, i);
760             }
761 
762             xml = new String(bam.toByteArray());
763 
764             is.close();
765             bam.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 byte[] URLtoByteArray(
784             String location, Cookie[] cookies, Http.Body body,
785             Map<String, String> parts, boolean post)
786         throws IOException {
787 
788         byte[] bytes = null;
789 
790         HttpMethod method = null;
791 
792         try {
793             if (location == null) {
794                 return bytes;
795             }
796             else if (!location.startsWith(Http.HTTP_WITH_SLASH) &&
797                      !location.startsWith(Http.HTTPS_WITH_SLASH)) {
798 
799                 location = Http.HTTP_WITH_SLASH + location;
800             }
801 
802             HostConfiguration hostConfig = getHostConfig(location);
803 
804             HttpClient client = getClient(hostConfig);
805 
806             if (post) {
807                 method = new PostMethod(location);
808 
809                 if (body != null) {
810                     RequestEntity requestEntity = new StringRequestEntity(
811                         body.getContent(), body.getContentType(),
812                         body.getCharset());
813 
814                     PostMethod postMethod = (PostMethod)method;
815 
816                     postMethod.setRequestEntity(requestEntity);
817                 }
818                 else if ((parts != null) && (parts.size() > 0)) {
819                     List<NameValuePair> nvpList =
820                         new ArrayList<NameValuePair>();
821 
822                     for (Map.Entry<String, String> entry : parts.entrySet()) {
823                         String key = entry.getKey();
824                         String value = entry.getValue();
825 
826                         if (value != null) {
827                             nvpList.add(new NameValuePair(key, value));
828                         }
829                     }
830 
831                     NameValuePair[] nvpArray = nvpList.toArray(
832                         new NameValuePair[nvpList.size()]);
833 
834                     PostMethod postMethod = (PostMethod)method;
835 
836                     postMethod.setRequestBody(nvpArray);
837                 }
838             }
839             else {
840                 method = new GetMethod(location);
841             }
842 
843             method.addRequestHeader(
844                 "Content-Type", "application/x-www-form-urlencoded");
845 
846             method.addRequestHeader(
847                 "User-agent",
848                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
849 
850             //method.setFollowRedirects(true);
851 
852             HttpState state = new HttpState();
853 
854             if ((cookies != null) && (cookies.length > 0)) {
855                 org.apache.commons.httpclient.Cookie[] commonsCookies =
856                     new org.apache.commons.httpclient.Cookie[0];
857 
858                 for (int i = 0; i < cookies.length; i++) {
859                     Cookie cookie = cookies[i];
860 
861                     commonsCookies[i] =
862                         new org.apache.commons.httpclient.Cookie(
863                             cookie.getDomain(), cookie.getName(),
864                             cookie.getValue(), cookie.getPath(),
865                             cookie.getMaxAge(), cookie.getSecure());
866                 }
867 
868                 state.addCookies(commonsCookies);
869 
870                 method.getParams().setCookiePolicy(
871                     CookiePolicy.BROWSER_COMPATIBILITY);
872             }
873 
874             proxifyState(state, hostConfig);
875 
876             client.executeMethod(hostConfig, method, state);
877 
878             Header locationHeader = method.getResponseHeader("location");
879 
880             if (locationHeader != null) {
881                 return URLtoByteArray(locationHeader.getValue(), cookies, post);
882             }
883 
884             InputStream is = method.getResponseBodyAsStream();
885 
886             if (is != null) {
887                 bytes = FileUtil.getBytes(is);
888 
889                 is.close();
890             }
891 
892             return bytes;
893         }
894         finally {
895             try {
896                 if (method != null) {
897                     method.releaseConnection();
898                 }
899             }
900             catch (Exception e) {
901                 _log.error(e, e);
902             }
903         }
904     }
905 
906     private static final int _MAX_CONNECTIONS_PER_HOST = GetterUtil.getInteger(
907         PropsUtil.get(HttpImpl.class.getName() + ".max.connections.per.host"),
908         2);
909 
910     private static final int _MAX_TOTAL_CONNECTIONS = GetterUtil.getInteger(
911         PropsUtil.get(HttpImpl.class.getName() + ".max.total.connections"),
912         20);
913 
914     private static final String _PROXY_HOST = GetterUtil.getString(
915         SystemProperties.get("http.proxyHost"));
916 
917     private static final int _PROXY_PORT = GetterUtil.getInteger(
918         SystemProperties.get("http.proxyPort"));
919 
920     private static final String _NON_PROXY_HOSTS =
921         SystemProperties.get("http.nonProxyHosts");
922 
923     private static final String _PROXY_AUTH_TYPE = GetterUtil.getString(
924         PropsUtil.get(HttpImpl.class.getName() + ".proxy.auth.type"));
925 
926     private static final String _PROXY_USERNAME = GetterUtil.getString(
927         PropsUtil.get(HttpImpl.class.getName() + ".proxy.username"));
928 
929     private static final String _PROXY_PASSWORD = GetterUtil.getString(
930         PropsUtil.get(HttpImpl.class.getName() + ".proxy.password"));
931 
932     private static final String _PROXY_NTLM_DOMAIN = GetterUtil.getString(
933         PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.domain"));
934 
935     private static final String _PROXY_NTLM_HOST = GetterUtil.getString(
936         PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.host"));
937 
938     private static final int _TIMEOUT = GetterUtil.getInteger(
939         PropsUtil.get(HttpImpl.class.getName() + ".timeout"), 5000);
940 
941     private static Log _log = LogFactory.getLog(HttpImpl.class);
942 
943     private HttpClient _client = new HttpClient();
944     private HttpClient _proxyClient = new HttpClient();
945     private Credentials _proxyCredentials;
946     private Pattern _nonProxyHostsPattern;
947 
948 }