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