001: // @(#)BrowserProxyInfo.java 1.5 "@(#)BrowserProxyInfo.java 1.5 01/07/16 Sun Microsystems"
002:
003: package com.sun.portal.netlet.client.applet;
004:
005: import netscape.javascript.JSObject;
006: import java.util.Enumeration;
007: import java.util.StringTokenizer;
008: import java.util.Vector;
009:
010: import com.sun.portal.netlet.client.common.ProxyInfo;
011: import com.sun.portal.netlet.client.common.BrowserType;
012: import com.sun.portal.netlet.client.common.ClientUtil;
013: import com.sun.portal.netlet.client.common.ResourceProperties;
014:
015: public class BrowserProxyInfo {
016:
017: JSObject window;
018: String js_proxy_ssl;
019: String proxy_ssl;
020: String js_proxy_ssl_port;
021: int proxy_ssl_port;
022: String js_proxy_type;
023: int proxy_type;
024: String js_localhost;
025: String localhost;
026: String js_set_localhost;
027:
028: String js_addlocal;
029: String addlocal;
030:
031: Vector noProxiesFor = new Vector();
032:
033: private boolean privilegesGranted = false;
034:
035: public BrowserProxyInfo(ProxyInfo pi) {
036: //System.out.println("Netlet reading browser proxy configuration");
037: BrowserType browser = BrowserType.getInstance();
038:
039: if (browser.getNetscapeBrowser()
040: && browser.getJavaVMVersion() == 1) {
041: try {
042: BrowserType.SecurityCall sc = browser
043: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_PREFERENCES_READ);
044: sc.getMethod().invoke(null, sc.getArgs());
045: sc = browser
046: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_PREFERENCES_WRITE);
047: sc.getMethod().invoke(null, sc.getArgs());
048: } catch (Exception e) {
049: privilegesGranted = false;
050: return;
051: }
052: privilegesGranted = true;
053: }
054:
055: window = JSObject.getWindow(NetletApplet.getInstance());
056: js_proxy_ssl = new String(
057: "netscape.security.PrivilegeManager.enablePrivilege(\"UniversalPreferencesRead\");navigator.preference(\"network.proxy.ssl\");");
058:
059: js_proxy_ssl_port = new String(
060: "netscape.security.PrivilegeManager.enablePrivilege(\"UniversalPreferencesRead\");navigator.preference(\"network.proxy.ssl_port\");");
061:
062: js_proxy_type = new String(
063: "netscape.security.PrivilegeManager.enablePrivilege(\"UniversalPreferencesRead\");navigator.preference(\"network.proxy.type\");");
064:
065: proxy_ssl = (String) window.eval(js_proxy_ssl);
066: proxy_ssl_port = ((Double) window.eval(js_proxy_ssl_port))
067: .intValue();
068: proxy_type = ((Double) window.eval(js_proxy_type)).intValue();
069:
070: if (proxy_type == 2) {
071: //automatic proxy
072: String autoProxyUrl = new String(
073: "netscape.security.PrivilegeManager.enablePrivilege(\"UniversalPreferencesRead\");navigator.preference(\"network.proxy.autoconfig_url\");");
074: String pacUrl = window.eval(autoProxyUrl).toString();
075:
076: // As the applet can not connect to any server it is not downloaded from, get the Privilage to access the Proxy server to get the PAC file
077: try {
078: BrowserType.SecurityCall sc = browser
079: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_CONNECT);
080: sc.getMethod().invoke(null, sc.getArgs());
081: sc = browser
082: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_LISTEN);
083: sc.getMethod().invoke(null, sc.getArgs());
084: sc = browser
085: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_ACCEPT);
086: sc.getMethod().invoke(null, sc.getArgs());
087: sc = browser
088: .enablePermission(BrowserType.NETSCAPE_UNIVERSAL_FILE_ACCESS);
089: sc.getMethod().invoke(null, sc.getArgs());
090: } catch (Exception e) {
091: System.out
092: .println("SClient exception (start) (no privilege): "
093: + e);
094: }
095:
096: String inputLine = ClientUtil.parsePACFile(pacUrl);
097:
098: if (inputLine.equalsIgnoreCase("DIRECT")) {
099: //no proxy required
100: proxy_type = 3;
101: } else {
102: if (inputLine.startsWith("PROXY")) {
103: /*
104: * @ modified by bshankar@sun.com for Bug - 4625682.
105: * Instead of taking the first proxy, use the first valid proxy
106: */
107: boolean isValid = false;
108: StringTokenizer tok = new StringTokenizer(
109: inputLine, ";");
110: while (tok.hasMoreTokens() && !isValid) {
111: String str = tok.nextToken().trim();
112: String proxyString = (str.indexOf("PROXY") == 0) ? str
113: .substring("PROXY".length()).trim()
114: : str;
115: int index = proxyString.indexOf(":");
116: String proxyHost = null;
117: String proxyPort = null;
118: if (index == -1) {
119: proxyHost = proxyString;
120: proxyPort = new String("80");
121: } else {
122: proxyHost = proxyString.substring(0, index);
123: proxyPort = proxyString
124: .substring(index + 1);
125: }
126: proxy_ssl = proxyHost.trim();
127: proxy_ssl_port = Integer.parseInt(proxyPort);
128: isValid = ClientUtil.isProxySettingsValid(
129: proxy_ssl, proxy_ssl_port);
130: //isValid = isProxySettingsValid(proxy_ssl, proxy_ssl_port);
131: }
132: if (!isValid) {
133: pi.getProxyWarning()
134: .setMessage(
135: ResourceProperties
136: .getString("pwarn.4"));
137: proxy_ssl = "";
138: proxy_ssl_port = 0;
139: }
140: }
141: }
142: //else it would have failed, donot do anything so that it will show the dialog
143: }
144:
145: findLocalhostNoProxy();
146: }
147:
148: public void setLocalhostNoProxy() {
149: js_set_localhost = new String(
150: "netscape.security.PrivilegeManager.enablePrivilege(\"UniversalPreferencesRead\");netscape.security.PrivilegeManager.enablePrivilege(\"UniversalPreferencesWrite\"); var addlocal = navigator.preference(\"network.proxy.no_proxies_on\"); myRe=/localhost/;if (!myRe.test(addlocal)) { navigator.preference(\"network.proxy.no_proxies_on\", addlocal + \", localhost\");}");
151:
152: //System.out.println("js_set_localhost = " + js_set_localhost);
153:
154: js_localhost = new String(
155: "netscape.security.PrivilegeManager.enablePrivilege(\"UniversalPreferencesRead\");navigator.preference(\"network.proxy.no_proxies_on\");");
156:
157: //localhost = (String)window.eval(js_set_localhost);
158: window.eval(js_set_localhost);
159: //System.out.println("Netlet set localhost as no-proxy");
160:
161: localhost = (String) window.eval(js_localhost);
162: System.out.println("Netlet no-proxy hosts:" + localhost);
163: }
164:
165: public String getProxySSL() {
166: return (proxy_ssl);
167: }
168:
169: public int getProxySSLPort() {
170: return (proxy_ssl_port);
171: }
172:
173: public int getProxyType() {
174: return (proxy_type);
175: }
176:
177: public String getLocalhostNoProxy() {
178: return (addlocal);
179: }
180:
181: public boolean inLocalhostNoProxy(String host) {
182: for (Enumeration e = noProxiesFor.elements(); e
183: .hasMoreElements();) {
184: String s = (String) e.nextElement();
185: if (host.toLowerCase().endsWith(s.toLowerCase())) {
186: return (true);
187: }
188: }
189: return (false);
190: }
191:
192: private void findLocalhostNoProxy() {
193: String js_addlocal;
194: String addlocal;
195:
196: js_addlocal = new String(
197: "netscape.security.PrivilegeManager.enablePrivilege(\"UniversalPreferencesRead\");navigator.preference(\"network.proxy.no_proxies_on\");");
198:
199: addlocal = (String) window.eval(js_addlocal);
200:
201: //System.out.println("Netlet got no-proxy settings from browser:" + addlocal);
202:
203: StringTokenizer nst = new StringTokenizer(addlocal, ",");
204: while (nst.hasMoreTokens()) {
205: String s = nst.nextToken().toLowerCase().trim();
206: if (!s.equals("")) {
207: noProxiesFor.addElement(s);
208: //System.out.println("no proxy for putting " + s);
209: }
210: }
211: }
212:
213: public boolean getPrivilegesGranted() {
214: return privilegesGranted;
215: }
216: }
|