001: // %Z%%M% %I% "%W% %E% Sun Microsystems"
002: package com.sun.portal.netlet.client.applet;
003:
004: import com.sun.portal.netlet.client.common.*;
005: import netscape.javascript.JSObject;
006: import java.applet.Applet;
007: import java.net.URLEncoder;
008: import java.util.Hashtable;
009: import java.awt.*;
010: import java.awt.event.ActionListener;
011: import java.awt.event.ActionEvent;
012:
013: public class NetletApplet extends Applet implements ActionListener {
014:
015: private static NetletApplet instance = null;
016: private SClientMgr scm = null;
017: private PortInfoDialog pid = null;
018: private boolean netletRunning = false;
019: private boolean netscapePrivilegesGranted = true;
020: private NetletErrorDialog cbiped = null;
021:
022: public void init() {
023: BrowserType browser;
024: synchronized (getClass()) {
025: if (instance != null) {
026: System.out.println("Netlet is already running.");
027: return;
028: }
029: instance = this ;
030: browser = BrowserType.getInstance();
031: }
032: if (browser.getNetscapeBrowser()
033: && browser.getJavaVMVersion() == 1) {
034: try {
035: BrowserType.SecurityCall sc = browser
036: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_CONNECT);
037: sc.getMethod().invoke(null, sc.getArgs());
038: sc = browser
039: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_LISTEN);
040: sc.getMethod().invoke(null, sc.getArgs());
041: sc = browser
042: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_ACCEPT);
043: sc.getMethod().invoke(null, sc.getArgs());
044: } catch (Exception ex) {
045: netscapePrivilegesGranted = false;
046: System.out.println("NetletApplet Exception " + ex);
047: makeInfoframe("nc5");
048: return;
049: }
050: }
051: cbiped = new NetletErrorDialog(new Frame(), this ,
052: ResourceProperties.getString("cbiped.1"),
053: ResourceProperties.getString("cbiped.2"));
054: }
055:
056: public void start() {
057: if (!netscapePrivilegesGranted) {
058: return;
059: }
060: if (netletRunning) {
061: resetSessionId();
062: return;
063: }
064: loadAll();
065:
066: if (scm == null) {
067: scm = new SClientMgr(ClientConfig.getNumOfRules());
068: }
069:
070: loadRules();
071:
072: String cbip = ClientConfig.getClientBindIP();
073: if (!ClientUtil.isClientBindIPValid(cbip)) {
074: // pop up error dialog
075: cbiped.setErrorMessage(ResourceProperties
076: .getString("cbiped.3")
077: + " "
078: + cbip
079: + ". "
080: + ResourceProperties.getString("cbiped.4"));
081: cbiped.setVisible(true);
082: cbiped.toFront();
083: cbiped.waitForAction();
084: return;
085: }
086:
087: if (scm != null) {
088: scm.start();
089: /**
090: * wait until all the SClient threads start, and send the clientport
091: * information to the servlet.
092: */
093: scm.waitToStart();
094: String clientPorts = scm.getFormattedClientPorts();
095: netletRunning = true;
096: ClientUtil.sendByPost("setLoaded", "clientPorts="
097: + URLEncoder.encode(clientPorts));
098: makeInfoframe("nc1");
099: } else {
100: makeInfoframe("nc5");
101: }
102: }
103:
104: private void loadAll() {
105: loadParameters();
106: ClientConfig.init();
107: ResourceProperties.loadResources();
108: loadProxyDetails();
109: }
110:
111: private void loadParameters() {
112: ClientConfig.addParam("encryptionType", this
113: .getParameter("encryptionType"));
114: ClientConfig.addParam("isJSSEEnabled", this
115: .getParameter("isJSSEEnabled"));
116: ClientConfig.addParam("isPDCEnabled", this
117: .getParameter("isPDCEnabled"));
118: ClientConfig
119: .addParam("numParms", this .getParameter("numParms"));
120: ClientConfig.addParam("doPortWarning", this
121: .getParameter("doPortWarning"));
122: ClientConfig.addParam("showPortWarnCheckbox", this
123: .getParameter("showPortWarnCheckbox"));
124: ClientConfig
125: .addParam("doReauth", this .getParameter("doReauth"));
126: ClientConfig.addParam("clientBindIP", this
127: .getParameter("clientBindIP"));
128: ClientConfig.addParam("sessionId", this
129: .getParameter("sessionId"));
130: String confURL = this .getParameter("configURL");
131: ClientConfig.addParam("jsDocumentDomain", this
132: .getParameter("document.domain"));
133: ClientConfig.addParam("configURL", confURL);
134: ClientConfig.addParam("gwURL", this .getParameter("gwURL"));
135: ClientConfig.addParam("cookiename", this
136: .getParameter("cookiename"));
137: ClientConfig.addParam("proxyoverride", this
138: .getParameter("proxyoverride"));
139: ClientConfig.addParam("proxytype", this
140: .getParameter("proxytype"));
141: ClientConfig.addParam("proxyhost", this
142: .getParameter("proxyhost"));
143: ClientConfig.addParam("proxyport", this
144: .getParameter("proxyport"));
145:
146: // code base url for proxylet mode.
147: ClientConfig.addParam("codebaseURL", this .getCodeBase()
148: .toString());
149:
150: if (confURL != null && confURL.startsWith("/")) {
151: ClientConfig.addParam("proxyletMode", "true");
152: }
153: //System.out.println(">>> configURL" + this.getParameter("configURL"));
154: }
155:
156: private void resetSessionId() {
157: ClientConfig.setSessionID(this .getParameter("sessionId"));
158: }
159:
160: private void loadProxyDetails() {
161: ProxyInfo appletPI = new AppletProxyInfo();
162: ClientConfig.loadProxyDetails(appletPI);
163: }
164:
165: private void loadRules() {
166: int srcPort = 0;
167: String ruleName;
168: String serverHost;
169: String serverPort;
170: String threadname;
171: String cipherName;
172:
173: int nor = ClientConfig.getNumOfRules();
174:
175: for (int i = 0; i < nor; i++) {
176: // read in the parms...
177: try {
178: srcPort = Integer.parseInt(this
179: .getParameter("listenPort_" + i));
180: } catch (NumberFormatException n) {
181: System.out
182: .println("Netlet unable to parse source port: "
183: + i);
184: }
185:
186: ruleName = this .getParameter("ruleName_" + i);
187: serverHost = this .getParameter("serverHost_" + i);
188: serverPort = this .getParameter("serverPort_" + i);
189:
190: System.out.println("Netlet rule " + i + ": local:"
191: + srcPort + " destination:" + serverHost + ":"
192: + serverPort);
193:
194: // create "unique" thread name...
195: threadname = "atprox_" + i + "_"
196: + ClientConfig.getDestPort() + "_"
197: + ClientConfig.getDestHost();
198:
199: cipherName = this .getParameter("cipher_" + i);
200:
201: scm.addconfig(ruleName, srcPort, serverPort, serverHost,
202: threadname, cipherName);
203: }
204: }
205:
206: // handle differently for Netscape vs. IE. In Netscape, close down
207: // everthing, and rebuild on start(). With IE (on Win95), close
208: // down existing connections, but leave applet running in accept()
209: // mode; only reread sessionId in that case.
210: public void stopProcessing() {
211: scm.stopProcessing();
212: if (scm != null) {
213: scm.stop();
214: scm = null;
215: }
216: netletRunning = false;
217: }
218:
219: public void stop() {
220: // any clean to do?
221: if (netletRunning) {
222: System.out.println("Netlet Stopping");
223: stopProcessing();
224: }
225: }
226:
227: public void destroy() {
228: // any clean to do?
229: ClientUtil.netletConfig("unload");
230: instance = null;
231: }
232:
233: public static NetletApplet getInstance() {
234: return instance;
235: }
236:
237: /*
238: * Added for bug 4644648
239: * Changes the message in the Netlet info frame
240: * based on the key passed by making appropriate
241: * call to NetletConfig servlet.
242: */
243:
244: public void makeInfoframe(String stat) {
245: try {
246: JSObject jswin = JSObject.getWindow(this );
247: JSObject parent = (JSObject) jswin.getMember("parent");
248: JSObject frames = (JSObject) parent.getMember("frames");
249: JSObject infoFrame = (JSObject) frames.getMember("info");
250:
251: String jsDocumentDomain = ClientConfig
252: .getParam("jsDocumentDomain");
253: String cmd = null;
254: if (jsDocumentDomain != null) {
255: cmd = "parent.frames[0].location = \""
256: + ClientConfig.getConfigURL()
257: + "?func=makeInfoFrame&stat=" + stat
258: + "&document.domain=" + jsDocumentDomain + "\"";
259: } else {
260: cmd = "parent.frames[0].location = \""
261: + ClientConfig.getConfigURL()
262: + "?func=makeInfoFrame&stat=" + stat + "\"";
263: }
264:
265: infoFrame.eval(cmd);
266:
267: } catch (Exception ex) {
268: System.out
269: .println("Netlet unable connect back to server : status update failed.");
270: }
271: }
272:
273: public void showClientPorts() {
274: System.out.println("showclientports() called...");
275: if (scm != null) {
276: if (pid == null || !pid.isShowing()) {
277: System.out.println("creating portinfodialog...");
278: Hashtable clientPorts = scm.getClientPorts();
279: pid = new PortInfoDialog(new Frame(), clientPorts);
280: }
281: pid.show();
282: pid.toFront();
283: }
284: System.out.println("showclientports() done...");
285: }
286:
287: public void actionPerformed(ActionEvent evt) {
288: if ("OK".equals(evt.getActionCommand())) {
289: Object obj = evt.getSource();
290: if (obj == cbiped.okButton) {
291: cbiped.notifyAction();
292: cbiped.setVisible(false);
293: }
294: }
295: }
296: }
|