001: package com.sun.portal.proxylet.servlet;
002:
003: /**
004: * This file depends on js.jar of www.mozilla.org/rhino/ the javascript engine in Java
005: * This implements all the desired method to parse FindProxyForURL
006: * Author: Rakesh Nayak
007: * Date: July 2001
008: *
009: */
010:
011: import org.mozilla.javascript.*;
012:
013: import java.net.InetAddress;
014: import java.util.StringTokenizer;
015:
016: public class PacScriptEvaluator extends ScriptableObject {
017: private Scriptable defaultInitializedScript;
018: private Context context;
019: private String clientIPAddress;
020:
021: public String getClassName() {
022: return "PacScriptEvaluator";
023: }
024:
025: // ======= Setting up the environment for PAC execution ===========
026:
027: public String myIpAddress() {
028: return clientIPAddress;
029: }
030:
031: public void setMyIpAddress(String clientIP) {
032: clientIPAddress = clientIP;
033: }
034:
035: /**
036: * This function can be used by the javascript to check
037: * if the given url falls under the domain exp
038: *
039: * For example, shExpMatch("http://epubs.siam.org/sam-bin",
040: * "http://epubs.siam.org/sam-bin/dbq/toclist/*")
041: * would evaluate to true
042: */
043: public static boolean shExpMatch(String url, String exp) {
044: StringTokenizer stTok = new StringTokenizer(exp, "*");
045: int startPos = 0;
046: while (stTok.hasMoreTokens()) {
047: String token = stTok.nextToken();
048: int temp = url.indexOf(token, startPos);
049: if (temp < 0)
050: return false;
051: else
052: startPos = temp + token.length();
053: }
054: return true;
055: }
056:
057: public static String dnsResolve(String host) {
058: String ipAddr = ""; //default as per Browser plugin
059: try {
060: ipAddr = (InetAddress.getByName(host)).getHostAddress();
061: } catch (Exception ex) {
062: }
063:
064: return ipAddr;
065:
066: }
067:
068: public static boolean isResolvable(String host) {
069: boolean result = false; //default as per Browser plugin
070: try {
071: (InetAddress.getByName(host)).getHostAddress();
072: result = true;
073: } catch (Exception ex) {
074:
075: result = false;
076: }
077:
078: return result;
079:
080: }
081:
082: /**
083: * This function checks whether the host IP matches
084: * the pattern based on the given mask
085: *
086: * For example isInNet( "127.123.2.2", "127.0.0.0", "255.0.0.0")
087: * would evaluate to true.
088: */
089: public static boolean isInNet(String host, String pattern,
090: String mask) {
091: //int count= number of 255's in mask
092: int count = 0;
093: int startPos = 0;
094: while ((startPos = mask.indexOf("255", startPos + 1)) > -1)
095: count++;
096:
097: //String tokenize host and pattern with "." as delimeter
098: StringTokenizer hostTok = new StringTokenizer(host, ".");
099: StringTokenizer patternTok = new StringTokenizer(pattern, ".");
100:
101: for (int i = 0; i <= count; i++) {
102: if ((!hostTok.hasMoreTokens())
103: || (!patternTok.hasMoreTokens()))
104: return false;
105: if (!(hostTok.nextToken()).equals(patternTok.nextToken())) {
106: return false;
107: }
108: }
109: return true;
110: }
111:
112: public static boolean dnsDomainIs(String url, String domain) {
113: if (url.endsWith(domain))
114: return true;
115: return false;
116:
117: }
118:
119: public static boolean localHostOrDomainIs(String host, String domain) {
120: if (domain.startsWith(host))
121: return true;
122: return false;
123:
124: }
125:
126: public static boolean isPlainHostName(String host) {
127: if (host.indexOf(".") > -1)
128: return false;
129: return true;
130:
131: }
132:
133: public static int dnsDomainLevels(String host) {
134: int count = 0;
135: int startPos = 0;
136: while ((startPos = host.indexOf(".", startPos + 1)) > -1) {
137: count++;
138: }
139: return count;
140:
141: }
142:
143: public void init() throws Exception {
144:
145: context = Context.enter();
146: // Define some global functions particular to the shell. Note
147: // that these functions are not part of ECMA.
148: String[] names = { "shExpMatch", "dnsResolve", "isResolvable",
149: "isInNet", "dnsDomainIs", "isPlainHostName",
150: "myIpAddress", "dnsDomainLevels", "localHostOrDomainIs" };
151:
152: try {
153: defineFunctionProperties(
154: names,
155: com.sun.portal.proxylet.servlet.PacScriptEvaluator.class,
156: ScriptableObject.DONTENUM);
157: } catch (PropertyException e) {
158: throw new Error(e.getMessage());
159: }
160:
161: defaultInitializedScript = context.initStandardObjects(this );
162:
163: }
164:
165: public PacScriptEvaluator() throws Exception {
166: init();
167: }
168:
169: public String evaluate(String pacFileBody, String url, String host)
170: throws Exception {
171:
172: Object result = null;
173: try {
174:
175: String evalSt = " ;FindProxyForURL (\"" + url + "\",\""
176: + host + "\")"; // FindProxyForURL function signature
177:
178: result = context.evaluateString(defaultInitializedScript,
179: pacFileBody + evalSt, "userPacFile", 1, null);
180:
181: } catch (Exception e) {
182: e.printStackTrace();
183: }
184:
185: return context.toString(result);
186: }
187:
188: public static void main(String[] args) {
189: try {
190: String pacFileBody = getPACContent();
191: String url = args[1];
192: String host = args[2];
193: String clientIP = args[3];
194: PacScriptEvaluator t = new PacScriptEvaluator();
195: t.setMyIpAddress(clientIP);
196:
197: System.out.println(t.evaluate(pacFileBody, url, host));
198: Context.exit();
199: } catch (Exception ex) {
200: ex.printStackTrace();
201:
202: }
203: }
204:
205: private static String getPACContent() {
206: // This would be ideally created by proxylet at the start
207: // and cached in AppletPropertyMgr class
208:
209: String pacContent = "function FindProxyForURL(url, host) \n"
210: + "{" + "\n"
211: + "return \"PROXY webcache.ebay.sun.com:8080\";" + "\n"
212: + "}" + "\n";
213:
214: return pacContent;
215:
216: }
217: }
|