0001: package com.sun.portal.proxylet.client.common;
0002:
0003: import com.sun.portal.proxylet.client.common.browser.BrowserType;
0004: import com.sun.portal.proxylet.client.common.browser.BrowserHelper;
0005: import com.sun.portal.proxylet.client.common.ui.ReadProgressDialog;
0006:
0007: import com.sun.portal.proxylet.client.common.server.Cookie;
0008:
0009: import java.applet.Applet;
0010: import java.io.*;
0011: import java.net.*;
0012: import java.util.*;
0013:
0014: /**
0015: * Param
0016: *
0017: * This class manages all the configuration parameters of the applet.
0018: * Some of them are got from Applet < PARAM > tag, while others are
0019: * set during initialization of the applet.
0020: */
0021: public class Param {
0022: private static BrowserType browserType;
0023: private static String vmVersion;
0024: private static String vmVendor;
0025: private static String sslProvider;
0026:
0027: /**
0028: * Holds the hosts that should bypass the enterprise proxy, incase of
0029: * manual config type.
0030: */
0031: private static Vector proxyBypassList;
0032: /**
0033: * Holds the client proxy host for HTTP
0034: */
0035: private static String clientProxyHostHTTP = null;
0036: /**
0037: * Holds the client proxy host for HTTPS
0038: */
0039: private static String clientProxyHostSSL = null;
0040: /**
0041: * Holds the client proxy host for FTP
0042: */
0043: private static String clientProxyHostFTP = null;
0044: /**
0045: * Holds the client proxy host for GOPHER
0046: */
0047: private static String clientProxyHostGopher = null;
0048: /**
0049: * Holds the client proxy port for HTTP
0050: */
0051: private static int clientProxyPortHTTP = -1;
0052: /**
0053: * Holds the client proxy port for SSL
0054: */
0055: private static int clientProxyPortSSL = -1;
0056: /**
0057: * Holds the client proxy port for FTP
0058: */
0059: private static int clientProxyPortFTP = -1;
0060: /**
0061: * Holds the client proxy port for Gopher
0062: */
0063: private static int clientProxyPortGopher = -1;
0064: /**
0065: /**
0066: * Holds the host address of gateway server
0067: */
0068: private static String gatewayHost = null;
0069: /**
0070: * Holds the gateway port
0071: */
0072: private static int gatewayPort = -1;
0073: /**
0074: * Holds the gateway mode - secure or non-secure
0075: */
0076: private static String gatewayMode = null;
0077: /**
0078: * Holds the URL of the proxylet servlet
0079: */
0080: private static String servletURL = null;
0081: /**
0082: * Holds the URL of the gateway server
0083: */
0084: private static String gwURL = null;
0085: /**
0086: * Holds the list of proxies that can be used; the primarly and alternate proxies
0087: */
0088: private static Vector proxyList = null;
0089: /**
0090: * Holds the original PAC file body
0091: */
0092: private static String clientPACContent = null;
0093: /**
0094: * Holds the modified PAC file body
0095: */
0096: private static String newPACContent = null;
0097: /**
0098: * Holds the proxylet rules
0099: */
0100: private static String rules = null;
0101: /**
0102: * Holds the session id
0103: */
0104: private static String sesId = null;
0105: /**
0106: * Holds the Operating System
0107: */
0108: private static String os = null;
0109: /**
0110: * Holds the current proxy method
0111: */
0112: private static String proxyMethod;
0113: /**
0114: * Holds the iPlanetDirectoryPro cookie
0115: */
0116: private static Cookie cookie = null;
0117: /**
0118: * Holds the raw Cookie string
0119: */
0120: private static String rawCookie = null;
0121: /**
0122: * Holds the resource bundle containing message text
0123: */
0124: private static PropertyResourceBundle resources = null;
0125: /**
0126: * Holds the custom resource bundle containing message text
0127: */
0128: private static PropertyResourceBundle customresources = null;
0129:
0130: /*
0131: * Holds the user configured host and port values
0132: */
0133: private static String bindPort = null;
0134: private static String bindIP = null;
0135:
0136: /*
0137: * Holds the download manager port no
0138: */
0139: private static String downloadMgrPort = null;
0140:
0141: /*
0142: * Holds the Pacfile location and the flag
0143: */
0144: private static String pacfileLocation = null;
0145: private static byte[] pacfilecontent;
0146:
0147: private static int windowWidth = 500;
0148: private static int windowHeight = 500;
0149:
0150: /*
0151: * Log level for logging to java console
0152: */
0153: static boolean logLevelConsole = false;
0154: static boolean logLevelUI = false;
0155:
0156: private static String source = "ProxyletApplet";
0157:
0158: //temp directory for holding various client files
0159: private static String tmpDirectory;
0160:
0161: private static String propertyfile = null;
0162:
0163: private static String platformLoginURL = null;
0164: private static String sessionIdleTimeout = null;
0165: //
0166: // Applet parameter constants
0167: //
0168: private static String btype_c = "BROWSER_TYPE";
0169: private static String gHost_c = "GATEWAY_HOST";
0170: private static String gPort_c = "GATEWAY_PORT";
0171: private static String gMode_c = "GATEWAY_MODE";
0172: private static String cookie_c = "COOKIE";
0173: private static String servletURL_c = "SERVLET_URL";
0174: private static String gwURL_c = "GW_URL";
0175: private static String domainList_c = "DOMAINLIST";
0176: private static String rules_c = "RULES";
0177: private static String sesId_c = "SESSION_ID";
0178: private static String os_c = "os.name";
0179: private static String bindport_c = "BINDPORT";
0180: //private static String pacfileLocation_c = "PACFILE_LOCATION";
0181: private static String bindip_c = "BINDIP";
0182: private static String downloadMgrPort_c = "DOWNLOAD_MANAGER_PORT";
0183: private static String width_c = "WIDTH";
0184: private static String height_c = "HEIGHT";
0185: private static String loginURL_c = "PLATFORM_LOGIN_URL";
0186: private static String sessionidletimeout_c = "SESSION_IDLE_TIMEOUT";
0187: private static String propertyfile_c = "propertyfile";
0188:
0189: public static void setSource(String src) {
0190: source = src;
0191: }
0192:
0193: public static String getSource() {
0194: return source;
0195: }
0196:
0197: public static void setWindowDimension(int width, int height) {
0198: windowWidth = width;
0199: windowHeight = height;
0200: }
0201:
0202: public static int getWindowWidth() {
0203: return windowWidth;
0204: }
0205:
0206: public static int getWindowHeight() {
0207: return windowHeight;
0208: }
0209:
0210: public static void readParameters(Applet appletContext) {
0211: proxyMethod = "DIRECT";
0212: if (appletContext != null) {
0213: String temp = appletContext.getParameter("DEBUGLEVEL");
0214: if (temp != null && temp.equals("1")) {
0215: logLevelConsole = true;
0216: }
0217:
0218: Log.setloglevel_JavaConsole(logLevelConsole);
0219: Log.setloglevel_UI(logLevelUI);
0220:
0221: browserType = BrowserType.getType(appletContext
0222: .getParameter(btype_c));
0223: setVMVersion();
0224: setVendor();
0225: gatewayHost = appletContext.getParameter(gHost_c);
0226: gatewayPort = Integer.parseInt(appletContext
0227: .getParameter(gPort_c));
0228: gatewayMode = appletContext.getParameter(gMode_c);
0229: try {
0230: pacfileLocation = (File.createTempFile("proxylet",
0231: ".pac", new File(getTempDirectory())))
0232: .toString();
0233: } catch (IOException ignore) {
0234: ignore.printStackTrace();
0235: pacfileLocation = null;
0236: }
0237: setSSLProvider();
0238: cookie = new Cookie(appletContext.getParameter(cookie_c),
0239: Cookie.APPLETPARAM_COOKIE_FORMAT);
0240: Log.debug("\nBrowserType:" + browserType.toString()
0241: + "\nGateway mode:" + gatewayMode
0242: + "\npacfileLocation:" + pacfileLocation);
0243: servletURL = appletContext.getParameter(servletURL_c);
0244: gwURL = appletContext.getParameter(gwURL_c);
0245: rules = appletContext.getParameter(rules_c);
0246: sesId = appletContext.getParameter(cookie_c);
0247: os = System.getProperty(os_c);
0248:
0249: bindIP = appletContext.getParameter(bindip_c).trim();
0250: bindPort = appletContext.getParameter(bindport_c).trim();
0251: downloadMgrPort = appletContext
0252: .getParameter(downloadMgrPort_c);
0253:
0254: if (rules != null && rules.equals("$RULES")) {
0255: loadPacContent();
0256: }
0257: windowWidth = Integer.parseInt(appletContext
0258: .getParameter(width_c));
0259: windowHeight = Integer.parseInt(appletContext
0260: .getParameter(height_c));
0261: platformLoginURL = appletContext.getParameter(loginURL_c);
0262: sessionIdleTimeout = appletContext
0263: .getParameter(sessionidletimeout_c);
0264: propertyfile = appletContext.getParameter(propertyfile_c);
0265: }
0266: }
0267:
0268: public static String getPlatformLoginURL() {
0269: return platformLoginURL;
0270: }
0271:
0272: public static void setPlatformLoginURL(String url) {
0273: platformLoginURL = url;
0274: }
0275:
0276: public static String getSessionIdleTimeout() {
0277: return sessionIdleTimeout;
0278: }
0279:
0280: public static void setSessionIdleTimeout(String session) {
0281: sessionIdleTimeout = session;
0282: }
0283:
0284: public static void setPropertyFile(String propertyf) {
0285: if (propertyf != null && !propertyf.equals("$PROPERTY_FILE"))
0286: propertyfile = propertyf;
0287: }
0288:
0289: public static void nullifyResource() {
0290: resources = null;
0291: }
0292:
0293: public static void loadPacContent() {
0294: try {
0295: Log.debugu(getString("pinfo.10", "Getting PAC content"));
0296: pacfilecontent = sendMsgtoServlet("?command=getPacfile",
0297: false, false, null);
0298: if (pacfilecontent == null) {
0299: Log
0300: .debugu(getString("pinfo.9",
0301: "Failed to retrive PAC file content from server"));
0302: }
0303: } catch (Exception e) {
0304: Log.debugu(getString("pinfo.9",
0305: "Failed to retrive PAC file content from server"));
0306: }
0307:
0308: }
0309:
0310: public static byte[] getPacfileContent() {
0311: return pacfilecontent;
0312: }
0313:
0314: public static void setPacfileLocation(String pacLocation) {
0315: pacfileLocation = pacLocation;
0316: }
0317:
0318: private static URLConnection getURLConnection(String commandURI)
0319: throws Exception {
0320: String resourceURL = gwURL + "/" + servletURL + commandURI;
0321: System.out.println("resourceURL " + resourceURL);
0322: URL url = new URL(resourceURL);
0323: URLConnection uconn = null;
0324: uconn = url.openConnection();
0325: uconn.setDoInput(true);
0326: uconn.setUseCaches(false);
0327: uconn.setAllowUserInteraction(false);
0328:
0329: return uconn;
0330: }
0331:
0332: public static byte[] sendMsgtoServlet(String command,
0333: boolean displayProgressbar, boolean postdataavailable,
0334: byte[] data) throws Exception {
0335: ReadProgressDialog diag = null;
0336: BufferedReader breader;
0337:
0338: System.out.println("sendMsgtoServlet");
0339: byte[] arr = new byte[0];
0340: try {
0341: System.out.println("Send Msg to server "
0342: + Param.getServletURL() + command);
0343: URLConnection uconn = getURLConnection(command);
0344:
0345: System.out.println("set cookie if its javawebstart app");
0346: //encode cookie value
0347: if (Param.getSource() != null
0348: && Param.getSource().equals("App")) {
0349: System.out.println("cookie " + Param.getRawCookie());
0350: uconn
0351: .setRequestProperty("Cookie", Param
0352: .getRawCookie());
0353: }
0354:
0355: if (postdataavailable) {
0356: uconn.setDoOutput(true);
0357: OutputStreamWriter wr = new OutputStreamWriter(uconn
0358: .getOutputStream());
0359: wr.write(new String(data));
0360: wr.flush();
0361: }
0362:
0363: // temp stream to hold the data
0364: ByteArrayOutputStream bos = new ByteArrayOutputStream();
0365: try {
0366: breader = new BufferedReader(new InputStreamReader(
0367: uconn.getInputStream()));
0368: } catch (Exception e) {
0369: System.out.println(e.getMessage());
0370: Log.debugu(Param.getString("perr.10",
0371: "ERR: Session Timeout.Please login again"));
0372: throw new SessionTimeoutException();
0373: }
0374: int contentLength = uconn.getContentLength();
0375:
0376: int respCode = ((HttpURLConnection) uconn)
0377: .getResponseCode();
0378: if (respCode == 504) {
0379: Log.debugu(Param.getString("perr.10",
0380: "ERR: Session Timeout.Please login again"));
0381: throw new SessionTimeoutException();
0382: }
0383: if (respCode == 302) {
0384: String location = ((HttpURLConnection) uconn)
0385: .getHeaderField("Location");
0386: if (location != null
0387: && location
0388: .indexOf(Param.getPlatformLoginURL()) != -1) {
0389: Log.debugu(Param.getString("perr.10",
0390: "ERR: Session Timeout.Please login again"));
0391: throw new SessionTimeoutException();
0392: }
0393: }
0394:
0395: if (displayProgressbar) {
0396: // create a dialog
0397: diag = new ReadProgressDialog();
0398: }
0399:
0400: String line = "";
0401: int bytesread = 0, percentdone = 0;
0402: do {
0403: line = (String) breader.readLine();
0404: if (line == null)
0405: break;
0406: line += "\r\n";
0407: bytesread = line.length();
0408: percentdone = Math.abs(bytesread / contentLength * 100);
0409: if (displayProgressbar)
0410: diag.setProgress(percentdone);
0411: bos.write(line.getBytes());
0412: } while (line != null);
0413:
0414: if (displayProgressbar)
0415: diag.setProgress(100);
0416:
0417: // temp stream to hold stream for PropertyResourceBundle
0418: if (bos.size() == 0) {
0419: System.out.println("bytes read => 0 bytes");
0420: return null;
0421: }
0422:
0423: arr = bos.toByteArray();
0424: try {
0425: breader.close();
0426: bos.close();
0427: } catch (Exception ignore) {
0428: }
0429: } catch (Exception e) {
0430: e.printStackTrace();
0431: System.out.println("exception " + e.getMessage());
0432: throw e;
0433: } finally {
0434: if (displayProgressbar) {
0435: System.out.println("Dispose progress dialog box");
0436: diag.close();
0437: }
0438:
0439: }
0440:
0441: return arr;
0442:
0443: }
0444:
0445: /**
0446: * loadResourceBundle displaying UI
0447: * Reads the default and custom resource bundle
0448: * Using sockets rather than URLconnection. Reason: Cookie value that is being
0449: * set as a part of the connection object gets URL encoded. The corresponding
0450: * URL decoding does not happen at the gateway.
0451: *
0452: */
0453: public static void loadResourceBundle(boolean displayUI)
0454: throws Exception {
0455: System.out.println("Loading Resource Bundle");
0456: String command = "?command=loadResourceBundle";
0457:
0458: byte[] arr = sendMsgtoServlet(command, displayUI, false, null);
0459: ByteArrayInputStream bis = new ByteArrayInputStream(arr);
0460:
0461: System.out.println("Resource Stream ");
0462: System.out.println("=================");
0463: System.out.println(new String(arr));
0464:
0465: resources = new PropertyResourceBundle(bis);
0466:
0467: // Read custom property file if any
0468: if (propertyfile != null) {
0469: command += "&propertyfile=" + propertyfile;
0470: arr = sendMsgtoServlet(command, displayUI, false, null);
0471: bis = new ByteArrayInputStream(arr);
0472:
0473: System.out.println("Custom Resource Stream ");
0474: System.out.println("=================");
0475: System.out.println(new String(arr));
0476:
0477: customresources = new PropertyResourceBundle(bis);
0478: }
0479: return;
0480:
0481: }
0482:
0483: public static String getString(String key, String def) {
0484: try {
0485: String temp = null;
0486: if (customresources != null)
0487: temp = customresources.getString(key);
0488:
0489: // if not in customresources, check the default one
0490: if (temp == null && resources != null)
0491: temp = resources.getString(key);
0492:
0493: return (temp == null || temp.equals("")) ? def : temp;
0494: } catch (Exception e) {
0495: return def;
0496: }
0497: }
0498:
0499: /*********************************************
0500: *
0501: * Getter Functions
0502: *
0503: *********************************************/
0504:
0505: public static String getProxyMethod() {
0506: return proxyMethod;
0507: }
0508:
0509: public static String getOS() {
0510: return os;
0511: }
0512:
0513: public static String getGatewayHost() {
0514: return gatewayHost;
0515: }
0516:
0517: public static int getGatewayPort() {
0518: return gatewayPort;
0519: }
0520:
0521: public static String getGatewayMode() {
0522: return gatewayMode;
0523: }
0524:
0525: public static String getVendor() {
0526: return vmVendor;
0527: }
0528:
0529: public static String getVMVersion() {
0530: return vmVersion;
0531: }
0532:
0533: public static BrowserType getBrowserType() {
0534: return browserType;
0535: }
0536:
0537: public static String getClientProxyHostFTP() {
0538: return clientProxyHostFTP;
0539: }
0540:
0541: public static int getClientProxyPortFTP() {
0542: return clientProxyPortFTP;
0543: }
0544:
0545: public static String getClientProxyHostGopher() {
0546: return clientProxyHostGopher;
0547: }
0548:
0549: public static int getClientProxyPortGopher() {
0550: return clientProxyPortGopher;
0551: }
0552:
0553: public static String getClientProxyHostHTTP() {
0554: return clientProxyHostHTTP;
0555: }
0556:
0557: public static int getClientProxyPortHTTP() {
0558: return clientProxyPortHTTP;
0559: }
0560:
0561: public static String getClientProxyHostSSL() {
0562: return clientProxyHostSSL;
0563: }
0564:
0565: public static int getClientProxyPortSSL() {
0566: return clientProxyPortSSL;
0567: }
0568:
0569: public static String getClientPACContent() {
0570: return clientPACContent;
0571: }
0572:
0573: public static String getNewPACContent() {
0574: return newPACContent;
0575: }
0576:
0577: public static String getRules() {
0578: return rules;
0579: }
0580:
0581: public static Cookie getCookie() {
0582: return cookie;
0583: }
0584:
0585: public static String getDownloadMgrPort() {
0586: return downloadMgrPort;
0587: }
0588:
0589: public static String getPacfileLocation() {
0590: return pacfileLocation;
0591: }
0592:
0593: public static boolean isPacfileNeedToStore() {
0594: return true;
0595: }
0596:
0597: public static String getSessionId() {
0598: return sesId;
0599: }
0600:
0601: public static String getRawCookie() {
0602: return rawCookie;
0603: }
0604:
0605: public static String getPropertyFile() {
0606: return propertyfile;
0607: }
0608:
0609: public static String getLang() {
0610: String lang = "en";
0611: if (resources != null)
0612: lang = resources.getString("lang");
0613:
0614: return lang;
0615: }
0616:
0617: public static String getBindIP() {
0618: return bindIP;
0619: }
0620:
0621: public static int getBindPort() {
0622: return Integer.parseInt(bindPort);
0623: }
0624:
0625: /**
0626: * getClientProxyHost - Call this function to find out the
0627: * enterprise proxy host and port that should
0628: * be used for the current configuration of
0629: * gateway ( HTTP or SSL )
0630: */
0631: public static String getClientProxyHost() {
0632:
0633: // Loop through proxyList and get a valid proxy
0634: // You can configure more than one proxy to be used like primary or alternate
0635: // in your browser.
0636: // If for some reason the primary proxy is not valid, the alternate ones would
0637: // be used.
0638: if (proxyList != null) {
0639: for (int i = 0; i < proxyList.size(); i++) {
0640: String proxyLine = (String) proxyList.get(i);
0641: String host, port;
0642: int delim = proxyLine.indexOf(":");
0643: host = proxyLine.substring(0, delim);
0644: port = proxyLine.substring(delim + 1, proxyLine
0645: .length());
0646: if (BrowserHelper.validateProxy()) {
0647: setClientProxy(host, port);
0648: break;
0649: }
0650:
0651: }
0652: }
0653:
0654: if (getGatewayMode().equals("secure"))
0655: return clientProxyHostSSL;
0656: else
0657: return clientProxyHostHTTP;
0658: }
0659:
0660: /**
0661: * getClientProxyPort - Call this function to find out the enterprise
0662: * proxy port that should be used for the current
0663: * configuration of gateway (HTTP or SSL ). If
0664: * the port value is missing, the default values
0665: * ( 443 or 80 ) would be returned
0666: */
0667: public static int getClientProxyPort() {
0668: if (getGatewayMode().equals("secure"))
0669: return (clientProxyPortSSL == -1) ? 443
0670: : clientProxyPortSSL;
0671: else
0672: return (clientProxyPortHTTP == -1) ? 80
0673: : clientProxyPortHTTP;
0674: }
0675:
0676: public static String getSSLProvider() {
0677: return sslProvider;
0678: }
0679:
0680: public static String getServletURL() {
0681: return servletURL;
0682: }
0683:
0684: public static String getGatewayURL() {
0685: return gwURL;
0686: }
0687:
0688: /**
0689: * getUseClientProxy
0690: *
0691: * This function based on the gatewayMode and client proxy
0692: * settings returns true or false. If gatewayMode is SSL, it means
0693: * that proxylet would establish a secure channel with the gateway
0694: * In that case, we need to look up clientProxyHostSSL to determine
0695: * if we need to use proxy server for SSL based transactions.
0696: * @return
0697: */
0698: public static boolean getUseClientProxy() {
0699: boolean useProxy = false;
0700:
0701: if (getGatewayMode().equals("secure")) {
0702: if (clientProxyHostSSL != null)
0703: useProxy = true;
0704: } else {
0705: if (clientProxyHostHTTP != null)
0706: useProxy = true;
0707: }
0708:
0709: return useProxy;
0710: }
0711:
0712: public static String getTempDirectory() {
0713: System.out.println("tmpDirectory = " + tmpDirectory);
0714: if (tmpDirectory == null || tmpDirectory.trim().length() == 0)
0715: createTemporaryDirectory();
0716:
0717: return tmpDirectory;
0718: }
0719:
0720: /*********************************************
0721: *
0722: * Setter Functions
0723: *
0724: *********************************************/
0725:
0726: public static void setBindPort(String port) {
0727: bindPort = port;
0728: }
0729:
0730: public static void setBindIP(String ip) {
0731: bindIP = ip;
0732: }
0733:
0734: public static void setProxyMethod(String method) {
0735: proxyMethod = method;
0736: }
0737:
0738: public static void setGatewayUrl(String g) {
0739: gwURL = g;
0740: }
0741:
0742: public static void setServletUrl(String s) {
0743: servletURL = s;
0744: }
0745:
0746: public static void setGatewayHost(String host) {
0747: gatewayHost = host;
0748: }
0749:
0750: public static void setGatewayPort(String port) {
0751: gatewayPort = Integer.parseInt(port);
0752: }
0753:
0754: public static void setGatewayMode(String mode) {
0755: gatewayMode = mode;
0756: }
0757:
0758: public static void setRules(String r) {
0759: rules = r;
0760: }
0761:
0762: public static void setPacfileLoc() {
0763: try {
0764: pacfileLocation = (File.createTempFile("proxylet", ".pac",
0765: new File(getTempDirectory()))).toString();
0766: } catch (IOException e) {
0767: e.printStackTrace();
0768: System.out
0769: .println("Unable to get temporary location for pacfile");
0770: }
0771: }
0772:
0773: public static void setBrowserType(BrowserType b) {
0774: browserType = b;
0775: }
0776:
0777: public static void setCookie(Cookie rcookie) {
0778: cookie = rcookie;
0779: }
0780:
0781: public static void setRawCookie(String rc) {
0782: rawCookie = rc;
0783: }
0784:
0785: public static void setVMVersion() {
0786: String temp = System.getProperty("java.version");
0787: char major = temp.charAt(2);
0788: if (major >= '2')
0789: vmVersion = "Java_2";
0790: else
0791: vmVersion = "Java_1";
0792:
0793: }
0794:
0795: public static void setVendor() {
0796: String temp = System.getProperty("java.vendor");
0797: if (temp == null) {
0798: vmVendor = "Not_Known";
0799: }
0800:
0801: if (temp.startsWith("Microsoft"))
0802: vmVendor = "Microsoft";
0803: else if (temp.startsWith("Sun"))
0804: vmVendor = "Sun";
0805: else if (temp.startsWith("APPLE"))
0806: vmVendor = "MRJ";
0807: else
0808: vmVendor = "Not_Known";
0809:
0810: }
0811:
0812: public static void setSSLProvider() {
0813: if (getGatewayMode().equalsIgnoreCase("secure")) {
0814: if (getVMVersion().equals("Java_2") && jsselibrarypresent())
0815: sslProvider = "JSSE";
0816: else
0817: sslProvider = "KSSL";
0818: return;
0819: }
0820:
0821: sslProvider = "Plain";
0822: }
0823:
0824: private static boolean jsselibrarypresent() {
0825: try {
0826: Class javaxc = Class.forName("javax.net.ssl.SSLSocket");
0827: } catch (ClassNotFoundException e) {
0828: return false;
0829: }
0830:
0831: return true;
0832: }
0833:
0834: /**
0835: * setClientProxyInfo
0836: *
0837: * This function parses the proxy server value based on the proxy type
0838: * and sets the clientproxyhost and clientproxyport fields.
0839: *
0840: * If mode is MANUAL, the proxyserver would be in the format
0841: * http://host:port;https://host:port;ftp://....etc. If mode is DIRECT,
0842: * do not use any proxy
0843: *
0844: * @param mode DIRECT or MANUAL
0845: * @param proxyModedata
0846: */
0847: public static void setClientProxyInfo(String mode,
0848: String proxyModedata) {
0849: mode = mode.trim();
0850:
0851: try {
0852: if (mode.equalsIgnoreCase("DIRECT")
0853: || mode.equalsIgnoreCase("PAC")) {
0854: proxyMethod = "DIRECT";
0855: return;
0856: }
0857:
0858: if (proxyModedata.toUpperCase().indexOf("HTTP") != -1) {
0859: StringTokenizer proxies = new StringTokenizer(
0860: proxyModedata, ";");
0861: while (proxies.hasMoreElements()) {
0862: String proxy = proxies.nextToken();
0863: int colon = 0;
0864: if (proxy.toUpperCase().indexOf("HTTPS") != -1) {
0865: // strip the protocol
0866: colon = proxy.indexOf("//");
0867: proxy = proxy.substring(colon + 2, proxy
0868: .length());
0869:
0870: colon = proxy.lastIndexOf(":");
0871: if (colon == -1) {
0872: clientProxyPortSSL = 443;
0873: colon = proxy.length();
0874: } else
0875: clientProxyPortSSL = Integer.parseInt(proxy
0876: .substring(colon + 1, proxy
0877: .length()));
0878: clientProxyHostSSL = proxy.substring(0, colon);
0879:
0880: } else if (proxy.toUpperCase().indexOf("HTTP") != -1) {
0881: // strip the protocol
0882: colon = proxy.indexOf("//");
0883: proxy = proxy.substring(colon + 2, proxy
0884: .length());
0885:
0886: colon = proxy.lastIndexOf(":");
0887: if (colon == -1) {
0888: colon = proxy.length();
0889: clientProxyPortHTTP = 80;
0890: } else {
0891: clientProxyPortHTTP = Integer
0892: .parseInt(proxy.substring(
0893: colon + 1, proxy.length()));
0894: }
0895:
0896: clientProxyHostHTTP = proxy.substring(0, colon);
0897:
0898: } else if (proxy.toUpperCase().indexOf("FTP") != -1) {
0899: // strip the protocol
0900: colon = proxy.indexOf("//");
0901: proxy = proxy.substring(colon + 2, proxy
0902: .length());
0903:
0904: colon = proxy.lastIndexOf(":");
0905: if (colon == -1) {
0906: colon = proxy.length();
0907: clientProxyPortFTP = 21;
0908: } else {
0909: clientProxyPortFTP = Integer.parseInt(proxy
0910: .substring(colon + 1, proxy
0911: .length()));
0912: }
0913:
0914: clientProxyHostFTP = proxy.substring(0, colon);
0915: } else if (proxy.toUpperCase().indexOf("GOPHER") != -1) {
0916: // strip the protocol
0917: colon = proxy.indexOf("//");
0918: proxy = proxy.substring(colon + 2, proxy
0919: .length());
0920:
0921: colon = proxy.lastIndexOf(":");
0922: if (colon == -1) {
0923: colon = proxy.length();
0924: clientProxyPortGopher = 70;
0925: } else {
0926: clientProxyPortGopher = Integer
0927: .parseInt(proxy.substring(
0928: colon + 1, proxy.length()));
0929: }
0930:
0931: clientProxyHostGopher = proxy.substring(0,
0932: colon);
0933: }
0934:
0935: }
0936: } else if (proxyModedata.toUpperCase().indexOf("//") == -1) {
0937: // check if its in the format localhost:port
0938: int colon = proxyModedata.indexOf(":");
0939: clientProxyHostHTTP = proxyModedata.substring(0, colon);
0940: clientProxyPortHTTP = Integer.parseInt(proxyModedata
0941: .substring(colon + 1, proxyModedata.length()));
0942: clientProxyHostSSL = clientProxyHostHTTP;
0943: clientProxyHostFTP = clientProxyHostHTTP;
0944: clientProxyHostGopher = clientProxyHostHTTP;
0945: clientProxyPortSSL = clientProxyPortHTTP;
0946: clientProxyPortFTP = clientProxyPortHTTP;
0947: clientProxyPortGopher = clientProxyPortHTTP;
0948: }
0949:
0950: } catch (Exception e) {
0951: Log
0952: .info("Unknown Error: Failed to process client proxy data");
0953: }
0954: }
0955:
0956: /**
0957: * setBypassList
0958: *
0959: * Parses the list of address and stores them in a Vector.
0960:
0961: * @param bypasslist list of hosts ( address seperated by a comma )that should
0962: * be left out from being proxied
0963: */
0964: public static void setByPassList(String bypasslist) {
0965: if (bypasslist == null || bypasslist.equals(""))
0966: return;
0967:
0968: proxyBypassList = new Vector();
0969: StringTokenizer listarr = new StringTokenizer(bypasslist, ",");
0970: while (listarr.hasMoreElements()) {
0971: proxyBypassList.add(listarr.nextToken());
0972: }
0973: }
0974:
0975: public static boolean maybeProxied(String host) {
0976: Object[] proxyignore = proxyBypassList.toArray();
0977: int len = proxyignore.length;
0978: for (int i = 0; i < len; i++) {
0979: if (host.startsWith((String) proxyignore[i])) {
0980: return false;
0981: }
0982: }
0983:
0984: return true;
0985: }
0986:
0987: /**
0988: * setClientProxyHTTP sets the enterprise proxy host and port
0989: * to be used for all HTTP transfers
0990: */
0991: public static void setClientProxyHTTP(String host, String port) {
0992: clientProxyHostHTTP = host;
0993: clientProxyPortHTTP = Integer.parseInt(port);
0994: }
0995:
0996: /**
0997: * setClientProxySSL sets the enterprise proxy host and port
0998: * to be used for all SSL transfers
0999: */
1000: public static void setClientProxySSL(String host, String port) {
1001: clientProxyHostSSL = host;
1002: clientProxyPortSSL = Integer.parseInt(port);
1003: }
1004:
1005: /**
1006: * setClientProxy This function sets the host and port value
1007: * of the enterprise proxy based on the current
1008: * configuration of gateway ( HTTP or SSL )
1009: */
1010: public static void setClientProxy(String host, String port) {
1011: if (gatewayMode.equals("secure")) {
1012: clientProxyHostSSL = host;
1013: clientProxyPortSSL = Integer.parseInt(port);
1014: } else {
1015: clientProxyHostHTTP = host;
1016: clientProxyPortHTTP = Integer.parseInt(port);
1017: }
1018: }
1019:
1020: public static void setClientProxyList(Vector pl) {
1021: proxyList = pl;
1022: }
1023:
1024: public static void setClientPACContent(String pacContent) {
1025: clientPACContent = pacContent;
1026: }
1027:
1028: public static void setNewPACContent(String pacContent) {
1029: newPACContent = pacContent;
1030: }
1031:
1032: public static boolean isProxyletRunning() {
1033: Socket s = null;
1034: boolean isrunning = true;
1035: try {
1036: s = new Socket("127.0.0.1", getBindPort());
1037: } catch (Exception e) {
1038: System.out.println(e.getMessage());
1039: isrunning = false;
1040: } finally {
1041: try {
1042: if (s != null)
1043: s.close();
1044: } catch (Exception ignore) {
1045: }
1046: }
1047: return isrunning;
1048: }
1049:
1050: /**
1051: * sendMsgToSelf sends a message to the local server
1052: * @param port The port on which the server is listening on local machine
1053: * @param command Msg to be sent
1054: */
1055:
1056: public static void sendMsgToSelf(String port, String command,
1057: String param) {
1058: Socket s = null;
1059: try {
1060: if (param != null) {
1061: String hostname = "localhost";
1062: String data = param;
1063: s = new Socket(hostname, new Integer(port).intValue());
1064:
1065: //Send header
1066: String path = "/" + command;
1067: BufferedWriter wr = new BufferedWriter(
1068: new OutputStreamWriter(s.getOutputStream(),
1069: "UTF8"));
1070: wr.write("POST " + path + " HTTP/1.0\r\n");
1071: wr.write("Content-Length: " + data.length() + "\r\n");
1072: wr
1073: .write("Content-Type: application/x-www-form-urlencoded\r\n");
1074: wr.write("\r\n");
1075:
1076: // Send data
1077: wr.write(data);
1078: wr.flush();
1079: wr.close();
1080:
1081: } else {
1082: s = new Socket("localhost", new Integer(port)
1083: .intValue());
1084: OutputStream out = s.getOutputStream();
1085: String msg = command + " localhost:" + port
1086: + " HTTP/1.0 \n\r\n\r";
1087: out.write(msg.getBytes());
1088: out.close();
1089: s.close();
1090: }
1091:
1092: } catch (IOException e) {
1093: System.out.println(e.getMessage());
1094: }
1095: }
1096:
1097: private static void createTemporaryDirectory() {
1098: tmpDirectory = System.getProperty("java.io.tmpdir");
1099: while (true) {
1100: String randomSuffix = (new Integer(
1101: (int) (Math.random() * 1000))).toString();
1102: tmpDirectory = System.getProperty("java.io.tmpdir")
1103: + "proxylet" + randomSuffix + File.separator;
1104: System.out.println("tmpDirectory = " + tmpDirectory);
1105: try {
1106: File tmpDir = new File(tmpDirectory);
1107: if (!tmpDir.exists()) {
1108: tmpDir.mkdir();
1109: break;
1110: }
1111: } catch (Exception e) {
1112: e.printStackTrace();
1113: System.out
1114: .println("could not create tmp directory with proxylet suffix.. "
1115: + "using default tmp dir.. "
1116: + e.getMessage());
1117: break;
1118: }
1119: }
1120: }
1121:
1122: public static void cleanupPreviousSessionsProxyletFiles() {
1123: System.out.println("inside Cleanup routine");
1124: try {
1125: String tempdirectory = System.getProperty("java.io.tmpdir");
1126:
1127: //get all the proxylet related files from previous session..
1128: //all these files/directories start with 'proxylet' prefix..
1129:
1130: File file = new File(tempdirectory);
1131: if (file.isDirectory()) {
1132: File[] flist = file.listFiles();
1133: for (int i = 0; i < flist.length; i++) {
1134: File fle = flist[i];
1135: if (fle.exists()
1136: && fle.getName().toLowerCase().startsWith(
1137: "proxylet"))
1138: recursivelyCleanupDirectory(fle, true);
1139: }
1140: }
1141: } catch (Exception ignore) {
1142: //ignore
1143: }
1144: }
1145:
1146: public static void setProxyletFilesForCleanup() {
1147:
1148: if (tmpDirectory.toLowerCase().indexOf("proxylet") != -1)
1149: recursivelyCleanupDirectory(new File(tmpDirectory), false);
1150: else {
1151: File tmpDir = new File(tmpDirectory);
1152: File[] flist = tmpDir.listFiles();
1153:
1154: for (int i = 0; i < flist.length; i++) {
1155: File f = flist[i];
1156: if (f.exists()
1157: && f.getName().toLowerCase().startsWith(
1158: "proxylet"))
1159: recursivelyCleanupDirectory(flist[i], false);
1160: }
1161: }
1162: }
1163:
1164: private static void recursivelyCleanupDirectory(File file,
1165: boolean cleanupNow) {
1166: if (file.isDirectory()) {
1167: File[] flist = file.listFiles();
1168: for (int i = 0; i < flist.length; i++) {
1169: File f = flist[i];
1170: if (f.exists())
1171: recursivelyCleanupDirectory(f, cleanupNow);
1172: }
1173: }
1174:
1175: System.out.println("marked file for delete = "
1176: + file.getAbsolutePath());
1177:
1178: if (cleanupNow)
1179: file.delete();
1180: else
1181: file.deleteOnExit();
1182: }
1183: }
|