001: package com.sun.portal.proxylet.client.applet;
002:
003: import com.sun.portal.proxylet.client.common.server.Server;
004: import com.sun.portal.proxylet.client.applet.PluginProxyInfo;
005: import com.sun.portal.proxylet.client.common.Param;
006: import com.sun.portal.proxylet.client.common.server.ssl.SSLProviderFactory;
007: import com.sun.portal.proxylet.client.common.server.ssl.AbstractSSLProvider;
008: import com.sun.portal.proxylet.client.common.Param;
009: import com.sun.portal.proxylet.client.common.Log;
010: import netscape.javascript.JSObject;
011:
012: import java.applet.Applet;
013: import java.awt.Color;
014: import java.awt.Graphics;
015: import java.net.InetAddress;
016: import java.net.ServerSocket;
017: import java.util.Enumeration;
018: import java.io.IOException;
019:
020: import javax.swing.JApplet;
021: import javax.swing.JLabel;
022:
023: /**
024: * DownloadMgr
025: *
026: * This applet runs on a fixed port and downloads the actual proxylet applet
027: * and the signed script.
028: *
029: * This was done as a workaround to a bug in Mozilla.
030: *
031: * For more information on signed script, please refer
032: * http://www.mozilla.org/projects/security/components/signed-scripts.html
033: *
034: * According to Mozilla security policy, only a signed script has expanded privilges
035: * like modifying browser proxy settings etc. Mozilla recognizes the signed script
036: * if its placed inside a JAR and referred using a JAR URL
037: * ( for example, JAR:http://somesite/myjar.jar!/signed.html ). For some reason,
038: * mozilla is not able to resolve https URL ( for example, JAR:https does not work ).
039: *
040: * Since we cannot expect the gateway to run in a non-secure mode, we have a
041: * small applet 'DownloadMgr' that gets downloaded and starts listening on particular port
042: * ( non-secure mode ).
043: * Now, the signed script and the applet gets downloaded through this applet.
044: * The JAR URL in this case looks like JAR:http://localhost:port/portalserver/signedjar!/signed.html
045: */
046:
047: public class DownloadMgr extends JApplet {
048: private static DownloadMgr applet;
049: Server p;
050:
051: public void init() {
052: getContentPane().setBackground(Color.white);
053: String temp = this .getParameter("DEBUGLEVEL");
054: Log
055: .setloglevel_JavaConsole((temp != null && temp
056: .equals("1")) ? true : false);
057:
058: synchronized (getClass()) {
059: if (applet == null)
060: applet = this ;
061: }
062:
063: }
064:
065: public void start() {
066:
067: try {
068:
069: String version = System.getProperty("java.version");
070: if (version.charAt(2) < '3'
071: || (version.charAt(2) <= '3' && (version.length() == 3 || version
072: .charAt(4) < '1'))) {
073: call("loadJVM", 0);
074: return;
075: }
076:
077: Param.readParameters(this );
078: Param.setBindPort(Param.getDownloadMgrPort());
079: Param
080: .setBindIP(InetAddress.getLocalHost()
081: .getHostAddress());
082:
083: Param.setSource("DownloadMgr");
084:
085: boolean openDownloadMgr = true;
086:
087: // If it's not direct connection then popup a dialog box for proxy setting values
088: if (!isDirectConnection()) {
089: PluginProxyInfo ppi = new PluginProxyInfo();
090: openDownloadMgr = ppi.isActionOk();
091: if (!openDownloadMgr) {
092: this
093: .getContentPane()
094: .add(
095: new JLabel(
096: Param
097: .getString(
098: "pinfo.26",
099: "Please enter valid proxy host and port values")));
100: }
101: }
102:
103: // If user clicks on Cancel button of proxy settings then don't create
104: // connection, which is anyway will fail, so postpone it to users next try
105: if (openDownloadMgr) {
106: p = new Server(Param.getBindPort());
107: //int localport = p.getLocalPort();
108: Thread t = new Thread(p);
109: t.start();
110: call("loadsignedscript", 0);
111: call("loadsignedapplet", 0);
112: }
113: } catch (Exception e) {
114: e.printStackTrace();
115: System.out.println(e.getMessage());
116: return;
117: }
118:
119: }
120:
121: public void stop() {
122: try {
123: super .stop();
124: } catch (Exception e) {
125: e.printStackTrace();
126: }
127: }
128:
129: public void addItem(String newWord) {
130: repaint();
131: }
132:
133: public void paint(Graphics g) {
134: super .paint(g);
135: }
136:
137: /**
138: * callJSFinish
139: * This function invokes a JS function in the downloadmgr page, that
140: * completes the downloadmgr loading.
141: */
142: private void call(String funcName, int localport) {
143: JSObject win;
144:
145: String hostname = null;
146: try {
147: hostname = InetAddress.getLocalHost().getHostAddress();
148: } catch (Exception ignore) {
149: }
150:
151: Object[] arg = { hostname, Integer.toString(localport) };
152:
153: win = getWindow();
154: win.call(funcName, arg);
155:
156: }
157:
158: public JSObject getWindow() {
159: JSObject win = null;
160: String vmversion = System.getProperty("java.version");
161:
162: // get the applets represented by this applet context
163: Enumeration applets = getAppletContext().getApplets();
164:
165: Applet _applet = null;
166: while (applets.hasMoreElements()) {
167: _applet = (Applet) applets.nextElement();
168: }
169:
170: //
171: // This was required in Netscape for Java 1.3.0
172: //
173: if (vmversion.indexOf("1.3.0") != -1) {
174: System.out.println("calling jsobject for 1.3.0 versions");
175: win = JSObject.getWindow(getAppletContext().getApplet(
176: "downapp"));
177: } else
178: win = JSObject.getWindow(this );
179:
180: return win;
181: }
182:
183: private static int getfreeport() {
184: ServerSocket s = null;
185: int port = 0;
186: try {
187: s = new ServerSocket(0);
188: port = s.getLocalPort();
189:
190: } catch (IOException e) {
191: getfreeport();
192: } finally {
193: if (s != null) {
194: try {
195: s.close();
196: } catch (IOException e) {
197: getfreeport();
198: }
199: }
200: }
201:
202: System.out.println("getfreeport " + port);
203: return port;
204: }
205:
206: /**
207: * This function just checks whether browser proxy settings has direction connection
208: * or not by just creating dummy connection.
209: * @return
210: */
211: private boolean isDirectConnection() {
212: try {
213: AbstractSSLProvider gwConnection = SSLProviderFactory
214: .createFactory(Param.getSSLProvider());
215: gwConnection.connect(Param.getGatewayHost(), Param
216: .getGatewayPort(), null);
217: gwConnection.close();
218: } catch (Exception e) {
219: return false;
220: }
221: return true;
222: }
223: }
|