001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: package gov.nist.siplite.address;
026:
027: import gov.nist.core.*;
028: import gov.nist.siplite.SIPConstants;
029:
030: /**
031: * Implementation of the URI class.
032: *
033: *
034: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
035: */
036: public class URI extends GenericObject {
037: /** POST Dial method label text. */
038: public static final String POSTDIAL = SIPConstants.GENERAL_POSTDIAL;
039: /** Phone context parameter label text. */
040: public static final String PHONE_CONTEXT_TAG = SIPConstants.GENERAL_PHONE_CONTEXT_TAG;
041: /** ISDN subaddress parameter label text. */
042: public static final String ISUB = SIPConstants.GENERAL_ISUB;
043: /** Provider parameter label text. */
044: public static final String PROVIDER_TAG = SIPConstants.GENERAL_PROVIDER_TAG;
045: /** User name parameter label text. */
046: public static final String USER = SIPConstants.GENERAL_USER;
047: /** Transport type parameter label text. */
048: public static final String TRANSPORT = SIPConstants.GENERAL_TRANSPORT;
049: /** Method name parameter label text. */
050: public static final String METHOD = SIPConstants.GENERAL_METHOD;
051: /** Time to live parameter label text. */
052: public static final String TTL = SIPConstants.GENERAL_TTL;
053: /** Mail address parameter label text. */
054: public static final String MADDR = SIPConstants.GENERAL_MADDR;
055: /** LR parameter label text. */
056: public static final String LR = SIPConstants.GENERAL_LR;
057:
058: /**
059: * Imbedded URI.
060: */
061: protected String uriString;
062: /** Current URI scheme. */
063: protected String scheme;
064:
065: /**
066: * Constuctor.
067: */
068: protected URI() {
069: }
070:
071: /**
072: * Constructor given the URI string.
073: * @param uriString The imbedded URI string.
074: * @throws URISyntaxException When there is a syntaz error in the
075: * imbedded URI.
076: */
077: public URI(String uriString) throws ParseException {
078: try {
079: this .uriString = uriString;
080: int colPos = uriString.indexOf(":");
081:
082: if (colPos == -1) { // no ":"
083: throw new ParseException(
084: "URI, no separator after scheme", 0);
085: }
086:
087: // Don't check the scheme's name, because according
088: // to the RFC 3261, p. 224 it may be almost any token,
089: // not only 'sip' and 'sips'.
090:
091: // rfc3261: when symbol '@' is present in URI, user part
092: // can't be empty
093: String uriCutScheme = uriString.substring(colPos + 1);
094: int symAtPos = uriCutScheme.indexOf("@");
095:
096: if (symAtPos == 0) { // first symbol is '@'
097: throw new ParseException("URI, no user part", 0);
098: }
099: } catch (Throwable e) {
100: throw new ParseException("URI, Bad URI format", 0);
101: }
102: }
103:
104: /**
105: * Encode the URI.
106: * @return The encoded URI
107: */
108: public String encode() {
109: return uriString;
110: }
111:
112: /**
113: * Encodes this URI.
114: * @return The encoded URI
115: */
116: public String toString() {
117: return this .encode();
118: }
119:
120: /**
121: * Returns the URI part of the address (without parameters)
122: * i.e. scheme:user@host:port.
123: * @return URI part of the address
124: */
125: public String getPlainURI() {
126: return encode();
127: }
128:
129: /**
130: * Overrides the base clone method.
131: * @return The Cloned strucutre,
132: */
133: public Object clone() {
134: try {
135: return new URI(this .uriString);
136: } catch (ParseException ex) {
137: throw new RuntimeException(ex.getMessage() + this .uriString);
138: }
139: }
140:
141: /**
142: * Returns the value of the "scheme" of
143: * this URI, for example "sip", "sips" or "tel".
144: *
145: * @return the scheme paramter of the URI
146: */
147: public String getScheme() {
148: return scheme;
149: }
150:
151: /**
152: * Sets the value of the "scheme" of
153: * this URI, for example "sip", "sips" or "tel".
154: *
155: * Ref. RFC 3261, p. 224:
156: * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
157: *
158: * @param sch the scheme of this URI
159: * @throws IllegalArgumentException if the scheme is invalid
160: */
161: public void setScheme(String sch) throws IllegalArgumentException {
162: final String errMsg = "Invalid scheme format";
163: char ch;
164:
165: // Check if the scheme is valid
166: if (sch == null || !StringTokenizer.isAlpha(sch.charAt(0))) {
167: throw new IllegalArgumentException(errMsg);
168: }
169:
170: for (int i = 1; i < sch.length(); i++) {
171: ch = sch.charAt(i);
172:
173: if (!StringTokenizer.isAlpha(ch)
174: && !StringTokenizer.isDigit(ch) && (ch != '+')
175: && (ch != '-') && (ch != '.')) {
176: throw new IllegalArgumentException(errMsg);
177: }
178: }
179:
180: scheme = sch;
181: }
182:
183: /**
184: * This method determines if this is a URI with a scheme of
185: * "sip" or "sips".
186: *
187: * @return true if the scheme is "sip" or "sips", false otherwise.
188: */
189: public boolean isSipURI() {
190: return this instanceof SipURI;
191:
192: }
193:
194: /**
195: * This method determines if this is a URI with a scheme of
196: * "tel"
197: *
198: * @return true if the scheme is "tel", false otherwise.
199: */
200: public boolean isTelURL() {
201: return this instanceof TelURL;
202: }
203:
204: }
|