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