001: /*
002: * $Id: HTTPRetriever.java,v 1.26 2006/01/27 23:21:33 js10465 Exp $
003: * $Source: /m/portal/ps/srap/src/com/sun/portal/rproxy/connectionhandler/HTTPRetriever.java,v $
004: * $Log: HTTPRetriever.java,v $
005: * Revision 1.26 2006/01/27 23:21:33 js10465
006: * Forward-port fix for 6.3.1 escalation. CRT333/334, CR 6356345 6365332
007: *
008: * Revision 1.25 2005/10/21 07:01:24 ss150821
009: * 6207673
010: *
011: * Revision 1.24 2005/03/01 10:30:36 np145014
012: * CR 6224556
013: *
014: * Revision 1.23 2005/03/01 10:17:24 np145014
015: * CR 6224556
016: *
017: * Revision 1.22 2005/02/25 09:44:13 ss150821
018: * RFE 6223490 - SRA Should use JDK based logging, changed to start throwing the full stacktrace for the exception in the logs
019: *
020: * Revision 1.21 2005/02/24 13:22:11 ss150821
021: * RFE 6223490 - SRA Should use JDK based logging
022: *
023: * Revision 1.20 2005/02/24 07:36:44 ss150821
024: * RFE 6223490 - SRA Should use JDK based logging
025: *
026: * Revision 1.19 2005/02/23 09:15:05 ss150821
027: * RFE 6223490 - SRA Should use JDK based logging
028: *
029: * Revision 1.18 2004/07/27 12:55:01 vt126379
030: * RFE#5075809, CRT#99
031: *
032: * Revision 1.17 2003/11/27 12:39:46 mm132998
033: * Restructuring and changes for PS-IS seperation
034: *
035: * Revision 1.16 2003/07/30 11:35:14 mm132998
036: * 4878523
037: *
038: * Revision 1.15 2003/07/10 14:14:06 mm132998
039: * Cookie parsing is being moved to independent file.
040: *
041: * Revision 1.14 2003/06/09 07:11:20 mm132998
042: * Bugs 4818006 , 4389707
043: *
044: * Revision 1.13 2003/05/16 15:23:14 mm132998
045: * PAC File support
046: *
047: * Revision 1.12 2003/03/27 13:27:57 bv131302
048: * Escalation 544720 fix
049: *
050: * Revision 1.11 2003/03/13 14:06:03 mm132998
051: * Bug Id's : 4783937 , 4821960 , 4809307, 4828938
052: *
053: * Revision 1.10 2002/09/18 12:40:06 bv131302
054: * CRT#1936 - yahoo
055: *
056: * Revision 1.9 2002/08/29 12:20:40 mm132998
057: * CRT # 1929
058: *
059: * Revision 1.8 2002/08/28 13:52:24 mm132998
060: * CRT # 1923
061: *
062: * Revision 1.7 2002/08/21 10:48:38 ss133690
063: * Bug 4710658
064: *
065: * Revision 1.6 2002/08/21 07:36:18 bv131302
066: * Fixes for 4681457, 4703696, 4713296, 4718232, 4726004, 4726008
067: *
068: * Revision 1.5 2002/08/16 15:13:06 bv131302
069: * Hana CRT#1888 - Check log settings before logging
070: *
071: * Revision 1.4 2002/08/16 12:19:54 bv131302
072: * Hana CRT#1884 - RProxy perf issues
073: *
074: * Revision 1.3 2002/06/26 12:21:34 mm132998
075: * Bugs : 4705244 4705237 4705232 4705991, CRT : 1525
076: *
077: * Revision 1.2 2002/06/21 13:04:14 bv131302
078: * LDAP Attribute name changes
079: *
080: * Revision 1.1 2002/06/14 09:53:52 rt130506
081: * SRAP rebranding
082: *
083: * Revision 1.12 2002/06/12 07:55:58 bv131302
084: * more rebranding - filenames
085: *
086: * Revision 1.11 2002/06/11 16:02:03 bv131302
087: * new branded
088: *
089: * Revision 1.10 2002/05/13 06:22:23 mm132998
090: * Perf related modifications
091: *
092: * Revision 1.9 2002/04/08 12:18:09 mm132998
093: * Bug ID - # 4664279 CRT : 742 , Desc - Handle '|' in the cookie value.
094: *
095: * Revision 1.8 2002/03/21 14:48:58 mm132998
096: * Bug ID : # 4655970 Hana CRT : # 615 Desc : Lihue PRD 7.4.1.1 - RProxy seperation
097: *
098: * Revision 1.7 2002/03/15 11:19:45 bv131302
099: * Hana CRT#559 - consistent attribute names
100: *
101: * Revision 1.6 2002/03/14 13:53:12 mm132998
102: * Bug ID : # 4652737 CRT : # 558 Desc : Cookie migration to iDSAME
103: *
104: * Revision 1.5 2002/03/14 08:59:18 mm132998
105: * Bug ID # 4652682 CRT # 557 Desc : gateway should not use uppercase letters for domainname while setting cookies
106: *
107: * Revision 1.4 2002/03/01 14:56:52 mm132998
108: * Bug ID : # 4618949 , CRT : # 416 , Desc : Gateway should pass through Authentication headers when the scheme is not Basic.
109: *
110: * Revision 1.3 2002/03/01 14:36:48 mm132998
111: * Bug ID : # 4619451 CRT : # 415 Desc : Improve POST performance
112: *
113: * Revision 1.2 2002/02/26 11:02:08 mm132998
114: * Bug ID : 4643126 CRT : 368 Desc: Lihue PRD 7.4.3
115: *
116: *
117: */
118: /*
119: * HTTPRetriever.java
120: *
121: * $Author: js10465 $
122: *
123: * $Date: 2006/01/27 23:21:33 $ $Revision: 1.26 $
124: *
125: * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
126: *
127: * Developed by SunPS and SunIR
128: */
129:
130: package com.sun.portal.rproxy.connectionhandler;
131:
132: import java.io.BufferedInputStream;
133: import java.io.ByteArrayOutputStream;
134: import java.io.EOFException;
135: import java.io.IOException;
136: import java.io.OutputStream;
137: import java.net.InetAddress;
138: import java.util.logging.Level;
139: import java.util.logging.Logger;
140:
141: import com.sun.portal.log.common.PortalLogger;
142: import com.sun.portal.rproxy.configservlet.client.GatewayProfile;
143: import com.sun.portal.rproxy.monitoring.util.RProxyEvent;
144: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
145: import com.sun.portal.util.DomainWebProxyConfig;
146: import com.sun.portal.util.ServiceIdentifier;
147:
148: /**
149: * This class retrieves objects using the HTTP protocol.
150: *
151: * @author Gabriel Lawrence
152: */
153: public class HTTPRetriever implements Retriever {
154: /**
155: * Sends request info to designated web host.
156: *
157: * @param req
158: * The request to send to the server.
159: * @param s
160: * The CachedSocket object to use for the output stream tp the
161: * server.
162: */
163: private static final String SET_COOKIE = "Set-Cookie";
164:
165: // Lihue PRD : 7.4.3.2
166: // End of Code : Lihue PRD : 7.4.3.2
167: private static String gateway_ip;
168:
169: private static boolean cookie_management;
170:
171: private static boolean doBasicAuthentication = false;
172:
173: public static int numBytesRead = 0;
174:
175: // Lihue - Bug ID # 4619451
176: private static final int defaultBufferSize = 2048;
177:
178: private static final int MARK_MAX_VALUE = 8192;
179:
180: // End of Code : Lihue - Bug ID # 4619451
181:
182: // private static Logger logger =
183: // Logger.getLogger("com.sun.portal.sra.rproxy");
184: private static Logger logger = PortalLogger
185: .getLogger(HTTPRetriever.class);
186:
187: // pass param
188: public Response getResponse2(Request req, String destinationHost,
189: Integer logId, byte[] param) {
190: CachedSocket s = null;
191: CSBufferedInputStream in = null;
192:
193: String password = null;
194: boolean found_pw = false;
195:
196: if (doBasicAuthentication) {
197: password = req.getRequestHeader("Authorization");
198:
199: if (password == null) {
200: found_pw = BasicAuthentication.getPassword(req,
201: destinationHost);
202: }
203: // Lihue Bug ID - 4618949
204: else {
205: String basic = password.substring(
206: password.indexOf(':') + 1).trim();
207: if (!basic.regionMatches(true, 0, "basic ", 0, 5)) {
208: password = null;
209: }
210: }
211: // EOC : Lihue Bug ID - 4618949
212: }
213: // Lihue PRD : 7.4.3.2
214: if (req.getUseProxy()) {
215: String prxyhost = req.getProxyHost();
216: String headerProxyAuth = req
217: .getRequestHeader("Proxy-Authorization");
218:
219: if (headerProxyAuth == null) {
220: String proxy_passwd = DomainWebProxyConfig
221: .getProxyPassword(prxyhost);
222: if (proxy_passwd != null) {
223: req.setRequestHeader("Proxy-Authorization",
224: "Proxy-Authorization: Basic "
225: + proxy_passwd);
226: }
227: }
228: }
229: // End of Code - Lihue PRD : 7.4.3.2
230: Response result = null;
231: try {
232: result = new HTTPResponse();
233: // logger.info("HTTPRetriever retrieving " + req);
234: Object[] params0 = { req };
235: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR070",
236: params0);
237:
238: if (req.getUseProxy()) {
239: s = CachedSocketFactory.getCachedSocket(req
240: .getProxyHost(), req.getProxyPort(), "plain",
241: logId);
242:
243: // logger.info("HTTPRetriever getProxyHost connecting to: " +
244: // req.getProxyHost());
245: Object[] params1 = { req.getProxyHost() };
246: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR071",
247: params1);
248: } else {
249: s = CachedSocketFactory
250: .getCachedSocket(req.getHost(), Integer
251: .parseInt(req.getPort()), "plain",
252: logId);
253: // logger.info("HTTPRetriever: Connecting to: " +
254: // req.getHost());
255: Object[] params2 = { req.getHost() };
256: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR072",
257: params2);
258: }
259:
260: if (s == null) {
261: logger
262: .warning("HTTPRetriever: Null socket returned. Probably couldn't resolve host.");
263: return null;
264: }
265:
266: try {
267: sendRequest2(req, s, param);
268: in = s.getInputStream();
269: getResponseHeader((HTTPResponse) result, in, req);
270: } catch (Exception e) {
271: // create a new socket
272: if (req.getUseProxy()) {
273: s = CachedSocketFactory.getNewCachedSocket(req
274: .getProxyHost(), req.getProxyPort(),
275: "plain", logId);
276: // logger.info("HTTPRetriever: Connecting to: " +
277: // req.getProxyHost());
278: Object[] params3 = { req.getProxyHost() };
279: logger.log(Level.INFO,
280: "PSSRRPROXY_CSPRCONHNDLR073", params3);
281: } else {
282: s = CachedSocketFactory.getNewCachedSocket(req
283: .getHost(),
284: Integer.parseInt(req.getPort()), "plain",
285: logId);
286: // logger.info("HTTPRetriever: Connecting to: " +
287: // req.getHost());
288: Object[] params4 = { req.getHost() };
289: logger.log(Level.INFO,
290: "PSSRRPROXY_CSPRCONHNDLR074", params4);
291: }
292:
293: sendRequest2(req, s, param);
294: in = s.getInputStream();
295: getResponseHeader((HTTPResponse) result, in, req);
296: }
297: if (doBasicAuthentication) {
298: if (result.getStatusCode().compareTo("401") != 0
299: && result.getStatusCode().compareTo("403") != 0) {
300: if (password != null) {
301: BasicAuthentication.storePassword(req,
302: destinationHost);
303: }
304: }
305: }
306:
307: // Get content length from response header. If not known then set it
308: // to the number of available bytes in the stream.
309: String cl = result.getResponseHeader("content-length");
310: int length = 0;
311: if (cl != null) {
312: length = Integer.parseInt(cl.substring(
313: cl.indexOf(':') + 1).trim());
314: } else {
315: length = -1; // indicates length is unknown
316: }
317:
318: /*
319: * //Look for a keep-alive timeout value to use for socket timeout
320: * //value String ka = result.getResponseHeader("keep-alive"); if
321: * (ka != null) { int to =
322: * Integer.parseInt(ka.substring(ka.indexOf("timeout=")+8,
323: * ka.indexOf(','))); if (to > 0) s.setTimeout(to); }
324: */
325:
326: // Check if socket is to be closed after buffer has been read.
327: /*
328: * if (result.getResponseHeader("Connection: close") != null)
329: * in.setClosed(); else in.setClosed();
330: */
331: in.setClosed();
332:
333: // Set the content stream with the CachedSocket and set buffer
334: // length. Length is number of bytes or -1 if unknown
335: ((CSBufferedInputStream) in).setLength(length);
336: ((HTTPResponse) result).setContentStream(in);
337: result.setSocket(s);
338: } catch (Exception ex) {
339: // logger.log(Level.SEVERE, "HTTPRetriever failed", ex);
340: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR075", ex);
341: result = null;
342: /**
343: * Bug 4710658
344: */
345: if (s != null) {
346: try {
347: s.close();
348: } catch (IOException e) {
349: }
350: }
351: // End of code change for Bug 4710658
352:
353: }
354: if (result != null)
355: result.setSocket(s);
356: return result;
357: }
358:
359: static {
360: // Multiple interface may get wrong IP address!?
361:
362: try {
363: byte[] ip = InetAddress.getLocalHost().getAddress();
364: gateway_ip = Integer.toString((ip[0] & 0xff)) + "."
365: + Integer.toString((ip[1] & 0xff)) + "."
366: + Integer.toString((ip[2] & 0xff)) + "."
367: + Integer.toString((ip[3] & 0xff));
368: } catch (Exception e) {
369: gateway_ip = null;
370: }
371:
372: cookie_management = GatewayProfile.getBoolean(
373: "CookieManagement", false)
374: && ServiceIdentifier.isGateway();
375:
376: doBasicAuthentication = GatewayProfile.getBoolean(
377: "DoBasicAuthentication", false)
378: && ServiceIdentifier.isGateway();
379: }
380:
381: public void sendRequest(Request req, CachedSocket s)
382: throws IOException, EOFException {
383:
384: if (cookie_management && (!req.getHost().equals(gateway_ip))) {
385:
386: CookieParser.restoreCookieHeader(req);
387: }
388:
389: OutputStream out = s.getOutputStream();
390:
391: // search for the thing here and replace with proxy
392:
393: // Write request header
394: out.write(req.getRequestHeaderBytes());
395:
396: out.flush();
397:
398: // If the request has a content-length, write the content
399:
400: String contentLength = req.getRequestHeader("Content-length");
401: if (contentLength != null) {
402: int length = Integer.parseInt(contentLength.substring(
403: contentLength.indexOf(':') + 1).trim());
404: // Lihue - Bug ID # 4619451
405: /*
406: * byte content [] = new byte [length];
407: *
408: * BufferedInputStream reqIn = req.getContentStream(); int
409: * read=0,totalRead=0; while (totalRead <length) { int max_read =
410: * length-totalRead; read=reqIn.read(content,totalRead, max_read);
411: * if (read==-1 && totalRead <length) { throw new EOFException(); }
412: * totalRead+=read; } numBytesRead += totalRead;
413: * req.addSize(totalRead);
414: *
415: * out.write(content);
416: */
417: byte content[] = new byte[defaultBufferSize];
418: int read = 0, totalRead = 0;
419: BufferedInputStream reqIn = req.getContentStream();
420: while (totalRead + defaultBufferSize < length) {
421: // Loop for more than content.length size
422: read = reqIn.read(content, 0, defaultBufferSize);
423: if (read == -1 && totalRead < length) {
424: throw new EOFException();
425: } else if (read == 0) {
426: break;
427: }
428: out.write(content, 0, read);
429: totalRead += read;
430: }
431: int buffoffset = 0;
432: while (totalRead < length) {
433: // Loop for not completely reading remaining bytes that can fill
434: // buffer
435: read = reqIn.read(content, buffoffset, length
436: - totalRead);
437: if (read == -1 && totalRead < length) {
438: throw new EOFException();
439: } else if (read == 0) {
440: break;
441: }
442: totalRead += read;
443: buffoffset += read;
444: }
445: out.write(content, 0, buffoffset);
446: req.addSize(totalRead);
447: MonitoringSubsystem.handleEvent(RProxyEvent.REQUEST_READ,
448: new Long(totalRead));
449: // End of Code : Lihue - Bug ID # 4619451
450: }
451: // Make sure we've sent everything before we start to look for the
452: // answer
453: out.flush();
454: }
455:
456: public void sendRequest2(Request req, CachedSocket s, byte[] content)
457: throws IOException, EOFException {
458:
459: if (cookie_management && (!req.getHost().equals(gateway_ip))) {
460:
461: CookieParser.restoreCookieHeader(req);
462: }
463:
464: OutputStream out = s.getOutputStream();
465:
466: // search for the thing here and replace with proxy
467:
468: // Write request header
469: out.write(req.getRequestHeaderBytes());
470:
471: out.flush();
472:
473: // If the request has a content-length, write the content
474:
475: String contentLength = req.getRequestHeader("Content-length");
476: if (contentLength != null) {
477: int length = Integer.parseInt(contentLength.substring(
478: contentLength.indexOf(':') + 1).trim());
479: out.write(content);
480: }
481: // Make sure we've sent everything before we start to look for the
482: // answer
483: out.flush();
484: }
485:
486: /**
487: * Get the response header back from the server
488: */
489: public void getResponseHeader(HTTPResponse result,
490: CSBufferedInputStream in, Request req) throws IOException,
491: EOFException {
492:
493: boolean toSetCookie = (cookie_management && (!req.getHost()
494: .equals(gateway_ip)));
495:
496: boolean keepGoing = true;
497: int b;
498:
499: in.setLength(4000); // for header length
500: String s;
501: String s_new;
502: ByteArrayOutputStream byteOutStrm = new ByteArrayOutputStream();
503: while (keepGoing) {
504: b = in.read();
505: if (b == -1) {
506: keepGoing = false;
507: throw new EOFException();
508: }
509: byteOutStrm.write(b);
510: if (b == 10) {
511: s = byteOutStrm.toString();
512: if (s.regionMatches(true, 0, SET_COOKIE, 0, SET_COOKIE
513: .length())
514: && toSetCookie) {
515: s_new = CookieParser.modifyCookieHeader(result,
516: req, s);
517: if (s_new != null) {
518: ((HTTPResponse) result).addHeaderLine(s_new);
519: }
520: } else {
521: /* fix for 4726004 - begin - fix came from 4516049 (SP3HP3) */
522: if ((s.toLowerCase()).startsWith("content-length")) {
523: String myHeader = ((HTTPResponse) result)
524: .getResponseHeader("Content-Length");
525: if (myHeader == null) {
526: ((HTTPResponse) result).addHeaderLine(s);
527: }
528: } else if ((s.toLowerCase())
529: .startsWith("content-disposition")) {
530: s = byteOutStrm.toString("ISO-8859-1");
531: ((HTTPResponse) result).addHeaderLine(s);
532: } else {
533: // BEGIN CR6365332
534: boolean stripHeader = false;
535: String ls = s.toLowerCase();
536: if (ls.startsWith("www-authenticate")
537: && (ls.indexOf("ntlm") != -1 || ls
538: .indexOf("negotiate") != -1)) {
539: stripHeader = true;
540: }
541: if (!stripHeader)
542: // END CR6365332
543: ((HTTPResponse) result).addHeaderLine(s);
544: } // end of else
545: /* fix for 4726004 - end */
546: }
547: keepGoing = !((HTTPResponse) result).isHeaderComplete();
548: byteOutStrm.reset();
549: }
550: }
551: }
552:
553: /**
554: * Get a response for a specific Request
555: */
556: public Response getResponse(Request req, String destinationHost,
557: Integer logId) {
558: CachedSocket s = null;
559: CSBufferedInputStream in = null;
560:
561: String password = null;
562: boolean found_pw = false;
563:
564: if (doBasicAuthentication) {
565: password = req.getRequestHeader("Authorization");
566:
567: if (password == null) {
568: found_pw = BasicAuthentication.getPassword(req,
569: destinationHost);
570: }
571: // Lihue Bug ID - 4618949
572: else {
573: String basic = password.substring(
574: password.indexOf(':') + 1).trim();
575: // System.out.println("Header value : " + basic);
576: if (!basic.regionMatches(true, 0, "basic ", 0, 5)) {
577: password = null;
578: // System.out.println("Not basic !");
579: }
580: }
581: // EOC : Lihue Bug ID - 4618949
582: }
583: // Lihue PRD : 7.4.3.2
584: if (req.getUseProxy()) {
585: String prxyhost = req.getProxyHost();
586: String headerProxyAuth = req
587: .getRequestHeader("Proxy-Authorization");
588: if (headerProxyAuth == null) {
589: String proxy_passwd = DomainWebProxyConfig
590: .getProxyPassword(prxyhost);
591: if (proxy_passwd != null) {
592: req.setRequestHeader("Proxy-Authorization",
593: "Proxy-Authorization: Basic "
594: + proxy_passwd);
595: }
596: }
597: }
598: // End of Code - Lihue PRD : 7.4.3.2
599:
600: Response result = null;
601: try {
602: result = new HTTPResponse();
603: // logger.info("HTTPRetriever retrieving " + req);
604: Object[] params6 = { req };
605: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR076",
606: params6);
607:
608: if (req.getUseProxy()) {
609: s = CachedSocketFactory.getCachedSocket(req
610: .getProxyHost(), req.getProxyPort(), "plain",
611: logId);
612:
613: // logger.info("HTTPRetriever getProxyHost connecting to: " +
614: // req.getProxyHost());
615: Object[] params7 = { req.getProxyHost() };
616: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR077",
617: params7);
618: } else {
619: s = CachedSocketFactory
620: .getCachedSocket(req.getHost(), Integer
621: .parseInt(req.getPort()), "plain",
622: logId);
623: // logger.info("HTTPRetriever: Connecting to: " +
624: // req.getHost());
625: Object[] params8 = { req.getHost() };
626: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR078",
627: params8);
628: }
629:
630: if (s == null) {
631: logger
632: .warning("HTTPRetriever: Null socket returned. Probably couldn't resolve host.");
633: return null;
634: }
635:
636: try {
637: // logger.info("HTTPRetriever before sendRequest(): req == " +
638: // req);
639: Object[] params9 = { req };
640: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR079",
641: params9);
642: if (req.getContentStream() != null) {
643: req.getContentStream().mark(MARK_MAX_VALUE);
644: }
645: sendRequest(req, s);
646: in = s.getInputStream();
647: getResponseHeader((HTTPResponse) result, in, req);
648: } catch (Exception e) {
649: MonitoringSubsystem
650: .handleEvent(RProxyEvent.RETRIEVAL_ERROR);
651: // logger.log(Level.SEVERE, "HTTPRetriever: Caught exception ",
652: // e);
653: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR080",
654: e);
655: // create a new socket
656: if (req.getUseProxy()) {
657: s = CachedSocketFactory.getNewCachedSocket(req
658: .getProxyHost(), req.getProxyPort(),
659: "plain", logId);
660: // logger.info("HTTPRetriever: Connecting to: " +
661: // req.getProxyHost());
662: Object[] params11 = { req.getProxyHost() };
663: logger.log(Level.INFO,
664: "PSSRRPROXY_CSPRCONHNDLR081", params11);
665: } else {
666: s = CachedSocketFactory.getNewCachedSocket(req
667: .getHost(),
668: Integer.parseInt(req.getPort()), "plain",
669: logId);
670: // logger.info("HTTPRetriever: Connecting to: " +
671: // req.getHost());
672: Object[] params12 = { req.getHost() };
673: logger.log(Level.INFO,
674: "PSSRRPROXY_CSPRCONHNDLR082", params12);
675: }
676:
677: // logger.info("HTTPRetriever: inside catch before sendRequest
678: // req.getObject() == " + req.getObject());
679: Object[] params13 = { req.getObject() };
680: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR083",
681: params13);
682:
683: if (req.getContentStream() != null) {
684: req.getContentStream().reset();
685: }
686: sendRequest(req, s);
687: in = s.getInputStream();
688: getResponseHeader((HTTPResponse) result, in, req);
689: }
690: // System.out.println("Our test : " + result.getStatusCode() + "
691: // pass : " + password);
692: if (doBasicAuthentication) {
693: if (result.getStatusCode().compareTo("401") != 0
694: && result.getStatusCode().compareTo("403") != 0) {
695: // System.out.println("Basic auth Failed !!");
696: if (password != null) {
697: BasicAuthentication.storePassword(req,
698: destinationHost);
699: }
700: }
701: }
702:
703: // Get content length from response header. If not known then set it
704: // to the number of available bytes in the stream.
705: String cl = result.getResponseHeader("content-length");
706: int length = 0;
707: if (cl != null) {
708: length = Integer.parseInt(cl.substring(
709: cl.indexOf(':') + 1).trim());
710: } else {
711: length = -1; // indicates length is unknown
712: }
713:
714: // Look for a keep-alive timeout value to use for socket timeout
715: // value
716: /*
717: * String ka = result.getResponseHeader("keep-alive"); if (ka !=
718: * null) { int to =
719: * Integer.parseInt(ka.substring(ka.indexOf("timeout=")+8,
720: * ka.indexOf(','))); if (to > 0) s.setTimeout(to); } // Check if
721: * socket is to be closed after buffer has been read. if
722: * (result.getResponseHeader("Connection: close") != null)
723: * in.setClosed(); else in.setClosed();
724: */
725: in.setClosed();
726:
727: // Set the content stream with the CachedSocket and set buffer
728: // length. Length is number of bytes or -1 if unknown
729: ((CSBufferedInputStream) in).setLength(length);
730: ((HTTPResponse) result).setContentStream(in);
731: result.setSocket(s);
732: } catch (Exception ex) {
733: MonitoringSubsystem
734: .handleEvent(RProxyEvent.RETRIEVAL_ERROR);
735: // logger.log(Level.SEVERE, "HTTPRetriever failed", ex);
736: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR084", ex);
737: result = null;
738: /**
739: * Bug 4710658
740: */
741: if (s != null) {
742: try {
743: s.close();
744: } catch (IOException e) {
745: }
746: }
747: // End of code change for Bug 4710658
748:
749: }
750: if (result != null)
751: result.setSocket(s);
752: return result;
753: }
754: }
|