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.GetterUtil;
27  import com.liferay.portal.kernel.util.Http;
28  import com.liferay.portal.kernel.util.StringMaker;
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         throws IOException {
231 
232         if (isProxyHost(hostConfig.getHost())) {
233             return _proxyClient;
234         }
235         else {
236             return _client;
237         }
238     }
239 
240     public String getCompleteURL(HttpServletRequest req) {
241         StringBuffer completeURL = req.getRequestURL();
242 
243         if (completeURL == null) {
244             completeURL = new StringBuffer();
245         }
246 
247         if (req.getQueryString() != null) {
248             completeURL.append(StringPool.QUESTION);
249             completeURL.append(req.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 req) {
342         return getProtocol(req.isSecure());
343     }
344 
345     public String getProtocol(ActionRequest req) {
346         return getProtocol(req.isSecure());
347     }
348 
349     public String getProtocol(RenderRequest req) {
350         return getProtocol(req.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 req) {
369         return req.getRequestURL().toString();
370     }
371 
372     public boolean hasProxyConfig() {
373         if (Validator.isNotNull(_PROXY_HOST) && (_PROXY_PORT > 0)) {
374             return true;
375         }
376         else {
377             return false;
378         }
379     }
380 
381     public boolean isNonProxyHost(String host) {
382         if (_nonProxyHostsPattern == null ||
383             _nonProxyHostsPattern.matcher(host).matches()) {
384 
385             return true;
386         }
387         else {
388             return false;
389         }
390     }
391 
392     public boolean isProxyHost(String host) {
393         if (hasProxyConfig() && !isNonProxyHost(host)) {
394             return true;
395         }
396         else {
397             return false;
398         }
399     }
400 
401     public Map<String, String[]> parameterMapFromString(String queryString) {
402         Map<String, String[]> parameterMap =
403             new LinkedHashMap<String, String[]>();
404 
405         if (Validator.isNull(queryString)) {
406             return parameterMap;
407         }
408 
409         Map<String, List<String>> tempParameterMap =
410             new LinkedHashMap<String, List<String>>();
411 
412         StringTokenizer st = new StringTokenizer(
413             queryString, StringPool.AMPERSAND);
414 
415         while (st.hasMoreTokens()) {
416             String token = st.nextToken();
417 
418             if (Validator.isNotNull(token)) {
419                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
420 
421                 String key = kvp[0];
422 
423                 String value = StringPool.BLANK;
424 
425                 if (kvp.length > 1) {
426                     value = kvp[1];
427                 }
428 
429                 List<String> values = tempParameterMap.get(key);
430 
431                 if (values == null) {
432                     values = new ArrayList<String>();
433 
434                     tempParameterMap.put(key, values);
435                 }
436 
437                 values.add(value);
438             }
439         }
440 
441         for (Map.Entry<String, List<String>> entry :
442                 tempParameterMap.entrySet()) {
443 
444             String key = entry.getKey();
445             List<String> values = entry.getValue();
446 
447             parameterMap.put(key, values.toArray(new String[values.size()]));
448         }
449 
450         return parameterMap;
451     }
452 
453     public String parameterMapToString(Map<String, String[]> parameterMap) {
454         return parameterMapToString(parameterMap, true);
455     }
456 
457     public String parameterMapToString(
458         Map<String, String[]> parameterMap, boolean addQuestion) {
459 
460         StringMaker sm = new StringMaker();
461 
462         if (parameterMap.size() > 0) {
463             if (addQuestion) {
464                 sm.append(StringPool.QUESTION);
465             }
466 
467             for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
468                 String name = entry.getKey();
469                 String[] values = entry.getValue();
470 
471                 for (String value : values) {
472                     sm.append(name);
473                     sm.append(StringPool.EQUAL);
474                     sm.append(encodeURL(value));
475                     sm.append(StringPool.AMPERSAND);
476                 }
477             }
478 
479             sm.deleteCharAt(sm.length() - 1);
480         }
481 
482         return sm.toString();
483     }
484 
485     public String protocolize(String url, boolean secure) {
486         if (secure) {
487             if (url.startsWith(Http.HTTP_WITH_SLASH)) {
488                 return StringUtil.replace(
489                     url, Http.HTTP_WITH_SLASH, Http.HTTPS_WITH_SLASH);
490             }
491         }
492         else {
493             if (url.startsWith(Http.HTTPS_WITH_SLASH)) {
494                 return StringUtil.replace(
495                     url, Http.HTTPS_WITH_SLASH, Http.HTTP_WITH_SLASH);
496             }
497         }
498 
499         return url;
500     }
501 
502     public String protocolize(String url, HttpServletRequest req) {
503         return protocolize(url, req.isSecure());
504     }
505 
506     public String protocolize(String url, ActionRequest req) {
507         return protocolize(url, req.isSecure());
508     }
509 
510     public String protocolize(String url, RenderRequest req) {
511         return protocolize(url, req.isSecure());
512     }
513 
514     public String removeParameter(String url, String name) {
515         int pos = url.indexOf(StringPool.QUESTION);
516 
517         if (pos == -1) {
518             return url;
519         }
520 
521         StringMaker sm = new StringMaker();
522 
523         sm.append(url.substring(0, pos + 1));
524 
525         StringTokenizer st = new StringTokenizer(
526             url.substring(pos + 1, url.length()), StringPool.AMPERSAND);
527 
528         while (st.hasMoreTokens()) {
529             String token = st.nextToken();
530 
531             if (Validator.isNotNull(token)) {
532                 String[] kvp = StringUtil.split(token, StringPool.EQUAL);
533 
534                 String key = kvp[0];
535 
536                 String value = StringPool.BLANK;
537 
538                 if (kvp.length > 1) {
539                     value = kvp[1];
540                 }
541 
542                 if (!key.equals(name)) {
543                     sm.append(key);
544                     sm.append(StringPool.EQUAL);
545                     sm.append(value);
546                     sm.append(StringPool.AMPERSAND);
547                 }
548             }
549         }
550 
551         url = StringUtil.replace(
552             sm.toString(), StringPool.AMPERSAND + StringPool.AMPERSAND,
553             StringPool.AMPERSAND);
554 
555         return url;
556     }
557 
558     public String removeProtocol(String url) {
559         if (url.startsWith(Http.HTTP_WITH_SLASH)) {
560             return url.substring(Http.HTTP_WITH_SLASH.length() , url.length());
561         }
562         else if (url.startsWith(Http.HTTPS_WITH_SLASH)) {
563             return url.substring(Http.HTTPS_WITH_SLASH.length() , url.length());
564         }
565         else {
566             return url;
567         }
568     }
569 
570     public void submit(String location) throws IOException {
571         submit(location, null);
572     }
573 
574     public void submit(String location, Cookie[] cookies) throws IOException {
575         submit(location, cookies, false);
576     }
577 
578     public void submit(String location, boolean post) throws IOException {
579         submit(location, null, post);
580     }
581 
582     public void submit(String location, Cookie[] cookies, boolean post)
583         throws IOException {
584 
585         URLtoByteArray(location, cookies, post);
586     }
587 
588     public void submit(
589             String location, Cookie[] cookies, Map<String, String> parts,
590             boolean post)
591         throws IOException {
592 
593         URLtoByteArray(location, cookies, parts, post);
594     }
595 
596     public byte[] URLtoByteArray(String location) throws IOException {
597         return URLtoByteArray(location, null);
598     }
599 
600     public byte[] URLtoByteArray(String location, Cookie[] cookies)
601         throws IOException {
602 
603         return URLtoByteArray(location, cookies, false);
604     }
605 
606     public byte[] URLtoByteArray(String location, boolean post)
607         throws IOException {
608 
609         return URLtoByteArray(location, null, post);
610     }
611 
612     public byte[] URLtoByteArray(
613             String location, Cookie[] cookies, boolean post)
614         throws IOException {
615 
616         return URLtoByteArray(location, cookies, null, post);
617     }
618 
619     public byte[] URLtoByteArray(
620             String location, Cookie[] cookies, Map<String, String> parts,
621             boolean post)
622         throws IOException {
623 
624         byte[] byteArray = null;
625 
626         HttpMethod method = null;
627 
628         try {
629             if (location == null) {
630                 return byteArray;
631             }
632             else if (!location.startsWith(Http.HTTP_WITH_SLASH) &&
633                      !location.startsWith(Http.HTTPS_WITH_SLASH)) {
634 
635                 location = Http.HTTP_WITH_SLASH + location;
636             }
637 
638             HostConfiguration hostConfig = getHostConfig(location);
639 
640             HttpClient client = getClient(hostConfig);
641 
642             if (post) {
643                 method = new PostMethod(location);
644 
645                 if ((parts != null) && (parts.size() > 0)) {
646                     List<NameValuePair> nvpList =
647                         new ArrayList<NameValuePair>();
648 
649                     for (Map.Entry<String, String> entry : parts.entrySet()) {
650                         String key = entry.getKey();
651                         String value = entry.getValue();
652 
653                         if (value != null) {
654                             nvpList.add(new NameValuePair(key, value));
655                         }
656                     }
657 
658                     NameValuePair[] nvpArray = nvpList.toArray(
659                         new NameValuePair[nvpList.size()]);
660 
661                     PostMethod postMethod = (PostMethod)method;
662 
663                     postMethod.setRequestBody(nvpArray);
664                 }
665             }
666             else {
667                 method = new GetMethod(location);
668             }
669 
670             method.addRequestHeader(
671                 "Content-Type", "application/x-www-form-urlencoded");
672 
673             method.addRequestHeader(
674                 "User-agent",
675                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
676 
677             //method.setFollowRedirects(true);
678 
679             HttpState state = new HttpState();
680 
681             if ((cookies != null) && (cookies.length > 0)) {
682                 org.apache.commons.httpclient.Cookie[] commonsCookies =
683                     new org.apache.commons.httpclient.Cookie[0];
684 
685                 for (int i = 0; i < cookies.length; i++) {
686                     Cookie cookie = cookies[i];
687 
688                     commonsCookies[i] =
689                         new org.apache.commons.httpclient.Cookie(
690                             cookie.getDomain(), cookie.getName(),
691                             cookie.getValue(), cookie.getPath(),
692                             cookie.getMaxAge(), cookie.getSecure());
693                 }
694 
695                 state.addCookies(commonsCookies);
696 
697                 method.getParams().setCookiePolicy(
698                     CookiePolicy.BROWSER_COMPATIBILITY);
699             }
700 
701             proxifyState(state, hostConfig);
702 
703             client.executeMethod(hostConfig, method, state);
704 
705             Header locationHeader = method.getResponseHeader("location");
706 
707             if (locationHeader != null) {
708                 return URLtoByteArray(locationHeader.getValue(), cookies, post);
709             }
710 
711             InputStream is = method.getResponseBodyAsStream();
712 
713             if (is != null) {
714                 ByteArrayMaker bam = new ByteArrayMaker();
715                 byte[] bytes = new byte[512];
716 
717                 for (int i = is.read(bytes, 0, 512); i != -1;
718                         i = is.read(bytes, 0, 512)) {
719 
720                     bam.write(bytes, 0, i);
721                 }
722 
723                 byteArray = bam.toByteArray();
724 
725                 is.close();
726                 bam.close();
727             }
728 
729             return byteArray;
730         }
731         finally {
732             try {
733                 if (method != null) {
734                     method.releaseConnection();
735                 }
736             }
737             catch (Exception e) {
738                 _log.error(e, e);
739             }
740         }
741     }
742 
743     public String URLtoString(String location) throws IOException {
744         return URLtoString(location, null);
745     }
746 
747     public String URLtoString(String location, Cookie[] cookies)
748         throws IOException {
749 
750         return URLtoString(location, cookies, false);
751     }
752 
753     public String URLtoString(String location, boolean post)
754         throws IOException {
755 
756         return URLtoString(location, null, post);
757     }
758 
759     public String URLtoString(String location, Cookie[] cookies, boolean post)
760         throws IOException {
761 
762         return new String(URLtoByteArray(location, cookies, post));
763     }
764 
765     public String URLtoString(
766             String location, Cookie[] cookies, Map<String, String> parts,
767             boolean post)
768         throws IOException {
769 
770         return new String(URLtoByteArray(location, cookies, parts, post));
771     }
772 
773     public String URLtoString(
774             String location, String host, int port, String realm,
775             String username, String password)
776         throws IOException {
777 
778         HostConfiguration hostConfig = getHostConfig(location);
779 
780         HttpClient client = getClient(hostConfig);
781 
782         GetMethod getMethod = new GetMethod(location);
783 
784         getMethod.setDoAuthentication(true);
785 
786         HttpState state = new HttpState();
787 
788         state.setCredentials(
789             new AuthScope(host, port, realm),
790             new UsernamePasswordCredentials(username, password));
791 
792         proxifyState(state, hostConfig);
793 
794         try {
795             client.executeMethod(hostConfig, getMethod, state);
796 
797             return getMethod.getResponseBodyAsString();
798         }
799         finally {
800             getMethod.releaseConnection();
801         }
802     }
803 
804     /**
805      * This method only uses the default Commons HttpClient implementation when
806      * the URL object represents a HTTP resource. The URL object could also
807      * represent a file or some JNDI resource. In that case, the default Java
808      * implementation is used.
809      *
810      * @param       url URL object
811      * @return      A string representation of the resource referenced by the
812      *              URL object
813      * @throws      IOException
814      */
815     public String URLtoString(URL url) throws IOException {
816         String xml = null;
817 
818         if (url != null) {
819             String protocol = url.getProtocol().toLowerCase();
820 
821             if (protocol.startsWith(Http.HTTP) ||
822                 protocol.startsWith(Http.HTTPS)) {
823 
824                 return URLtoString(url.toString());
825             }
826 
827             URLConnection con = url.openConnection();
828 
829             InputStream is = con.getInputStream();
830 
831             ByteArrayMaker bam = new ByteArrayMaker();
832             byte[] bytes = new byte[512];
833 
834             for (int i = is.read(bytes, 0, 512); i != -1;
835                     i = is.read(bytes, 0, 512)) {
836 
837                 bam.write(bytes, 0, i);
838             }
839 
840             xml = new String(bam.toByteArray());
841 
842             is.close();
843             bam.close();
844         }
845 
846         return xml;
847     }
848 
849     protected void proxifyState(HttpState state, HostConfiguration hostConfig) {
850         Credentials proxyCredentials = _proxyCredentials;
851 
852         String host = hostConfig.getHost();
853 
854         if (isProxyHost(host) && (proxyCredentials != null)) {
855             AuthScope scope = new AuthScope(_PROXY_HOST, _PROXY_PORT, null);
856 
857             state.setProxyCredentials(scope, proxyCredentials);
858         }
859     }
860 
861     private static final int _MAX_CONNECTIONS_PER_HOST = GetterUtil.getInteger(
862         PropsUtil.get(HttpImpl.class.getName() + ".max.connections.per.host"),
863         2);
864 
865     private static final int _MAX_TOTAL_CONNECTIONS = GetterUtil.getInteger(
866         PropsUtil.get(HttpImpl.class.getName() + ".max.total.connections"),
867         20);
868 
869     private static final String _PROXY_HOST = GetterUtil.getString(
870         SystemProperties.get("http.proxyHost"));
871 
872     private static final int _PROXY_PORT = GetterUtil.getInteger(
873         SystemProperties.get("http.proxyPort"));
874 
875     private static final String _NON_PROXY_HOSTS =
876         SystemProperties.get("http.nonProxyHosts");
877 
878     private static final String _PROXY_AUTH_TYPE = GetterUtil.getString(
879         PropsUtil.get(HttpImpl.class.getName() + ".proxy.auth.type"));
880 
881     private static final String _PROXY_USERNAME = GetterUtil.getString(
882         PropsUtil.get(HttpImpl.class.getName() + ".proxy.username"));
883 
884     private static final String _PROXY_PASSWORD = GetterUtil.getString(
885         PropsUtil.get(HttpImpl.class.getName() + ".proxy.password"));
886 
887     private static final String _PROXY_NTLM_DOMAIN = GetterUtil.getString(
888         PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.domain"));
889 
890     private static final String _PROXY_NTLM_HOST = GetterUtil.getString(
891         PropsUtil.get(HttpImpl.class.getName() + ".proxy.ntlm.host"));
892 
893     private static final int _TIMEOUT = GetterUtil.getInteger(
894         PropsUtil.get(HttpImpl.class.getName() + ".timeout"), 5000);
895 
896     private static Log _log = LogFactory.getLog(HttpImpl.class);
897 
898     private HttpClient _client = new HttpClient();
899     private HttpClient _proxyClient = new HttpClient();
900     private Credentials _proxyCredentials;
901     private Pattern _nonProxyHostsPattern;
902 
903 }