Source Code Cross Referenced for URLUtils.java in  » Web-Server » Jigsaw » org » w3c » 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 » Web Server » Jigsaw » org.w3c.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // URLUtils.java
002:        // $Id: URLUtils.java,v 1.3 2007/02/11 18:39:47 ylafon Exp $
003:        // (c) COPYRIGHT ERCIM, Keio and MIT, 2003.
004:        // Please first read the full copyright statement in file COPYRIGHT.html
005:
006:        package org.w3c.util;
007:
008:        import java.lang.reflect.Method;
009:        import java.lang.reflect.InvocationTargetException;
010:        import java.net.MalformedURLException;
011:        import java.net.URL;
012:
013:        public class URLUtils {
014:
015:            static Method url_defport;
016:
017:            static {
018:                try {
019:                    Class c = java.net.URL.class;
020:                    url_defport = c.getMethod("getDefaultPort", (Class[]) null);
021:                } catch (NoSuchMethodException ex) {
022:                    // not using a recent jdk...
023:                    url_defport = null;
024:                }
025:            }
026:
027:            /**
028:             * Checks that the protocol://host:port part of two URLs are equal.
029:             * @param u1, the first URL to check
030:             * @param u2, the second URL to check
031:             * @return a boolean, true if the protocol://host:port part of the URL
032:             * are equals, false otherwise
033:             */
034:            public static boolean equalsProtocolHostPort(URL u1, URL u2) {
035:                if ((u1 == null) || (u2 == null)) {
036:                    return false;
037:                }
038:                // check that the protocol are the same (as it impacts the 
039:                // default port check
040:                if (!u1.getProtocol().equalsIgnoreCase(u2.getProtocol())) {
041:                    return false;
042:                }
043:                // check that both hostnames are equal
044:                if (!u1.getHost().equalsIgnoreCase(u2.getHost())) {
045:                    return false;
046:                }
047:                int u1p = u1.getPort();
048:                int u2p = u2.getPort();
049:                // if port is ok, it's good!
050:                if (u1p == u2p) {
051:                    return true;
052:                } else if ((u1p > 0) && (u2p > 0)) {
053:                    return false;
054:                }
055:                // otherwise, the painful comparison of -1 and such
056:                if (url_defport != null) {
057:                    if (u1p == -1) {
058:                        try {
059:                            int u1dp;
060:                            u1dp = ((Integer) url_defport.invoke(u1,
061:                                    (Object[]) null)).intValue();
062:                            return (u2p == u1dp);
063:                        } catch (InvocationTargetException ex) {
064:                        } catch (IllegalAccessException iex) {
065:                        }
066:                    } else {
067:                        try {
068:                            int u2dp;
069:                            u2dp = ((Integer) url_defport.invoke(u2,
070:                                    (Object[]) null)).intValue();
071:                            return (u1p == u2dp);
072:                        } catch (InvocationTargetException ex) {
073:                        } catch (IllegalAccessException iex) {
074:                        }
075:                    }
076:                }
077:                // no JDK 1.4 this is becoming painful...
078:                if (u1p == -1) {
079:                    String s = u1.getProtocol();
080:                    int u1dp = 0;
081:                    if (s.equalsIgnoreCase("http")) {
082:                        u1dp = 80;
083:                    } else if (s.equalsIgnoreCase("https")) {
084:                        u1dp = 443;
085:                    } // FIXME do others?
086:                    return (u2p == u1dp);
087:                } else {
088:                    String s = u2.getProtocol();
089:                    int u2dp = 0;
090:                    if (s.equalsIgnoreCase("http")) {
091:                        u2dp = 80;
092:                    } else if (s.equalsIgnoreCase("https")) {
093:                        u2dp = 443;
094:                    } // FIXME do others?
095:                    return (u1p == u2dp);
096:                }
097:            }
098:
099:            /**
100:             * normalize an URL, 
101:             * @param u, the URL to normalize
102:             * @return a new URL, the normalized version of the parameter, or
103:             * the u URL, if something failed in the process
104:             */
105:            public static URL normalize(URL u) {
106:                String proto = u.getProtocol().toLowerCase();
107:                String host = u.getHost().toLowerCase();
108:                int port = u.getPort();
109:
110:                if (port != -1) {
111:                    if (url_defport != null) {
112:                        try {
113:                            int udp;
114:                            udp = ((Integer) url_defport.invoke(u,
115:                                    (Object[]) null)).intValue();
116:                            // we have the default, skip the port part
117:                            if (udp == port) {
118:                                port = -1;
119:                            }
120:                        } catch (InvocationTargetException ex) {
121:                        } catch (IllegalAccessException iex) {
122:                        }
123:                    } else {
124:                        switch (port) {
125:                        case 21:
126:                            if (proto.equals("ftp")) {
127:                                port = -1;
128:                            }
129:                            break;
130:                        case 80:
131:                            if (proto.equals("http")) {
132:                                port = -1;
133:                            }
134:                            break;
135:                        case 443:
136:                            if (proto.equals("https")) {
137:                                port = -1;
138:                            }
139:                            break;
140:                        }
141:                    }
142:                }
143:                try {
144:                    URL _nu;
145:                    if (port == -1) {
146:                        _nu = new URL(proto, host, u.getFile());
147:                    } else {
148:                        _nu = new URL(proto, host, port, u.getFile());
149:                    }
150:                    return _nu;
151:                } catch (MalformedURLException ex) {
152:                }
153:                return u;
154:            }
155:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.