| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.util; |
| 16 |
|
| 17 |
import java.net.MalformedURLException; |
| 18 |
import java.net.URL; |
| 19 |
|
| 20 |
/** |
| 21 |
* This class represents the URLs needed by various Globus services, |
| 22 |
* including: |
| 23 |
* <ul> |
| 24 |
* <li> GASS </li> |
| 25 |
* <li> GRAM </li> |
| 26 |
* <li> FTP </li> |
| 27 |
* <li> GSIFTP </li> |
| 28 |
* </ul> |
| 29 |
* |
| 30 |
* This class is not extended from URL since it is not intended to do stream |
| 31 |
* handling. Instead it is primarily for the parsing of Globus URLs and is |
| 32 |
* able to handle the extraction of user names and passwords from the URL |
| 33 |
* as well. It also can parse literal IPv6 addresses (as per RFC 2732). |
| 34 |
*/ |
| 35 |
public class GlobusURL { |
| 36 |
|
| 37 |
protected String protocol = null; |
| 38 |
protected String host = null; |
| 39 |
protected String urlPath = null; |
| 40 |
protected String user = null; |
| 41 |
protected String pwd = null; |
| 42 |
protected String url = null; |
| 43 |
protected int port = -1; |
| 44 |
|
| 45 |
/** |
| 46 |
* Parses the url and extracts the url parts. |
| 47 |
* |
| 48 |
* @param url the url to parse. |
| 49 |
* @throws MalformedURLException if the url |
| 50 |
* is malformed. |
| 51 |
*/ |
| 52 |
public GlobusURL(String url) throws MalformedURLException { |
| 53 |
|
| 54 |
int p1, p2; |
| 55 |
|
| 56 |
url = url.trim(); |
| 57 |
p1 = url.indexOf("://"); |
| 58 |
if (p1 == -1) { |
| 59 |
throw new MalformedURLException("Missing '[protocol]://'"); |
| 60 |
} |
| 61 |
|
| 62 |
protocol = url.substring(0, p1).toLowerCase(); |
| 63 |
p1 += 3; |
| 64 |
|
| 65 |
String base = null; |
| 66 |
|
| 67 |
p2 = url.indexOf('/', p1); |
| 68 |
|
| 69 |
if (p2 == -1) { |
| 70 |
/*throw new MalformedURLException("Missing '/' at [host]:[port]/");*/ |
| 71 |
|
| 72 |
base = url.substring(p1); |
| 73 |
urlPath = null; |
| 74 |
|
| 75 |
} else { |
| 76 |
|
| 77 |
base = url.substring(p1, p2); |
| 78 |
|
| 79 |
// this is after / |
| 80 |
p2++; |
| 81 |
if (p2 != url.length()) { |
| 82 |
urlPath = url.substring(p2); |
| 83 |
} else { |
| 84 |
urlPath = null; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// this is [user]:[pwd]@[host]:[port] |
| 89 |
p1 = base.indexOf('@'); |
| 90 |
|
| 91 |
if (p1 == -1) { |
| 92 |
parseHostPort(base); |
| 93 |
} else { |
| 94 |
parseUserPwd( base.substring(0, p1) ); |
| 95 |
parseHostPort( base.substring(p1+1) ); |
| 96 |
} |
| 97 |
|
| 98 |
if (port == -1) { |
| 99 |
port = getPort(protocol); |
| 100 |
} |
| 101 |
|
| 102 |
if (protocol.equals("ftp") && user == null && pwd == null) { |
| 103 |
user = "anonymous"; |
| 104 |
pwd = "[email protected]"; |
| 105 |
} |
| 106 |
|
| 107 |
this.url = url; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Creates a GlobusURL instance from URL instance. |
| 112 |
* <BR><B>Note: </B><I>Not all the url parts are |
| 113 |
* copied.</I> |
| 114 |
*/ |
| 115 |
public GlobusURL(URL url) { |
| 116 |
// TODO: does not handle the password:user spec |
| 117 |
protocol = url.getProtocol(); |
| 118 |
host = url.getHost(); |
| 119 |
port = url.getPort(); |
| 120 |
urlPath = url.getFile(); |
| 121 |
} |
| 122 |
|
| 123 |
public static int getPort(String protocol) { |
| 124 |
if (protocol.equals("ftp")) { |
| 125 |
return 21; |
| 126 |
} else if (protocol.equals("gsiftp") || |
| 127 |
protocol.equals("gridftp")) { |
| 128 |
return 2811; |
| 129 |
} else if (protocol.equals("http")) { |
| 130 |
return 80; |
| 131 |
} else if (protocol.equals("https")) { |
| 132 |
return 443; |
| 133 |
} else if (protocol.equals("ldap")) { |
| 134 |
return 389; |
| 135 |
} else if (protocol.equals("ldaps")) { |
| 136 |
return 636; |
| 137 |
} else { |
| 138 |
return -1; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
private void parseHostPort(String str) throws MalformedURLException { |
| 143 |
int start = 0; |
| 144 |
|
| 145 |
if (str.length() > 0 && str.charAt(0) == '[') { |
| 146 |
start = str.indexOf(']'); |
| 147 |
if (start == -1) { |
| 148 |
throw new MalformedURLException( |
| 149 |
"Missing ']' in IPv6 address: " + str |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
int p1 = str.indexOf(':', start); |
| 155 |
|
| 156 |
if (p1 == -1) { |
| 157 |
host = str; |
| 158 |
} else { |
| 159 |
host = str.substring(0, p1); |
| 160 |
String pp = str.substring(p1+1); |
| 161 |
try { |
| 162 |
port = Integer.parseInt(pp); |
| 163 |
} catch(NumberFormatException e) { |
| 164 |
throw new MalformedURLException("Invalid port number: " + pp); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
private void parseUserPwd(String str) { |
| 170 |
int p1; |
| 171 |
|
| 172 |
p1 = str.indexOf(':'); |
| 173 |
|
| 174 |
if (p1 == -1) { |
| 175 |
user = Util.decode(str); |
| 176 |
} else { |
| 177 |
user = Util.decode(str.substring(0, p1)); |
| 178 |
pwd = Util.decode(str.substring(p1+1)); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Returns the string representation of |
| 184 |
* an url. |
| 185 |
* |
| 186 |
* @return the url as string. |
| 187 |
*/ |
| 188 |
public String getURL() { |
| 189 |
return url; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Returns the protocol of an url. |
| 194 |
* |
| 195 |
* @return the protocol part of the url. |
| 196 |
*/ |
| 197 |
public String getProtocol() { |
| 198 |
return protocol; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Returns the host name of an url. |
| 203 |
* |
| 204 |
* @return the host name part of the url. |
| 205 |
*/ |
| 206 |
public String getHost() { |
| 207 |
return host; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Returns the port number of an url. |
| 212 |
* |
| 213 |
* @return the port name of the url. -1 if |
| 214 |
* the port was not specified. |
| 215 |
*/ |
| 216 |
public int getPort() { |
| 217 |
return port; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns the url path part of an url. |
| 222 |
* |
| 223 |
* @return the url path part of the url. |
| 224 |
* Returns null if the url path is |
| 225 |
* not specified. |
| 226 |
*/ |
| 227 |
public String getPath() { |
| 228 |
return urlPath; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Returns the user name of an url. |
| 233 |
* |
| 234 |
* @return the user name if present in the url, |
| 235 |
* otherwise returns null. |
| 236 |
*/ |
| 237 |
public String getUser() { |
| 238 |
return user; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Returns the password of an url. |
| 243 |
* |
| 244 |
* @return the password if present in the url, |
| 245 |
* otherwise returns null. |
| 246 |
*/ |
| 247 |
public String getPwd() { |
| 248 |
return pwd; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Compares two urls. |
| 253 |
* |
| 254 |
* @param obj could be a string representation of an url |
| 255 |
* or an instance of this class. |
| 256 |
* @return true if the urls are the same, false otherwise. |
| 257 |
*/ |
| 258 |
public boolean equals(Object obj) { |
| 259 |
GlobusURL cUrl = null; |
| 260 |
|
| 261 |
if (obj instanceof String) { |
| 262 |
try { |
| 263 |
cUrl = new GlobusURL((String)obj); |
| 264 |
} catch(MalformedURLException e) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
} else if (obj instanceof GlobusURL) { |
| 268 |
cUrl = (GlobusURL)obj; |
| 269 |
} else { |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
// do the comparison |
| 274 |
|
| 275 |
// compare ports |
| 276 |
if (getPort() != cUrl.getPort()) return false; |
| 277 |
if (!compare(getProtocol(), cUrl.getProtocol(), false)) return false; |
| 278 |
if (!compare(getHost(), cUrl.getHost(), false)) return false; |
| 279 |
if (!compare(urlPath, cUrl.urlPath, false)) return false; |
| 280 |
if (!compare(getUser(), cUrl.getUser(), false)) return false; |
| 281 |
if (!compare(getPwd(), cUrl.getPwd(), false)) return false; |
| 282 |
|
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
private boolean compare(String s1, String s2, boolean ignoreCase) { |
| 287 |
if (s1 == null) { |
| 288 |
return (s2 == null); |
| 289 |
} else if (s2 == null) { |
| 290 |
return false; |
| 291 |
} else { |
| 292 |
return (ignoreCase) ? s1.equalsIgnoreCase(s2) : s1.equals(s2); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
public int hashCode() { |
| 297 |
int value = this.port; |
| 298 |
if (this.protocol != null) { |
| 299 |
value += this.protocol.hashCode(); |
| 300 |
} |
| 301 |
if (this.host != null) { |
| 302 |
value += this.host.hashCode(); |
| 303 |
} |
| 304 |
if (this.urlPath != null) { |
| 305 |
value += this.urlPath.hashCode(); |
| 306 |
} |
| 307 |
if (this.user != null) { |
| 308 |
value += this.user.hashCode(); |
| 309 |
} |
| 310 |
if (this.pwd != null) { |
| 311 |
value += this.pwd.hashCode(); |
| 312 |
} |
| 313 |
return value; |
| 314 |
} |
| 315 |
|
| 316 |
public String toString() { |
| 317 |
StringBuffer info = new StringBuffer(); |
| 318 |
info.append("Protocol : " + protocol + "\n"); |
| 319 |
info.append("Host name : " + host + "\n"); |
| 320 |
info.append("Port number : " + port + "\n"); |
| 321 |
info.append("Url path : " + urlPath + "\n"); |
| 322 |
info.append("User : " + user + "\n"); |
| 323 |
info.append("Pwd : " + pwd + "\n"); |
| 324 |
return info.toString(); |
| 325 |
} |
| 326 |
|
| 327 |
} |