001: //
002: // Copyright 09/18/01 Sun Microsystems, Inc. All Rights Reserved.
003: //
004:
005: /* This file depends on js.jar of www.mozilla.org/rhino/ the javascript engine in Java
006: This implements all the desired method to parse FindProxyForURL
007: This is a kind of singleton class , not thread safe , specailly because of the ClientIPAddr: Take care
008: Author: Rakesh Nayak
009: Date: July 2001
010: */
011: package com.sun.portal.netlet.servlet;
012:
013: import java.net.InetAddress;
014: import java.net.URLDecoder;
015: import java.util.StringTokenizer;
016: import java.util.Vector;
017:
018: import org.mozilla.javascript.Context;
019: import org.mozilla.javascript.PropertyException;
020: import org.mozilla.javascript.Scriptable;
021: import org.mozilla.javascript.ScriptableObject;
022:
023: public class EvalPAC extends ScriptableObject {
024: // this class is kind of Singleton
025: // not thread safe
026:
027: static EvalPAC global = null;;
028:
029: static Scriptable defaultInitializedScript;
030:
031: static Context context;
032:
033: String clientIPAddress = "255.255.255.255";
034:
035: public String getClassName() {
036: return "EvalPAC";
037: }
038:
039: public static boolean shExpMatch(String url, String exp) {
040: StringTokenizer stTok = new StringTokenizer(exp, "*");
041: int startPos = 0;
042: while (stTok.hasMoreTokens()) {
043: String token = stTok.nextToken();
044: int temp = url.indexOf(token, startPos);
045: if (temp < 0)
046: return false;
047: else
048: startPos = temp + token.length();
049: }
050: return true;
051:
052: }
053:
054: public String myIpAddress() {
055: return clientIPAddress;
056: }
057:
058: public static String dnsResolve(String host) {
059: String ipAddr = ""; // default as per Browser plugin
060: try {
061: ipAddr = (InetAddress.getByName(host)).getHostAddress();
062: } catch (Exception ex) {
063: }
064:
065: return ipAddr;
066:
067: }
068:
069: public static boolean isResolvable(String host) {
070: boolean result = false; // default as per Browser plugin
071: try {
072: (InetAddress.getByName(host)).getHostAddress();
073: result = true;
074: } catch (Exception ex) {
075:
076: result = false;
077: }
078:
079: return result;
080:
081: }
082:
083: public static boolean isInNet(String host, String pattern,
084: String mask) {
085: // int count= number of 255's in mask
086: int count = 0;
087: int startPos = 0;
088: while ((startPos = mask.indexOf("255", startPos + 1)) > -1)
089: count++;
090:
091: // String tokenize host and pattern with "." as delimeter
092: StringTokenizer hostTok = new StringTokenizer(host, ".");
093: StringTokenizer patternTok = new StringTokenizer(pattern, ".");
094:
095: for (int i = 0; i <= count; i++) {
096: if ((!hostTok.hasMoreTokens())
097: || (!patternTok.hasMoreTokens()))
098: return false;
099: if (!(hostTok.nextToken()).equals(patternTok.nextToken())) {
100: return false;
101: }
102: }
103: return true;
104:
105: // compare count times the tokens of host and pattern
106:
107: // default impl as per browser's Java Plugin
108: // return false;
109:
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 static EvalPAC getInstance() {
144: context = Context.enter();
145: if (global == null)
146: global = new EvalPAC();
147: // Define some global functions particular to the shell. Note
148: // that these functions are not part of ECMA.
149: String[] names = { "shExpMatch", "dnsResolve", "isResolvable",
150: "isInNet", "dnsDomainIs", "isPlainHostName",
151: "myIpAddress", "dnsDomainLevels", "localHostOrDomainIs" };
152: // isPlainHostName , dnsDomainIs , localHostOrDomainIs ,isResolvable ,
153: // isInNet , dnsResolve , myIpAddress , dnsDomainLevels , shExpMatch
154: try {
155: global.defineFunctionProperties(names,
156: com.sun.portal.netlet.servlet.EvalPAC.class,
157: ScriptableObject.DONTENUM);
158: } catch (PropertyException e) {
159: throw new Error(e.getMessage());
160: }
161: defaultInitializedScript = context.initStandardObjects(global);
162: return global;
163: }
164:
165: private EvalPAC() {
166:
167: }
168:
169: public void setIPAddress(String ipAddr) {
170: clientIPAddress = ipAddr;
171: }
172:
173: public String evaluate(String pacFileBody, Vector arguments)
174: throws Exception {
175:
176: // System.out.println( pacFileBody );
177: // System.out.println(arguments.toString() );
178:
179: /*
180: * Function func =
181: * context.compileFunction(defaultInitializedScript,pacFileBody,"userPacFile",1,null);
182: * Object result = func.call(context, defaultInitializedScript,func ,
183: * arguments.toArray());
184: */
185: String evalSt = " ;FindProxyForURL (\""
186: + arguments.elementAt(0) + "\",\""
187: + arguments.elementAt(1) + "\")";
188: Object result = context.evaluateString(
189: defaultInitializedScript, pacFileBody + evalSt,
190: "userPacFile", 1, null);
191:
192: return context.toString(result);
193: }
194:
195: public static void main(String[] args) {
196: try {
197: // String sourceFileName = args[0] ; //"/h/nsx/nsx/htdocs/eng.pac";
198: String url = args[1];
199: String host = args[2];
200: String clientIP = args[3];
201:
202: String pacFileBody = URLDecoder.decode(args[0]);
203:
204: /*
205: * FileInputStream fIn=new FileInputStream(sourceFileName);
206: *
207: * //byte data[]=new byte[in.available()]; //String pacFileBody =
208: * new String(data);
209: *
210: * String inputLine; BufferedReader in = new BufferedReader(new
211: * InputStreamReader(fIn)); while ((inputLine = in.readLine()) !=
212: * null) { pacFileBody += "\n"+ inputLine ; }
213: *
214: * //System.out.println(pacFileBody);
215: * // String methodName ="FindProxyForURL";
216: */
217: Vector arguments = new Vector();
218: arguments.add(url);
219: arguments.add(host);
220: EvalPAC t = EvalPAC.getInstance();
221: t.setIPAddress(clientIP);
222:
223: System.out.println(t.evaluate(pacFileBody, arguments));
224: Context.exit();
225: } catch (Exception ex) {
226: ex.printStackTrace();
227:
228: }
229: }
230: }
|