1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
// URL.java - A Uniform Resource Locator.
/* Copyright (C) 1999 Cygnus Solutions
This file is part of libgcj.
This software is copyrighted work licensed under the terms of the
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
package java.net;
import java.io.*;
import java.util.Hashtable;
import java.util.StringTokenizer;
/**
* @author Warren Levy <warrenl@cygnus.com>
* @date March 4, 1999.
*/
/**
* Written using on-line Java Platform 1.2 API Specification, as well
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
* Status: Believed complete and correct.
*/
public final class URL implements Serializable
{
private String protocol;
private String host;
private int port = -1; // Initialize for constructor using context.
private String file;
private String ref;
private URLStreamHandler handler;
private static Hashtable handlers = new Hashtable();
private static URLStreamHandlerFactory factory;
public URL(String protocol, String host, int port, String file)
throws MalformedURLException
{
this(protocol, host, port, file, null);
}
public URL(String protocol, String host, String file)
throws MalformedURLException
{
this(protocol, host, -1, file, null);
}
// JDK1.2
public URL(String protocol, String host, int port, String file,
URLStreamHandler handler) throws MalformedURLException
{
if (protocol == null)
throw new MalformedURLException("null protocol");
this.protocol = protocol;
if (handler != null)
{
// TODO12: Need SecurityManager.checkPermission and
// TODO12: java.net.NetPermission from JDK 1.2 to be implemented.
// Throw an exception if an extant security mgr precludes
// specifying a StreamHandler.
//
// SecurityManager s = System.getSecurityManager();
// if (s != null)
// s.checkPermission(NetPermission("specifyStreamHandler"));
this.handler = handler;
}
else
this.handler = setURLStreamHandler(protocol);
if (this.handler == null)
throw new MalformedURLException("Handler for protocol not found");
this.host = host;
this.port = port;
int hashAt = file.indexOf('#');
if (hashAt < 0)
{
this.file = file;
this.ref = null;
}
else
{
this.file = file.substring(0, hashAt);
this.ref = file.substring(hashAt + 1);
}
}
public URL(String spec) throws MalformedURLException
{
this((URL) null, spec, (URLStreamHandler) null);
}
public URL(URL context, String spec) throws MalformedURLException
{
this(context, spec, (URLStreamHandler) null);
}
// JDK1.2
public URL(URL context, String spec, URLStreamHandler handler)
throws MalformedURLException
{
/* A protocol is defined by the doc as the substring before a ':'
* as long as the ':' occurs before any '/'.
*
* If context is null, then spec must be an absolute URL.
*
* The relative URL need not specify all the components of a URL.
* If the protocol, host name, or port number is missing, the value
* is inherited from the context. A bare file component is appended
* to the context's file. The optional anchor is not inherited.
*/
// If this is an absolute URL, then ignore context completely.
// An absolute URL must have chars prior to "://" but cannot have a colon
// right after the "://". The second colon is for an optional port value
// and implies that the host from the context is used if available.
int colon;
if ((colon = spec.indexOf("://", 1)) > 0 &&
! spec.regionMatches(colon, "://:", 0, 4))
context = null;
int slash;
if ((colon = spec.indexOf(':')) > 0 &&
(colon < (slash = spec.indexOf('/')) || slash < 0))
{
// Protocol specified in spec string.
protocol = spec.substring(0, colon);
if (context != null && context.protocol.equals(protocol))
{
// The 1.2 doc specifically says these are copied to the new URL.
host = context.host;
port = context.port;
file = context.file;
}
}
else if (context != null)
{
// Protocol NOT specified in spec string.
// Use context fields (except ref) as a foundation for relative URLs.
colon = -1;
protocol = context.protocol;
host = context.host;
port = context.port;
file = context.file;
}
else // Protocol NOT specified in spec. and no context available.
throw new
MalformedURLException("Absolute URL required with null context");
if (handler != null)
{
// TODO12: Need SecurityManager.checkPermission and
// TODO12: java.net.NetPermission from JDK 1.2 to be implemented.
// Throw an exception if an extant security mgr precludes
// specifying a StreamHandler.
//
// SecurityManager s = System.getSecurityManager();
// if (s != null)
// s.checkPermission(NetPermission("specifyStreamHandler"));
this.handler = handler;
}
else
this.handler = setURLStreamHandler(protocol);
if (this.handler == null)
throw new MalformedURLException("Handler for protocol not found");
// JDK 1.2 doc for parseURL specifically states that any '#' ref
// is to be excluded by passing the 'limit' as the indexOf the '#'
// if one exists, otherwise pass the end of the string.
int hashAt = spec.indexOf('#', colon + 1);
this.handler.parseURL(this, spec, colon + 1,
hashAt < 0 ? spec.length() : hashAt);
if (hashAt >= 0)
ref = spec.substring(hashAt + 1);
}
public boolean equals(Object obj)
{
if (obj == null || ! (obj instanceof URL))
return false;
URL uObj = (URL) obj;
// This comparison is very conservative. It assumes that any
// field can be null.
return (port == uObj.port
&& ((protocol == null && uObj.protocol == null)
|| (protocol != null && protocol.equals(uObj.protocol)))
&& ((host == null && uObj.host == null)
|| (host != null && host.equals(uObj.host)))
&& ((file == null && uObj.file == null)
|| (file != null && file.equals(uObj.file)))
&& ((ref == null && uObj.ref == null)
|| (ref != null && ref.equals(uObj.ref))));
}
public final Object getContent() throws IOException
{
return openConnection().getContent();
}
public String getFile()
{
return file;
}
public String getHost()
{
return host;
}
public int getPort()
{
return port;
}
public String getProtocol()
{
return protocol;
}
public String getRef()
{
return ref;
}
public int hashCode()
{
// JCL book says this is computed using (only) the hashcodes of the
// protocol, host and file fields. Empirical evidence indicates this
// is probably XOR in JDK 1.1. In JDK 1.2 it seems to be a sum including
// the port.
//
// JDK 1.2 online doc infers that host could be null because it
// explicitly states that file cannot be null but is silent on host.
// A simple example with protocol "http" (hashcode 3213448), host null,
// file "/" (hashcode 47) produced a hashcode (3213494) which appeared
// to be the sum of the two hashcodes plus the port. Another example
// using "/index.html" for file bore this out; as well as "#" for file
// (which was reduced to "" with a hashcode of zero). A "" host also
// causes the port number and the two hashcodes to be summed.
return (protocol.hashCode() + ((host == null) ? 0 : host.hashCode()) +
port + file.hashCode());
}
public URLConnection openConnection() throws IOException
{
return handler.openConnection(this);
}
public final InputStream openStream() throws IOException
{
return openConnection().getInputStream();
}
public boolean sameFile(URL other)
{
// This comparison is very conservative. It assumes that any
// field can be null.
return (other != null
&& port == other.port
&& ((protocol == null && other.protocol == null)
|| (protocol != null && protocol.equals(other.protocol)))
&& ((host == null && other.host == null)
|| (host != null && host.equals(other.host)))
&& ((file == null && other.file == null)
|| (file != null && file.equals(other.file))));
}
protected void set(String protocol, String host, int port, String file,
String ref)
{
// TBD: Theoretically, a poorly written StreamHandler could pass an
// invalid protocol. It will cause the handler to be set to null
// thus overriding a valid handler. Callers of this method should
// be aware of this.
this.handler = setURLStreamHandler(protocol);
this.protocol = protocol;
this.port = port;
this.host = host;
this.file = file;
this.ref = ref;
}
public static synchronized void
setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
{
if (factory != null)
throw new Error("URLStreamHandlerFactory already set");
// Throw an exception if an extant security mgr precludes
// setting the factory.
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkSetFactory();
factory = fac;
}
public String toExternalForm()
{
// Identical to toString().
return handler.toExternalForm(this);
}
public String toString()
{
// Identical to toExternalForm().
return handler.toExternalForm(this);
}
private URLStreamHandler setURLStreamHandler(String protocol)
{
URLStreamHandler handler;
// See if a handler has been cached for this protocol.
if ((handler = (URLStreamHandler) handlers.get(protocol)) != null)
return handler;
// If a non-default factory has been set, use it to find the protocol.
if (factory != null)
handler = factory.createURLStreamHandler(protocol);
// Non-default factory may have returned null or a factory wasn't set.
// Use the default search algorithm to find a handler for this protocol.
if (handler == null)
{
// Get the list of packages to check and append our default handler
// to it, along with the JDK specified default as a last resort.
// Except in very unusual environments the JDK specified one shouldn't
// ever be needed (or available).
String propVal = System.getProperty("java.protocol.handler.pkgs");
propVal = (propVal == null) ? "" : (propVal + "|");
propVal = propVal + "gnu.gcj.protocol|sun.net.www.protocol";
StringTokenizer pkgPrefix = new StringTokenizer(propVal, "|");
do
{
String facName = pkgPrefix.nextToken() + "." + protocol +
".Handler";
try
{
handler =
(URLStreamHandler) Class.forName(facName).newInstance();
}
catch (Exception e)
{
// Can't instantiate; handler still null, go on to next element.
}
} while ((handler == null ||
! (handler instanceof URLStreamHandler)) &&
pkgPrefix.hasMoreTokens());
}
// Update the hashtable with the new protocol handler.
if (handler != null)
if (handler instanceof URLStreamHandler)
handlers.put(protocol, handler);
else
handler = null;
return handler;
}
}
|