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: }
|