001: /*
002: * $Id: JSSProxyRunnable.java,v 1.7 2005/11/30 11:27:27 ss150821 Exp $
003: * $Source: /m/portal/ps/srap/src/com/sun/portal/rproxy/https/JSSProxyRunnable.java,v $
004: * $Log: JSSProxyRunnable.java,v $
005: * Revision 1.7 2005/11/30 11:27:27 ss150821
006: * 6356996 - Srap Code base needs to save files in the unix file format and not windows
007: *
008: * Revision 1.6 2005/02/25 09:44:16 ss150821
009: * RFE 6223490 - SRA Should use JDK based logging, changed to start throwing the full stacktrace for the exception in the logs
010: *
011: * Revision 1.5 2005/02/23 09:02:01 ss150821
012: * RFE 6223490 - SRA Should use JDK based logging
013: *
014: * Revision 1.4 2005/02/23 08:59:22 ss150821
015: * RFE 6223490 - SRA Should use JDK based logging
016: *
017: * Revision 1.3 2004/07/27 12:58:28 vt126379
018: * RFE#5075809, CRT#99
019: *
020: * Revision 1.2 2002/08/21 10:38:36 ss133690
021: * Bug 4710658
022: *
023: * Revision 1.1 2002/06/14 09:53:57 rt130506
024: * SRAP rebranding
025: *
026: * Revision 1.2 2002/06/11 16:02:09 bv131302
027: * new branded
028: *
029: * Revision 1.1 2002/05/28 09:38:19 mm132998
030: * Bug id - 4692062 , CRT - 1215 , Desc - Support for iDSAME in https mode.
031: *
032: *
033: */
034: package com.sun.portal.rproxy.https;
035:
036: import java.io.DataInputStream;
037: import java.io.IOException;
038: import java.io.InputStream;
039: import java.io.OutputStream;
040: import java.io.UnsupportedEncodingException;
041: import java.net.ServerSocket;
042: import java.net.Socket;
043: import java.net.SocketException;
044: import java.util.HashMap;
045: import java.util.StringTokenizer;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048:
049: import com.sun.portal.log.common.PortalLogger;
050:
051: public class JSSProxyRunnable implements Runnable {
052: public static HashMap connectHashMap = new HashMap();
053:
054: // private static Logger logger =
055: // Logger.getLogger("com.sun.portal.sra.rproxy");
056: private static Logger logger = PortalLogger
057: .getLogger(JSSProxyRunnable.class);
058:
059: private class JSSProxySessionRunnable implements Runnable {
060: private Socket inconnection = null;
061:
062: private Socket toProxySocket = null;
063:
064: JSSProxySessionRunnable(Socket socket) {
065: inconnection = socket;
066: }
067:
068: public void run() {
069: Integer remotePort = new Integer(inconnection.getPort());
070: byte[] prebuffer = new byte[1];
071:
072: DataInputStream inFrom;
073: try {
074: inFrom = new DataInputStream(inconnection
075: .getInputStream());
076: inFrom.readFully(prebuffer, 0, 1);
077: } catch (IOException e) {
078: // logger.log(Level.SEVERE, "JSSProxySessionRunnable: Unable to
079: // open input stream on "+ inconnection, e);
080: Object[] params = { inconnection, e };
081: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRH011", params);
082:
083: connectHashMap.remove(remotePort);
084: closeSockets();
085: return;
086: }
087:
088: String info = (String) connectHashMap.remove(remotePort);
089:
090: StringTokenizer st = new StringTokenizer(info);
091:
092: String host = st.nextToken();
093: int port;
094: try {
095: port = Integer.parseInt(st.nextToken());
096: } catch (Exception ex) {
097: port = 8080;
098: }
099:
100: try {
101: toProxySocket = new Socket(host, port);
102: toProxySocket.setTcpNoDelay(true);
103: } catch (Exception ex) {
104: toProxySocket = null;
105: // logger.log(Level.SEVERE, "JSSProxySessionRunnable: " +
106: // "Unable to connect to " + host + ":" + port + ". ",ex);
107: Object[] params = { host, port + "", ex };
108: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRH012", params);
109: }
110:
111: if (toProxySocket == null) {
112: closeSockets();
113: return;
114: }
115:
116: String desthost = st.nextToken();
117: String destport = st.nextToken();
118:
119: OutputStream out = null;
120: InputStream in = null;
121: byte reply[] = new byte[200];
122: int replyLen = 0;
123: int newlinesSeen = 0;
124: boolean headerDone = false; // Done on first newline
125:
126: try {
127: out = toProxySocket.getOutputStream();
128: String msg = "CONNECT "
129: + desthost
130: + ":"
131: + destport
132: + " HTTP/1.0\n"
133: + "User-Agent: "
134: + sun.net.www.protocol.http.HttpURLConnection.userAgent
135: + "\r\n\r\n";
136:
137: // logger.info(msg);
138: Object[] params = { msg };
139: logger.log(Level.INFO, "PSSRRPROXY_CSPRH013", params);
140: byte b[];
141: try {
142: b = msg.getBytes("ASCII7");
143: } catch (UnsupportedEncodingException ignored) {
144: // If ASCII7 isn't there, something serious is wrong, but
145: // Paranoia Is Good (tm)
146: b = msg.getBytes();
147: }
148:
149: out.write(b);
150: out.flush();
151:
152: // We need to store the reply so we can create a detailed
153: // error message to the user.
154:
155: in = toProxySocket.getInputStream();
156:
157: while (newlinesSeen < 2) {
158: int i = in.read();
159: if (i < 0) {
160: // logger.info("JSSProxySessionRunnable: Unexpected EOF
161: // from proxy");
162: logger.info("PSSRRPROXY_CSPRH014");
163: closeSockets();
164: return;
165: }
166:
167: if (i == '\n') {
168: headerDone = true;
169: ++newlinesSeen;
170: } else if (i != '\r') {
171: newlinesSeen = 0;
172: if (!headerDone && replyLen < reply.length) {
173: reply[replyLen++] = (byte) i;
174: }
175: }
176: }
177: } catch (IOException ioe) {
178: // logger.log(Level.SEVERE, "JSSProxySessionRunnable: "+ "Unable
179: // to get OutputStream", ioe);
180: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRH015", ioe);
181: closeSockets();
182: return;
183: }
184:
185: // Converting the byte array to a string is slightly wasteful
186: // in the case where the connection was successful, but it's
187: // insignificant compared to the network overhead.
188: String replyStr;
189: try {
190: replyStr = new String(reply, 0, replyLen, "ASCII7");
191: } catch (UnsupportedEncodingException ignored) {
192: replyStr = new String(reply, 0, replyLen);
193: }
194:
195: // We asked for HTTP/1.0, so we should get that back
196: if (!replyStr.startsWith("HTTP/1.0 200")) {
197: // logger.info("JSSProxySessionRunnable: Unable to tunnel
198: // through ");
199: logger.info("PSSRRPROXY_CSPRH016");
200: closeSockets();
201: return;
202: }
203:
204: try {
205: out.write(prebuffer);
206: out.flush();
207: } catch (IOException ioe) {
208: // logger.log(Level.SEVERE, "JSSProxySessionRunnable: " +
209: // "Unable to write prebuffer.", ioe);
210: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRH017", ioe);
211: closeSockets();
212: return;
213: }
214:
215: new RWGroupJSSProxy(inconnection, toProxySocket);
216: }
217:
218: void closeSockets() {
219: if (inconnection != null) {
220: try {
221: inconnection.close();
222:
223: } catch (Exception e1) {
224: }
225: /**
226: * Bug 4710658
227: */
228: finally {
229: inconnection = null;
230: }
231: // End of code change for Bug 4710658
232: }
233: if (toProxySocket != null) {
234: try {
235: toProxySocket.close();
236: } catch (Exception e1) {
237: }
238: /**
239: * Bug 4710658
240: */
241: finally {
242: toProxySocket = null;
243: }
244: // End of code change for Bug 4710658
245: }
246: }
247: }
248:
249: private ServerSocket sconnection = null;
250:
251: JSSProxyRunnable(ServerSocket ss) {
252: sconnection = ss;
253: }
254:
255: public void run() {
256: boolean go = true;
257: Socket inconnection;
258:
259: while (go) {
260: try {
261: try {
262: inconnection = sconnection.accept();
263: } catch (IOException e) {
264: // logger.log( Level.SEVERE, "JSSProxyRunnable: Unable to
265: // accept new connection.", e);
266: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRH018", e);
267: /**
268: * Bug 4710658
269: */
270: if (sconnection != null) {
271: try {
272: sconnection.close();
273: } catch (IOException ee) {
274: // logger.log( Level.SEVERE,"JSSProxyRunnable:
275: // Unable to close server socket.", e);
276: logger.log(Level.SEVERE,
277: "PSSRRPROXY_CSPRH019", ee);
278: } finally {
279: sconnection = null;
280: }
281: }
282: // End of code change for Bug 4710658
283: go = false;
284: continue;
285: }
286: try {
287: inconnection.setTcpNoDelay(true);
288: } catch (SocketException e) {
289: // logger.log(Level.SEVERE, "JSSProxyRunnable: Unable to
290: // TcpNoDelay.", e);
291: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRH020", e);
292: /**
293: * Bug 4710658
294: */
295: if (inconnection != null) {
296:
297: try {
298: inconnection.close();
299: } catch (IOException ee) {
300: } finally {
301: inconnection = null;
302: }
303: }
304: // End of code change for the bug 4710658
305: continue;
306: }
307:
308: JSSProxyRunnable.JSSProxySessionRunnable p = new JSSProxySessionRunnable(
309: inconnection);
310: try {
311: JSSThreadPool.run(p);
312: } catch (InterruptedException e) {
313: // logger.log(Level.SEVERE, "JSSProxyRunnable: Unable to run
314: // new JSSProxySession", e);
315: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRH021", e);
316: /**
317: * Bug 4710658
318: */
319: if (inconnection != null) {
320: try {
321: inconnection.close();
322: // p = null;
323: } catch (IOException ee) {
324: } finally {
325: inconnection = null;
326: p = null;
327: }
328: }
329: // End of code change for the bug 4710658
330: }
331:
332: } catch (Throwable t) {
333: // logger.log(Level.SEVERE, "JSSProxyRunnable: Uncaught
334: // exception:", t);
335: logger.log(Level.SEVERE, "PSSRRPROXY_CSPRH022", t);
336: }
337: }
338: }
339: }
|