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

Address

public class Address extends GenericObject
Address structure. Imbeds a URI and adds a display name. This code is in the public domain.
version
JAIN-SIP-1.1

Fields Summary
public static final int
NAME_ADDR
Constant field.
public static final int
ADDRESS_SPEC
Constant field.
public static final int
WILD_CARD
Constant field.
protected int
addressType
Address type.
protected String
displayName
Display name field.
protected URI
address
Address field.
Constructors Summary
public Address()
Default constructor.

        this.addressType = NAME_ADDR;
    
Methods Summary
public java.lang.Objectclone()
Clone this structure.

return
Object Address

        Address retval = new Address();
        retval.addressType = this.addressType;
        if (displayName != null) {
            retval.displayName = new String(displayName);
        }
        if (address != null) {
            retval.address = (URI) address.clone();
        }
        return (Object) retval;

    
public java.lang.Stringencode()
Encodes the address as a string and return it.

return
String canonical encoded version of this address.

        if (this.addressType == WILD_CARD) {
            return "*";
        }

        StringBuffer encoding = new StringBuffer();

        if (displayName != null) {
            // Now the quotes are added to the displayName in AddressParser
            // if they presented in the original header
            encoding.append(displayName).append(Separators.SP);
        }

        if (address != null) {
            if (addressType == NAME_ADDR || displayName != null) {
                encoding.append(Separators.LESS_THAN);
            }
            encoding.append(address.encode());
            if (addressType == NAME_ADDR || displayName != null) {
                encoding.append(Separators.GREATER_THAN);
            }
        }

        return encoding.toString();
    
public booleanequals(java.lang.Object other)
Compares two address specs for equality.

param
other Object to compare this this address
return
true if the objects match

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

        Address that = (Address) other;
        if (this.addressType == WILD_CARD &&
            that.addressType != WILD_CARD) {
            return false;
        }

        // Ignore the display name; only compare the address spec.
        boolean retval = this.address.equals(that.address);
        return retval;
    
public intgetAddressType()
Gets the address type.

return
the addres type

        return addressType;
    
public java.lang.StringgetDisplayName()
Gets the display name.

return
the display name

        return displayName;
    
public java.lang.StringgetHost()
Gets the host name from the address.

return
the host name.

        // if the address is wildcard ("*"), all properties are null
        if (this.addressType == WILD_CARD) {
            return null;
        }
        if (address.isSipURI()) {
            // IMPL_NOTE: why not SipURI.getHost()?
            return ((SipURI) address).getHostPort().getHost().getHostname();
        }
        if (Logging.REPORT_LEVEL <= Logging.ERROR) {
            Logging.report(Logging.ERROR, LogChannels.LC_JSR180,
                           "getHost() for wrong URI");
        }
        return null;
    
public HostPortgetHostPort()
Gets the host port portion of the address spec.

return
host:port in a HostPort structure.


                        
       
        // if the address is wildcard ("*"), all properties are null
        if (this.addressType == WILD_CARD) {
            return null;
        }
        if (address.isSipURI()) {
            return ((SipURI) address).getHostPort();
        }
        if (Logging.REPORT_LEVEL <= Logging.ERROR) {
            Logging.report(Logging.ERROR, LogChannels.LC_JSR180,
                           "Wrong URI HostPort request");
        }
        return null;
    
public java.lang.StringgetParameter(java.lang.String name)
Returns the value associated with the named URI parameter.

param
name - the name of the parameter
return
the value of the named parameter, or empty string for parameters without value and null if the parameter is not defined

        URI uri = getURI();
        if (uri.isSipURI()) {
            return ((SipURI)uri).getParameter(name);
        }
        // IMPL_NOTE : return something for the tel URL
        return null;
    
public java.lang.String[]getParameterNames()
Returns a String array of all parameter names.

return
String array of parameter names. Returns null if the address does not have any parameters.

        if (addressType == WILD_CARD) {
            return null;
        }
        Vector parameterNameList;
        if (address.isSipURI()) {
            parameterNameList = ((SipURI)address).getParameterNames();

        } else if (address.isTelURL()) {
            parameterNameList = ((TelURL)address).getParameterNames();
        } else {
            return null;
        }
        if (null == parameterNameList || parameterNameList.size() == 0) {
            return null;
        }

        String parameterNames[] = new String[parameterNameList.size()];

        for (int i = 0; i < parameterNameList.size(); i++)
            parameterNames[i] = (String)parameterNameList.elementAt(i);

        return parameterNames;
    
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

        if (addressType == WILD_CARD) {
            return null;
        }
        return address.getPlainURI();
    
public intgetPort()
Gets the port from the imbedded URI. This assumes that a SIP URL is encapsulated in this address object.

return
the port from the address.

        // if the address is wildcard ("*"), return 0
        if (this.addressType == WILD_CARD) {
            return 0;
        }
        if (address.isSipURI()) {
            SipURI uri = (SipURI) address;
            int port = uri.getPort();
            if (port < 0) {
                port = uri.getDefaultPort();
            }
            return port;
        }
        if (Logging.REPORT_LEVEL <= Logging.ERROR) {
            Logging.report(Logging.ERROR, LogChannels.LC_JSR180,
                           "getPort() for wrong URI");
        }
        // if the port is not set, return the default (5060)
        return 5060;
    
public java.lang.StringgetScheme()
Returns the scheme of SIP address.

return
the scheme of this SIP address e.g. sip or sips

        if (addressType == WILD_CARD) {
            return null;
        }
        return address.getScheme();
    
public URIgetURI()
Returns the URI address of this Address. The type of URI can be determined by the scheme.

return
address parmater of the Address object

        return this.address;
    
public java.lang.StringgetUser()
Returns the user part of SIP address.

return
user part of SIP address. Returns null if the user part is missing or URI has unknown type

        // RFC3261 p.222
        // SIP-URI   =  "sip:" [ userinfo ] hostport
        //             uri-parameters [ headers ]
        // SIPS-URI  =  "sips:" [ userinfo ] hostport
        //             uri-parameters [ headers ]
        // userinfo =  ( user / telephone-subscriber ) [ ":" password ] "@"
        //
        // This function returns user name/telephon number + password if it is
        // existing
        //
        // if the address is wildcard ("*"), all properties are null

        if (addressType == WILD_CARD) {
            return null;
        }
        if (address.isSipURI()) {
            SipURI uri = (SipURI) address;
            String psswd = uri.getUserPassword();
            if (null != psswd) {
                return new String(uri.getUser() +
                                  Separators.COLON +
                                  psswd);
            } else {
                return uri.getUser();
            }
        } else if (address.isTelURL()) {
            // IMPL_NOTE: Do we have TelURL only?
            return ((TelURL) address).getPhoneNumber();
        } else {
            return null;
        }
    
public java.lang.StringgetUserAtHostPort()
Gets the user@host:port for the address field. This assumes that the encapsulated object is a SipURI.

return
string containing user@host:port.

        // if the address is wildcard ("*"), all properties are null
        if (addressType == WILD_CARD) {
            return null;
        }

        if (address.isSipURI()) {
            SipURI uri = (SipURI) address;
            return uri.getUserAtHostPort();
        }
        if (Logging.REPORT_LEVEL <= Logging.ERROR) {
            Logging.report(Logging.ERROR, LogChannels.LC_JSR180,
                           "getUserAtHostPort() for wrong URI");
        }
        return address.toString();

    
public booleanhasDisplayName()
return true if DisplayName exist.

return
boolean

        return (displayName != null);
    
public booleanisSIPAddress()
Return true if the embedded URI is a sip URI.

return
true if the embedded URI is a SIP URI.

        return address.isSipURI();
    
public booleanisWildcard()
This determines if this address is a wildcard address. That is Address.getAddress.getUserInfo() == *;

return
true if this name address is a wildcard, false otherwise.

        return this.addressType == WILD_CARD;
    
public voidremoveDisplayName()
remove the displayName field

        displayName = null;
    
public voidremoveParameter(java.lang.String parameterName)
Removes a parameter from the address.

param
parameterName is the name of the parameter to remove.

        // if the address is wildcard ("*"), all properties are null
        if (this.addressType == WILD_CARD) {
            return;
        }
        if (address.isSipURI()) {
            ((SipURI) address).removeParameter(parameterName);
        }
    
public voidsetAddressType(int atype)
Sets the address type. The address can be NAME_ADDR, ADDR_SPEC or WILD_CARD.

param
atype int to set

        addressType = atype;
    
public voidsetDisplayName(java.lang.String dn)
Sets the display name member.

param
dn String to set
throws
IllegalArgumentException if the name contains invalid characters


        if (null == dn) {
            throw new IllegalArgumentException("Null pointer argument");
        }

        if (dn.equals("")) {
            // JSR180: Empty string "" removes the display name.
            //         getDisplayName() returns null if
            //         the display name is not available.
            displayName = null;
        } else {
            if (false == Lexer.isValidDisplayName(dn)) {
                throw new
                        IllegalArgumentException("Invalid display name " +
                                                 dn);
            }
            displayName = dn;
            addressType = NAME_ADDR;
        }
    
public voidsetHost(java.lang.String host)
Sets the host part of the SIP address.

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

        if (address.isSipURI()) {
            ((SipURI)address).setHost(host);
        } else
            ((TelURL)address).setPostDial(host);
    
public voidsetParameter(java.lang.String name, java.lang.String value)
Sets the named URI parameter to the specified value. If the value is null the parameter is interpreted as a parameter without value. Existing parameter will be overwritten, otherwise the parameter is added.

param
name - the named URI parameter
param
value - the value
throws
IllegalArgumentException - if the parameter is invalid RFC 3261, chapter 19.1.1 SIP and SIPS URI Components "URI parameters" p.149

        // Validating the name.
        if (!Lexer.isValidName(name)) {
            throw new IllegalArgumentException("Invalid parameter's name.");
        }

        // Validating the value.
        if (!Lexer.isValidParameterValue(value)) {
            throw new IllegalArgumentException("Invalid parameter's value.");
        }

        URI uri = getURI();
        if (uri.isSipURI()) {
            try {
                ((SipURI)uri).setParameter(name, value);
            } catch (ParseException pe) {
                throw new IllegalArgumentException(pe.getMessage());
            }
        }
        // IMPL_NOTE : do something for the tel URL
    
public voidsetURI(URI addr)
Sets the address field.

param
addr SipURI to set

        address = (URI) addr;
    
public voidsetURI(java.lang.String URI)
Parses and creates the URI field.

param
URI SipURI to set

        URI uri = null;
        try {
            uri = StackConnector.addressFactory.createURI(URI);
        } catch (ParseException pe) {
            throw new IllegalArgumentException(pe.getMessage());
        }

        if (uri == null)
            throw new IllegalArgumentException("The URI is invalid");

        if (uri.isSipURI()) {
            ((SipURI)uri).clearUriParms();

            // copy parameters value from old uri
            if (address.isSipURI()) {
                SipURI addr = (SipURI)address;
                Vector names = addr.getParameterNames();
                String value;
                String name;
                for (int i = 0; i < names.size(); i++) {
                    name = (String)names.elementAt(i);
                    value = addr.getParameter(name);
                    try {
                        ((SipURI)uri).setParameter(name, value);
                    } catch (ParseException ex) {
                        // intentionally ignored
                        // parameters were parsed already
                    }
                }

            }
        }

        setURI(uri);
    
public voidsetUser(java.lang.String user)
Sets the user part of the embedded URI.

param
user user name to set for the embedded URI.
throws
IllegalArgumentException if the user part contains invalid characters

        // if the address is wildcard ("*"), all properties are null
        if (addressType == WILD_CARD) {
            return;
        }

        if (address.isSipURI()) {
            SipURI uri = (SipURI) address;
            int colonPos;
            if (null != user && 
                (colonPos = user.indexOf(Separators.COLON)) != -1) {
                uri.setUser(user.substring(0, colonPos));
                uri.setUserPassword(user.substring(colonPos + 1));
            } else {
                uri.setUser(user);
                uri.clearPassword();
            }
        } else if (address.isTelURL()) {
            // IMPL_NOTE : check if the tel number is valid
            ((TelURL) address).setPhoneNumber(user);
        } else {
            throw new IllegalArgumentException("Can't set a user " +
                                               "for this type of address");
        }
    
public java.lang.StringtoString()
Creates a string representation of the address object.

return
String canonical encoded version of this address.

        return encode();