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.siplite.parser.*;
028: import gov.nist.core.*;
029: import gov.nist.siplite.SIPConstants;
030:
031: /**
032: * Implementation of the JAIN-SIP address factory.
033: * @version JAIN-SIP-1.1
034: *
035: *
036: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
037: *
038: *
039: */
040: public class AddressFactory {
041:
042: /**
043: * Creates a new instance ofAddressFactoryImpl.
044: */
045: public AddressFactory() {
046: }
047:
048: /**
049: * Creates anAddress with the new display name and URI attribute
050: * values.
051: *
052: * @param displayName - the new string value of the display name of the
053: * address. A <code>null</code> value does not set the display name.
054: * @param uri - the new URI value of the address.
055: * @throws ParseException which signals that an error has been reached
056: * unexpectedly while parsing the displayName value.
057: * @return the new address
058: */
059: public Address createAddress(String displayName, URI uri) {
060: if (uri == null)
061: throw new NullPointerException("null URI");
062: Address addressImpl = new Address();
063: if (displayName != null)
064: addressImpl.setDisplayName(displayName);
065: addressImpl.setURI(uri);
066: return addressImpl;
067:
068: }
069:
070: /**
071: * Creates a sip uri.
072: *
073: * @param uri the uri to parse.
074: * @return the new URI
075: */
076: public SipURI createSipURI(String uri)
077: // throws java.netURISyntaxException {
078: throws ParseException {
079: if (uri == null)
080: throw new NullPointerException("null URI");
081: try {
082: StringMsgParser smp = new StringMsgParser();
083: SipURI sipUri = smp.parseSIPUrl(uri);
084: return (SipURI) sipUri;
085: } catch (ParseException ex) {
086: // throw new java.netURISyntaxException(uri, ex.getMessage());
087: throw new ParseException(ex.getMessage(), 0);
088: }
089:
090: }
091:
092: /**
093: * Creates a SipURI.
094: *
095: * @param user the user
096: * @param host the host.
097: * @return the new SIP URI
098: */
099: public SipURI createSipURI(String user, String host)
100: throws ParseException {
101: if (host == null)
102: throw new NullPointerException("null host");
103:
104: StringBuffer uriString = new StringBuffer("sip:");
105: if (user != null) {
106: uriString.append(user);
107: uriString.append("@");
108: }
109:
110: // if host is an IPv6 string we should enclose it in sq brackets
111: if (host.indexOf(':') != host.lastIndexOf(':')
112: && host.trim().charAt(0) != '[')
113: host = '[' + host + ']';
114:
115: uriString.append(host);
116:
117: StringMsgParser smp = new StringMsgParser();
118: try {
119:
120: SipURI sipUri = smp.parseSIPUrl(uriString.toString());
121: return sipUri;
122: } catch (ParseException ex) {
123: throw new ParseException(ex.getMessage(), 0);
124: }
125: }
126:
127: /**
128: * Creates a TelURL based on given URI string. The scheme or '+' should
129: * not be included in the phoneNumber string argument.
130: *
131: * @param uri the new string value of the phoneNumber.
132: * @return the new telephone URL
133: * @throws URISyntaxException if the URI string is malformed.
134: */
135: public TelURL createTelURL(String uri) throws ParseException {
136: if (uri == null)
137: throw new NullPointerException("null url");
138: String telUrl = "tel:" + uri;
139: try {
140: StringMsgParser smp = new StringMsgParser();
141: TelURL timp = (TelURL) smp.parseUrl(telUrl);
142: return (TelURL) timp;
143: } catch (ParseException ex) {
144: throw new ParseException(ex.getMessage(), 0);
145: }
146: }
147:
148: /**
149: * Creates a new address.
150: * @param uri the location to use
151: * @return the address
152: * @exception NullPointerException if uri is null
153: */
154: public Address createAddress(URI uri) {
155: if (uri == null)
156: throw new NullPointerException("null address");
157: Address addressImpl = new Address();
158: addressImpl.setURI(uri);
159: return addressImpl;
160: }
161:
162: /**
163: * Creates anAddress with the new address string value. The address
164: * string is parsed in order to create the new Address instance. Create
165: * with a String value of "*" creates a wildcard address. The wildcard
166: * can be determined if
167: * <code>(SipURIAddress.getURI).getUser() == *;</code>.
168: *
169: * @param address the new string value of the address.
170: * @return the new Address
171: * @throws ParseException which signals that an error has been reached
172: * unexpectedly while parsing the address value.
173: * @exception NullPointerException if address is null
174: */
175: public Address createAddress(String address) throws ParseException {
176: if (address == null)
177: throw new NullPointerException("null address");
178:
179: if (address.equals("*")) {
180: Address addressImpl = new Address();
181: addressImpl.setAddressType(Address.WILD_CARD);
182: return addressImpl;
183: } else {
184: StringMsgParser smp = new StringMsgParser();
185: return smp.parseAddress(address);
186: }
187: }
188:
189: /**
190: * Creates a URI based on given URI string. The URI string is parsed in
191: * order to create the new URI instance. Depending on the scheme the
192: * returned may or may not be aSipURI or TelURL cast as a URI.
193: *
194: * @param uri the new string value of the URI.
195: * @return the new SIP URI
196: * @throws URISyntaxException if the URI string is malformed.
197: * @exception NullPointerException if uri is null
198: */
199: public URI createURI(String uri) throws ParseException {
200: if (uri == null)
201: throw new NullPointerException("null arg");
202: try {
203: Lexer lexer = new Lexer("sip_urlLexer", uri);
204: Token token = lexer.peekNextToken();
205: // For cases when URI begins with "<" and for TCK passing
206: if (token.getTokenType() == LexerCore.LESS_THAN) {
207: lexer.consume();
208: token = lexer.peekNextToken();
209: uri = uri.substring(
210: uri.indexOf(LexerCore.LESS_THAN) + 1, uri
211: .lastIndexOf(LexerCore.GREATER_THAN));
212: }
213: URLParser urlParser = new URLParser(uri);
214: String scheme = token.getTokenValue();
215: if (scheme == null || !Lexer.isValidScheme(scheme))
216: throw new ParseException("bad scheme", 0);
217: if (Utils.equalsIgnoreCase(scheme, SIPConstants.SCHEME_SIP)) {
218: return (URI) urlParser.sipURL(token);
219: } else if (Utils.equalsIgnoreCase(scheme,
220: SIPConstants.SCHEME_SIPS)) {
221: return (URI) urlParser.sipURL(token);
222: } else if (Utils.equalsIgnoreCase(scheme,
223: SIPConstants.SCHEME_TEL)) {
224: return (URI) urlParser.telURL();
225: }
226: } catch (ParseException ex) {
227: throw new ParseException(ex.getMessage(), 0);
228: }
229: return new URI(uri);
230: }
231:
232: }
|