001: /*
002: * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.jndi.toolkit.url;
027:
028: import java.net.MalformedURLException;
029:
030: /**
031: * A Uri object represents an absolute Uniform Resource Identifier
032: * (URI) as defined by RFC 2396 and updated by RFC 2373 and RFC 2732.
033: * The most commonly used form of URI is the Uniform Resource Locator (URL).
034: *
035: * <p> The java.net.URL class cannot be used to parse URIs since it
036: * requires the installation of URL stream handlers that may not be
037: * available. The hack of getting around this by temporarily
038: * replacing the scheme part of a URI is not appropriate here: JNDI
039: * service providers must work on older Java platforms, and we want
040: * new features and bug fixes that are not available in old versions
041: * of the URL class.
042: *
043: * <p> It may be appropriate to drop this code in favor of the
044: * java.net.URI class. The changes would need to be written so as to
045: * still run on pre-1.4 platforms not containing that class.
046: *
047: * <p> The format of an absolute URI (see the RFCs mentioned above) is:
048: * <p><blockquote><pre>
049: * absoluteURI = scheme ":" ( hier_part | opaque_part )
050: *
051: * scheme = alpha *( alpha | digit | "+" | "-" | "." )
052: *
053: * hier_part = ( net_path | abs_path ) [ "?" query ]
054: * opaque_part = uric_no_slash *uric
055: *
056: * net_path = "//" authority [ abs_path ]
057: * abs_path = "/" path_segments
058: *
059: * authority = server | reg_name
060: * reg_name = 1*( unreserved | escaped | "$" | "," |
061: * ";" | ":" | "@" | "&" | "=" | "+" )
062: * server = [ [ userinfo "@" ] hostport ]
063: * userinfo = *( unreserved | escaped |
064: * ";" | ":" | "&" | "=" | "+" | "$" | "," )
065: *
066: * hostport = host [ ":" port ]
067: * host = hostname | IPv4address | IPv6reference
068: * port = *digit
069: *
070: * IPv6reference = "[" IPv6address "]"
071: * IPv6address = hexpart [ ":" IPv4address ]
072: * IPv4address = 1*3digit "." 1*3digit "." 1*3digit "." 1*3digit
073: * hexpart = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
074: * hexseq = hex4 *( ":" hex4)
075: * hex4 = 1*4hex
076: *
077: * path = [ abs_path | opaque_part ]
078: * path_segments = segment *( "/" segment )
079: * segment = *pchar *( ";" param )
080: * param = *pchar
081: * pchar = unreserved | escaped |
082: * ":" | "@" | "&" | "=" | "+" | "$" | ","
083: *
084: * query = *uric
085: *
086: * uric = reserved | unreserved | escaped
087: * uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
088: * "&" | "=" | "+" | "$" | ","
089: * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
090: * "$" | "," | "[" | "]"
091: * unreserved = alphanum | mark
092: * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
093: * escaped = "%" hex hex
094: * unwise = "{" | "}" | "|" | "\" | "^" | "`"
095: * </pre></blockquote>
096: *
097: * <p> Currently URIs containing <tt>userinfo</tt> or <tt>reg_name</tt>
098: * are not supported.
099: * The <tt>opaque_part</tt> of a non-hierarchical URI is treated as if
100: * if were a <tt>path</tt> without a leading slash.
101: */
102:
103: public class Uri {
104:
105: protected String uri;
106: protected String scheme;
107: protected String host = null;
108: protected int port = -1;
109: protected boolean hasAuthority;
110: protected String path;
111: protected String query = null;
112:
113: /**
114: * Creates a Uri object given a URI string.
115: */
116: public Uri(String uri) throws MalformedURLException {
117: init(uri);
118: }
119:
120: /**
121: * Creates an uninitialized Uri object. The init() method must
122: * be called before any other Uri methods.
123: */
124: protected Uri() {
125: }
126:
127: /**
128: * Initializes a Uri object given a URI string.
129: * This method must be called exactly once, and before any other Uri
130: * methods.
131: */
132: protected void init(String uri) throws MalformedURLException {
133: this .uri = uri;
134: parse(uri);
135: }
136:
137: /**
138: * Returns the URI's scheme.
139: */
140: public String getScheme() {
141: return scheme;
142: }
143:
144: /**
145: * Returns the host from the URI's authority part, or null
146: * if no host is provided. If the host is an IPv6 literal, the
147: * delimiting brackets are part of the returned value (see
148: * {@link java.net.URI#getHost}).
149: */
150: public String getHost() {
151: return host;
152: }
153:
154: /**
155: * Returns the port from the URI's authority part, or -1 if
156: * no port is provided.
157: */
158: public int getPort() {
159: return port;
160: }
161:
162: /**
163: * Returns the URI's path. The path is never null. Note that a
164: * slash following the authority part (or the scheme if there is
165: * no authority part) is part of the path. For example, the path
166: * of "http://host/a/b" is "/a/b".
167: */
168: public String getPath() {
169: return path;
170: }
171:
172: /**
173: * Returns the URI's query part, or null if no query is provided.
174: * Note that a query always begins with a leading "?".
175: */
176: public String getQuery() {
177: return query;
178: }
179:
180: /**
181: * Returns the URI as a string.
182: */
183: public String toString() {
184: return uri;
185: }
186:
187: /*
188: * Parses a URI string and sets this object's fields accordingly.
189: */
190: private void parse(String uri) throws MalformedURLException {
191: int i; // index into URI
192:
193: i = uri.indexOf(':'); // parse scheme
194: if (i < 0) {
195: throw new MalformedURLException("Invalid URI: " + uri);
196: }
197: scheme = uri.substring(0, i);
198: i++; // skip past ":"
199:
200: hasAuthority = uri.startsWith("//", i);
201: if (hasAuthority) { // parse "//host:port"
202: i += 2; // skip past "//"
203: int slash = uri.indexOf('/', i);
204: if (slash < 0) {
205: slash = uri.length();
206: }
207: if (uri.startsWith("[", i)) { // at IPv6 literal
208: int brac = uri.indexOf(']', i + 1);
209: if (brac < 0 || brac > slash) {
210: throw new MalformedURLException("Invalid URI: "
211: + uri);
212: }
213: host = uri.substring(i, brac + 1); // include brackets
214: i = brac + 1; // skip past "[...]"
215: } else { // at host name or IPv4
216: int colon = uri.indexOf(':', i);
217: int hostEnd = (colon < 0 || colon > slash) ? slash
218: : colon;
219: if (i < hostEnd) {
220: host = uri.substring(i, hostEnd);
221: }
222: i = hostEnd; // skip past host
223: }
224:
225: if ((i + 1 < slash) && uri.startsWith(":", i)) { // parse port
226: i++; // skip past ":"
227: port = Integer.parseInt(uri.substring(i, slash));
228: }
229: i = slash; // skip to path
230: }
231: int qmark = uri.indexOf('?', i); // look for query
232: if (qmark < 0) {
233: path = uri.substring(i);
234: } else {
235: path = uri.substring(i, qmark);
236: query = uri.substring(qmark);
237: }
238: }
239:
240: /*
241: // Debug
242: public static void main(String args[]) throws MalformedURLException {
243: for (int i = 0; i < args.length; i++) {
244: Uri uri = new Uri(args[i]);
245:
246: String h = (uri.getHost() != null) ? uri.getHost() : "";
247: String p = (uri.getPort() != -1) ? (":" + uri.getPort()) : "";
248: String a = uri.hasAuthority ? ("//" + h + p) : "";
249: String q = (uri.getQuery() != null) ? uri.getQuery() : "";
250:
251: String str = uri.getScheme() + ":" + a + uri.getPath() + q;
252: if (! uri.toString().equals(str)) {
253: System.out.println(str);
254: }
255: System.out.println(h);
256: }
257: }
258: */
259: }
|