FileDocCategorySizeDatePackage
SipURI.javaAPI DocphoneME MR2 API (J2ME)32247Wed May 02 18:00:42 BST 2007gov.nist.siplite.address

SipURI

public class SipURI extends URI
The SipURI structure. This code is in the public domain.

Fields Summary
protected Authority
authority
Authority for the uri.
protected NameValueList
uriParms
uriParms list
protected NameValueList
qheaders
qheaders list
protected TelephoneNumber
telephoneSubscriber
telephoneSubscriber field
private boolean
isServer
Flag if the instance is server URI.
private boolean
isShared
Flag if the instance is server shared URI.
Constructors Summary
public SipURI()
Default constructor.


       
      
        scheme = SIPConstants.SCHEME_SIP;
        uriParms = new NameValueList("uriparms");
        qheaders = new NameValueList("qheaders");
        qheaders.setSeparator("&");
    
Methods Summary
public voidclearPassword()
Clears the password from the user part if it exists.

        if (this.authority != null) {
            UserInfo userInfo = authority.getUserInfo();
            if (userInfo != null) userInfo.clearPassword();
        }
    
public voidclearQheaders()
Clears all Qheaders.

        qheaders = new NameValueList("qheaders");
    
public voidclearUriParms()
Clears all URI Parameters.

since
v1.0

        uriParms = new NameValueList("uriparms");
    
public java.lang.Objectclone()
Copies the current instance.

return
copy of current instance

        SipURI retval = new SipURI();
        retval.scheme = new String(this.scheme);
        retval.authority = (Authority) this.authority.clone();
        retval.uriParms = (NameValueList) this.uriParms.clone();
        if (this.qheaders != null)
            retval.qheaders =
                    (NameValueList) this.qheaders.clone();

        if (this.telephoneSubscriber != null) {
            retval.telephoneSubscriber =
                    (TelephoneNumber)
                    this.telephoneSubscriber.clone();
        }
        return retval;
    
public java.lang.Stringencode()
Constructs a URL from the parsed structure.

return
String

        StringBuffer retval =
                new StringBuffer(scheme).append(Separators.COLON);
        if (authority != null) {
            retval.append(authority.encode());
        }

        if (!uriParms.isEmpty()) {
            retval.append(Separators.SEMICOLON).append(uriParms.encode());
        }

        if (!qheaders.isEmpty()) {
            retval.append(Separators.QUESTION).append(qheaders.encode());
        }

        return retval.toString();
    
public booleanequals(java.lang.Object that)
Compares two URIs and return true if they are equal.

param
that the object to compare to.
return
true if the object is equal to this object.


        if (that == null) {
            return false;
        }

        if (!this.getClass().equals(that.getClass())) {
            return false;
        }

        SipURI other = (SipURI) that;

        // Compare the authority portion.
        if (!this.authority.equals(other.authority))
            return false;

        // compare the parameter lists.
        // Assuming default for TRANSPORT so cannot use the method
        // of NameValueList to check for equality
        NameValueList hisParms = other.uriParms;

        // transport comparation
        String currTransport = uriParms.getValueDefault
            (SIPConstants.GENERAL_TRANSPORT, SIPConstants.TRANSPORT_UDP);
        String hisTransport = hisParms.getValueDefault
            (SIPConstants.GENERAL_TRANSPORT, SIPConstants.TRANSPORT_UDP);
        if (!currTransport.equals(hisTransport))
            return false;

        // other parameters
        Enumeration enumNames = uriParms.getKeys();
        String currKey;
        while (enumNames.hasMoreElements()) {
            currKey = (String)enumNames.nextElement();
            if (currKey.equals(SIPConstants.GENERAL_TRANSPORT)) continue;
            // check that other URI contains this parameter
            if (!hisParms.hasNameValue(currKey))
                return false;
            // check that other URI contains the same value of this parameter
            if (!uriParms.getValue(currKey).equals(
                hisParms.getValue(currKey)))
                    return false;
        }

        // leave headers alone - they are just a screwy way of constructing
        // an entire sip message header as part of a URL.
        return true;
    
public AuthoritygetAuthority()
Gets the authority.

return
the authority information

        return this.authority;
    
public intgetDefaultPort()
Returns default port number according to current scheme.

return
Current scheme default port.

        return (scheme.equalsIgnoreCase(SIPConstants.SCHEME_SIP)) ?
            SIPConstants.DEFAULT_NONTLS_PORT : SIPConstants.DEFAULT_TLS_PORT;
    
public java.lang.StringgetHeader(java.lang.String name)
Returns the value of the named header, or null if it is not set. SIP/SIPS URIs may specify headers. As an example, the URI sip:joe@jcp.org?priority=urgent has a header "priority" whose value is "urgent".

param
name name of header to retrieve
return
the value of specified header

        return this.qheaders.getValue(name) != null ?
            this.qheaders.getValue(name).toString() : null;

    
public java.util.VectorgetHeaderNames()
Returns an Iterator over the names (Strings) of all headers present in this SipURI.

return
an Iterator over all the header names

        return this.qheaders.getNames();
    
public java.lang.StringgetHost()
Gets the host protion of the URI.

return
the host portion of the url.

        Host h = authority.getHost();
        return (h == null) ? "" : h.encode();
    
public HostPortgetHostPort()
Gets the host and port of the server.

return
get the host:port part of the url parsed into a structure.

        if (authority == null)
            return null;
        else {
            return authority.getHostPort();
        }
    
public java.lang.StringgetLrParam()
Returns the value of the lr parameter, or null if this is not set. This is equivalent to getParameter("lr").

return
the value of the lr parameter

        boolean haslr = this.hasParameter(LR);
        return haslr? "true":null;
    
public java.lang.StringgetMAddrParam()
Returns the value of the maddr parameter, or null if this is not set. This is equivalent to getParameter("maddr").

return
the value of the maddr parameter

        NameValue maddr = uriParms.getNameValue(MADDR);
        if (maddr == null)
            return null;
        String host = (String) maddr.getValue();
        return host;
    
public java.lang.StringgetMethod()
Gets the method parameter.

return
Method parameter.

        return (String) getParm(SIPConstants.GENERAL_METHOD);
    
public java.lang.StringgetMethodParam()
Returns the value of the method parameter, or null if this is not set. This is equivalent to getParameter("method").

return
the value of the method parameter

        return this.getParameter(METHOD);
    
public java.lang.StringgetParameter(java.lang.String name)
Returns the value of the named parameter, or null if it is not set. A zero-length String indicates flag parameter.

param
name name of parameter to retrieve
return
the value of specified parameter

        Object val = uriParms.getValue(name.toLowerCase());
        if (val == null)
            return null;
        if (val instanceof GenericObject)
            return ((GenericObject)val).encode();
        else return val.toString();
    
public java.util.VectorgetParameterNames()
Returns an Iterator over the names (Strings) of all parameters present in this ParametersHeader.

return
an Iterator over all the parameter names

        return uriParms.getNames();
    
public java.lang.ObjectgetParm(java.lang.String parmname)
Gets the parameter (do a name lookup) and return null if none exists.

param
parmname Name of the parameter to get.
return
Parameter of the given name (null if none exists).

        Object obj = uriParms.getValue(parmname);
        return obj;
    
public java.lang.StringgetPlainURI()
Returns the URI part of the address (without parameters) i.e. scheme:user@host:port.

return
URI part of the address

        StringBuffer retval =
                new StringBuffer(scheme).append(Separators.COLON);
        retval.append(getUserAtHostPort());
        return retval.toString();
    
public intgetPort()
Gets the port from the authority field.

return
the port from the authority field.

        HostPort hp = this.getHostPort();

        if (hp == null) {
            return -1;
        }

        return hp.getPort();
    
public NameValueListgetQheaders()
Accessor forSIPObjects

return
Get the query headers (that appear after the ? in the URL)

        return qheaders;
    
public intgetTTLParam()
Returns the value of the "ttl" parameter, or -1 if this is not set. This method is equivalent to getParameter("ttl").

return
the value of the ttl parameter

        Integer ttl = (Integer) uriParms.getValue(SIPConstants.GENERAL_TTL);
        if (ttl != null)
            return ttl.intValue();
        else
            return -1;
    
public TelephoneNumbergetTelephoneSubscriber()
Returns the stucture corresponding to the telephone number provided that the user is a telephone subscriber.

return
TelephoneNumber part of the url (only makes sense when user = phone is specified)

        if (telephoneSubscriber == null) {
            telephoneSubscriber = new TelephoneNumber();
        }
        return telephoneSubscriber;
    
public java.lang.StringgetTransportParam()
Returns the value of the "transport" parameter, or null if this is not set. This is equivalent to getParameter("transport").

return
the transport paramter of the SipURI

        if (uriParms != null) {
            return (String) uriParms.getValue(SIPConstants.GENERAL_TRANSPORT);
        } else return null;
    
public java.lang.StringgetTypeParam()
Returns the value of the "type" parameter, or null if this is not set. This is equivalent to getParameter("type"). The result is unquoted

return
the transport paramter of the SipURI

        if (uriParms != null) {
            NameValue nv = uriParms.getNameValue(SIPConstants.GENERAL_TYPE);
            if (nv == null) {
                return null;
            }
            return (String)nv.getUnquotedValue();
        } else return null;
    
public NameValueListgetUriParms()
Accessor for URI parameters

return
A name-value list containing the parameters.

        return uriParms;
    
public java.lang.StringgetUser()
Returns the value of the userParam, or null if this is not set.

This is equivalent to getParameter("user").

return
the value of the userParam of the SipURI

        return authority.getUser();
    
public java.lang.StringgetUserAtHost()
Gets user at host information.

return
user@host portion of the uri (null if none exists).

        if (authority == null) {
            return null;
        }

        String user = authority.getUserInfo().getUser();
        String host = authority.getHost().encode();

        return new StringBuffer(user).
            append(Separators.AT).append(host).toString();
    
public java.lang.StringgetUserAtHostPort()
Gets user at host and port infromation. instead of the current function body.

return
user@host portion of the uri (null if none exists).

        return authority.encode();
    
public java.lang.StringgetUserParam()
Returns the user part of this SipURI, or null if it is not set.

return
the user part of this SipURI

        return getParameter("user");
    
public java.lang.StringgetUserPassword()
Gets the password of the user.

return
User password when it embedded as part of the uri ( a very bad idea).

        if (authority == null)
            return null;
        return authority.getPassword();
    
public java.lang.StringgetUserType()
Gets the user parameter.

return
User parameter (user= phone or user=ip).

        return (String) uriParms.getValue(SIPConstants.GENERAL_USER);
    
public booleanhasLrParam()
Returns whether the the lr parameter is set. This is equivalent to hasParameter("lr"). This interface has no getLrParam as RFC3261 does not specify any values for the "lr" paramater.

return
true if the "lr" parameter is set, false otherwise.

        return uriParms.getNameValue(SIPConstants.GENERAL_LR) != null;
    
public booleanhasParameter(java.lang.String name)
Boolean to check if a parameter of a given name exists.

param
name Name of the parameter to check on.
return
a boolean indicating whether the parameter exists.

        return uriParms.getValue(name) != null;
    
public booleanhasTransport()
Returns true if the transport parameter is defined.

return
true if transport appears as a parameter and false otherwise.

        return hasParameter(TRANSPORT);
    
public booleanisSecure()
Returns true if this SipURI is secure i.e. if this SipURI represents a sips URI. A sip URI returns false.

return
true if this SipURI represents a sips URI, and false if it represents a sip URI.

        return equalsIgnoreCase(this.getScheme(), SIPConstants.SCHEME_SIPS);
    
public booleanisServer()
Checks if URI is shared.

return
true when URI is server else false

        return isServer;
    
public booleanisShared()
Checks if URI is shared.

return
true when URI is shared else false

        return isShared;
    
public booleanisSipURI()
This method determines if this is a URI with a scheme of "sip" or "sips".

return
true if the scheme is "sip" or "sips", false otherwise.

        return true;
    
public booleanisTelURL()
This method determines if this is a URI with a scheme of "tel"

return
true if the scheme is "tel", false otherwise.

        return false;
    
public booleanisUserTelephoneSubscriber()
Returns true if the user is a telephone subscriber. If the host is an Internet telephony gateway, a telephone-subscriber field MAY be used instead of a user field. The telephone-subscriber field uses the notation of RFC 2806 [19]. Any characters of the un-escaped "telephone-subscriber" that are not either in the set "unreserved" or "user-unreserved" MUST be escaped. The set of characters not reserved in the RFC 2806 description of telephone-subscriber contains a number of characters in various syntax elements that need to be escaped when used in SIP URLs, for example quotation marks (%22), hash (%23), colon (%3a), at-sign (%40) and the "unwise" characters, i.e., punctuation of %5b and above.

The telephone number is a special case of a user name and cannot be distinguished by a BNF. Thus, a URL parameter, user, is added to distinguish telephone numbers from user names.

The user parameter value "phone" indicates that the user part contains a telephone number. Even without this parameter, recipients of SIP URLs MAY interpret the pre-@ part as a telephone number if local restrictions on the

return
true if the user is a telephone subscriber.

        String usrtype = (String) uriParms.getValue(USER);
        if (usrtype == null) {
            return false;
        }
        return usrtype.equals("phone");
    
public voidremoveHeader(java.lang.String name)
Removes a header given its name (provided it exists).

param
name name of the header to remove.

        if (qheaders != null) qheaders.delete(name);
    
public voidremoveHeaders()
Removes all headers.

        qheaders = new NameValueList("qheaders");
    
public voidremoveMAddr()
Removes the maddr param if it exists.

        if (uriParms != null) uriParms.delete(MADDR);
    
public voidremoveMethod()
Removes the Method.

        if (uriParms != null) uriParms.delete(METHOD);
    
public voidremoveParameter(java.lang.String name)
Removes a parameter given its name

param
name -- name of the parameter to remove.

        uriParms.delete(name);
    
public voidremovePort()
Removes the port setting.

        authority.removePort();
    
public voidremoveTTL()
Removes the ttl value from the parameter list if it exists.

        if (uriParms != null) uriParms.delete(TTL);
    
public voidremoveTransport()
Deletes the transport string.

        if (uriParms != null) uriParms.delete(TRANSPORT);
    
public voidremoveUriParms()
Removes the URI parameters.

        uriParms = new NameValueList();
    
public voidremoveUser()
Removes the user.

        authority.removeUserInfo();
    
public voidremoveUserType()
Sets the user type.

        if (uriParms != null) uriParms.delete(USER);
    
public voidsetAuthority(Authority newAuthority)
Sets the authority member

param
newAuthority Authority to set.

        authority = newAuthority;
    
public voidsetDefaultParm(java.lang.String name, java.lang.Object value)
Sets the default parameters for this URI. Do nothing if the parameter is already set to some value. Otherwise set it to the given value.

param
name Name of the parameter to set.
param
value value of the parameter to set.

        if (uriParms.getValue(name) == null) {
            NameValue nv = new NameValue(name, value);
            uriParms.add(nv);
        }
    
public voidsetHeader(java.lang.String name, java.lang.String value)
Sets the value of the specified header fields to be included in a request constructed from the URI. If the header already had a value it will be overwritten.

param
name - a String specifying the header name
param
value - a String specifying the header value

        if (qheaders.getValue(name) == null) {
            NameValue nv = new NameValue(name, value);
            qheaders.add(nv);
        } else {
            NameValue nv = qheaders.getNameValue(name);
            nv.setValue(value);
        }
    
public voidsetHost(Host h)
Sets the host for this URI.

param
h host to set.

        // authority = new Authority();
        if (authority == null) {
            authority = new Authority();
        }
        authority.setHost(h);
    
public voidsetHost(java.lang.String host)
Returns the host part of this SipURI.

param
host the host part of the URI
throws
IllegalArgumentException if the host part is formated wrong way

        Lexer lexer = new Lexer("sip_urlLexer", host);
        HostNameParser hnp = new HostNameParser(lexer);
        try {
            Host h = new Host(hnp.hostName());
            if (GenericObject.compareToIgnoreCase(h.getHostname(), host) == 0) {
                this.setHost(h);
                return;
            }
        } catch (ParseException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
        throw new IllegalArgumentException("Illegal host name");
    
public voidsetHostPort(HostPort hostPort)
Sets the hostPort field of the embedded authority field.

param
hostPort is the hostPort to set.

        if (this.authority == null) {
            this.authority = new Authority();
        }
        authority.setHostPort(hostPort);
    
public voidsetIsdnSubAddress(java.lang.String isdnSubAddress)
Sets ISDN subaddress of SipURL.

param
isdnSubAddress ISDN subaddress

        if (telephoneSubscriber == null)
            telephoneSubscriber = new TelephoneNumber();
        telephoneSubscriber.setIsdnSubaddress(isdnSubAddress);
    
public voidsetLrParam()
Sets the value of the lr parameter of this SipURI. The lr parameter, when present, indicates that the element responsible for this resource implements the routing mechanisms specified in RFC 3261. This parameter will be used in the URIs proxies place in the Record-Route header field values, and may appear in the URIs in a pre-existing route set.

        if (uriParms.getValue(SIPConstants.GENERAL_LR) != null)
            return;
        NameValue nv = new NameValue(SIPConstants.GENERAL_LR, null);
        uriParms.add(nv);
    
public voidsetMAddr(java.lang.String mAddr)
Sets the MADDR parameter.

param
mAddr Host Name to set

        NameValue nameValue = uriParms.getNameValue(MADDR);
        Host host = new Host();
        host.setAddress(mAddr);

        if (nameValue != null) {
            nameValue.setValue(host);
        } else {
            nameValue = new NameValue(MADDR, host);
            uriParms.add(nameValue);
        }
    
public voidsetMAddrParam(java.lang.String maddr)
Sets the value of the maddr parameter of this SipURI. The maddr parameter indicates the server address to be contacted for this user, overriding any address derived from the host field. This is equivalent to setParameter("maddr", maddr).

param
maddr new value of the maddr parameter

        if (maddr == null)
            throw new NullPointerException("bad maddr");
        setParameter(SIPConstants.GENERAL_MADDR, maddr);
    
public voidsetMethod(java.lang.String method)
Sets the Method.

param
method method parameter

        uriParms.add(METHOD, method);
    
public voidsetMethodParam(java.lang.String method)
Sets the value of the method parameter. This specifies which SIP method to use in requests directed at this URI. This is equivalent to setParameter("method", method).

param
method - new value String value of the method parameter

        setParameter(SIPConstants.GENERAL_METHOD, method);
    
public voidsetParameter(java.lang.String name, java.lang.String value)
Sets the value of the specified parameter. If the parameter already had a value it will be overwritten. A zero-length String indicates flag parameter.

param
name - a String specifying the parameter name
param
value - a String specifying the parameter value
throws
ParseException which signals that an error has been reached unexpectedly while parsing the parameter name or value.

        if (name.equalsIgnoreCase(SIPConstants.GENERAL_TTL)) {
            try {
                int ttl = Integer.parseInt(value);
            } catch (NumberFormatException ex) {
                throw new ParseException("bad parameter " + value, 0);
            }
        }
        name = name.toLowerCase();
        // If the value is null the parameter is interpreted
        // as a parameter without value.
        NameValue nv = new NameValue(name,
                                     (value == null) ? "" : value);
        uriParms.delete(name);
        uriParms.add(nv);
    
public voidsetPort(int p)
Sets the port to a given value.

param
p Port to set.

        if (authority == null) authority = new Authority();
        authority.setPort(p);
    
public voidsetQHeader(NameValue nameValue)
Sets the query header when provided as a name-value pair.

param
nameValue qeuery header provided as a name,value pair.

        this.qheaders.set(nameValue);
    
public voidsetQheaders(NameValueList parms)
Sets the qheaders member.

param
parms query headers to set.

        qheaders = parms;
    
public voidsetScheme(java.lang.String scheme)
Constructor given the scheme. The scheme must be either Sip or Sips.

param
scheme URI protocol scheme
exception
IllegalArgumentException if scheme is no sip or sips

        if (compareToIgnoreCase(scheme, SIPConstants.SCHEME_SIP) != 0 &&
                compareToIgnoreCase(scheme, SIPConstants.SCHEME_SIPS) != 0)
            throw new IllegalArgumentException("bad scheme " + scheme);
        this.scheme = scheme.toLowerCase();
    
public voidsetSecure(boolean secure)
Sets the scheme of this URI to sip or sips depending on whether the argument is true or false. The default value is false.

param
secure - the boolean value indicating if the SipURI is secure.

        if (secure)
            scheme = SIPConstants.SCHEME_SIPS;
        else
            scheme = SIPConstants.SCHEME_SIP;
    
public voidsetServer()
Sets URI as server.

        isServer = true;
    
public voidsetShared()
Sets URI as shared.

        isShared = true;
    
public voidsetTTLParam(int ttl)
Sets the value of the ttl parameter. The ttl parameter specifies the time-to-live value when packets are sent using UDP multicast. This is equivalent to setParameter("ttl", ttl).

param
ttl - new value of the ttl parameter

        if (ttl <= 0)
            throw new IllegalArgumentException("Bad ttl value");
        if (uriParms != null) {
            uriParms.delete(SIPConstants.GENERAL_TTL);
            NameValue nv = new NameValue(SIPConstants.GENERAL_TTL,
                new Integer(ttl));
            uriParms.add(nv);
        }
    
public voidsetTelephoneSubscriber(TelephoneNumber tel)
Sets the telephone subscriber field.

param
tel Telephone subscriber field to set.

        telephoneSubscriber = tel;
    
public voidsetTransportParam(java.lang.String transport)
Sets the value of the "transport" parameter. This parameter specifies which transport protocol to use for sending requests and responses to this entity. The following values are defined: "udp", "tcp", "sctp", "tls", but other values may be used also. This method is equivalent to setParameter("transport", transport). Transport parameter constants are defined in the {@link gov.nist.siplite.ListeningPoint}.

param
transport - new value for the "transport" parameter
see
gov.nist.siplite.ListeningPoint

        if (transport == null)
            throw new NullPointerException("null arg");
        if (equalsIgnoreCase(transport, SIPConstants.TRANSPORT_UDP) ||
                equalsIgnoreCase(transport, SIPConstants.TRANSPORT_TCP)) {
            NameValue nv = new NameValue(SIPConstants.GENERAL_TRANSPORT,
                transport.toLowerCase());
            uriParms.delete(SIPConstants.GENERAL_TRANSPORT);
            uriParms.add(nv);
        } else
            throw new ParseException("bad transport " + transport, 0);
    
public voidsetUriParameter(NameValue nameValue)
Sets the parameter as given.

param
nameValue - parameter to set.

        uriParms.set(nameValue);
    
public voidsetUser(java.lang.String uname)
Sets the user of SipURI. The identifier of a particular resource at the host being addressed. The user and the user password including the "at" sign make up the user-info.

param
uname the new String value of the user.
throws
IllegalArgumentException if the user name is invalid

        if (authority == null) {
            authority = new Authority();
        }

        authority.setUser(uname);
    
public voidsetUserParam(java.lang.String usertype)
Sets the value of the user parameter. The user URI parameter exists to distinguish telephone numbers from user names that happen to look like telephone numbers. This is equivalent to setParameter("user", user).

param
usertype new value String value of the method parameter

        uriParms.delete(USER);
        uriParms.add(USER, usertype);
    
public voidsetUserPassword(java.lang.String password)
Sets the user password.

param
password - password to set.
throws
IllegalArgumentException if password contains invalid characters

        if (authority == null) {
            authority = new Authority();
        }
        authority.setPassword(password);
    
public java.lang.StringtoString()
Returns a string representation.

return
the String representation of this URI.

 return this.encode();