001: /**
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: *
013: * Functionality :
014: * Reads the Proxy information from Java Plugin.
015: * Works for Java plugin 1.3.1_02 and 1.4 versions.
016: * Singleton class, multithreding is not taken care of.
017: *
018: * @ created 04/02/2002
019: * @ Author Bhavanishankar
020: */package com.sun.portal.netlet.client.applet;
021:
022: import com.sun.portal.netlet.client.common.*;
023: import javax.swing.*;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.awt.event.ItemEvent;
027: import java.awt.event.ItemListener;
028: import java.io.InputStream;
029: import java.net.Socket;
030: import java.util.Enumeration;
031: import java.util.StringTokenizer;
032: import java.util.Vector;
033: import java.lang.reflect.Method;
034:
035: public class PluginProxyInfo implements ActionListener, ItemListener {
036:
037: // Plugin versions
038: public static final int UNSUPPORTED = 0; // not supported by netlet
039: public static final int VER2 = 2; // 1.2
040: public static final int VER3 = 3; // 1.3
041: public static final int VER4 = 4; // 1.3
042:
043: private int pluginVersion = UNSUPPORTED;
044: private int proxyType = ClientConfig.DIRECT; // Direct, Manual, or Automatic
045:
046: private String proxySSL = ""; // SSL proxy host
047: private int proxySSLPort = 0; // SSL proxy port
048: private Vector noProxiesFor = new Vector(); // Bypass proxy list
049:
050: private static ProxyInfoDialog pid = null;
051: private static ProxyInfoHelpDialog pihd = null;
052:
053: /*
054: * Default constructor - reads the proxy information ( Proxy mode,
055: * SSL proxy host, SSL proxy port, Proxy Bypass list).
056: */
057: public PluginProxyInfo() {
058: // Read the VM version from system property java.version
059: readVMVersion();
060:
061: // Read the proxy type form system property javaplugin.proxy.config.type
062: readProxyType();
063:
064: if (ClientConfig.isProxyletMode()) {
065: loadProxyFromProxylet();
066: }
067: /*
068: * Proxy information stored in the plugin are invalid if the type is BROWSER.
069: * (This happens only in Netscape 6.2). Use the Netlet Profile.
070: */
071: else if (proxyType == ClientConfig.BROWSER) {
072: useNetletProfile();
073: }
074: /*
075: * If the browser is using Automatic proxy configuration, parse the PAC file
076: * and get the proxy Host and Port. If the PAC file URL is invalid or has
077: * invalid proxy settings, then use Netlet Profile.
078: */
079: else if (proxyType == ClientConfig.AUTO) {
080: String autoConfURL = System
081: .getProperty("javaplugin.proxy.auto.url");
082: String useBS = System
083: .getProperty("javaplugin.proxy.usebrowsersettings");
084: if (useBS != null
085: && useBS.equalsIgnoreCase("true")
086: && (autoConfURL == null || autoConfURL.trim()
087: .length() <= 0)) {
088: // Works only for 1.4.x plugins
089: // Using plugin's private APIs
090: loadProxyUsingReflection();
091: } else {
092: parsePACFileURL(autoConfURL);
093: }
094: if (!isProxySettingsValid()) {
095: useNetletProfile();
096: }
097: }
098: /*
099: * Plugin has the the correct proxy settings, use it.
100: */
101: else if (proxyType == ClientConfig.MANUAL) {
102: extractSSLProxyHostAndPort(System
103: .getProperty("javaplugin.proxy.config.list"));
104: parseProxyOverrideList(System
105: .getProperty("javaplugin.proxy.config.bypass"));
106: System.out.println("Proxy override - "
107: + noProxiesFor.toString());
108: }
109:
110: if (inNoProxiesFor(ClientConfig.getDestHost())) {
111: proxyType = ClientConfig.DIRECT;
112: System.out.println("Host : " + ClientConfig.getDestHost()
113: + " in noProxyList, setting proxyType to DIRECT");
114: }
115: }
116:
117: /*
118: * Reads the plugin version and stores in - pluginVersion.
119: */
120: private void readVMVersion() {
121: String version = System.getProperty("java.version");
122: System.out.println("Java version : " + version);
123: if (version == null || version.trim().length() == 0) {
124: pluginVersion = UNSUPPORTED;
125: } else if (version.startsWith("1.4")) {
126: pluginVersion = VER4;
127: } else if (version.startsWith("1.3")) {
128: pluginVersion = VER3;
129: } else if (version.startsWith("1.2")) {
130: pluginVersion = VER2;
131: } else {
132: pluginVersion = UNSUPPORTED;
133: }
134: }
135:
136: /*
137: * Reads the "proxyType" from plugin and stores in member variable.
138: */
139: private void readProxyType() {
140: String temp = System
141: .getProperty("javaplugin.proxy.config.type");
142: System.out.println("Proxy type : " + temp);
143: if (temp.equalsIgnoreCase("direct")) {
144: proxyType = ClientConfig.DIRECT;
145: } else if (temp.equalsIgnoreCase("manual")) {
146: proxyType = ClientConfig.MANUAL;
147: } else if (temp.equalsIgnoreCase("browser")) {
148: proxyType = ClientConfig.BROWSER;
149: } else {
150: proxyType = ClientConfig.AUTO;
151: }
152: }
153:
154: private void loadProxyFromProxylet() {
155: proxySSL = System
156: .getProperty("com.sun.portal.proxylet.proxyHost");
157: String portStr = System
158: .getProperty("com.sun.portal.proxylet.proxyPort");
159: if (portStr != null && portStr.length() > 0) {
160: proxySSLPort = Integer.parseInt(portStr);
161: }
162:
163: System.out
164: .println("PluginProxyManager.loadProxyFromProxylet proxySSL "
165: + proxySSL);
166: System.out
167: .println("PluginProxyManager.loadProxyFromProxylet proxySSLPort "
168: + proxySSLPort);
169:
170: if (proxySSL == null || proxySSLPort < 0) {
171: proxySSL = "";
172: proxySSLPort = 0;
173: proxyType = ClientConfig.DIRECT;
174: } else {
175: proxyType = ClientConfig.MANUAL;
176: }
177: }
178:
179: private void loadProxyUsingReflection() {
180: System.out.println("Trying to load PluginProxyManager");
181: try {
182: Class pluginProxyManagerClass = Class
183: .forName("sun.plugin.net.proxy.PluginProxyManager");
184: String gwURL = ClientConfig.getConfigURL();
185: java.net.URL gatewayUrl = new java.net.URL(gwURL);
186:
187: // These are the unexposed methods and classes in javaplugin.jar
188: // sun.plugin.net.proxy.ProxyInfo pI=s.p.n.p.PluginProxyManager.getProxyInfo(gatewayUrl);
189: // proxyHost = pI.getProxy();
190: // proxyPort = pI.getPort(); //for reference only : dont uncomment the above 3 lines
191:
192: //proxySSL = sun.plugin.net.proxy.PluginProxyManager.getProxyInfo(gatewayUrl).getProxy();
193: //proxySSLPort = sun.plugin.net.proxy.PluginProxyManager.getProxyInfo(gatewayUrl).getPort();
194:
195: //Trying reflection
196: Method getProxyInfoMethod = pluginProxyManagerClass
197: .getMethod("getProxyInfo", new Class[] { Class
198: .forName("java.net.URL") });
199: Object resultProxyInfo = getProxyInfoMethod.invoke(null,
200: new Object[] { gatewayUrl });
201: Class proxyInfoClass = resultProxyInfo.getClass();
202: Method getProxyMethod = proxyInfoClass.getMethod(
203: "getProxy", null);
204: proxySSL = (String) getProxyMethod.invoke(resultProxyInfo,
205: null);
206: Method getPortMethod = proxyInfoClass.getMethod("getPort",
207: null);
208: proxySSLPort = ((Integer) getPortMethod.invoke(
209: resultProxyInfo, null)).intValue();
210: System.out.println("PluginProxyManager returned proxySSL "
211: + proxySSL);
212: System.out
213: .println("PluginProxyManager returned proxySSLPort "
214: + proxySSLPort);
215: if (proxySSL == null || proxySSLPort < 0) {
216: proxySSL = "";
217: proxySSLPort = 0;
218: proxyType = ClientConfig.DIRECT;
219: } else {
220: proxyType = ClientConfig.MANUAL;
221: }
222: } catch (Exception e) {
223: System.out.println("Error in Reflection " + e);
224: proxyType = ClientConfig.DIRECT;
225: }
226: }
227:
228: /*
229: * Sends POST request to Netlet servlet to store proxy settings.
230: * Assumes that member variables proxyType, proxyHost, proxyPort,
231: * noProxiesFor have a valid values.
232: */
233: private void storeProxySettings() {
234: String postBody = "&proxytype=";
235: if (proxyType == ClientConfig.DIRECT) {
236: postBody += "DIRECT";
237: } else {
238: postBody += "MANUAL";
239: }
240: postBody += "&proxyhost=" + proxySSL;
241: postBody += "&proxyport=" + proxySSLPort;
242: String temp = noProxiesFor.toString();
243: temp = temp.substring(temp.indexOf("[") + 1, temp.indexOf("]"))
244: .trim();
245: postBody += "&proxyoverride=" + temp;
246: InputStream in = ClientUtil.sendByPost("storeProxySettings",
247: postBody);
248: }
249:
250: /*
251: * Functionality:
252: * 1. Read proxySettings from the applet params which intern is obtained from profile.
253: * 2. If these are valid use them, otherwise prompt the user to enter proxy settings.
254: * 3. Check whether the proxy settings provided by user are valid.
255: * 4. If valid store them to the profile, ignore otherwise.
256: */
257: private void useNetletProfile() {
258: readNetletProfile();
259: // Test whether the proxy information stored in the user profile are valid
260: boolean valid = isProxySettingsValid();
261: if (!valid) {
262: // Ask the user for the proxy settings
263: pid = new ProxyInfoDialog(new JFrame(), this , this );
264: pid.showWarning();
265: pid.waitForAction();
266:
267: /* Now all proxy informations are ready
268: * test whether they are correct, is yes store to the profile
269: */
270: valid = isProxySettingsValid();
271: if (valid) {
272: // Store it to the user profile
273: storeProxySettings();
274: }
275: } else { // Store the proxy override list
276: parseProxyOverrideList(ClientConfig
277: .getParam("proxyoverride"));
278: }
279: }
280:
281: /*
282: * Read the proxy settings from Applet parameters,
283: * which intern is read from Netlet Profile.
284: * @ Stores these values in member variables.
285: */
286: private void readNetletProfile() {
287: String temp = ClientConfig.getParam("proxytype");
288: if ((temp != null) && (temp.equalsIgnoreCase("DIRECT"))) {
289: proxyType = ClientConfig.DIRECT;
290: } else {
291: proxyType = ClientConfig.MANUAL;
292: }
293: temp = ClientConfig.getParam("proxyhost");
294: if ((temp != null) && (temp.trim().length() != 0)) {
295: proxySSL = temp;
296: }
297: temp = ClientConfig.getParam("proxyport");
298: if ((temp != null) && (temp.trim().length() != 0)) {
299: try {
300: proxySSLPort = Integer.parseInt(temp);
301: } catch (NumberFormatException nfe) {
302: proxySSLPort = 0;
303: }
304: }
305: }
306:
307: /*
308: * Extracts proxy host and proxy port from proxy configuration list
309: */
310: private void extractSSLProxyHostAndPort(String proxyConfigList) {
311: if ((proxyType == ClientConfig.DIRECT)
312: || (proxyConfigList == null)
313: || (proxyConfigList.trim().length() == 0)) {
314: return;
315: }
316: String pport = null;
317: try {
318: if ((proxyConfigList.indexOf("https") == -1)) { // Same proxy for all protocols
319: proxySSL = proxyConfigList.substring(0,
320: proxyConfigList.indexOf(':')).trim();
321: pport = proxyConfigList.substring(
322: proxyConfigList.indexOf(':') + 1).trim();
323: proxySSLPort = Integer.parseInt(pport);
324: } else {
325: String temp = proxyConfigList.substring(
326: proxyConfigList.indexOf("https") + 6).trim();
327: if (temp.indexOf(",") != -1)
328: temp = temp.substring(0, temp.indexOf(","));
329: proxySSL = temp.substring(0, temp.indexOf(':')).trim();
330: pport = temp.substring(temp.indexOf(':') + 1).trim();
331: proxySSLPort = Integer.parseInt(pport);
332: }
333: } catch (IndexOutOfBoundsException iobe) {
334: System.out
335: .println("Exception when reading proxy information from plugin - "
336: + iobe);
337: } catch (NumberFormatException nfe) {
338: System.out
339: .println("Exception when reading proxy information from plugin - "
340: + nfe);
341: } catch (NullPointerException npe) {
342: System.out
343: .println("Exception when reading proxy information from plugin - "
344: + npe);
345: }
346: }
347:
348: /*
349: * Parses the proxyOverrideList and stores in noProxiesFor vector.
350: */
351: private void parseProxyOverrideList(String proxyOverrideList) {
352: if (proxyOverrideList == null
353: || proxyOverrideList.trim().length() == 0)
354: return;
355: StringTokenizer nst = new StringTokenizer(proxyOverrideList,
356: ",");
357: while (nst.hasMoreTokens()) {
358: String s = nst.nextToken().toLowerCase().trim();
359: if (!(s.trim().length() == 0)) {
360: noProxiesFor.addElement(s);
361: }
362: }
363: }
364:
365: /*
366: * Returns SSL proxy host
367: */
368:
369: public String getProxySSL() {
370: return proxySSL;
371: }
372:
373: /*
374: * Returns SSL proxy port
375: */
376:
377: public int getProxySSLPort() {
378: return proxySSLPort;
379: }
380:
381: /*
382: * Returns false if direct connection to the internet, true otherwise
383: */
384:
385: public boolean getProxyMode() {
386: if (proxyType == ClientConfig.DIRECT) {
387: return false;
388: }
389: return true;
390: }
391:
392: /*
393: * Checks whether the proxy settings stored in member variables is valid or not.
394: * @ SClientMgr is required for proxy authentication if necessary.
395: */
396: private boolean isProxySettingsValid() {
397: ProxySConn sconn = null;
398: if (proxyType == ClientConfig.MANUAL) {
399: try {
400: // try regular connection
401: sconn = new ProxySConn(proxySSLPort, proxySSL,
402: ClientConfig.getDestPort(), ClientConfig
403: .getDestHost(), null);
404: } catch (ProxyAuthNeededException e) {
405: // try again with proxy auth
406: try {
407: sconn = new ProxySConn(proxySSLPort, proxySSL,
408: ClientConfig.getDestPort(), ClientConfig
409: .getDestHost(), true, null);
410: } catch (ProxyAuthNeededException ee) {
411: System.out.println("Invalid proxy information");
412: } catch (ProxyAuthFailedException ee) {
413: System.out.println("Invalid proxy information");
414: }
415: } catch (ProxyAuthFailedException e) {
416: System.out.println("Invalid proxy information");
417: }
418: } else if (proxyType == ClientConfig.DIRECT) {
419: sconn = new ProxySConn(ClientConfig.getDestPort(),
420: ClientConfig.getDestHost(), null);
421: } else {
422: return false;
423: }
424: Socket out_s = sconn.getconn();
425: if (out_s != null) {
426: return true;
427: } else {
428: return false;
429: }
430: }
431:
432: /*
433: * Parses the PAC file URL, extracts SSL proxy host and port.
434: * - stores these values in the member variables.
435: * isProxySettingsValid() should be invoked immediately after this.
436: */
437: private void parsePACFileURL(String pacFileURL) {
438: /*
439: * If the PAC file URL is invalid, try direct connection.
440: */
441: if (pacFileURL == null || pacFileURL.trim().length() == 0) {
442: proxyType = ClientConfig.DIRECT;
443: return;
444: }
445: String inputLine = ClientUtil.parsePACFile(pacFileURL);
446: /*
447: * If the proxy settings extracted from PAC file URL
448: * is invalid, try direct connection
449: */
450: if (inputLine == null || inputLine.trim().length() == 0
451: || inputLine.equals("null")) {
452: proxyType = ClientConfig.DIRECT;
453: return;
454: }
455: if (inputLine.equalsIgnoreCase("DIRECT")) {
456: proxyType = ClientConfig.DIRECT;
457: return;
458: }
459: if (inputLine.startsWith("PROXY")) {
460: proxyType = ClientConfig.MANUAL;
461: if (inputLine.length() > 5) {
462: String proxySt = inputLine.substring(5);
463: int firstProxy = proxySt.indexOf(";");
464: if (firstProxy < 0)
465: firstProxy = proxySt.length();
466:
467: String firstProxyURL = proxySt.substring(0, firstProxy);
468: int index = firstProxyURL.lastIndexOf(":");
469: String proxyPort = firstProxyURL.substring(index + 1);
470: String proxyHost = firstProxyURL.substring(0, index);
471: proxySSL = proxyHost.trim();
472: try {
473: proxySSLPort = Integer.parseInt(proxyPort);
474: } catch (NumberFormatException nfe) {
475: System.out.println("Invalid SSL Proxy port");
476: proxySSLPort = 0;
477: }
478: }
479: }
480: }
481:
482: /*
483: * Checks whether the given host belongs to the proxy override list
484: * return true if belongs false otherwise
485: */
486:
487: public boolean inNoProxiesFor(String host) {
488: for (Enumeration e = noProxiesFor.elements(); e
489: .hasMoreElements();) {
490: String s = (String) e.nextElement();
491: if (ClientUtil.wildcardMatch(host.toLowerCase(), s
492: .toLowerCase())) {
493: return true;
494: }
495: }
496: return false;
497: }
498:
499: /*
500: * Process the proxy information entered by user.
501: */
502: public void actionPerformed(ActionEvent evt) {
503: Object obj = evt.getSource();
504: if ("OK".equals(evt.getActionCommand())) {
505: if (obj == pid.ok) {
506: pid.setVisible(false);
507: // Now find the proxy type and store it to the back end
508: if (pid.direct.isSelected()) {
509: proxyType = ClientConfig.DIRECT;
510: } else if (pid.manual.isSelected()) {
511: proxyType = ClientConfig.MANUAL;
512: proxySSL = pid.proxyHost.getText();
513: try {
514: proxySSLPort = Integer.parseInt(pid.proxyPort
515: .getText());
516: } catch (NumberFormatException nfe) {
517: proxySSLPort = 0;
518: System.out.println("Invalid SSL proxy host: "
519: + nfe);
520: }
521: parseProxyOverrideList(pid.noProxy.getText());
522: } else if (pid.auto.isSelected()) {
523: parsePACFileURL(pid.autoURL.getText());
524: }
525: if (pihd != null) {
526: pihd.setVisible(false);
527: pihd = null;
528: }
529: pid.notifyAction();
530: }
531: } else if ("Cancel".equals(evt.getActionCommand())) {
532: if (obj == pid.cancel) {
533: pid.setVisible(false);
534: if (pihd != null) {
535: pihd.setVisible(false);
536: pihd = null;
537: }
538: pid.notifyAction();
539: }
540: } else if ("Help".equals(evt.getActionCommand())) {
541: if (obj == pid.help) {
542: if (pihd == null) {
543: pihd = new ProxyInfoHelpDialog(new JFrame());
544: }
545: pihd.show();
546: }
547: }
548: }
549:
550: /*
551: * Process the change in selection, esp change the colors.
552: */
553: public void itemStateChanged(ItemEvent evt) {
554: if (pid.direct.isSelected()) {
555: pid.setState(ClientConfig.DIRECT);
556: } else if (pid.manual.isSelected()) {
557: pid.setState(ClientConfig.MANUAL);
558: } else if (pid.auto.isSelected()) {
559: pid.setState(ClientConfig.AUTO);
560: }
561: }
562:
563: }
|