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