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:
026: package javax.microedition.sip;
027:
028: import java.util.Vector;
029:
030: import gov.nist.core.ParseException;
031: import gov.nist.microedition.sip.StackConnector;
032: import gov.nist.siplite.address.Address;
033: import gov.nist.siplite.address.SipURI;
034: import gov.nist.siplite.address.TelURL;
035: import gov.nist.siplite.address.URI;
036: import gov.nist.siplite.parser.Lexer;
037:
038: /**
039: * SipAddress provides a generic SIP address parser.
040: * @see JSR180 spec, v 1.0.1, p 52-57
041: *
042: */
043: public class SipAddress {
044: /**
045: * the nist-siplite corresponding address
046: */
047: private Address address;
048:
049: /**
050: * Construct a new SipAddress from string.
051: * @see JSR180 spec, v 1.0.1, p 60
052: *
053: */
054: public SipAddress(java.lang.String address)
055: throws IllegalArgumentException {
056: try {
057: this .address = StackConnector.addressFactory
058: .createAddress(address);
059: } catch (ParseException pe) {
060: throw new IllegalArgumentException(pe.getMessage());
061: }
062:
063: // NullPointerException may be thrown in case if address is null.
064: // We don't catch it because TCK expects this exception.
065: }
066:
067: /**
068: * Construct a new SipAddress from display name and URI.
069: * @param displayName - user display name
070: * @param URI - SIP URI
071: * @throws IllegalArgumentException - if there was an error
072: * in parsing the arguments.
073: */
074: public SipAddress(java.lang.String displayName, java.lang.String URI)
075: throws IllegalArgumentException {
076: try {
077: URI uri = StackConnector.addressFactory.createURI(URI);
078: this .address = StackConnector.addressFactory.createAddress(
079: displayName, uri);
080: if (false == address.isSIPAddress()) {
081: throw new IllegalArgumentException(
082: "Wrong requested scheme");
083: }
084: } catch (ParseException pe) {
085: throw new IllegalArgumentException(pe.getMessage());
086: } catch (java.lang.NullPointerException pe) {
087: throw new IllegalArgumentException(pe.getMessage());
088: }
089: }
090:
091: /**
092: * Returns the display name of SIP address.
093: * @return display name or null if not available
094: */
095: public java.lang.String getDisplayName() {
096: return address.getDisplayName();
097: }
098:
099: /**
100: * Sets the display name. Empty string "" removes the display name.
101: * @param name - display name
102: * @throws IllegalArgumentException - if the display name is invalid
103: */
104: public void setDisplayName(java.lang.String name)
105: throws IllegalArgumentException {
106: address.setDisplayName(name);
107: }
108:
109: /**
110: * Returns the scheme of SIP address.
111: * @return the scheme of this SIP address e.g. sip or sips
112: */
113: public java.lang.String getScheme() {
114: return address.getScheme();
115: }
116:
117: /**
118: * Sets the scheme of SIP address. Valid scheme format is defined
119: * in RFC 3261 [1] p.224
120: * @param scheme the scheme of SIP address
121: * @throws IllegalArgumentException if the scheme is invalid
122: */
123: public void setScheme(java.lang.String scheme)
124: throws IllegalArgumentException {
125: address.getURI().setScheme(scheme);
126: }
127:
128: /**
129: * Returns the user part of SIP address.
130: * @return user part of SIP address. Returns null if the
131: * user part is missing.
132: */
133: public java.lang.String getUser() {
134: return address.getUser();
135: }
136:
137: /**
138: * Sets the user part of SIP address.
139: * @param user - the user part
140: * @throws IllegalArgumentException - if the user part is invalid
141: */
142: public void setUser(java.lang.String user)
143: throws IllegalArgumentException {
144: address.setUser(user);
145: }
146:
147: /**
148: * Returns the URI part of the address (without parameters)
149: * i.e. scheme:user@host:port.
150: * @return the URI part of the address
151: */
152: public java.lang.String getURI() {
153: return address.getPlainURI();
154: }
155:
156: /**
157: * Sets the URI part of the SIP address (without parameters)
158: * i.e. scheme:user@host:port. Possible URI parameters are ignored.
159: * @param URI - URI part
160: * @throws IllegalArgumentException - if the URI is invalid
161: */
162: public void setURI(java.lang.String URI)
163: throws IllegalArgumentException {
164: address.setURI(URI);
165: }
166:
167: /**
168: * Returns the host part of the SIP address.
169: * @return host part of this address.
170: */
171: public java.lang.String getHost() {
172: return address.getHost();
173: }
174:
175: /**
176: * Sets the host part of the SIP address.
177: * @param host - host part
178: * @throws IllegalArgumentException - if the post part is invalid
179: */
180: public void setHost(java.lang.String host)
181: throws IllegalArgumentException {
182: address.setHost(host);
183: }
184:
185: /**
186: * Returns the port number of the SIP address. If port number is
187: * not set, return 5060.
188: * If the address is wildcard "*" return 0.
189: * @return the port number
190: */
191: public int getPort() {
192: return address.getPort();
193: }
194:
195: /**
196: * Sets the port number of the SIP address. Valid range is
197: * 0-65535, where 0 means that the port number is removed
198: * from the address URI.
199: * @param port port number, valid range 0-65535, 0 means
200: * that port number is removed from the address URI.
201: * @throws IllegalArgumentException if the port number is invalid.
202: */
203: public void setPort(int port) throws IllegalArgumentException {
204: if (port < 0 || port > 65535) {
205: throw new IllegalArgumentException("Invalid port: " + port);
206: }
207:
208: URI uri = address.getURI();
209: if (uri.isSipURI()) {
210: if (port == 0) {
211: ((SipURI) uri).removePort();
212: } else {
213: ((SipURI) uri).setPort(port);
214: }
215: }
216: // IMPL_NOTE : do something for the tel URL
217: }
218:
219: /**
220: * Returns the value associated with the named URI parameter.
221: * @param name - the name of the parameter
222: * @return the value of the named parameter,
223: * or empty string for parameters
224: * without value and null if the parameter is not defined
225: */
226: public java.lang.String getParameter(java.lang.String name) {
227: return address.getParameter(name);
228: }
229:
230: /**
231: * Sets the named URI parameter to the specified value. If the
232: * value is null
233: * the parameter is interpreted as a parameter without value.
234: * Existing parameter will be overwritten, otherwise the parameter
235: * is added.
236: * @param name - the named URI parameter
237: * @param value - the value
238: * @throws IllegalArgumentException - if the parameter is
239: * invalid RFC 3261,
240: * chapter 19.1.1 SIP and SIPS URI Components "URI parameters" p.149
241: */
242: public void setParameter(java.lang.String name,
243: java.lang.String value) throws IllegalArgumentException {
244: address.setParameter(name, value);
245: }
246:
247: /**
248: * Removes the named URI parameter.
249: * @param name - name of the parameter to be removed
250: */
251: public void removeParameter(java.lang.String name) {
252: URI uri = address.getURI();
253: if (uri.isSipURI())
254: ((SipURI) uri).removeParameter(name);
255: // IMPL_NOTE : do something for the tel URL
256: }
257:
258: /**
259: * Returns a String array of all parameter names.
260: * @return String array of parameter names. Returns null if
261: * the address does not have any parameters.
262: */
263: public java.lang.String[] getParameterNames() {
264: return address.getParameterNames();
265: }
266:
267: /**
268: * Returns a fully qualified SIP address, with display name, URI and URI
269: * parameters. If display name is not specified only a SIP URI is returned.
270: * If the port is not explicitly set (to 5060 or other value) it
271: * will be omitted
272: * from the address URI in returned String.
273: * @return a fully qualified SIP name address, SIP or SIPS URI
274: */
275: public java.lang.String toString() {
276: return address.toString();
277: }
278: }
|