001: // @(#)RWGroupForward.java 1.1 "@(#)RWGroupForward.java 1.1 99/09/25 Sun Microsystems"
002:
003: /*
004: * Forward's netlet traffic from Gateway to NetletProxy in case Netlet proxy is
005: * enabled.
006: *
007: * @ modified Rajesh T
008: */
009:
010: package com.sun.portal.netlet.eproxy;
011:
012: import java.io.DataOutputStream;
013: import java.io.IOException;
014: import java.net.Socket;
015: import java.util.logging.Level;
016: import java.util.logging.Logger;
017:
018: import com.sun.portal.log.common.PortalLogger;
019: import com.sun.portal.netlet.econnection.ESessionMsg;
020: import com.sun.portal.netlet.econnection.KeyConstants;
021: import com.sun.portal.netlet.econnection.ReaderWriter;
022: import com.sun.portal.netlet.econnection.ReaderWriterClear;
023: import com.sun.portal.netlet.econnection.ReaderWriterLock;
024: import com.sun.portal.netlet.econnection.SessionCipherMsg;
025: import com.sun.portal.netlet.econnection.SessionKey;
026: import com.sun.portal.util.GWLocale;
027: import com.sun.portal.util.GWThreadPool;
028: import com.sun.portal.util.NetletLogMgr;
029: import com.sun.portal.util.SRAEvent;
030: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
031:
032: public class RWGroupForward extends ReaderWriterLock implements
033: KeyConstants {
034: public boolean done = false;
035:
036: private int npPort;
037:
038: private String npHost;
039:
040: private Socket fromClient;
041:
042: private Socket toNP;
043:
044: private ReaderWriterClear src_to_dst;
045:
046: private ReaderWriterClear dst_to_src;
047:
048: private boolean src_to_dst_clean = false;
049:
050: private boolean dst_to_src_clean = false;
051:
052: private Integer netletLogId; // For Netlet PRD 5.1
053:
054: private String dynamicAlgo = KeyConstants.DEFAULT_CIPHER;
055:
056: private long startTime;
057:
058: private boolean stopLogged = false;
059:
060: private String sessID = null;
061:
062: private static Logger logger = PortalLogger
063: .getLogger(RWGroupForward.class);
064:
065: public RWGroupForward(Socket in, String nphost, int npport,
066: /* int srcport, int destport, String desthost, */
067: String sessionId) {
068: fromClient = in;
069: npPort = npport;
070: npHost = nphost;
071: sessID = sessionId;
072:
073: /*
074: * Tunneling netlet traffic via proxy
075: */
076: NetletRequestRouter netletrouter = new NetletRequestRouter(in
077: .getPort());
078: toNP = netletrouter.getConnection(npHost, npPort);
079:
080: // do the connect in here to see if we can "save" the connect obj
081: // open connection to remote for this client
082: // toNP = SConn.sslconnect(npPort,npHost);
083:
084: if (toNP == null) {
085: // logger.severe("RWGroupForward: unable to make server
086: // connection!");
087: logger.severe("PSSRNTLT_CSPNEPROX081");
088: cleanup();
089: return;
090: }
091: // logger.info("RWGroupForward: made the connection request to
092: // NetletProxy");
093: logger.info("PSSRNTLT_CSPNEPROX082");
094:
095: // okay, special case. need to write previously ingested bytes!
096: try {
097: DataOutputStream out = new DataOutputStream(toNP
098: .getOutputStream());
099:
100: ESessionMsg eMsg = new ESessionMsg(("RP1.0").getBytes());
101: if (eMsg.writeMsg(out) != 0) {
102: cleanup();
103: return;
104: }
105:
106: if (encryptSessionId(sessionId.getBytes(), sessionId
107: .getBytes().length, out) != 0) {
108: cleanup();
109: return;
110: }
111: out.flush();
112: out = null;
113: } catch (IOException e) {
114: // logger.log(Level.SEVERE, "Exception in sending header to Netlet
115: // proxy...", e);
116: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX083");
117: }
118:
119: // Added for Netlet PRD 5.1 for Lihue
120: if (NetletLogMgr.loggingEnabled) {
121: try {
122: netletLogId = new Integer(NetletLogMgr.getLogId());
123: NetletLogMgr.write("Netlet", GWLocale.getPFString(
124: "en1", new Object[] {
125: netletLogId,
126: new String(NetletLogMgr
127: .getUserId(sessID)), "START",
128: fromClient.getInetAddress().toString(),
129: new Integer(fromClient.getPort()),
130: toNP.getInetAddress().toString(),
131: new Integer(toNP.getPort()) }));
132: } catch (Exception e) {
133: } // May cause null pointer exception
134: }
135:
136: try {
137: src_to_dst = new ReaderWriterClear(this , fromClient
138: .getInputStream(), toNP.getOutputStream());
139: dst_to_src = new ReaderWriterClear(this , toNP
140: .getInputStream(), fromClient.getOutputStream());
141: } catch (Exception ex) {
142: // logger.log(Level.SEVERE, "Unable to create ReaderWriter
143: // threads.", ex);
144: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX084");
145: }
146:
147: try {
148: GWThreadPool.run(src_to_dst);
149: GWThreadPool.run(dst_to_src);
150: } catch (InterruptedException e) {
151: // logger.log(Level.SEVERE, "RWGroupForward: Could not start
152: // ReaderWriterClear tasks -> ", e);
153: logger.log(Level.SEVERE, "PSSRNTLT_CSPNEPROX085");
154: }
155: startTime = System.currentTimeMillis();
156: }
157:
158: public synchronized void notifyFinished(ReaderWriter obj) {
159: if (!stopLogged && NetletLogMgr.loggingEnabled) {
160: try {
161: netletLogId = new Integer(NetletLogMgr.getLogId());
162: NetletLogMgr.write("Netlet", GWLocale.getPFString(
163: "en1", new Object[] {
164: netletLogId,
165: new String(NetletLogMgr
166: .getUserId(sessID)),
167: new String("STOP"),
168: fromClient.getInetAddress().toString(),
169: new Integer(fromClient.getPort()),
170: toNP.getInetAddress().toString(),
171: new Integer(toNP.getPort()) }));
172: stopLogged = true;
173: } catch (Exception e) {
174: } // May cause Null pointer exception
175: }
176: if (obj == src_to_dst) {
177: if (dst_to_src.isAlive()) {
178: dst_to_src.stop();
179: }
180: } else if (obj == dst_to_src) {
181: if (src_to_dst.isAlive()) {
182: src_to_dst.stop();
183: }
184: }
185: cleanup();
186: if (obj == src_to_dst) {
187: src_to_dst_clean = true;
188: } else if (obj == dst_to_src) {
189: dst_to_src_clean = true;
190: }
191: }
192:
193: public void cleanup() {
194: try {
195: if (fromClient != null) {
196: try {
197: fromClient.close();
198: MonitoringSubsystem
199: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
200: } catch (Exception e) {
201: } finally {
202: fromClient = null;
203: }
204: }
205: if (toNP != null) {
206: try {
207: toNP.close();
208: MonitoringSubsystem
209: .handleEvent(SRAEvent.PLAIN_SOCKET_DESTROYED);
210: } catch (Exception e) {
211: } finally {
212: toNP = null;
213: }
214: }
215: } finally {
216: done = true;
217: npHost = null;
218: }
219: }
220:
221: public boolean isDone() {
222: if (dst_to_src_clean && src_to_dst_clean) {
223: dst_to_src = null;
224: src_to_dst = null;
225: }
226: return (dst_to_src_clean && src_to_dst_clean);
227: }
228:
229: /*
230: * Forwards netlet traffic from gateway to netlet proxy. Encrypts the
231: * sessionid with system cipher along with the default Key and cipher. Pls
232: * note that it is assumed that the default key is public and freely
233: * available to everyone.
234: *
235: * It also appends the dynamic algorithm to this header which helps the
236: * netlet proxy to identify the user/admin preferred algorithm for that
237: * netlet rule.
238: */
239:
240: private int encryptSessionId(byte[] id, int id_len,
241: DataOutputStream out) {
242: int rc = 0;
243: // Pluggable Cipher
244: SessionKey initKey = new SessionKey(INIT_SESSION_KEY.getBytes());
245: // Pluggable Cipher
246: // Sending the Dynamic Algorithm choosen by the user as a part of the
247: // header.
248: // Passing the required info to Netlet Proxy.
249: id = new StringBuffer(new String(id)).append("|").append(
250: dynamicAlgo).toString().getBytes();
251: id_len += dynamicAlgo.length() + 1;
252: SessionCipherMsg sMsg = new SessionCipherMsg(id, id_len);
253: if (sMsg.writeMsg(out) != 0) {
254: // logger.severe("RWGroupForward: unable to process session id");
255: logger.severe("PSSRNTLT_CSPNEPROX086");
256: rc = -1;
257: }
258: return (rc);
259: }
260:
261: /*
262: * Added by Rajesh T for RFE 4492648. @ returns the latest of the two access
263: * time of the 2 ReaderWriter's associated in this Group in milliseconds
264: *
265: * @see com.sun.portal.netlet.eproxy.NetletGroup.
266: */
267:
268: public long getLastActivityTime() {
269:
270: if (src_to_dst.getLastActivityTime() > dst_to_src
271: .getLastActivityTime())
272: return src_to_dst.getLastActivityTime();
273: else
274: return dst_to_src.getLastActivityTime();
275:
276: }
277:
278: public synchronized void stopAll() {
279: if (!done) {
280: if (dst_to_src != null) {
281: dst_to_src.stop();
282: }
283: if (src_to_dst != null) {
284: src_to_dst.stop();
285: }
286: cleanup();
287: }
288: }
289:
290: /*
291: * Added for Netlet PRD 1.8.1 - Has no significance in EProxy since
292: * NetletProxy is enabled
293: */
294:
295: public int getAppletSrcPort() {
296: return 0;
297: }
298:
299: /*
300: * @ returns the start time when this reader writer group is created used to
301: * reset the session timer based on netlet activity
302: *
303: * @see com.sun.portal.netlet.eproxy.NetletGroup.
304: */
305:
306: public long getStartTime() {
307: return startTime;
308: }
309:
310: }
|