1/*
2* Conditions Of Use
3*
4* This software was developed by employees of the National Institute of
5* Standards and Technology (NIST), an agency of the Federal Government.
6* Pursuant to title 15 Untied States Code Section 105, works of NIST
7* employees are not subject to copyright protection in the United States
8* and are considered to be in the public domain.  As a result, a formal
9* license is not needed to use the software.
10*
11* This software is provided by NIST as a service and is expressly
12* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15* AND DATA ACCURACY.  NIST does not warrant or make any representations
16* regarding the use of the software or the results thereof, including but
17* not limited to the correctness, accuracy, reliability or usefulness of
18* the software.
19*
20* Permission to use this software is contingent upon your acceptance
21* of the terms of this agreement
22*
23* .
24*
25*/
26package gov.nist.javax.sip.address;
27
28import gov.nist.javax.sip.parser.*;
29
30import java.text.ParseException;
31import javax.sip.address.*;
32
33/**
34 * Implementation of the JAIN-SIP address factory.
35 * @version 1.2 $Revision: 1.9 $ $Date: 2009/10/22 10:25:56 $
36 *
37 * @author M. Ranganathan   <br/>
38 *
39 *
40 *
41 * IPv6 Support added by Emil Ivov (emil_ivov@yahoo.com)<br/>
42 * Network Research Team (http://www-r2.u-strasbg.fr))<br/>
43 * Louis Pasteur University - Strasbourg - France<br/>
44 *
45 */
46public class AddressFactoryImpl implements javax.sip.address.AddressFactory {
47
48    /** Creates a new instance of AddressFactoryImpl
49     */
50    public AddressFactoryImpl() {
51    }
52
53
54    /**
55     *
56     *Create an empty address object.
57     *
58     *SPEC_REVISION
59     */
60
61    public javax.sip.address.Address createAddress() {
62        return new AddressImpl();
63    }
64    /**
65     * Creates an Address with the new display name and URI attribute
66     * values.
67     *
68     * @param displayName - the new string value of the display name of the
69     * address. A <code>null</code> value does not set the display name.
70     * @param uri - the new URI value of the address.
71     * @throws ParseException which signals that an error has been reached
72     * unexpectedly while parsing the displayName value.
73     */
74    public javax.sip.address.Address createAddress(
75        String displayName,
76        javax.sip.address.URI uri) {
77        if (uri == null)
78            throw new NullPointerException("null  URI");
79        AddressImpl addressImpl = new AddressImpl();
80        if (displayName != null)
81            addressImpl.setDisplayName(displayName);
82        addressImpl.setURI(uri);
83        return addressImpl;
84
85    }
86
87    /** create a sip uri.
88     *
89     *@param uri -- the uri to parse.
90     */
91    public javax.sip.address.SipURI createSipURI(String uri)
92    //  throws java.net.URISyntaxException {
93    throws ParseException {
94        if (uri == null)
95            throw new NullPointerException("null URI");
96        try {
97            StringMsgParser smp = new StringMsgParser();
98            SipUri sipUri = smp.parseSIPUrl(uri);
99            return (SipURI) sipUri;
100        } catch (ParseException ex) {
101            //  throw new java.net.URISyntaxException(uri, ex.getMessage());
102            throw new ParseException(ex.getMessage(), 0);
103        }
104
105    }
106
107    /** Create a SipURI
108     *
109     *@param user -- the user
110     *@param host -- the host.
111     */
112    public javax.sip.address.SipURI createSipURI(String user, String host)
113        throws ParseException {
114        if (host == null)
115            throw new NullPointerException("null host");
116
117        StringBuffer uriString = new StringBuffer("sip:");
118        if (user != null) {
119            uriString.append(user);
120            uriString.append("@");
121        }
122
123        //if host is an IPv6 string we should enclose it in sq brackets
124        if (host.indexOf(':') != host.lastIndexOf(':')
125            && host.trim().charAt(0) != '[')
126            host = '[' + host + ']';
127
128        uriString.append(host);
129
130        StringMsgParser smp = new StringMsgParser();
131        try {
132
133            SipUri sipUri = smp.parseSIPUrl(uriString.toString());
134            return sipUri;
135        } catch (ParseException ex) {
136            throw new ParseException(ex.getMessage(), 0);
137        }
138    }
139
140    /**
141     * Creates a TelURL based on given URI string. The scheme or '+' should
142     * not be included in the phoneNumber string argument.
143     *
144     * @param uri - the new string value of the phoneNumber.
145     * @throws URISyntaxException if the URI string is malformed.
146     */
147    public javax.sip.address.TelURL createTelURL(String uri)
148        throws ParseException {
149        if (uri == null)
150            throw new NullPointerException("null url");
151        String telUrl = "tel:" + uri;
152        try {
153            StringMsgParser smp = new StringMsgParser();
154            TelURLImpl timp = (TelURLImpl) smp.parseUrl(telUrl);
155            return (TelURL) timp;
156        } catch (ParseException ex) {
157            throw new ParseException(ex.getMessage(), 0);
158        }
159    }
160
161    public javax.sip.address.Address createAddress(javax.sip.address.URI uri) {
162        if (uri == null)
163            throw new NullPointerException("null address");
164        AddressImpl addressImpl = new AddressImpl();
165        addressImpl.setURI(uri);
166        return addressImpl;
167    }
168
169    /**
170     * Creates an Address with the new address string value. The address
171     * string is parsed in order to create the new Address instance. Create
172     * with a String value of "*" creates a wildcard address. The wildcard
173     * can be determined if
174     * <code>((SipURI)Address.getURI).getUser() == *;</code>.
175     *
176     * @param address - the new string value of the address.
177     * @throws ParseException which signals that an error has been reached
178     * unexpectedly while parsing the address value.
179     */
180    public javax.sip.address.Address createAddress(String address)
181        throws java.text.ParseException {
182        if (address == null)
183            throw new NullPointerException("null address");
184
185        if (address.equals("*")) {
186            AddressImpl addressImpl = new AddressImpl();
187            addressImpl.setAddressType(AddressImpl.WILD_CARD);
188            SipURI uri = new SipUri();
189            uri.setUser("*");
190            addressImpl.setURI( uri );
191            return addressImpl;
192        } else {
193            StringMsgParser smp = new StringMsgParser();
194            return smp.parseAddress(address);
195        }
196    }
197
198    /**
199     * Creates a URI based on given URI string. The URI string is parsed in
200     * order to create the new URI instance. Depending on the scheme the
201     * returned may or may not be a SipURI or TelURL cast as a URI.
202     *
203     * @param uri - the new string value of the URI.
204     * @throws URISyntaxException if the URI string is malformed.
205     */
206
207    public javax.sip.address.URI createURI(String uri) throws ParseException {
208        if (uri == null)
209            throw new NullPointerException("null arg");
210        try {
211            URLParser urlParser = new URLParser(uri);
212            String scheme = urlParser.peekScheme();
213            if (scheme == null)
214                throw new ParseException("bad scheme", 0);
215            if (scheme.equalsIgnoreCase("sip")) {
216                return (javax.sip.address.URI) urlParser.sipURL(true);
217            } else if (scheme.equalsIgnoreCase("sips")) {
218                return (javax.sip.address.URI) urlParser.sipURL(true);
219            } else if (scheme.equalsIgnoreCase("tel")) {
220                return (javax.sip.address.URI) urlParser.telURL(true);
221            }
222        } catch (ParseException ex) {
223            throw new ParseException(ex.getMessage(), 0);
224        }
225        return new gov.nist.javax.sip.address.GenericURI(uri);
226    }
227
228}
229