FileDocCategorySizeDatePackage
ViaHeader.javaAPI DocphoneME MR2 API (J2ME)14828Wed May 02 18:00:42 BST 2007gov.nist.siplite.header

ViaHeader

public class ViaHeader extends gov.nist.siplite.header.ParametersHeader
Via Header (these are strung together in a ViaList).
version
JAIN-SIP-1.1 This code is in the public domain.

Fields Summary
public static Class
clazz
Class handle.
public static final String
NAME
Via header field label.
public static final String
BRANCH
The branch parameter is included by every forking proxy.
public static final String
HIDDEN
The "hidden" paramter is included if this header field was hidden by the upstream proxy.
public static final String
RECEIVED
The "received" parameter is added only for receiver-added Via Fields.
public static final String
MADDR
The "maddr" parameter is designating the multicast address.
public static final String
TTL
The "TTL" parameter is designating the time-to-live value.
protected Protocol
sentProtocol
Sent protocol field.
protected HostPort
sentBy
Sent by field.
protected String
comment
Comment field.
Constructors Summary
public ViaHeader()
Default constructor.



     
        clazz = new ViaHeader().getClass();
    
        super(VIA);
        this.sentBy = new HostPort();

        sentProtocol = new Protocol();
    
Methods Summary
public java.lang.Objectclone()
Clone - do a deep copy.

return
Object Via

        ViaHeader retval = new ViaHeader();

        if (this.comment != null) retval.comment = new String(this.comment);
        if (this.parameters != null) retval.parameters =
                (NameValueList) parameters.clone();
        if (this.sentBy != null) retval.sentBy = (HostPort)sentBy.clone();
        if (this.sentProtocol != null)
            retval.sentProtocol = (Protocol)sentProtocol.clone();
        return retval;
    
public java.lang.StringencodeBody()
Encodes the via header into a cannonical string.

return
String containing cannonical encoding of via header.

        String encoding = "";
        encoding += sentProtocol.encode() + Separators.SP + sentBy.encode();

        // Add the default port if there is no port specified.
        if (! sentBy.hasPort()) encoding += Separators.COLON + "5060";

        if (comment != null) {
            encoding += Separators.LPAREN + comment + Separators.RPAREN;
        }

        encoding += encodeWithSep();

        return encoding;
    
public booleanequals(java.lang.Object other)
Compares two via headers for equaltiy.

param
other Object to set.
return
true if the two via headers are the same.

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

        ViaHeader that = (ViaHeader) other;

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

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

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

        return true;
    
public java.lang.StringgetBranch()
Gets the Branch parameter if it exists.

return
Branch field.

        return super.getParameter(ViaHeader.BRANCH);
    
public java.lang.StringgetComment()
Accessor for the comment field.

return
comment field.

        return comment;
    
public java.lang.StringgetHost()
Gest the host name. (null if not yet set).

return
host name from the via header.

        if (sentBy == null)
            return null;
        else {
            Host host = sentBy.getHost();
            if (host == null)
                return null;
            else return host.getHostname();
        }
    
public java.lang.StringgetMaddr()
Gets the maddr parameter if it exists.

return
maddr parameter.

        return super.getParameter(ViaHeader.MADDR);

    
public intgetPort()
Port of the Via header.

return
port field.

        if (sentBy == null)
            return -1;
        return sentBy.getPort();
    
public java.lang.StringgetProtocolVersion()
Gets the Protocol Version.

return
String

        if (sentProtocol == null)
            return null;
        else
            return sentProtocol.getProtocolVersion();
    
public java.lang.StringgetReceived()
Gets the received parameter if it exists.

return
received parameter.

        return super.getParameter(ViaHeader.RECEIVED);

    
public HostPortgetSentBy()
Accessor for the sentBy field

return
SentBy field

        return sentBy;
    
public ProtocolgetSentProtocol()
Accessor for the sentProtocol field.

return
Protocol field


        return sentProtocol;
    
public java.lang.StringgetTTL()
get the ttl parameter if it exists.

return
ttl parameter.

        return super.getParameter(ViaHeader.TTL);
    
public java.lang.StringgetTransport()
Gets the current transport.

return
the current transport

        if (this.sentProtocol == null)
            return null;
        else return this.sentProtocol.getTransport();
    
public java.lang.ObjectgetValue()
Gets the value portion of this header (does nto include the parameters).

return
the via header field value

        return sentProtocol.encode() + " " + sentBy.encode();

    
public booleanhasComment()
Comment of the Via Header.

return
false if comment does not exist and true otherwise.

        return comment != null;
    
public booleanhasPort()
Port of the Via Header.

return
true if Port exists.

        if (sentBy == null)
            return false;
        return (getSentBy()).hasPort();
    
public voidremoveComment()
Removes the comment field.

        comment = null;
    
public voidsetBranch(java.lang.String branch)
Sets the branch field. BNF (RFC3261, p. 232): via-branch = "branch" EQUAL token

param
branch the new branch value

        super.setParameter(BRANCH, branch);
    
public voidsetComment(java.lang.String c)
Sets the comment member

param
c String to set.

        comment = c;
    
public voidsetHeaderValue(java.lang.String value)
Sets the header value field (without parameters).

param
value is the value field to set.
throws
IllegalArgumentException if the value is invalid.


        StringMsgParser smp = new StringMsgParser();
        ViaList hl = null;
        String strNewHeader = NAME + Separators.COLON + value;

        try {
            hl = (ViaList)smp.parseHeader(strNewHeader);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e.toString());
        }

        if (hl.size() > 1) {
            throw new IllegalArgumentException("Ivalid Via header " +
                                               "value: " + value);
        }

        ViaHeader header = (ViaHeader)hl.elementAt(0);

        // Copy the values from the header created from the parsed value
        setSentBy(header.getSentBy());
        setSentProtocol(header.getSentProtocol());

        if (sentProtocol != null) {
            setProtocolVersion(header.getProtocolVersion());
            setTransport(header.getTransport());
        }

        if (sentBy != null) {
            setHost(header.getHost());
            setPort(header.getPort());
        }

        // IMPL_NOTE:
        // System.out.println("new: '" + strNewHeader + "'");
        // System.out.println("this: '" + strNewHeader + "'");
    
public voidsetHost(java.lang.String host)
Sets the host field. BNF (RFC3261, p. 222): host = hostname / IPv4address / IPv6reference See setMaddr() for the full BNF.

param
host the new host value

        this.sentBy.setHost(new Host(host));
    
public voidsetHost(Host host)
Sets the host field.

param
host the new host value

        this.sentBy.setHost(host);
    
public voidsetMaddr(java.lang.String maddr)
Sets the maddr parameter. BNF (RFC3261, p. 222, 232): via-maddr = "maddr" EQUAL host host = hostname / IPv4address / IPv6reference hostname = *( domainlabel "." ) toplabel [ "." ] domainlabel = alphanum / alphanum *( alphanum / "-" ) alphanum toplabel = ALPHA / ALPHA *( alphanum / "-" ) alphanum IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT IPv6reference = "[" IPv6address "]" IPv6address = hexpart [ ":" IPv4address ] hexpart = hexseq / hexseq "::" [ hexseq ] / "::" [ hexseq ] hexseq = hex4 *( ":" hex4) hex4 = 1*4HEXDIG IMPL_NOTE: check maddr for validity.

param
maddr the new maddr value.
throws
IllegalArgumentException if the new maddr value is invalid.

        super.setParameter(MADDR, maddr);
    
public voidsetParameter(java.lang.String name, java.lang.String value)
This function is overloaded in order to validate the parameters. 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. IMPL_NOTE: add a support for all parameters that are stored as members of this class.

param
name a String specifying the parameter name
param
value a String specifying the parameter value
throws
IllegalArgumentException if the parameter's name or its value is invalid.

        if (name.equalsIgnoreCase(ViaHeader.TTL)) {
            setTTL(value);
        } else if (name.equalsIgnoreCase(ViaHeader.RECEIVED)) {
            setReceived(value);
        } else {
            super.setParameter(name, value);
        }
    
public voidsetPort(int port)
Sets the port field. BNF (RFC3261, p. 232): port = 1*DIGIT

param
port the new port value

        this.sentBy.setPort(port);
    
public voidsetProtocolVersion(java.lang.String protocolVersion)
Sets the Protocol Version. BNF (RFC3261, p. 222, 232): protocol-version = token

param
protocolVersion String to set

        if (sentProtocol == null) sentProtocol = new Protocol();
        sentProtocol.setProtocolVersion(protocolVersion);
    
public voidsetReceived(java.lang.String received)
Sets the 'received' parameter. BNF (RFC3261, p. 223, 232): via-received = "received" EQUAL (IPv4address / IPv6address) IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT IPv6address = hexpart [ ":" IPv4address ] hexpart = hexseq / hexseq "::" [ hexseq ] / "::" [ hexseq ] hexseq = hex4 *( ":" hex4) hex4 = 1*4HEXDIG

param
received the new 'received' value.
throws
IllegalArgumentException if the new 'received' value is invalid.

        if (!Lexer.isValidIpv4Address(received) &&
            !Lexer.isValidIpv6Address(received)) {
            throw new IllegalArgumentException("Invalid IP address");
        }
        
        super.setParameter(RECEIVED, received);
    
public voidsetSentBy(HostPort s)
Sets the sentBy member

param
s HostPort to set.

        sentBy = s;
    
public voidsetSentProtocol(Protocol s)
Sets the sentProtocol member

param
s Protocol to set.

        sentProtocol = s;
    
public voidsetTTL(java.lang.String strTTL)
Sets the ttl parameter. BNF (RFC3261, p. 232): ttl = 1*3DIGIT ; 0 to 255

param
strTTL the new ttl value given in a string form.
throws
IllegalArgumentException if the new ttl value is invalid.

        int ttl = 0;

        try {
            ttl = Integer.parseInt(strTTL);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Cannot parse TTL '" +
                strTTL + "': " + e);
        }

        if (ttl < 0 || ttl > 255) {
            throw new IllegalArgumentException("Invalid TTL: " + strTTL);
        }

        super.setParameter(TTL, strTTL);
    
public voidsetTransport(java.lang.String transport)
Sets the transport string. BNF (RFC3261, p. 222, 232): transport = "UDP" / "TCP" / "TLS" / "SCTP" / other-transport

param
transport String to set

        if (sentProtocol == null) sentProtocol = new Protocol();
        sentProtocol.setTransport(transport);