Source Code Cross Referenced for EvalPAC.java in  » Portal » Open-Portal » com » sun » portal » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.util;
012:
013:        import java.io.ByteArrayOutputStream;
014:        import java.io.IOException;
015:        import java.io.InputStream;
016:        import java.net.InetAddress;
017:        import java.net.URL;
018:        import java.net.URLConnection;
019:        import java.util.StringTokenizer;
020:        import java.util.Vector;
021:        import java.util.logging.Level;
022:        import java.util.logging.Logger;
023:
024:        import org.mozilla.javascript.Context;
025:        import org.mozilla.javascript.PropertyException;
026:        import org.mozilla.javascript.Scriptable;
027:        import org.mozilla.javascript.ScriptableObject;
028:
029:        import com.sun.portal.log.common.PortalLogger;
030:
031:        public class EvalPAC extends ScriptableObject {
032:
033:            private static String pacFileBody = "";
034:
035:            // private static Logger logger =
036:            // Logger.getLogger("com.sun.portal.sra.rproxy");
037:            private static Logger logger = PortalLogger
038:                    .getLogger(EvalPAC.class);
039:
040:            // EvalPAC global = null;
041:            private Scriptable defaultInitializedScript;
042:
043:            private Context context;
044:
045:            private String clientIPAddress = "255.255.255.255";
046:
047:            public String getClassName() {
048:                return "EvalPAC";
049:            }
050:
051:            public boolean shExpMatch(String url, String exp) {
052:                StringTokenizer stTok = new StringTokenizer(exp, "*");
053:                int startPos = 0;
054:                while (stTok.hasMoreTokens()) {
055:                    String token = stTok.nextToken();
056:                    int temp = url.indexOf(token, startPos);
057:                    if (temp < 0)
058:                        return false;
059:                    else
060:                        startPos = temp + token.length();
061:                }
062:                return true;
063:
064:            }
065:
066:            public String myIpAddress() {
067:                return clientIPAddress;
068:            }
069:
070:            public String dnsResolve(String host) {
071:                String ipAddr = ""; // default as per Browser plugin
072:                try {
073:                    ipAddr = (InetAddress.getByName(host)).getHostAddress();
074:                } catch (Exception ex) {
075:                }
076:
077:                return ipAddr;
078:
079:            }
080:
081:            public boolean isResolvable(String host) {
082:                boolean result = false; // default as per Browser plugin
083:                try {
084:                    (InetAddress.getByName(host)).getHostAddress();
085:                    result = true;
086:                } catch (Exception ex) {
087:
088:                    result = false;
089:                }
090:
091:                return result;
092:
093:            }
094:
095:            public boolean isInNet(String host, String pattern, String mask) {
096:                // int count= number of 255's in mask
097:                int count = 0;
098:                int startPos = 0;
099:                while ((startPos = mask.indexOf("255", startPos + 1)) > -1)
100:                    count++;
101:
102:                // String tokenize host and pattern with "." as delimeter
103:                StringTokenizer hostTok = new StringTokenizer(host, ".");
104:                StringTokenizer patternTok = new StringTokenizer(pattern, ".");
105:
106:                for (int i = 0; i <= count; i++) {
107:                    if ((!hostTok.hasMoreTokens())
108:                            || (!patternTok.hasMoreTokens()))
109:                        return false;
110:                    if (!(hostTok.nextToken()).equals(patternTok.nextToken())) {
111:                        return false;
112:                    }
113:                }
114:                return true;
115:
116:                // compare count times the tokens of host and pattern
117:
118:                // default impl as per browser's Java Plugin
119:                // return false;
120:
121:            }
122:
123:            public boolean dnsDomainIs(String url, String domain) {
124:                if (url.endsWith(domain))
125:                    return true;
126:                return false;
127:
128:            }
129:
130:            public boolean localHostOrDomainIs(String host, String domain) {
131:                if (domain.startsWith(host))
132:                    return true;
133:                return false;
134:
135:            }
136:
137:            public boolean isPlainHostName(String host) {
138:                if (host.indexOf(".") > -1)
139:                    return false;
140:                return true;
141:
142:            }
143:
144:            public int dnsDomainLevels(String host) {
145:                int count = 0;
146:                int startPos = 0;
147:                while ((startPos = host.indexOf(".", startPos + 1)) > -1) {
148:                    count++;
149:                }
150:                return count;
151:
152:            }
153:
154:            public static EvalPAC getInstance(Context context) {
155:                return new EvalPAC(context);
156:            }
157:
158:            public EvalPAC(Context context) {
159:                String[] names = { "shExpMatch", "dnsResolve", "isResolvable",
160:                        "isInNet", "dnsDomainIs", "isPlainHostName",
161:                        "myIpAddress", "dnsDomainLevels", "localHostOrDomainIs" };
162:                // isPlainHostName , dnsDomainIs , localHostOrDomainIs ,isResolvable ,
163:                // isInNet , dnsResolve , myIpAddress , dnsDomainLevels , shExpMatch
164:                try {
165:                    this .defineFunctionProperties(names,
166:                            com.sun.portal.util.EvalPAC.class,
167:                            ScriptableObject.DONTENUM);
168:                } catch (PropertyException e) {
169:                    throw new Error(e.getMessage());
170:                }
171:                this .context = context;
172:                defaultInitializedScript = context.initStandardObjects(this );
173:            }
174:
175:            public void setIPAddress(String ipAddr) {
176:                clientIPAddress = ipAddr;
177:            }
178:
179:            public String evaluate(String pacFileBody, Vector arguments)
180:                    throws Exception {
181:
182:                // System.out.println( pacFileBody );
183:                // System.out.println(arguments.toString() );
184:
185:                /*
186:                 * Function func =
187:                 * context.compileFunction(defaultInitializedScript,pacFileBody,"userPacFile",1,null);
188:                 * Object result = func.call(context, defaultInitializedScript,func ,
189:                 * arguments.toArray());
190:                 */
191:                String evalSt = " ;FindProxyForURL (\""
192:                        + arguments.elementAt(0) + "\",\""
193:                        + arguments.elementAt(1) + "\")";
194:                Object result = context.evaluateString(
195:                        defaultInitializedScript, pacFileBody + evalSt,
196:                        "userPacFile", 1, null);
197:
198:                String resultToString = context.toString(result);
199:                return resultToString;
200:            }
201:
202:            public static String getProxy(String url, String host,
203:                    String clientIP) {
204:                try {
205:                    Vector arguments = new Vector();
206:                    arguments.add(url);
207:                    arguments.add(host);
208:                    Context context = Context.enter();
209:                    EvalPAC t = EvalPAC.getInstance(context);
210:                    t.setIPAddress(clientIP);
211:                    String result = t.evaluate(pacFileBody, arguments);
212:                    Context.exit();
213:                    return result;
214:                } catch (Exception ex) {
215:                    // ex.printStackTrace();
216:                    // logger.log(Level.SEVERE, "PAC : getProxy exception ", ex);
217:                    logger.log(Level.SEVERE, "PSSR_CSPU012", ex);
218:                }
219:                return null;
220:            }
221:
222:            private static byte[] readContent(URLConnection configURLConn)
223:                    throws IOException {
224:                InputStream autoConfigURLInputStream = configURLConn
225:                        .getInputStream();
226:                int length = configURLConn.getContentLength();
227:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
228:                byte content[] = new byte[2048];
229:
230:                if (length <= 0) {
231:                    int read = 0, totalRead = 0;
232:                    int left;
233:                    while (totalRead < length) {
234:                        left = length - totalRead;
235:                        read = autoConfigURLInputStream.read(content, 0,
236:                                left < content.length ? left : content.length);
237:                        if (read == -1) {
238:                            if (totalRead < length) {
239:                                continue;
240:                            }
241:                            break;
242:                        } else {
243:                            if (read > 0) {
244:                                totalRead += read;
245:                                baos.write(content, 0, read);
246:                            }
247:                        }
248:                    }
249:                    return baos.toByteArray();
250:                } else {
251:                    int numbytes;
252:                    int totalRead = 0;
253:
254:                    while (true) {
255:                        numbytes = autoConfigURLInputStream.read(content);
256:                        if (numbytes == -1) {
257:                            break;
258:                        }
259:
260:                        totalRead += numbytes;
261:
262:                        baos.write(content, 0, numbytes);
263:                    }// while loop
264:                    return baos.toByteArray();
265:                }// if/else
266:            }
267:
268:            public static void initPACFile(String pacFileLocation)
269:                    throws Exception {
270:                try {
271:                    // logger.info("PAC File URL : " + pacFileLocation);
272:                    Object[] params1 = { pacFileLocation };
273:                    logger.log(Level.INFO, "PSSR_CSPU013", params1);
274:                    URL url = new URL(pacFileLocation);
275:                    URLConnection configURLConn = url.openConnection();
276:                    configURLConn.setUseCaches(false);
277:                    configURLConn.setDoInput(true);
278:                    configURLConn.setAllowUserInteraction(false);
279:                    // contentType = configURLConn.getContentType();
280:                    byte data[] = readContent(configURLConn);
281:                    pacFileBody = new String(data, "UTF8");
282:                    // logger.info("PAC file body : \n" + pacFileBody);
283:                    Object[] params2 = { "\n", pacFileBody };
284:                    logger.log(Level.INFO, "PSSR_CSPU014", params2);
285:                } catch (Exception ex) {
286:                    // logger.log(Level.SEVERE, "Error processing pac file : " +
287:                    // pacFileLocation, ex);
288:                    Object[] params3 = { pacFileLocation, ex };
289:                    logger.log(Level.SEVERE, "PSSR_CSPU015", params3);
290:                    throw new Exception("Unable to load PAC File.");
291:                }
292:                return;
293:            }
294:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.