001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpURL.java,v 1.18 2004/09/30 17:26:41 oglueck Exp $
003: * $Revision: 507324 $
004: * $Date: 2007-02-14 01:12:11 +0100 (Wed, 14 Feb 2007) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import org.apache.commons.httpclient.util.URIUtil;
034:
035: /**
036: * The HTTP URL.
037: *
038: * @author <a href="mailto:jericho at apache.org">Sung-Gu</a>
039: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
040: */
041: public class HttpURL extends URI {
042:
043: // ----------------------------------------------------------- Constructors
044:
045: /** Create an instance as an internal use. */
046: protected HttpURL() {
047: }
048:
049: /**
050: * Construct a HTTP URL as an escaped form of a character array with the
051: * given charset to do escape encoding.
052: *
053: * @param escaped the HTTP URL character sequence
054: * @param charset the charset string to do escape encoding
055: * @throws URIException If {@link #checkValid()} fails
056: * @throws NullPointerException if <code>escaped</code> is <code>null</code>
057: * @see #getProtocolCharset
058: */
059: public HttpURL(char[] escaped, String charset) throws URIException,
060: NullPointerException {
061: protocolCharset = charset;
062: parseUriReference(new String(escaped), true);
063: checkValid();
064: }
065:
066: /**
067: * Construct a HTTP URL as an escaped form of a character array.
068: *
069: * @param escaped the HTTP URL character sequence
070: * @throws URIException If {@link #checkValid()} fails
071: * @throws NullPointerException if <code>escaped</code> is <code>null</code>
072: * @see #getDefaultProtocolCharset
073: */
074: public HttpURL(char[] escaped) throws URIException,
075: NullPointerException {
076: parseUriReference(new String(escaped), true);
077: checkValid();
078: }
079:
080: /**
081: * Construct a HTTP URL from a given string with the given charset to do
082: * escape encoding.
083: *
084: * @param original the HTTP URL string
085: * @param charset the charset string to do escape encoding
086: * @throws URIException If {@link #checkValid()} fails
087: * @see #getProtocolCharset
088: */
089: public HttpURL(String original, String charset) throws URIException {
090: protocolCharset = charset;
091: parseUriReference(original, false);
092: checkValid();
093: }
094:
095: /**
096: * Construct a HTTP URL from a given string.
097: *
098: * @param original the HTTP URL string
099: * @throws URIException If {@link #checkValid()} fails
100: * @see #getDefaultProtocolCharset
101: */
102: public HttpURL(String original) throws URIException {
103: parseUriReference(original, false);
104: checkValid();
105: }
106:
107: /**
108: * Construct a HTTP URL from given components.
109: *
110: * @param host the host string
111: * @param port the port number
112: * @param path the path string
113: * @throws URIException If {@link #checkValid()} fails
114: * @see #getDefaultProtocolCharset
115: */
116: public HttpURL(String host, int port, String path)
117: throws URIException {
118: this (null, null, host, port, path, null, null);
119: }
120:
121: /**
122: * Construct a HTTP URL from given components.
123: *
124: * @param host the host string
125: * @param port the port number
126: * @param path the path string
127: * @param query the query string
128: * @throws URIException If {@link #checkValid()} fails
129: * @see #getDefaultProtocolCharset
130: */
131: public HttpURL(String host, int port, String path, String query)
132: throws URIException {
133:
134: this (null, null, host, port, path, query, null);
135: }
136:
137: /**
138: * Construct a HTTP URL from given components.
139: *
140: * @param user the user name
141: * @param password his or her password
142: * @param host the host string
143: * @throws URIException If {@link #checkValid()} fails
144: * @see #getDefaultProtocolCharset
145: */
146: public HttpURL(String user, String password, String host)
147: throws URIException {
148:
149: this (user, password, host, -1, null, null, null);
150: }
151:
152: /**
153: * Construct a HTTP URL from given components.
154: *
155: * @param user the user name
156: * @param password his or her password
157: * @param host the host string
158: * @param port the port number
159: * @throws URIException If {@link #checkValid()} fails
160: * @see #getDefaultProtocolCharset
161: */
162: public HttpURL(String user, String password, String host, int port)
163: throws URIException {
164:
165: this (user, password, host, port, null, null, null);
166: }
167:
168: /**
169: * Construct a HTTP URL from given components.
170: *
171: * @param user the user name
172: * @param password his or her password
173: * @param host the host string
174: * @param port the port number
175: * @param path the path string
176: * @throws URIException If {@link #checkValid()} fails
177: * @see #getDefaultProtocolCharset
178: */
179: public HttpURL(String user, String password, String host, int port,
180: String path) throws URIException {
181:
182: this (user, password, host, port, path, null, null);
183: }
184:
185: /**
186: * Construct a HTTP URL from given components.
187: *
188: * @param user the user name
189: * @param password his or her password
190: * @param host the host string
191: * @param port the port number
192: * @param path the path string
193: * @param query The query string.
194: * @throws URIException If {@link #checkValid()} fails
195: * @see #getDefaultProtocolCharset
196: */
197: public HttpURL(String user, String password, String host, int port,
198: String path, String query) throws URIException {
199:
200: this (user, password, host, port, path, query, null);
201: }
202:
203: /**
204: * Construct a HTTP URL from given components.
205: *
206: * @param host the host string
207: * @param path the path string
208: * @param query the query string
209: * @param fragment the fragment string
210: * @throws URIException If {@link #checkValid()} fails
211: * @see #getDefaultProtocolCharset
212: */
213: public HttpURL(String host, String path, String query,
214: String fragment) throws URIException {
215:
216: this (null, null, host, -1, path, query, fragment);
217: }
218:
219: /**
220: * Construct a HTTP URL from given components.
221: *
222: * Note: The <code>userinfo</code> format is normally
223: * <code><username>:<password></code> where
224: * username and password must both be URL escaped.
225: *
226: * @param userinfo the userinfo string whose parts are URL escaped
227: * @param host the host string
228: * @param path the path string
229: * @param query the query string
230: * @param fragment the fragment string
231: * @throws URIException If {@link #checkValid()} fails
232: * @see #getDefaultProtocolCharset
233: */
234: public HttpURL(String userinfo, String host, String path,
235: String query, String fragment) throws URIException {
236:
237: this (userinfo, host, -1, path, query, fragment);
238: }
239:
240: /**
241: * Construct a HTTP URL from given components.
242: *
243: * Note: The <code>userinfo</code> format is normally
244: * <code><username>:<password></code> where
245: * username and password must both be URL escaped.
246: *
247: * @param userinfo the userinfo string whose parts are URL escaped
248: * @param host the host string
249: * @param port the port number
250: * @param path the path string
251: * @throws URIException If {@link #checkValid()} fails
252: * @see #getDefaultProtocolCharset
253: */
254: public HttpURL(String userinfo, String host, int port, String path)
255: throws URIException {
256:
257: this (userinfo, host, port, path, null, null);
258: }
259:
260: /**
261: * Construct a HTTP URL from given components.
262: *
263: * Note: The <code>userinfo</code> format is normally
264: * <code><username>:<password></code> where
265: * username and password must both be URL escaped.
266: *
267: * @param userinfo the userinfo string whose parts are URL escaped
268: * @param host the host string
269: * @param port the port number
270: * @param path the path string
271: * @param query the query string
272: * @throws URIException If {@link #checkValid()} fails
273: * @see #getDefaultProtocolCharset
274: */
275: public HttpURL(String userinfo, String host, int port, String path,
276: String query) throws URIException {
277:
278: this (userinfo, host, port, path, query, null);
279: }
280:
281: /**
282: * Construct a HTTP URL from given components.
283: *
284: * Note: The <code>userinfo</code> format is normally
285: * <code><username>:<password></code> where
286: * username and password must both be URL escaped.
287: *
288: * @param userinfo the userinfo string whose parts are URL escaped
289: * @param host the host string
290: * @param port the port number
291: * @param path the path string
292: * @param query the query string
293: * @param fragment the fragment string
294: * @throws URIException If {@link #checkValid()} fails
295: * @see #getDefaultProtocolCharset
296: */
297: public HttpURL(String userinfo, String host, int port, String path,
298: String query, String fragment) throws URIException {
299:
300: // validate and contruct the URI character sequence
301: StringBuffer buff = new StringBuffer();
302: if (userinfo != null || host != null || port != -1) {
303: _scheme = DEFAULT_SCHEME; // in order to verify the own protocol
304: buff.append(_default_scheme);
305: buff.append("://");
306: if (userinfo != null) {
307: buff.append(userinfo);
308: buff.append('@');
309: }
310: if (host != null) {
311: buff.append(URIUtil.encode(host, URI.allowed_host));
312: if (port != -1 || port != DEFAULT_PORT) {
313: buff.append(':');
314: buff.append(port);
315: }
316: }
317: }
318: if (path != null) { // accept empty path
319: if (scheme != null && !path.startsWith("/")) {
320: throw new URIException(URIException.PARSING,
321: "abs_path requested");
322: }
323: buff.append(URIUtil.encode(path, URI.allowed_abs_path));
324: }
325: if (query != null) {
326: buff.append('?');
327: buff.append(URIUtil.encode(query, URI.allowed_query));
328: }
329: if (fragment != null) {
330: buff.append('#');
331: buff.append(URIUtil.encode(fragment, URI.allowed_fragment));
332: }
333: parseUriReference(buff.toString(), true);
334: checkValid();
335: }
336:
337: /**
338: * Construct a HTTP URL from given components.
339: *
340: * @param user the user name
341: * @param password his or her password
342: * @param host the host string
343: * @param port the port number
344: * @param path the path string
345: * @param query the query string
346: * @param fragment the fragment string
347: * @throws URIException If {@link #checkValid()} fails
348: * @see #getDefaultProtocolCharset
349: */
350: public HttpURL(String user, String password, String host, int port,
351: String path, String query, String fragment)
352: throws URIException {
353: this (toUserinfo(user, password), host, port, path, query,
354: fragment);
355: }
356:
357: protected static String toUserinfo(String user, String password)
358: throws URIException {
359: if (user == null)
360: return null;
361: StringBuffer usrinfo = new StringBuffer(20); //sufficient for real world
362: usrinfo.append(URIUtil
363: .encode(user, URI.allowed_within_userinfo));
364: if (password == null)
365: return usrinfo.toString();
366: usrinfo.append(':');
367: usrinfo.append(URIUtil.encode(password,
368: URI.allowed_within_userinfo));
369: return usrinfo.toString();
370: }
371:
372: /**
373: * Construct a HTTP URL with a given relative URL string.
374: *
375: * @param base the base HttpURL
376: * @param relative the relative HTTP URL string
377: * @throws URIException If {@link #checkValid()} fails
378: */
379: public HttpURL(HttpURL base, String relative) throws URIException {
380: this (base, new HttpURL(relative));
381: }
382:
383: /**
384: * Construct a HTTP URL with a given relative URL.
385: *
386: * @param base the base HttpURL
387: * @param relative the relative HttpURL
388: * @throws URIException If {@link #checkValid()} fails
389: */
390: public HttpURL(HttpURL base, HttpURL relative) throws URIException {
391: super (base, relative);
392: checkValid();
393: }
394:
395: // -------------------------------------------------------------- Constants
396:
397: /**
398: * Default scheme for HTTP URL.
399: */
400: public static final char[] DEFAULT_SCHEME = { 'h', 't', 't', 'p' };
401:
402: /**
403: * Default scheme for HTTP URL.
404: * @deprecated Use {@link #DEFAULT_SCHEME} instead. This one doesn't
405: * conform to the project naming conventions.
406: */
407: public static final char[] _default_scheme = DEFAULT_SCHEME;
408:
409: /**
410: * Default port for HTTP URL.
411: */
412: public static final int DEFAULT_PORT = 80;
413:
414: /**
415: * Default port for HTTP URL.
416: * @deprecated Use {@link #DEFAULT_PORT} instead. This one doesn't conform
417: * to the project naming conventions.
418: */
419: public static final int _default_port = DEFAULT_PORT;
420:
421: /**
422: * The serialVersionUID.
423: */
424: static final long serialVersionUID = -7158031098595039459L;
425:
426: // ------------------------------------------------------------- The scheme
427:
428: /**
429: * Get the scheme. You can get the scheme explicitly.
430: *
431: * @return the scheme
432: */
433: public char[] getRawScheme() {
434: return (_scheme == null) ? null : HttpURL.DEFAULT_SCHEME;
435: }
436:
437: /**
438: * Get the scheme. You can get the scheme explicitly.
439: *
440: * @return the scheme null if empty or undefined
441: */
442: public String getScheme() {
443: return (_scheme == null) ? null : new String(
444: HttpURL.DEFAULT_SCHEME);
445: }
446:
447: // --------------------------------------------------------------- The port
448:
449: /**
450: * Get the port number.
451: * @return the port number
452: */
453: public int getPort() {
454: return (_port == -1) ? HttpURL.DEFAULT_PORT : _port;
455: }
456:
457: // ----------------------------------------------------------- The userinfo
458:
459: /**
460: * Set the raw-escaped user and password.
461: *
462: * @param escapedUser the raw-escaped user
463: * @param escapedPassword the raw-escaped password; could be null
464: * @throws URIException escaped user not valid or user required; escaped
465: * password not valid or username missed
466: */
467: public void setRawUserinfo(char[] escapedUser,
468: char[] escapedPassword) throws URIException {
469:
470: if (escapedUser == null || escapedUser.length == 0) {
471: throw new URIException(URIException.PARSING,
472: "user required");
473: }
474: if (!validate(escapedUser, within_userinfo)
475: || ((escapedPassword != null) && !validate(
476: escapedPassword, within_userinfo))) {
477: throw new URIException(URIException.ESCAPING,
478: "escaped userinfo not valid");
479: }
480: String username = new String(escapedUser);
481: String password = (escapedPassword == null) ? null
482: : new String(escapedPassword);
483: String userinfo = username
484: + ((password == null) ? "" : ":" + password);
485: String hostname = new String(getRawHost());
486: String hostport = (_port == -1) ? hostname : hostname + ":"
487: + _port;
488: String authority = userinfo + "@" + hostport;
489: _userinfo = userinfo.toCharArray();
490: _authority = authority.toCharArray();
491: setURI();
492: }
493:
494: /**
495: * Set the raw-escaped user and password.
496: *
497: * @param escapedUser the escaped user
498: * @param escapedPassword the escaped password; could be null
499: * @throws URIException escaped user not valid or user required; escaped
500: * password not valid or username missed
501: * @throws NullPointerException null user
502: */
503: public void setEscapedUserinfo(String escapedUser,
504: String escapedPassword) throws URIException,
505: NullPointerException {
506:
507: setRawUserinfo(escapedUser.toCharArray(),
508: (escapedPassword == null) ? null : escapedPassword
509: .toCharArray());
510: }
511:
512: /**
513: * Set the user and password.
514: *
515: * @param user the user
516: * @param password the password; could be null
517: * @throws URIException encoding error or username missed
518: * @throws NullPointerException null user
519: */
520: public void setUserinfo(String user, String password)
521: throws URIException, NullPointerException {
522: // set the charset to do escape encoding
523: String charset = getProtocolCharset();
524: setRawUserinfo(encode(user, within_userinfo, charset),
525: (password == null) ? null : encode(password,
526: within_userinfo, charset));
527: }
528:
529: /**
530: * Set the raw-escaped user.
531: *
532: * @param escapedUser the raw-escaped user
533: * @throws URIException escaped user not valid or user required
534: */
535: public void setRawUser(char[] escapedUser) throws URIException {
536: if (escapedUser == null || escapedUser.length == 0) {
537: throw new URIException(URIException.PARSING,
538: "user required");
539: }
540: if (!validate(escapedUser, within_userinfo)) {
541: throw new URIException(URIException.ESCAPING,
542: "escaped user not valid");
543: }
544: String username = new String(escapedUser);
545: char[] rawPassword = getRawPassword();
546: String password = rawPassword == null ? null : new String(
547: rawPassword);
548: String userinfo = username
549: + ((password == null) ? "" : ":" + password);
550: String hostname = new String(getRawHost());
551: String hostport = (_port == -1) ? hostname : hostname + ":"
552: + _port;
553: String authority = userinfo + "@" + hostport;
554: _userinfo = userinfo.toCharArray();
555: _authority = authority.toCharArray();
556: setURI();
557: }
558:
559: /**
560: * Set the escaped user string.
561: *
562: * @param escapedUser the escaped user string
563: * @throws URIException escaped user not valid
564: * @throws NullPointerException null user
565: */
566: public void setEscapedUser(String escapedUser) throws URIException,
567: NullPointerException {
568: setRawUser(escapedUser.toCharArray());
569: }
570:
571: /**
572: * Set the user string.
573: *
574: * @param user the user string
575: * @throws URIException user encoding error
576: * @throws NullPointerException null user
577: */
578: public void setUser(String user) throws URIException,
579: NullPointerException {
580: setRawUser(encode(user, allowed_within_userinfo,
581: getProtocolCharset()));
582: }
583:
584: /**
585: * Get the raw-escaped user.
586: *
587: * @return the raw-escaped user
588: */
589: public char[] getRawUser() {
590: if (_userinfo == null || _userinfo.length == 0) {
591: return null;
592: }
593: int to = indexFirstOf(_userinfo, ':');
594: // String.indexOf(':', 0, _userinfo.length, _userinfo, 0, 1, 0);
595: if (to == -1) {
596: return _userinfo; // only user.
597: }
598: char[] result = new char[to];
599: System.arraycopy(_userinfo, 0, result, 0, to);
600: return result;
601: }
602:
603: /**
604: * Get the escaped user
605: *
606: * @return the escaped user
607: */
608: public String getEscapedUser() {
609: char[] user = getRawUser();
610: return (user == null) ? null : new String(user);
611: }
612:
613: /**
614: * Get the user.
615: *
616: * @return the user name
617: * @throws URIException If {@link #decode} fails
618: */
619: public String getUser() throws URIException {
620: char[] user = getRawUser();
621: return (user == null) ? null : decode(user,
622: getProtocolCharset());
623: }
624:
625: /**
626: * Set the raw-escaped password.
627: *
628: * @param escapedPassword the raw-escaped password; could be null
629: * @throws URIException escaped password not valid or username missed
630: */
631: public void setRawPassword(char[] escapedPassword)
632: throws URIException {
633: if (escapedPassword != null
634: && !validate(escapedPassword, within_userinfo)) {
635: throw new URIException(URIException.ESCAPING,
636: "escaped password not valid");
637: }
638: if (getRawUser() == null || getRawUser().length == 0) {
639: throw new URIException(URIException.PARSING,
640: "username required");
641: }
642: String username = new String(getRawUser());
643: String password = escapedPassword == null ? null : new String(
644: escapedPassword);
645: // an emtpy string is allowed as a password
646: String userinfo = username
647: + ((password == null) ? "" : ":" + password);
648: String hostname = new String(getRawHost());
649: String hostport = (_port == -1) ? hostname : hostname + ":"
650: + _port;
651: String authority = userinfo + "@" + hostport;
652: _userinfo = userinfo.toCharArray();
653: _authority = authority.toCharArray();
654: setURI();
655: }
656:
657: /**
658: * Set the escaped password string.
659: *
660: * @param escapedPassword the escaped password string; could be null
661: * @throws URIException escaped password not valid or username missed
662: */
663: public void setEscapedPassword(String escapedPassword)
664: throws URIException {
665: setRawPassword((escapedPassword == null) ? null
666: : escapedPassword.toCharArray());
667: }
668:
669: /**
670: * Set the password string.
671: *
672: * @param password the password string; could be null
673: * @throws URIException encoding error or username missed
674: */
675: public void setPassword(String password) throws URIException {
676: setRawPassword((password == null) ? null : encode(password,
677: allowed_within_userinfo, getProtocolCharset()));
678: }
679:
680: /**
681: * Get the raw-escaped password.
682: *
683: * @return the raw-escaped password
684: */
685: public char[] getRawPassword() {
686: int from = indexFirstOf(_userinfo, ':');
687: if (from == -1) {
688: return null; // null or only user.
689: }
690: int len = _userinfo.length - from - 1;
691: char[] result = new char[len];
692: System.arraycopy(_userinfo, from + 1, result, 0, len);
693: return result;
694: }
695:
696: /**
697: * Get the escaped password.
698: *
699: * @return the escaped password
700: */
701: public String getEscapedPassword() {
702: char[] password = getRawPassword();
703: return (password == null) ? null : new String(password);
704: }
705:
706: /**
707: * Get the password.
708: *
709: * @return the password
710: * @throws URIException If {@link #decode(char[],String)} fails.
711: */
712: public String getPassword() throws URIException {
713: char[] password = getRawPassword();
714: return (password == null) ? null : decode(password,
715: getProtocolCharset());
716: }
717:
718: // --------------------------------------------------------------- The path
719:
720: /**
721: * Get the raw-escaped current hierarchy level.
722: *
723: * @return the raw-escaped current hierarchy level
724: * @throws URIException If {@link #getRawCurrentHierPath(char[])} fails.
725: */
726: public char[] getRawCurrentHierPath() throws URIException {
727: return (_path == null || _path.length == 0) ? rootPath : super
728: .getRawCurrentHierPath(_path);
729: }
730:
731: /**
732: * Get the level above the this hierarchy level.
733: *
734: * @return the raw above hierarchy level
735: * @throws URIException If {@link #getRawCurrentHierPath(char[])} fails.
736: */
737: public char[] getRawAboveHierPath() throws URIException {
738: char[] path = getRawCurrentHierPath();
739: return (path == null || path.length == 0) ? rootPath
740: : getRawCurrentHierPath(path);
741: }
742:
743: /**
744: * Get the raw escaped path.
745: *
746: * @return the path '/' if empty or undefined
747: */
748: public char[] getRawPath() {
749: char[] path = super .getRawPath();
750: return (path == null || path.length == 0) ? rootPath : path;
751: }
752:
753: // -------------------------------------------------------------- The query
754:
755: /**
756: * Set the query as the name and value pair.
757: *
758: * @param queryName the query string.
759: * @param queryValue the query string.
760: * @throws URIException incomplete trailing escape pattern
761: * Or unsupported character encoding
762: * @throws NullPointerException null query
763: * @see #encode
764: */
765: public void setQuery(String queryName, String queryValue)
766: throws URIException, NullPointerException {
767:
768: StringBuffer buff = new StringBuffer();
769: // set the charset to do escape encoding
770: String charset = getProtocolCharset();
771: buff.append(encode(queryName, allowed_within_query, charset));
772: buff.append('=');
773: buff.append(encode(queryValue, allowed_within_query, charset));
774: _query = buff.toString().toCharArray();
775: setURI();
776: }
777:
778: /**
779: * Set the query as the name and value pairs.
780: *
781: * @param queryName the array of the query string.
782: * @param queryValue the array of the query string.
783: * @throws URIException incomplete trailing escape pattern,
784: * unsupported character encoding or wrong array size
785: * @throws NullPointerException null query
786: * @see #encode
787: */
788: public void setQuery(String[] queryName, String[] queryValue)
789: throws URIException, NullPointerException {
790:
791: int length = queryName.length;
792: if (length != queryValue.length) {
793: throw new URIException("wrong array size of query");
794: }
795:
796: StringBuffer buff = new StringBuffer();
797: // set the charset to do escape encoding
798: String charset = getProtocolCharset();
799: for (int i = 0; i < length; i++) {
800: buff.append(encode(queryName[i], allowed_within_query,
801: charset));
802: buff.append('=');
803: buff.append(encode(queryValue[i], allowed_within_query,
804: charset));
805: if (i + 1 < length) {
806: buff.append('&');
807: }
808: }
809: _query = buff.toString().toCharArray();
810: setURI();
811: }
812:
813: // ---------------------------------------------------------------- Utility
814:
815: /**
816: * Verify the valid class use for construction.
817: *
818: * @throws URIException the wrong scheme use
819: */
820: protected void checkValid() throws URIException {
821: // could be explicit protocol or undefined.
822: if (!(equals(_scheme, DEFAULT_SCHEME) || _scheme == null)) {
823: throw new URIException(URIException.PARSING,
824: "wrong class use");
825: }
826: }
827:
828: /**
829: * Once it's parsed successfully, set this URI.
830: *
831: * @see #getRawURI
832: */
833: protected void setURI() {
834: // set _uri
835: StringBuffer buf = new StringBuffer();
836: // ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
837: if (_scheme != null) {
838: buf.append(_scheme);
839: buf.append(':');
840: }
841: if (_is_net_path) {
842: buf.append("//");
843: if (_authority != null) { // has_authority
844: if (_userinfo != null) { // by default, remove userinfo part
845: if (_host != null) {
846: buf.append(_host);
847: if (_port != -1) {
848: buf.append(':');
849: buf.append(_port);
850: }
851: }
852: } else {
853: buf.append(_authority);
854: }
855: }
856: }
857: if (_opaque != null && _is_opaque_part) {
858: buf.append(_opaque);
859: } else if (_path != null) {
860: // _is_hier_part or _is_relativeURI
861: if (_path.length != 0) {
862: buf.append(_path);
863: }
864: }
865: if (_query != null) { // has_query
866: buf.append('?');
867: buf.append(_query);
868: }
869: // ignore the fragment identifier
870: _uri = buf.toString().toCharArray();
871: hash = 0;
872: }
873:
874: }
|