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