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(boolean secure) {
339         if (!secure) {
340             return Http.HTTP;
341         }
342         else {
343             return Http.HTTPS;
344         }
345     }
346 
347     public String getProtocol(String url) {
348         int pos = url.indexOf(Http.PROTOCOL_DELIMITER);
349 
350         if (pos != -1) {
351             return url.substring(0, pos);
352         }
353         else {
354             return Http.HTTP;
355         }
356     }
357 
358     public String getProtocol(HttpServletRequest request) {
359         return getProtocol(request.isSecure());
360     }
361 
362     public String getProtocol(ActionRequest actionRequest) {
363         return getProtocol(actionRequest.isSecure());
364     }
365 
366     public String getProtocol(RenderRequest renderRequest) {
367         return getProtocol(renderRequest.isSecure());
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, boolean secure) {
507         if (secure) {
508             if (url.startsWith(Http.HTTP_WITH_SLASH)) {
509                 return StringUtil.replace(
510                     url, Http.HTTP_WITH_SLASH, Http.HTTPS_WITH_SLASH);
511             }
512         }
513         else {
514             if (url.startsWith(Http.HTTPS_WITH_SLASH)) {
515                 return StringUtil.replace(
516                     url, Http.HTTPS_WITH_SLASH, Http.HTTP_WITH_SLASH);
517             }
518         }
519 
520         return url;
521     }
522 
523     public String protocolize(String url, HttpServletRequest request) {
524         return protocolize(url, request.isSecure());
525     }
526 
527     public String protocolize(String url, ActionRequest actionRequest) {
528         return protocolize(url, actionRequest.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 void submit(String location) throws IOException {
652         submit(location, null);
653     }
654 
655     public void submit(String location, Cookie[] cookies) throws IOException {
656         submit(location, cookies, false);
657     }
658 
659     public void submit(String location, boolean post) throws IOException {
660         submit(location, null, post);
661     }
662 
663     public void submit(String location, Cookie[] cookies, boolean post)
664         throws IOException {
665 
666         URLtoByteArray(location, cookies, post);
667     }
668 
669     public void submit(
670             String location, Cookie[] cookies, Http.Body body, boolean post)
671         throws IOException {
672 
673         URLtoByteArray(location, cookies, body, post);
674     }
675 
676     public void submit(
677             String location, Cookie[] cookies, Map<String, String> parts,
678             boolean post)
679         throws IOException {
680 
681         URLtoByteArray(location, cookies, parts, post);
682     }
683 
684     public byte[] URLtoByteArray(String location) throws IOException {
685         return URLtoByteArray(location, null);
686     }
687 
688     public byte[] URLtoByteArray(String location, Cookie[] cookies)
689         throws IOException {
690 
691         return URLtoByteArray(location, cookies, false);
692     }
693 
694     public byte[] URLtoByteArray(String location, boolean post)
695         throws IOException {
696 
697         return URLtoByteArray(location, null, post);
698     }
699 
700     public byte[] URLtoByteArray(
701             String location, Cookie[] cookies, boolean post)
702         throws IOException {
703 
704         return URLtoByteArray(location, cookies, null, null, post);
705     }
706 
707     public byte[] URLtoByteArray(
708             String location, Cookie[] cookies, Http.Body body, boolean post)
709         throws IOException {
710 
711         return URLtoByteArray(location, cookies, body, null, post);
712     }
713 
714     public byte[] URLtoByteArray(
715             String location, Cookie[] cookies, Map<String, String> parts,
716             boolean post)
717         throws IOException {
718 
719         return URLtoByteArray(location, cookies, null, parts, post);
720     }
721 
722     public String URLtoString(String location) throws IOException {
723         return URLtoString(location, null);
724     }
725 
726     public String URLtoString(String location, Cookie[] cookies)
727         throws IOException {
728 
729         return URLtoString(location, cookies, false);
730     }
731 
732     public String URLtoString(String location, boolean post)
733         throws IOException {
734 
735         return URLtoString(location, null, post);
736     }
737 
738     public String URLtoString(String location, Cookie[] cookies, boolean post)
739         throws IOException {
740 
741         return new String(URLtoByteArray(location, cookies, post));
742     }
743 
744     public String URLtoString(
745             String location, Cookie[] cookies, Http.Body body, boolean post)
746         throws IOException {
747 
748         return new String(URLtoByteArray(location, cookies, body, post));
749     }
750 
751     public String URLtoString(
752             String location, Cookie[] cookies, Map<String, String> parts,
753             boolean post)
754         throws IOException {
755 
756         return new String(URLtoByteArray(location, cookies, parts, post));
757     }
758 
759     public String URLtoString(
760             String location, String host, int port, String realm,
761             String username, String password)
762         throws IOException {
763 
764         HostConfiguration hostConfig = getHostConfig(location);
765 
766         HttpClient client = getClient(hostConfig);
767 
768         GetMethod getMethod = new GetMethod(location);
769 
770         getMethod.setDoAuthentication(true);
771 
772         HttpState state = new HttpState();
773 
774         state.setCredentials(
775             new AuthScope(host, port, realm),
776             new UsernamePasswordCredentials(username, password));
777 
778         proxifyState(state, hostConfig);
779 
780         try {
781             client.executeMethod(hostConfig, getMethod, state);
782 
783             return getMethod.getResponseBodyAsString();
784         }
785         finally {
786             getMethod.releaseConnection();
787         }
788     }
789 
790     /**
791      * This method only uses the default Commons HttpClient implementation when
792      * the URL object represents a HTTP resource. The URL object could also
793      * represent a file or some JNDI resource. In that case, the default Java
794      * implementation is used.
795      *
796      * @param       url URL object
797      * @return      A string representation of the resource referenced by the
798      *              URL object
799      * @throws      IOException
800      */
801     public String URLtoString(URL url) throws IOException {
802         String xml = null;
803 
804         if (url != null) {
805             String protocol = url.getProtocol().toLowerCase();
806 
807             if (protocol.startsWith(Http.HTTP) ||
808                 protocol.startsWith(Http.HTTPS)) {
809 
810                 return URLtoString(url.toString());
811             }
812 
813             URLConnection con = url.openConnection();
814 
815             InputStream is = con.getInputStream();
816 
817             ByteArrayMaker bam = new ByteArrayMaker();
818             byte[] bytes = new byte[512];
819 
820             for (int i = is.read(bytes, 0, 512); i != -1;
821                     i = is.read(bytes, 0, 512)) {
822 
823                 bam.write(bytes, 0, i);
824             }
825 
826             xml = new String(bam.toByteArray());
827 
828             is.close();
829             bam.close();
830         }
831 
832         return xml;
833     }
834 
835     protected void proxifyState(HttpState state, HostConfiguration hostConfig) {
836         Credentials proxyCredentials = _proxyCredentials;
837 
838         String host = hostConfig.getHost();
839 
840         if (isProxyHost(host) && (proxyCredentials != null)) {
841             AuthScope scope = new AuthScope(_PROXY_HOST, _PROXY_PORT, null);
842 
843             state.setProxyCredentials(scope, proxyCredentials);
844         }
845     }
846 
847     protected byte[] URLtoByteArray(
848             String location, Cookie[] cookies, Http.Body body,
849             Map<String, String> parts, boolean post)
850         throws IOException {
851 
852         byte[] bytes = null;
853 
854         HttpMethod method = null;
855 
856         try {
857             if (location == null) {
858                 return bytes;
859             }
860             else if (!location.startsWith(Http.HTTP_WITH_SLASH) &&
861                      !location.startsWith(Http.HTTPS_WITH_SLASH)) {
862 
863                 location = Http.HTTP_WITH_SLASH + location;
864             }
865 
866             HostConfiguration hostConfig = getHostConfig(location);
867 
868             HttpClient client = getClient(hostConfig);
869 
870             if (post) {
871                 method = new PostMethod(location);
872 
873                 if (body != null) {
874                     RequestEntity requestEntity = new StringRequestEntity(
875                         body.getContent(), body.getContentType(),
876                         body.getCharset());
877 
878                     PostMethod postMethod = (PostMethod)method;
879 
880                     postMethod.setRequestEntity(requestEntity);
881                 }
882                 else if ((parts != null) && (parts.size() > 0)) {
883                     List<NameValuePair> nvpList =
884                         new ArrayList<NameValuePair>();
885 
886                     for (Map.Entry<String, String> entry : parts.entrySet()) {
887                         String key = entry.getKey();
888                         String value = entry.getValue();
889 
890                         if (value != null) {
891                             nvpList.add(new NameValuePair(key, value));
892                         }
893                     }
894 
895                     NameValuePair[] nvpArray = nvpList.toArray(
896                         new NameValuePair[nvpList.size()]);
897 
898                     PostMethod postMethod = (PostMethod)method;
899 
900                     postMethod.setRequestBody(nvpArray);
901                 }
902             }
903             else {
904                 method = new GetMethod(location);
905             }
906 
907             method.addRequestHeader(
908                 "Content-Type", "application/x-www-form-urlencoded");
909 
910             method.addRequestHeader(
911                 "User-agent",
912                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
913 
914             //method.setFollowRedirects(true);
915 
916             HttpState state = new HttpState();
917 
918             if ((cookies != null) && (cookies.length > 0)) {
919                 org.apache.commons.httpclient.Cookie[] commonsCookies =
920                     new org.apache.commons.httpclient.Cookie[0];
921 
922                 for (int i = 0; i < cookies.length; i++) {
923                     Cookie cookie = cookies[i];
924 
925                     commonsCookies[i] =
926                         new org.apache.commons.httpclient.Cookie(
927                             cookie.getDomain(), cookie.getName(),
928                             cookie.getValue(), cookie.getPath(),
929                             cookie.getMaxAge(), cookie.getSecure());
930                 }
931 
932                 state.addCookies(commonsCookies);
933 
934                 method.getParams().setCookiePolicy(
935                     CookiePolicy.BROWSER_COMPATIBILITY);
936             }
937 
938             proxifyState(state, hostConfig);
939 
940             client.executeMethod(hostConfig, method, state);
941 
942             Header locationHeader = method.getResponseHeader("location");
943 
944             if (locationHeader != null) {
945                 return URLtoByteArray(locationHeader.getValue(), cookies, post);
946             }
947 
948             InputStream is = method.getResponseBodyAsStream();
949 
950             if (is != null) {
951                 bytes = FileUtil.getBytes(is);
952 
953                 is.close();
954             }
955 
956             return bytes;
957         }
958         finally {
959             try {
960                 if (method != null) {
961                     method.releaseConnection();
962                 }
963             }
964             catch (Exception e) {
965                 _log.error(e, e);
966             }
967         }
968     }
969 
970     private static final int _MAX_CONNECTIONS_PER_HOST = GetterUtil.getInteger(
971         PropsUtil.get(HttpImpl.class.getName() + ".max.connections.per.host"),
972         2);
973 
974     private static final int _MAX_TOTAL_CONNECTIONS = GetterUtil.getInteger(
975         PropsUtil.get(HttpImpl.class.getName() + ".max.total.connections"),
976         20);
977 
978     private static final String _PROXY_HOST = GetterUtil.getString(
979         SystemProperties.get("http.proxyHost"));
980 
981     private static final int _PROXY_PORT = GetterUtil.getInteger(
982         SystemProperties.get("http.proxyPort"));
983 
984     private static final String _NON_PROXY_HOSTS =
985         SystemProperties.get("http.nonProxyHosts");
986 
987     private static final String _PROXY_AUTH_TYPE = GetterUtil.getString(
988         PropsUtil.get(HttpImpl.class.getName() + ".proxy.auth.type"));
989 
990     private static final String _PROXY_USERNAME = GetterUtil.getString(
991         PropsUtil.get(HttpImpl.class.getName() + ".proxy.username"));
992 
993     private static final String _PROXY_PASSWORD = GetterUtil.getString(
994         PropsUtil.get(HttpImpl.class.getName() + ".proxy.password"));
995 
996     private static final String _PROXY_NTLM_DOMAIN = GetterUtil.getString(
997         PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.domain"));
998 
999     private static final String _PROXY_NTLM_HOST = GetterUtil.getString(
1000        PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.host"));
1001
1002    private static final int _TIMEOUT = GetterUtil.getInteger(
1003        PropsUtil.get(HttpImpl.class.getName() + ".timeout"), 5000);
1004
1005    private static Log _log = LogFactoryUtil.getLog(HttpImpl.class);
1006
1007    private HttpClient _client = new HttpClient();
1008    private HttpClient _proxyClient = new HttpClient();
1009    private Credentials _proxyCredentials;
1010    private Pattern _nonProxyHostsPattern;
1011
1012}