You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by je...@apache.org on 2001/02/23 10:43:54 UTC
cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/util MalformedURIException.java GenericURI.java
jericho 01/02/23 01:43:54
Added: src/webdav/client/src/org/apache/webdav/util
MalformedURIException.java GenericURI.java
Log:
It's experimental to parse and get the correct value for WebDAV.
The HttpURL will extend this generic URI version.
Revision Changes Path
1.1 jakarta-slide/src/webdav/client/src/org/apache/webdav/util/MalformedURIException.java
Index: MalformedURIException.java
===================================================================
/*
* $Header:
* $Revision:
* $Date:
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.webdav.util;
import java.io.IOException;
/**
* Malformed URI Exception for URI of RFC 2396.
*
* @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
*/
public class MalformedURIException extends IOException {
/**
* This Constructor.
*/
public MalformedURIException() {
}
/**
* This Constructor.
*
* @param message the exception message.
*/
public MalformedURIException(String message) {
super(message);
}
}
1.1 jakarta-slide/src/webdav/client/src/org/apache/webdav/util/GenericURI.java
Index: GenericURI.java
===================================================================
/*
* $Header:
* $Revision:
* $Date:
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.webdav.util;
/**
* URI (Uniform Resource Identifiers), RFC 2396.
* This is the generic URI version.
*
* If a pathname is a collection or directory, it always ends with "/".
*
* @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
*/
public class GenericURI {
// --------------------------------------------------------- Constructors
/**
* This Constructor
*
* @param the URI string.
*/
public GenericURI(String uri) {
this.uri = uri;
}
/**
* This Constructor
*
* @param the scheme string.
* @param the host string.
* @param the path string.
*/
public GenericURI(String scheme, String host, String path) {
if (!path.startsWith("/"))
path = "/" + path;
uri = scheme + "://" + host + path;
}
/**
* This Constructor
*
* @param the scheme string.
* @param the host string.
* @param the port number.
* @param the path string.
*/
public GenericURI(String scheme, String host, int port, String path) {
if (!path.startsWith("/"))
path = "/" + path;
uri = (port == defaultPort) ? scheme + "://" + host + path :
scheme + "://" + host + ":" + port + path;
}
// --------------------------------------------------- Instance Variables
/**
* A Uniform Resource Identifier (URI).
*/
protected String uri;
/**
* The default scheme is unset.
* IMPORTANT: need to set at the subclass.
*
* @see #setDefaultScheme(java.lang.String)
*/
protected static String defaultScheme = null;
/**
* The default port is unset.
* IMPORTANT: need to set at the subclass.
*
* @see #setDefaultPort(int)
*/
protected static int defaultPort = -1;
// -------------------------------------------------------------- Setter
/**
* Set the scheme for this Generic URI.
*
* @param the scheme for this Generic URI.
*/
public void setDefaultScheme(String scheme) {
defaultScheme = scheme.toLowerCase();
}
/**
* Set the default port number for this generic URI.
*
* @param The port number to set for this generic URI.
*/
public void setDefaultPort(int port) {
defaultPort = port;
}
// ------------------------------------------------------ Public Methods
/**
* Get the scheme for this Generic URI.
*
* @return the scheme for this Generic URI.
* @exception MalformedURIException no scheme.
*/
public String getScheme()
throws MalformedURIException {
return getScheme(uri);
}
/**
* Get the scheme of the given URI string.
*
* @param uri the URI string to get the scheme.
* @return the scheme of the given URI.
* @exception MalformedURIException no scheme.
*/
public static String getScheme(String uri)
throws MalformedURIException {
return getScheme(uri, defaultScheme);
}
/**
* Get the scheme of the given URI string.
*
* @param uri the URI string to get the scheme.
* @param defaultScheme the scheme to set.
* @return the scheme of the given URI.
* @exception MalformedURIException no scheme.
*/
public static String getScheme(String uri, String defaultScheme)
throws MalformedURIException {
int at = uri.indexOf("://");
if (at > 0)
return uri.substring(0, at);
// Check the default.
if (defaultScheme == null)
throw new MalformedURIException("No default scheme: " + uri);
return defaultScheme;
}
/*
* Get the authority part for this generic URI.
*
* @return the authority part of this generic URI.
* @exception MalformedURIException
*/
public String getAuthority()
throws MalformedURIException {
return getAuthority(uri);
}
/*
* Get the authority part of the given URI string.
*
* @param uri the URI string to get the authority part.
* @return the authority part of the given URI.
* @exception MalformedURIException
*/
public static String getAuthority(String uri)
throws MalformedURIException {
int at = uri.indexOf("://");
if (at > 0) {
int from = at + 3;
int to = uri.indexOf("/", from);
if (to > 0)
return uri.substring(from, to);
else
return uri.substring(from);
} else
throw new MalformedURIException("No scheme: " + uri);
}
/*
* Get the userinfo part for this generic URI.
*
* @return the userifno of this generic URI.
* @exception MalformedURIException
*/
public String getUserInfo()
throws MalformedURIException {
return getUserInfo(uri);
}
/*
* Get the userinfo part of the given URI string.
*
* @param uri the URI string to get the userinfo part.
* @return the userifno of the given URI.
* @exception MalformedURIException
*/
public static String getUserInfo(String uri)
throws MalformedURIException {
String authority = getAuthority(uri);
int to = authority.indexOf("@");
if (to > 0)
return authority.substring(0, to);
else
return null;
}
/*
* Get the username on the userinfo for this generic URI.
*
* @return the username of URI
* @exception MalformedURIException
* @see #getUserName(java.lang.String)
*/
public String getUserName()
throws MalformedURIException {
return getUserName(uri);
}
/*
* Get the username on the userinfo of the given URI string.
*
* @param uri the URI string to get the username.
* @return the username of URI
* @exception MalformedURIException
*/
public static String getUserName(String uri)
throws MalformedURIException {
try {
String userInfo = getUserInfo(uri);
int to = userInfo.indexOf(":");
if (to > 0)
return userInfo.substring(0, to);
else
return userInfo;
} catch (NullPointerException npe) {
// No userinfo
return null;
}
}
/*
* Get the password on the userinfo for this URI string.
*
* @return the password of URI
* @exception MalformedURIException
*/
public String getPassword()
throws MalformedURIException {
return getPassword(uri);
}
/*
* Get the password on the userinfo of the given URI string.
*
* @param uri the URI string to get the password.
* @return the password of URI
* @exception MalformedURIException
*/
public static String getPassword(String uri)
throws MalformedURIException {
try {
String userInfo = getUserInfo(uri);
int at = userInfo.indexOf(":");
if (at >= 0)
return userInfo.substring(at + 1);
else
return null;
} catch (NullPointerException npe) {
// No userinfo
return null;
}
}
/*
* Get the hostport part for this generic URI.
*
* @return the hostport string of the URI
* @exception MalformedURIException
*/
public String getHostPort()
throws MalformedURIException {
return getHostPort(uri);
}
/*
* Get the hostport part of the given URI string.
*
* @param uri the URI string to get the hostport part.
* @return the hostport of URI
* @exception MalformedURIException
*/
public static String getHostPort(String uri)
throws MalformedURIException {
String authority = getAuthority(uri);
int at = authority.indexOf("@");
if (at > 0)
return authority.substring(at + 1);
else
return authority;
}
/**
* Get the hostname for this generic URI.
*
* @return The hostname string
* @exception MalformedURIException
*/
public String getHost()
throws MalformedURIException {
return getHost(uri);
}
/**
* Get the hostname of the given URI string.
*
* @param uri the URI string to get the hostname.
* @return The hostname string
* @exception MalformedURIException
*/
public static String getHost(String uri)
throws MalformedURIException {
String hostPort = getHostPort(uri);
int to = hostPort.indexOf(":");
if (to > 0)
return hostPort.substring(0, to);
else
return hostPort;
}
/**
* Get the port number for this generic URI.
*
* @return The port number
* @exception MalformedURIException
*/
public int getPort()
throws MalformedURIException {
return getPort(uri);
}
/**
* Get the port number for this generic URI.
*
* @param uri the URI string to get the port number.
* @return The port number
* @exception MalformedURIException
*/
public static int getPort(String uri)
throws MalformedURIException {
return getPort(uri, defaultPort);
}
/**
* Get the port number of the given URI string.
*
* @param uri the URI string to get the port number.
* @param defaultPort the default port for the given URI.
* @return The port number
* @exception MalformedURIException
*/
public static int getPort(String uri, int defaultPort)
throws MalformedURIException {
String hostPort = getHostPort(uri);
int at = hostPort.indexOf(":");
if (at > 0) {
String port = hostPort.substring(at + 1);
try {
// Support the form like http://thinkfree.com:/~jericho
return port.equals("") ? defaultPort : Integer.parseInt(port);
} catch (NumberFormatException nfe) {
// The wrong port or unset number returns -1.
return -1;
}
}
if (defaultPort == -1)
throw new MalformedURIException("No default port: " + uri);
// The default port number.
return defaultPort;
}
/*
* Get the path for this generic URI.
* This method ignores the scheme and authority part of the URI path.
*
* @return the path.
*/
public String getPath() {
return getPath(uri.toString());
}
/*
* Get the path of the given URI string.
* This method ignores the scheme and authority part of the URI path.
*
* @param uri the specified URI string.
* @return the path.
*/
public static String getPath(String uri) {
int at = uri.indexOf("://");
// Ignore the authority part of URI
int to = uri.length();
// check the query
if (uri.indexOf("?") > 0)
to = uri.indexOf("?");
// check the fragment
if (uri.indexOf("#") > 0 && uri.indexOf("#") < to)
to = uri.indexOf("#");
if (at > 0) {
int from = uri.indexOf("/", at + 3);
uri = uri.substring(from, to);
} else {
int from = uri.indexOf("/");
if (from > 0) {
uri = uri.substring(from, to);
} else {
uri = "/";
}
}
return uri;
}
/*
* Get the parent path for this generic URI.
* This method ignores the forward part of the URI path.
*
* @return The parent path of the given already-normalized path.
*/
public String getParent() {
return getParent(uri);
}
/*
* Get the parent path of the given already-normalized path.
* This method ignores the forward part of the URI path.
*
* @param path A path string.
* @return The parent path of the given already-normalized path.
*/
public static String getParent(String path) {
String pathname = getPath(path);
if (pathname.endsWith("/")) {
int at = pathname.lastIndexOf("/");
// consider of root pathname
if (at > 0) {
pathname = pathname.substring(0, at);
at = pathname.lastIndexOf("/");
pathname = pathname.substring(0, at+1);
}
} else {
int at = pathname.lastIndexOf("/");
pathname = pathname.substring(0, at+1);
}
return pathname;
}
/*
* Get the resource or file name for this generic URI.
*
* @return The resource or file name string for this generic URI.
* If it's a collection or directory,
* the empty string is returned.
*/
public String getName() {
return getName(uri);
}
/*
* Get the resource or file name of the given path.
*
* @param path A path string.
* @return The resource or file name string of the given path.
* If it's a collection or directory,
* the empty string is returned.
*/
public static String getName(String path) {
String pathname = getPath(path);
int at = pathname.lastIndexOf("/");
return (at >= 0) ? pathname.substring(at+1) : pathname;
}
/*
* Get the query for this generic URI.
* This method ignores the scheme and authority part for this generic URI.
*
* @return the query string.
* @exception MalformedURIException
*/
public String getQuery()
throws MalformedURIException {
return getQuery(uri);
}
/*
* Get the query of the given URI string.
* This method ignores the scheme and authority part of the given URI.
*
* @param uri the specified URI string.
* @return the query string.
* @exception MalformedURIException
*/
public static String getQuery(String uri)
throws MalformedURIException {
int at = uri.indexOf("://");
// Just ignore the authority part of URI
if (at > 0) {
at = uri.indexOf("/", at + 3);
} else {
// FIXME: net_path?
at = uri.indexOf("/");
}
if (at > 0) {
int from = -1;
// check the query
if (uri.indexOf("?", at) > 0)
from = uri.indexOf("?") + 1;
int to = uri.length();
// check the fragment
if (uri.indexOf("#", at) > 0 && uri.indexOf("#", at) < to)
to = uri.indexOf("#");
if (from > 0)
return uri.substring(from, to);
else
return null;
} else
throw new MalformedURIException("Need to have the path part");
}
/*
* Get the fragment for this generic URI.
* This method ignores the scheme and authority part of the given URI.
*
* @return the fragment string.
* @exception MalformedURIException
*/
public String getFragment()
throws MalformedURIException {
return getFragment(uri);
}
/*
* Get the fragment of the given URI string.
* This method ignores the scheme and authority part of the given URI.
*
* @param uri the specified URI string.
* @return the fragment string.
* @exception MalformedURIException
*/
public static String getFragment(String uri)
throws MalformedURIException {
int at = uri.indexOf("://");
// Just ignore the authority part of URI
if (at > 0) {
at = uri.indexOf("/", at + 3);
} else {
// FIXME: net_path?
at = uri.indexOf("/");
}
if (at > 0) {
int from = -1;
// check the fragment
if (uri.indexOf("#", at) > 0)
from = uri.indexOf("#") + 1;
if (from > 0)
return uri.substring(from);
else
return null;
} else
throw new MalformedURIException("Need to have the path part");
}
// ------------------------------------------ Methods for basic function
/*
* Test a object if this is equal with another.
*
* @param obj A object to compare.
* @return true if two URI objects are equal.
*/
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof GenericURI)) {
try {
GenericURI uri = (GenericURI) obj;
if (getScheme().equalsIgnoreCase(uri.getScheme()) &&
getUserInfo().equals(uri.getUserInfo()) &&
getHost().equalsIgnoreCase(uri.getHost()) &&
getPort() == uri.getPort() &&
getPath().equals(uri.getPath()))
return true;
} catch(MalformedURIException ignored) {
}
}
return false;
}
/*
* Get a URI string.
*
* @return A URI string.
*/
public String toString() {
return uri;
}
}