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 Deepak
020: */package com.sun.portal.netlet.client.jnlp;
021:
022: import com.sun.portal.netlet.client.common.*;
023:
024: import javax.swing.*;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.awt.event.ItemEvent;
028: import java.awt.event.ItemListener;
029: import java.awt.*;
030: import java.io.InputStream;
031: import java.net.Socket;
032: import java.util.Enumeration;
033: import java.util.StringTokenizer;
034: import java.util.Vector;
035:
036: public class JWSProxyInfo implements ProxyInfo, ActionListener,
037: ItemListener {
038:
039: private boolean proxyMode = false;
040: private String proxySSL = ""; // SSL proxy host
041: private int proxySSLPort = 0; // SSL proxy port
042: private Vector noProxiesFor = new Vector(); // Bypass proxy list
043:
044: private ProxyWarning pw = null;
045: private static ProxyInfoDialog pid = null;
046: private static ProxyInfoHelpDialog pihd = null;
047:
048: /*
049: * Default constructor - reads the proxy information ( Proxy mode,
050: * SSL proxy host, SSL proxy port, Proxy Bypass list).
051: */
052: public JWSProxyInfo() {
053:
054: // Read the proxy type form system property javaplugin.proxy.config.type
055:
056: /*
057: Properties props = System.getProperties();
058: Enumeration enum = props.propertyNames();
059: while (enum.hasMoreElements()) {
060: String name = (String)enum.nextElement();
061: String value = props.getProperty(name);
062: //System.out.println(">> Property " + name + " : " + value);
063: }
064: */
065: if (ClientConfig.isProxyletMode()) {
066: loadProxyFromProxylet();
067: } else {
068: loadProxyFromJWS();
069: }
070:
071: if (inNoProxiesFor(ClientConfig.getDestHost())) {
072: proxyMode = false;
073: System.out.println("Host : " + ClientConfig.getDestHost()
074: + " in noProxyList, setting proxyType to DIRECT");
075: }
076: }
077:
078: private void loadProxyFromProxylet() {
079: proxySSL = System
080: .getProperty("com.sun.portal.proxylet.proxyHost");
081: String portStr = System
082: .getProperty("com.sun.portal.proxylet.proxyPort");
083: if (portStr != null && portStr.length() > 0) {
084: proxySSLPort = Integer.parseInt(portStr);
085: }
086:
087: System.out
088: .println("JWSProxyInfo.loadProxyFromProxylet proxySSL "
089: + proxySSL);
090: System.out
091: .println("JWSProxyInfo.loadProxyFromProxylet proxySSLPort "
092: + proxySSLPort);
093:
094: if (proxySSL == null || proxySSLPort < 0) {
095: proxySSL = "";
096: proxySSLPort = 0;
097: proxyMode = false;
098: } else {
099: proxyMode = true;
100: }
101: }
102:
103: private void loadProxyFromJWS() {
104: proxySSL = System.getProperty("https.proxyHost");
105: String strPort = System.getProperty("https.proxyPort");
106: if (strPort != null && strPort.length() > 0) {
107: proxySSLPort = Integer.parseInt(strPort);
108: }
109: if (proxySSL == null || proxySSLPort < 0) {
110: proxySSL = "";
111: proxySSLPort = 0;
112: proxyMode = false;
113: } else {
114: proxyMode = true;
115: }
116: String proxyOverrideList = System
117: .getProperty("https.nonProxyHosts");
118: System.out.println("Proxy override - " + proxyOverrideList);
119: parseProxyOverrideList(proxyOverrideList);
120: if (!isProxySettingsValid()) {
121: useNetletProfile();
122: }
123: }
124:
125: /*
126: * Parses the proxyOverrideList and stores in noProxiesFor vector.
127: */
128:
129: private void parseProxyOverrideList(String proxyOverrideList) {
130: if (proxyOverrideList == null
131: || proxyOverrideList.trim().length() == 0)
132: return;
133: StringTokenizer nst = new StringTokenizer(proxyOverrideList,
134: "|");
135: while (nst.hasMoreTokens()) {
136: String s = nst.nextToken().toLowerCase().trim();
137: if (!(s.trim().length() == 0)) {
138: noProxiesFor.addElement(s);
139: }
140: }
141: }
142:
143: /*
144: * Checks whether the proxy settings stored in member variables is valid or not.
145: * @ SClientMgr is required for proxy authentication if necessary.
146: */
147: private boolean isProxySettingsValid() {
148: ProxySConn sconn = null;
149: if (proxyMode) {
150: try {
151: // try regular connection
152: sconn = new ProxySConn(proxySSLPort, proxySSL,
153: ClientConfig.getDestPort(), ClientConfig
154: .getDestHost(), null);
155: } catch (ProxyAuthNeededException e) {
156: // try again with proxy auth
157: try {
158: sconn = new ProxySConn(proxySSLPort, proxySSL,
159: ClientConfig.getDestPort(), ClientConfig
160: .getDestHost(), true, null);
161: } catch (ProxyAuthNeededException ee) {
162: System.out.println("Invalid proxy information");
163: } catch (ProxyAuthFailedException ee) {
164: System.out.println("Invalid proxy information");
165: }
166: } catch (ProxyAuthFailedException e) {
167: System.out.println("Invalid proxy information");
168: }
169: } else {
170: sconn = new ProxySConn(ClientConfig.getDestPort(),
171: ClientConfig.getDestHost(), null);
172: }
173: Socket out_s = sconn.getconn();
174: if (out_s != null) {
175: return true;
176: } else {
177: return false;
178: }
179: }
180:
181: /*
182: * Functionality:
183: * 1. Read proxySettings from the applet params which intern is obtained from profile.
184: * 2. If these are valid use them, otherwise prompt the user to enter proxy settings.
185: * 3. Check whether the proxy settings provided by user are valid.
186: * 4. If valid store them to the profile, ignore otherwise.
187: */
188: private void useNetletProfile() {
189: readNetletProfile();
190: // Test whether the proxy information stored in the user profile are valid
191: boolean valid = isProxySettingsValid();
192: if (!valid) {
193: // Ask the user for the proxy settings
194: pid = new ProxyInfoDialog(new JFrame(), this , this );
195: pid.showWarning();
196: pid.waitForAction();
197:
198: /* Now all proxy informations are ready
199: * test whether they are correct, is yes store to the profile
200: */
201: valid = isProxySettingsValid();
202: if (valid) {
203: //Set System props.
204: System.setProperty("https.proxyHost", proxySSL);
205: System.setProperty("https.proxyPort", Integer
206: .toString(proxySSLPort));
207: // Store it to the user profile
208: storeProxySettings();
209: }
210: } else { // Store the proxy override list
211: //Set System props.
212: System.setProperty("https.proxyHost", proxySSL);
213: System.setProperty("https.proxyPort", Integer
214: .toString(proxySSLPort));
215: parseProxyOverrideList(ClientConfig
216: .getParam("proxyoverride"));
217: }
218: }
219:
220: /*
221: * Read the proxy settings from Applet parameters,
222: * which intern is read from Netlet Profile.
223: * @ Stores these values in member variables.
224: */
225: private void readNetletProfile() {
226: String temp = ClientConfig.getParam("proxytype");
227: if ((temp != null) && (temp.equalsIgnoreCase("DIRECT"))) {
228: proxyMode = false;
229: } else {
230: proxyMode = true;
231: }
232: temp = ClientConfig.getParam("proxyhost");
233: if ((temp != null) && (temp.trim().length() != 0)) {
234: proxySSL = temp;
235: }
236: temp = ClientConfig.getParam("proxyport");
237: if ((temp != null) && (temp.trim().length() != 0)) {
238: try {
239: proxySSLPort = Integer.parseInt(temp);
240: } catch (NumberFormatException nfe) {
241: proxySSLPort = 0;
242: }
243: }
244: }
245:
246: /*
247: * Sends POST request to Netlet servlet to store proxy settings.
248: * Assumes that member variables proxyType, proxyHost, proxyPort,
249: * noProxiesFor have a valid values.
250: */
251: private void storeProxySettings() {
252:
253: String postBody = "&proxytype=";
254: if (!proxyMode) {
255: postBody += "DIRECT";
256: } else {
257: postBody += "MANUAL";
258: }
259: postBody += "&proxyhost=" + proxySSL;
260: postBody += "&proxyport=" + proxySSLPort;
261: String temp = noProxiesFor.toString();
262: temp = temp.substring(temp.indexOf("[") + 1, temp.indexOf("]"))
263: .trim();
264: postBody += "&proxyoverride=" + temp;
265: InputStream in = ClientUtil.sendByPost("storeProxySettings",
266: postBody);
267: }
268:
269: /*
270: * Returns SSL proxy host
271: */
272:
273: public String getProxySSL() {
274: return proxySSL;
275: }
276:
277: /*
278: * Returns SSL proxy port
279: */
280:
281: public int getProxySSLPort() {
282: return proxySSLPort;
283: }
284:
285: /*
286: * Returns false if direct connection to the internet, true otherwise
287: */
288:
289: public boolean getProxyMode() {
290: return proxyMode;
291: }
292:
293: /*
294: * Checks whether the given host belongs to the proxy override list
295: * return true if belongs false otherwise
296: */
297:
298: private boolean inNoProxiesFor(String host) {
299: System.out.println("inNoProxiesFor host " + host);
300: for (Enumeration e = noProxiesFor.elements(); e
301: .hasMoreElements();) {
302: String s = (String) e.nextElement();
303: System.out.println("noProxyList element " + s);
304: if (ClientUtil.wildcardMatch(host.toLowerCase(), s
305: .toLowerCase())) {
306: return true;
307: }
308: }
309: return false;
310: }
311:
312: /*
313: * Process the proxy information entered by user.
314: */
315: public void actionPerformed(ActionEvent evt) {
316: Object obj = evt.getSource();
317: if ("OK".equals(evt.getActionCommand())) {
318: if (obj == pid.ok) {
319: pid.setVisible(false);
320: // Now find the proxy type and store it to the back end
321: if (pid.direct.isSelected()) {
322: proxyMode = false;
323: } else if (pid.manual.isSelected()) {
324: proxyMode = true;
325: proxySSL = pid.proxyHost.getText();
326: try {
327: proxySSLPort = Integer.parseInt(pid.proxyPort
328: .getText());
329: } catch (NumberFormatException nfe) {
330: proxySSLPort = 0;
331: System.out.println("Invalid SSL proxy host: "
332: + nfe);
333: }
334: parseProxyOverrideList(pid.noProxy.getText());
335: } else if (pid.auto.isSelected()) {
336: parsePACFileURL(pid.autoURL.getText());
337: }
338: if (pihd != null) {
339: pihd.setVisible(false);
340: pihd = null;
341: }
342: pid.notifyAction();
343: }
344: } else if ("Cancel".equals(evt.getActionCommand())) {
345: if (obj == pid.cancel) {
346: pid.setVisible(false);
347: if (pihd != null) {
348: pihd.setVisible(false);
349: pihd = null;
350: }
351: pid.notifyAction();
352: }
353: } else if ("Help".equals(evt.getActionCommand())) {
354: if (obj == pid.help) {
355: if (pihd == null) {
356: pihd = new ProxyInfoHelpDialog(new JFrame());
357: }
358: pihd.show();
359: }
360: }
361: }
362:
363: /*
364: * Parses the PAC file URL, extracts SSL proxy host and port.
365: * - stores these values in the member variables.
366: * isProxySettingsValid() should be invoked immediately after this.
367: */
368: private void parsePACFileURL(String pacFileURL) {
369: /*
370: * If the PAC file URL is invalid, try direct connection.
371: */
372: if (pacFileURL == null || pacFileURL.trim().length() == 0) {
373: proxyMode = false;
374: return;
375: }
376: String inputLine = ClientUtil.parsePACFile(pacFileURL);
377: /*
378: * If the proxy settings extracted from PAC file URL
379: * is invalid, try direct connection
380: */
381: if (inputLine == null || inputLine.trim().length() == 0
382: || inputLine.equals("null")) {
383: proxyMode = false;
384: return;
385: }
386: if (inputLine.equalsIgnoreCase("DIRECT")) {
387: proxyMode = false;
388: return;
389: }
390: if (inputLine.startsWith("PROXY")) {
391: proxyMode = true;
392: if (inputLine.length() > 5) {
393: String proxySt = inputLine.substring(5);
394: int firstProxy = proxySt.indexOf(";");
395: if (firstProxy < 0)
396: firstProxy = proxySt.length();
397:
398: String firstProxyURL = proxySt.substring(0, firstProxy);
399: int index = firstProxyURL.lastIndexOf(":");
400: String proxyPort = firstProxyURL.substring(index + 1);
401: String proxyHost = firstProxyURL.substring(0, index);
402: proxySSL = proxyHost.trim();
403: try {
404: proxySSLPort = Integer.parseInt(proxyPort);
405: } catch (NumberFormatException nfe) {
406: System.out.println("Invalid SSL Proxy port");
407: proxySSLPort = 0;
408: }
409: }
410: }
411: }
412:
413: /*
414: * Process the change in selection, esp change the colors.
415: */
416: public void itemStateChanged(ItemEvent evt) {
417: if (pid.direct.isSelected()) {
418: pid.setState(ClientConfig.DIRECT);
419: } else if (pid.manual.isSelected()) {
420: pid.setState(ClientConfig.MANUAL);
421: } else if (pid.auto.isSelected()) {
422: pid.setState(ClientConfig.AUTO);
423: }
424: }
425:
426: public ProxyWarning getProxyWarning() {
427: if (pw == null) {
428: pw = new ProxyWarning(new Frame(), this, BrowserType
429: .getInstance().getBrowserName());
430: }
431: return pw;
432: }
433:
434: }
|