001: /*
002: * $Id: JSSProxyRunnable.java,v 1.14 2005/09/21 11:07:13 dg154973 Exp $
003: * $Source: /m/portal/ps/srap/src/com/sun/portal/rproxy/connectionhandler/JSSProxyRunnable.java,v $
004: * $Log: JSSProxyRunnable.java,v $
005: * Revision 1.14 2005/09/21 11:07:13 dg154973
006: * CR 6280402 - log system results in excessive lock contention
007: *
008: * Revision 1.13 2005/03/30 09:04:14 dg154973
009: * CR 6247264 PortalLogger should not be used with 'this' option
010: *
011: * Revision 1.12 2005/03/01 10:28:46 np145014
012: * CR 6224556
013: *
014: * Revision 1.11 2005/03/01 10:17:24 np145014
015: * CR 6224556
016: *
017: * Revision 1.10 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.9 2005/02/24 07:36:45 ss150821
021: * RFE 6223490 - SRA Should use JDK based logging
022: *
023: * Revision 1.8 2005/02/23 09:15:07 ss150821
024: * RFE 6223490 - SRA Should use JDK based logging
025: *
026: * Revision 1.7 2004/07/27 12:55:01 vt126379
027: * RFE#5075809, CRT#99
028: *
029: * Revision 1.6 2003/11/27 12:39:46 mm132998
030: * Restructuring and changes for PS-IS seperation
031: *
032: * Revision 1.5 2003/05/26 10:30:47 rt130506
033: * Netlet- ProxyTunnel
034: *
035: * Revision 1.4 2003/03/17 11:12:08 mm132998
036: * 4784015 : proxy auth not working while accessing ssl sites.
037: *
038: * Revision 1.3 2002/08/21 10:35:49 ss133690
039: * Bug 4710658
040: *
041: * Revision 1.2 2002/08/16 15:13:06 bv131302
042: * Hana CRT#1888 - Check log settings before logging
043: *
044: * Revision 1.1 2002/06/14 09:53:53 rt130506
045: * SRAP rebranding
046: *
047: * Revision 1.4 2002/06/11 16:02:03 bv131302
048: * new branded
049: *
050: * Revision 1.3 2002/05/13 06:22:24 mm132998
051: * Perf related modifications
052: *
053: * Revision 1.2 2002/02/26 11:02:08 mm132998
054: * Bug ID : 4643126 CRT : 368 Desc: Lihue PRD 7.4.3
055: *
056: *
057: */
058: package com.sun.portal.rproxy.connectionhandler;
059:
060: import java.io.DataInputStream;
061: import java.io.IOException;
062: import java.io.InputStream;
063: import java.io.OutputStream;
064: import java.io.UnsupportedEncodingException;
065: import java.net.ServerSocket;
066: import java.net.Socket;
067: import java.net.SocketException;
068: import java.util.HashMap;
069: import java.util.logging.Level;
070: import java.util.logging.Logger;
071:
072: import com.sun.portal.util.*;
073: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
074: import com.sun.portal.log.common.PortalLogger;
075:
076: public class JSSProxyRunnable implements Runnable {
077: public static HashMap connectHashMap = new HashMap();
078:
079: private class JSSProxySessionRunnable implements Runnable {
080: private Socket inconnection = null;
081:
082: private Socket toProxySocket = null;
083:
084: JSSProxySessionRunnable(Socket socket) {
085: inconnection = socket;
086: }
087:
088: public void run() {
089: Integer remotePort = new Integer(inconnection.getPort());
090: byte[] prebuffer = new byte[1];
091:
092: DataInputStream inFrom;
093: try {
094: inFrom = new DataInputStream(inconnection
095: .getInputStream());
096: inFrom.readFully(prebuffer, 0, 1);
097: } catch (IOException e) {
098: // logger.log(Level.SEVERE, "JSSProxySessionRunnable: Unable to
099: // open input stream on " + inconnection, e);
100: Object[] params0 = { inconnection, e };
101: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR100",
102: params0);
103:
104: connectHashMap.remove(remotePort);
105: closeSockets();
106: return;
107: }
108:
109: ServiceReachabilityInfo sInfo = (ServiceReachabilityInfo) connectHashMap
110: .remove(remotePort);
111:
112: String host = sInfo.getProxyHost();
113: int port = sInfo.getProxyPort();
114:
115: try {
116: toProxySocket = new Socket(host, port);
117: toProxySocket.setTcpNoDelay(true);
118: MonitoringSubsystem
119: .handleEvent(SRAEvent.PLAIN_SOCKET_CREATED);
120: } catch (Exception ex) {
121: toProxySocket = null;
122: // logger.severe("JSSProxySessionRunnable: " + "Unable to
123: // connect to " + host + ":" + port + ". " + ex);
124: Object[] params = { host, port + "", ex };
125: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR101",
126: params);
127: }
128:
129: if (toProxySocket == null) {
130: closeSockets();
131: return;
132: }
133:
134: String desthost = sInfo.getHost();
135: String destport = Integer.toString(sInfo.getPort());
136:
137: OutputStream out = null;
138: InputStream in = null;
139: byte reply[] = new byte[200];
140: int replyLen = 0;
141: int newlinesSeen = 0;
142: boolean headerDone = false; // Done on first newline
143:
144: try {
145: out = toProxySocket.getOutputStream();
146: // Lihue PRD : 7.4.3.2
147: // String msg =
148: // "CONNECT " + desthost + ":" + destport + " HTTP/1.0\n" +
149: // "User-Agent: " +
150: // sun.net.www.protocol.http.HttpURLConnection.userAgent +
151: // "\r\n\r\n";
152:
153: StringBuffer strsb = new StringBuffer();
154: String msg = null;
155: strsb
156: .append("CONNECT ")
157: .append(desthost)
158: .append(":")
159: .append(destport)
160: .append(" HTTP/1.0\r\n")
161: .append("User-Agent: ")
162: .append(
163: sun.net.www.protocol.http.HttpURLConnection.userAgent);
164: String authinfo = DomainWebProxyConfig
165: .getProxyPassword(host);
166: if (authinfo != null) {
167: strsb.append("\r\nProxy-Authorization: Basic ")
168: .append(authinfo).append("\r\n");
169: } else {
170: strsb.append("\r\n\r\n");
171: }
172: msg = strsb.toString();
173: // End of Code - Lihue PRD : 7.4.3.2
174:
175: // logger.info(msg);
176: Object[] params = { msg };
177: logger.log(Level.INFO, "PSSRRPROXY_CSPRCONHNDLR102",
178: params);
179:
180: byte b[];
181: try {
182: b = msg.getBytes("ASCII7");
183: } catch (UnsupportedEncodingException ignored) {
184: // If ASCII7 isn't there, something serious is wrong, but
185: // Paranoia Is Good (tm)
186: b = msg.getBytes();
187: }
188:
189: out.write(b);
190: out.flush();
191:
192: // We need to store the reply so we can create a detailed
193: // error message to the user.
194:
195: in = toProxySocket.getInputStream();
196:
197: boolean error = false;
198:
199: while (newlinesSeen < 2) {
200: int i = in.read();
201: if (i < 0) {
202: logger
203: .warning("JSSProxySessionRunnable: Unexpected EOF from proxy");
204: closeSockets();
205: return;
206: }
207:
208: if (i == '\n') {
209: headerDone = true;
210: ++newlinesSeen;
211: } else if (i != '\r') {
212: newlinesSeen = 0;
213: if (!headerDone && replyLen < reply.length) {
214: reply[replyLen++] = (byte) i;
215: }
216: }
217: }
218: } catch (IOException ioe) {
219: // logger.severe("JSSProxySessionRunnable: " + "Unable to get
220: // OutputStream" + ioe);
221: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR103",
222: ioe);
223: closeSockets();
224: return;
225: }
226:
227: // Converting the byte array to a string is slightly wasteful
228: // in the case where the connection was successful, but it's
229: // insignificant compared to the network overhead.
230: String replyStr;
231: try {
232: replyStr = new String(reply, 0, replyLen, "ASCII7");
233: } catch (UnsupportedEncodingException ignored) {
234: replyStr = new String(reply, 0, replyLen);
235: }
236:
237: // We asked for HTTP/1.0, so we should get that back
238: if (!replyStr.startsWith("HTTP/1.0 200")) {
239: // logger.severe("JSSProxySessionRunnable: Unable to tunnel
240: // through ");
241: logger.severe("PSSRRPROXY_CSPRCONHNDLR104");
242: closeSockets();
243: return;
244: }
245:
246: try {
247: out.write(prebuffer);
248: out.flush();
249: } catch (IOException ioe) {
250: // logger.severe("JSSProxySessionRunnable: " + "Unable to write
251: // prebuffer." + ioe);
252: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR105",
253: ioe);
254: closeSockets();
255: return;
256: }
257:
258: new RWGroupJSSProxy(inconnection, toProxySocket);
259: }
260:
261: void closeSockets() {
262: if (inconnection != null) {
263: try {
264: inconnection.close();
265: } catch (Exception e1) {
266: }
267: /**
268: * Bug 4710658
269: */
270: finally {
271: if (inconnection instanceof org.mozilla.jss.ssl.SSLSocket) {
272: MonitoringSubsystem
273: .handleEvent(SRAEvent.SSL_SOCKET_DESTROYED);
274: } else {
275: MonitoringSubsystem
276: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
277: }
278: inconnection = null;
279: }
280: // End of code change for Bug 4710658
281:
282: }
283: if (toProxySocket != null) {
284: boolean isHTTPS = toProxySocket instanceof org.mozilla.jss.ssl.SSLSocket;
285: try {
286: toProxySocket.close();
287: if (isHTTPS) {
288: MonitoringSubsystem
289: .handleEvent(SRAEvent.SSL_SOCKET_DESTROYED);
290: } else {
291: MonitoringSubsystem
292: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
293: }
294: } catch (Exception e1) {
295: }
296: /**
297: * Bug 4710658
298: */
299: finally {
300: toProxySocket = null;
301: }
302: // End of code change for Bug 4710658
303: }
304: }
305: }
306:
307: private ServerSocket sconnection = null;
308:
309: private static Logger logger = PortalLogger
310: .getLogger(JSSProxyRunnable.class);
311:
312: public JSSProxyRunnable(ServerSocket ss) {
313: sconnection = ss;
314: }
315:
316: public void run() {
317: boolean go = true;
318: Socket inconnection;
319: boolean isHTTPS = sconnection instanceof org.mozilla.jss.ssl.SSLServerSocket;
320:
321: while (go) {
322: try {
323: try {
324: inconnection = sconnection.accept();
325: if (isHTTPS) {
326: MonitoringSubsystem
327: .handleEvent(SRAEvent.SSL_SOCKET_CREATED);
328: } else {
329: MonitoringSubsystem
330: .handleEvent(SRAEvent.PLAIN_SOCKET_CREATED);
331: }
332:
333: } catch (IOException e) {
334: // logger.severe("JSSProxyRunnable: Unable to accept new
335: // connection." + e);
336: logger.log(Level.SEVERE,
337: "PSSRRPROXY_CSPRCONHNDLR106", e);
338: /**
339: * Bug 4710658
340: */
341: if (sconnection != null) {
342: try {
343: sconnection.close();
344: MonitoringSubsystem
345: .handleEvent(SRAEvent.SERVER_SOCKET_DESTROYED);
346: } catch (IOException ee) {
347: // logger.severe("JSSProxyRunnable: Unable to close
348: // server socket." + e);
349: logger.log(Level.SEVERE,
350: "PSSRRPROXY_CSPRCONHNDLR107", e);
351: } finally {
352: sconnection = null;
353: }
354: }
355: // End of code change for Bug 4710658
356: go = false;
357: continue;
358: }
359:
360: try {
361: inconnection.setTcpNoDelay(true);
362: } catch (SocketException e) {
363: // logger.severe("JSSProxyRunnable: Unable to TcpNoDelay." +
364: // e);
365: logger.log(Level.SEVERE,
366: "PSSRRPROXY_CSPRCONHNDLR108", e);
367: /**
368: * Bug 4710658
369: */
370: if (inconnection != null) {
371: try {
372: inconnection.close();
373: } catch (IOException ee) {
374: } finally {
375: if (isHTTPS) {
376: MonitoringSubsystem
377: .handleEvent(SRAEvent.SSL_SOCKET_DESTROYED);
378: } else {
379: MonitoringSubsystem
380: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
381: }
382: inconnection = null;
383: }
384: }
385: // End of code change for Bug 4710658
386: continue;
387: }
388:
389: JSSProxyRunnable.JSSProxySessionRunnable p = new JSSProxySessionRunnable(
390: inconnection);
391: try {
392: GWThreadPool.run(p);
393: } catch (InterruptedException e) {
394: // logger.log(Level.SEVERE, "JSSProxyRunnable: Unable to run
395: // new JSSProxySession", e);
396: logger.log(Level.SEVERE,
397: "PSSRRPROXY_CSPRCONHNDLR109", e);
398: /**
399: * Bug 4710658
400: */
401: if (inconnection != null) {
402: try {
403: inconnection.close();
404: //p = null;
405: } catch (IOException ee) {
406: } finally {
407: if (isHTTPS) {
408: MonitoringSubsystem
409: .handleEvent(SRAEvent.SSL_SOCKET_DESTROYED);
410: } else {
411: MonitoringSubsystem
412: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
413: }
414: inconnection = null;
415: p = null;
416: }
417: // End of code change for Bug 4710658
418: }
419: }
420:
421: } catch (Throwable t) {
422: // logger.log(Level.SEVERE, "JSSProxyRunnable: Uncaught
423: // exception:", t);
424: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRCONHNDLR110",
425: t);
426: }
427: }
428: }
429: }
|