de.fhg.igd.util
Class URL

java.lang.Object
  extended by de.fhg.igd.util.URL
All Implemented Interfaces:
Serializable

public class URL
extends Object
implements Serializable

Represents a URL. This class serves as an alternative to the java.net.URL class. This class does not require protocol handlers for the protocol identifiers that are used with it. It is merely a container for URL strings that allows easy access to the individual components of a URL.

The rationale for this class is to have the URL parsing and handling functionality without the overhead caused by connection handlers and content handlers. From a security perspective this is desireable because there are viewer weak points that could be exploited for an attack. Certain applications, such as mobile agent systems, may want to support some protocols by means other than a connection handler.

This class parses URLs according to RFC 1738. It supports all aspects of the Common Internet Scheme Syntax: protocol identifiers, user names, passwords, host names, ports, and a scheme-specific path. The path itself is not parsed. This could be done in subclasses, if desired.

Host names are converted to lower case by this class. Passwords are not serialized.

Version:
"$Id: URL.java 1913 2007-08-08 02:41:53Z jpeters $"
Author:
Volker Roth, Jan Peters
See Also:
Serialized Form

Field Summary
private static int HOST
           
private  String host_
          The host part.
private  String passwd_
          The password part.
private static int PATH
           
private  String path_
          The scheme-specific part.
private static int PHP
           
private static int PORT
           
private  int port_
          The port.
private static int PROTO
           
private  String protocol_
          The protocol part in lower case.
private static int SLASH
           
private static int SLASH2
           
private static int UPHP
           
private  String user_
          The user part.
 
Constructor Summary
URL(String urlString)
          Creates an instance with the given URL string.
URL(String protocol, String host, int port, String path)
          Creates an URL with the given protocol identifier, host name, port, and path.
URL(String protocol, String host, String path)
          Creates an URL with the given protocol identifier, host name, and path.
URL(String protocol, String user, String passwd, String host, int port, String path)
          Creates an instance with the given values.
 
Method Summary
private  String cut(StringBuffer buf)
          Returns the string conatined in the global string buffer, clears the string buffer, and sets the given state.
 boolean equals(Object o)
          Compares this URL with the given object for equality.
 String getHost()
          Returns the host name or null if none was defined.
 String getPassword()
          Returns the password or null if none was defined.
 String getPath()
          Returns the url-path or null if none was defined.
 int getPort()
          Returns the port number or -1 if none was defined.
 String getProtocol()
          Returns the protocol identifier.
 String getUser()
          Returns the user name or null if none was defined.
 int hashCode()
          Returns the hash code of this URL.
private  void initURL(String ref)
          Creates an instance of a URL from the given string representation.
private  boolean isHost(char c)
          Tests if the given character is of the set [a-z], [A-Z], [0-9], '-', '_', '.'.
private  boolean isProto(char c)
          Tests if the given character is of the set [a-z], [A-Z], [0-9], '+', '-', '.'.
private  boolean isUser(char c)
          Tests if the given character is of the set [a-z], [A-Z], [0-9], '$', '-', '_', '.', '+', ';', '?', '&', '=', '%'.
static void main(String[] argv)
           
 void setHost(String host)
          Sets the hostname.
 void setPassword(String passwd)
          Sets the password.
 void setPath(String path)
          Sets the url-path.
 void setPort(int port)
          Sets the port number.
 void setProtocol(String protocol)
          Sets the protocol identifier.
 void setUser(String user)
          Sets the user name.
 String toString()
          Returns a string representation of this URL.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

PROTO

private static final int PROTO
See Also:
Constant Field Values

SLASH

private static final int SLASH
See Also:
Constant Field Values

SLASH2

private static final int SLASH2
See Also:
Constant Field Values

UPHP

private static final int UPHP
See Also:
Constant Field Values

PHP

private static final int PHP
See Also:
Constant Field Values

HOST

private static final int HOST
See Also:
Constant Field Values

PORT

private static final int PORT
See Also:
Constant Field Values

PATH

private static final int PATH
See Also:
Constant Field Values

protocol_

private String protocol_
The protocol part in lower case.


user_

private String user_
The user part.


passwd_

private transient String passwd_
The password part. Passwords are not serialized.


host_

private String host_
The host part.


port_

private int port_
The port.


path_

private String path_
The scheme-specific part.

Constructor Detail

URL

public URL(String urlString)
    throws MalformedURLException
Creates an instance with the given URL string.

Parameters:
urlString - The URL string.
Throws:
MalformedURLException - if url is not well-formed.

URL

public URL(String protocol,
           String host,
           String path)
Creates an URL with the given protocol identifier, host name, and path.

Parameters:
protocol - The protocol identifier.
host - The host name.
path - The path or null.

URL

public URL(String protocol,
           String host,
           int port,
           String path)
Creates an URL with the given protocol identifier, host name, port, and path.

Parameters:
protocol - The protocol identifier.
host - The host name.
port - The port number.
path - The path or null.

URL

public URL(String protocol,
           String user,
           String passwd,
           String host,
           int port,
           String path)
Creates an instance with the given values.

Parameters:
protocol - The protocol identifier.
user - The user name or null.
passwd - The password or null.
host - The host name.
port - The port.
path - The path or null.
Method Detail

getProtocol

public String getProtocol()
Returns the protocol identifier.

Returns:
The protocol.

setProtocol

public void setProtocol(String protocol)
Sets the protocol identifier.

Parameters:
protocol - The protocol identifier.
Throws:
NullPointerExcepetion - if protocol is null

getUser

public String getUser()
Returns the user name or null if none was defined.

Returns:
The user name or null.

setUser

public void setUser(String user)
Sets the user name.

Parameters:
user - The user name or null.

getPassword

public String getPassword()
Returns the password or null if none was defined.

Returns:
The password or null.

setPassword

public void setPassword(String passwd)
Sets the password.

Parameters:
passwd - The password or null.

getHost

public String getHost()
Returns the host name or null if none was defined.

Returns:
The host name or null.

setHost

public void setHost(String host)
Sets the hostname.

Parameters:
host - The host name.

getPort

public int getPort()
Returns the port number or -1 if none was defined.

Returns:
The port number or -1.

setPort

public void setPort(int port)
Sets the port number.

Parameters:
port - The port.

getPath

public String getPath()
Returns the url-path or null if none was defined.

Returns:
The url-path or null.

setPath

public void setPath(String path)
Sets the url-path.

Parameters:
path - The path or null.

toString

public String toString()
Returns a string representation of this URL.

Overrides:
toString in class Object
Returns:
The string representation.

equals

public boolean equals(Object o)
Compares this URL with the given object for equality. Both objects are equal if their string representations are equal.

Overrides:
equals in class Object
Parameters:
o - The object to compare with.
Returns:
true if this URL equals the given one.

hashCode

public int hashCode()
Returns the hash code of this URL. The hash code of this URL equals the hash code of its string representation.

Overrides:
hashCode in class Object
Returns:
The hash code of this URL.

main

public static void main(String[] argv)

initURL

private void initURL(String ref)
              throws MalformedURLException
Creates an instance of a URL from the given string representation.

Parameters:
ref - The URL in string form.
Throws:
MalformedURLException - if the given URL reference is not well-formed.

cut

private String cut(StringBuffer buf)
Returns the string conatined in the global string buffer, clears the string buffer, and sets the given state.

Parameters:
buf - The StringBuffer with the decoded data.

isProto

private boolean isProto(char c)
Tests if the given character is of the set [a-z], [A-Z], [0-9], '+', '-', '.'.

Returns:
true if c is in the set given above.

isUser

private boolean isUser(char c)
Tests if the given character is of the set [a-z], [A-Z], [0-9], '$', '-', '_', '.', '+', ';', '?', '&', '=', '%'.

Returns:
true if c is in the set given above.

isHost

private boolean isHost(char c)
Tests if the given character is of the set [a-z], [A-Z], [0-9], '-', '_', '.'.

Returns:
true if c is in the set given above.


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.