0N/A/*
2362N/A * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.toolkit.url;
0N/A
0N/A
0N/Aimport java.net.MalformedURLException;
0N/A
0N/A
0N/A/**
0N/A * A Uri object represents an absolute Uniform Resource Identifier
0N/A * (URI) as defined by RFC 2396 and updated by RFC 2373 and RFC 2732.
0N/A * The most commonly used form of URI is the Uniform Resource Locator (URL).
0N/A *
0N/A * <p> The java.net.URL class cannot be used to parse URIs since it
0N/A * requires the installation of URL stream handlers that may not be
0N/A * available. The hack of getting around this by temporarily
0N/A * replacing the scheme part of a URI is not appropriate here: JNDI
0N/A * service providers must work on older Java platforms, and we want
0N/A * new features and bug fixes that are not available in old versions
0N/A * of the URL class.
0N/A *
0N/A * <p> It may be appropriate to drop this code in favor of the
0N/A * java.net.URI class. The changes would need to be written so as to
0N/A * still run on pre-1.4 platforms not containing that class.
0N/A *
0N/A * <p> The format of an absolute URI (see the RFCs mentioned above) is:
0N/A * <p><blockquote><pre>
0N/A * absoluteURI = scheme ":" ( hier_part | opaque_part )
0N/A *
0N/A * scheme = alpha *( alpha | digit | "+" | "-" | "." )
0N/A *
0N/A * hier_part = ( net_path | abs_path ) [ "?" query ]
0N/A * opaque_part = uric_no_slash *uric
0N/A *
0N/A * net_path = "//" authority [ abs_path ]
0N/A * abs_path = "/" path_segments
0N/A *
0N/A * authority = server | reg_name
0N/A * reg_name = 1*( unreserved | escaped | "$" | "," |
0N/A * ";" | ":" | "@" | "&" | "=" | "+" )
0N/A * server = [ [ userinfo "@" ] hostport ]
0N/A * userinfo = *( unreserved | escaped |
0N/A * ";" | ":" | "&" | "=" | "+" | "$" | "," )
0N/A *
0N/A * hostport = host [ ":" port ]
0N/A * host = hostname | IPv4address | IPv6reference
0N/A * port = *digit
0N/A *
0N/A * IPv6reference = "[" IPv6address "]"
0N/A * IPv6address = hexpart [ ":" IPv4address ]
0N/A * IPv4address = 1*3digit "." 1*3digit "." 1*3digit "." 1*3digit
0N/A * hexpart = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
0N/A * hexseq = hex4 *( ":" hex4)
0N/A * hex4 = 1*4hex
0N/A *
0N/A * path = [ abs_path | opaque_part ]
0N/A * path_segments = segment *( "/" segment )
0N/A * segment = *pchar *( ";" param )
0N/A * param = *pchar
0N/A * pchar = unreserved | escaped |
0N/A * ":" | "@" | "&" | "=" | "+" | "$" | ","
0N/A *
0N/A * query = *uric
0N/A *
0N/A * uric = reserved | unreserved | escaped
0N/A * uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
0N/A * "&" | "=" | "+" | "$" | ","
0N/A * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
0N/A * "$" | "," | "[" | "]"
0N/A * unreserved = alphanum | mark
0N/A * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
0N/A * escaped = "%" hex hex
0N/A * unwise = "{" | "}" | "|" | "\" | "^" | "`"
0N/A * </pre></blockquote>
0N/A *
0N/A * <p> Currently URIs containing <tt>userinfo</tt> or <tt>reg_name</tt>
0N/A * are not supported.
0N/A * The <tt>opaque_part</tt> of a non-hierarchical URI is treated as if
0N/A * if were a <tt>path</tt> without a leading slash.
0N/A */
0N/A
0N/A
0N/Apublic class Uri {
0N/A
0N/A protected String uri;
0N/A protected String scheme;
0N/A protected String host = null;
0N/A protected int port = -1;
0N/A protected boolean hasAuthority;
0N/A protected String path;
0N/A protected String query = null;
0N/A
0N/A
0N/A /**
0N/A * Creates a Uri object given a URI string.
0N/A */
0N/A public Uri(String uri) throws MalformedURLException {
0N/A init(uri);
0N/A }
0N/A
0N/A /**
0N/A * Creates an uninitialized Uri object. The init() method must
0N/A * be called before any other Uri methods.
0N/A */
0N/A protected Uri() {
0N/A }
0N/A
0N/A /**
0N/A * Initializes a Uri object given a URI string.
0N/A * This method must be called exactly once, and before any other Uri
0N/A * methods.
0N/A */
0N/A protected void init(String uri) throws MalformedURLException {
0N/A this.uri = uri;
0N/A parse(uri);
0N/A }
0N/A
0N/A /**
0N/A * Returns the URI's scheme.
0N/A */
0N/A public String getScheme() {
0N/A return scheme;
0N/A }
0N/A
0N/A /**
0N/A * Returns the host from the URI's authority part, or null
0N/A * if no host is provided. If the host is an IPv6 literal, the
0N/A * delimiting brackets are part of the returned value (see
0N/A * {@link java.net.URI#getHost}).
0N/A */
0N/A public String getHost() {
0N/A return host;
0N/A }
0N/A
0N/A /**
0N/A * Returns the port from the URI's authority part, or -1 if
0N/A * no port is provided.
0N/A */
0N/A public int getPort() {
0N/A return port;
0N/A }
0N/A
0N/A /**
0N/A * Returns the URI's path. The path is never null. Note that a
0N/A * slash following the authority part (or the scheme if there is
0N/A * no authority part) is part of the path. For example, the path
0N/A * of "http://host/a/b" is "/a/b".
0N/A */
0N/A public String getPath() {
0N/A return path;
0N/A }
0N/A
0N/A /**
0N/A * Returns the URI's query part, or null if no query is provided.
0N/A * Note that a query always begins with a leading "?".
0N/A */
0N/A public String getQuery() {
0N/A return query;
0N/A }
0N/A
0N/A /**
0N/A * Returns the URI as a string.
0N/A */
0N/A public String toString() {
0N/A return uri;
0N/A }
0N/A
0N/A /*
0N/A * Parses a URI string and sets this object's fields accordingly.
0N/A */
0N/A private void parse(String uri) throws MalformedURLException {
0N/A int i; // index into URI
0N/A
0N/A i = uri.indexOf(':'); // parse scheme
0N/A if (i < 0) {
0N/A throw new MalformedURLException("Invalid URI: " + uri);
0N/A }
0N/A scheme = uri.substring(0, i);
0N/A i++; // skip past ":"
0N/A
0N/A hasAuthority = uri.startsWith("//", i);
0N/A if (hasAuthority) { // parse "//host:port"
0N/A i += 2; // skip past "//"
0N/A int slash = uri.indexOf('/', i);
0N/A if (slash < 0) {
0N/A slash = uri.length();
0N/A }
0N/A if (uri.startsWith("[", i)) { // at IPv6 literal
0N/A int brac = uri.indexOf(']', i + 1);
0N/A if (brac < 0 || brac > slash) {
0N/A throw new MalformedURLException("Invalid URI: " + uri);
0N/A }
0N/A host = uri.substring(i, brac + 1); // include brackets
0N/A i = brac + 1; // skip past "[...]"
0N/A } else { // at host name or IPv4
0N/A int colon = uri.indexOf(':', i);
0N/A int hostEnd = (colon < 0 || colon > slash)
0N/A ? slash
0N/A : colon;
0N/A if (i < hostEnd) {
0N/A host = uri.substring(i, hostEnd);
0N/A }
0N/A i = hostEnd; // skip past host
0N/A }
0N/A
0N/A if ((i + 1 < slash) &&
0N/A uri.startsWith(":", i)) { // parse port
0N/A i++; // skip past ":"
0N/A port = Integer.parseInt(uri.substring(i, slash));
0N/A }
0N/A i = slash; // skip to path
0N/A }
0N/A int qmark = uri.indexOf('?', i); // look for query
0N/A if (qmark < 0) {
0N/A path = uri.substring(i);
0N/A } else {
0N/A path = uri.substring(i, qmark);
0N/A query = uri.substring(qmark);
0N/A }
0N/A }
0N/A
0N/A/*
0N/A // Debug
0N/A public static void main(String args[]) throws MalformedURLException {
0N/A for (int i = 0; i < args.length; i++) {
0N/A Uri uri = new Uri(args[i]);
0N/A
0N/A String h = (uri.getHost() != null) ? uri.getHost() : "";
0N/A String p = (uri.getPort() != -1) ? (":" + uri.getPort()) : "";
0N/A String a = uri.hasAuthority ? ("//" + h + p) : "";
0N/A String q = (uri.getQuery() != null) ? uri.getQuery() : "";
0N/A
0N/A String str = uri.getScheme() + ":" + a + uri.getPath() + q;
0N/A if (! uri.toString().equals(str)) {
0N/A System.out.println(str);
0N/A }
0N/A System.out.println(h);
0N/A }
0N/A }
0N/A*/
0N/A}