FileDocCategorySizeDatePackage
SipHeader.javaAPI DocphoneME MR2 API (J2ME)11390Wed May 02 18:00:42 BST 2007javax.microedition.sip

SipHeader

public class SipHeader extends Object
SipHeader provides generic SIP header parser helper. This class can be used to parse bare String header values that are read from SIP message using e.g. SipConnection.getHeader() method. It should be noticed that SipHeader is separate helper class and not mandatory to use for creating SIP connections.
see
JSR180 spec, v 1.0.1, p 47-51

Fields Summary
private gov.nist.siplite.header.ExtensionHeader
header
The nist-siplite corresponding header.
Constructors Summary
public SipHeader(String name, String value)
Constructs a SipHeader from name value pair. For example:
name = Contact
value = <sip:UserB@192.168.200.201>;expires=3600

param
name name of the header (Contact, Call-ID, ...)
param
value full header value as String
throws
IllegalArgumentException if the header value or name are invalid


                                                    
        
              
        if (name != null) {
            // trim() will cut control characters, so at first we have to check
            // that the header's name doesn't contain them.
            if ((name.indexOf('\n") != -1) || (name.indexOf('\r") != -1)) {
                throw new IllegalArgumentException("'" + name +
                    "' contains control character(s).");
            }

            name = name.trim();
        }

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

        // Validating the value.
        if (value != null) {
            value = value.trim();
        }

        if (value == null || value.equals("")) {
            throw new IllegalArgumentException(
                "Header's value must not be null or empty.");
        }

        try {
            if (Header.isAuthorization(name)) {
                Header h = StackConnector.headerFactory.
                    createHeader(name, value);
                NameValueList authParamList = h.getParameters();
                String authVal = h.getValue().toString();

                header = new ExtensionHeader(name, value, authVal);
                header.setParameters(authParamList);
            } else {
                ExtensionParser ep = new ExtensionParser(name + ":" + value);
                header = (ExtensionHeader)ep.parse();
            }
        } catch (ParseException pe) {
            throw new IllegalArgumentException(pe.getMessage());
        } catch (NullPointerException npe) {
            throw new IllegalArgumentException(npe.getMessage());
        }
    
Methods Summary
public java.lang.StringgetHeaderValue()
Returns the full header value including parameters. For example Alice <sip:alice@atlanta.com>;tag=1928301774

return
full header value including parameters

        return header.getHeaderValue();
    
public java.lang.StringgetName()
Returns the name of this header

return
the name of this header as String

        return header.getName();
    
public java.lang.StringgetParameter(java.lang.String name)
Returns the value of one header parameter. For example, from value <sip:UserB@192.168.200.201>;expires=3600 the method call getParameter(expires) will return 3600.

param
name name of the header parameter
return
value of header parameter. returns empty string for a parameter without value and null if the parameter does not exist.

        NameValueList parameterList = header.getParameters();

        if (parameterList == null || name == null) {
            return null;
        }

        name = name.trim();

        return parameterList.getParameter(name);
    
public java.lang.String[]getParameterNames()
Returns the names of header parameters. Returns null if there are no header parameters.

return
names of the header parameters. Returns null if there are no parameters.

        NameValueList parameterList = header.getParameters();

        if (parameterList == null || parameterList.size() == 0) {
            return null;
        }

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

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

        return parameterNames;
    
public java.lang.StringgetValue()
Returns the header value without header parameters. For example for header <sip:UserB@192.168.200.201>;expires=3600 method returns <sip:UserB@192.168.200.201> In the case of an authorization or authentication header getValue() returns only the authentication scheme e.g. Digest.

return
header value without header parameters

        String name  = header.getName();
        String value = header.getValue().toString();

        if (Header.isAuthorization(name)) {
            value = value.trim();
            int end = value.indexOf(' ");

            if (end == -1) {
                end = value.length();
            }

            return value.substring(0, end);
        } else {
            return value;
        }
    
public voidremoveParameter(java.lang.String name)
Removes the header parameter, if it is found in this header.

param
name name of the header parameter

        NameValueList parameterList = header.getParameters();

        if (parameterList != null && name != null) {
            parameterList.delete(name);
        }
    
public voidsetName(java.lang.String name)
Sets the header name, for example Contact

param
name Header name
throws
IllegalArgumentException if the name is invalid

        if (name != null) {
            name = name.trim();
        }

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

        header.setHeaderName(name);
    
public voidsetParameter(java.lang.String name, java.lang.String value)
Sets value of header parameter. If parameter does not exist it will be added. For example, for header value <sip:UserB@192.168.200.201> calling setParameter(expires, 3600) will construct header value <sip:UserB@192.168.200.201>;expires=3600. If the value is null, the parameter is interpreted as a parameter without value.

param
name name of the header parameter
param
value value of the parameter
throws
IllegalArgumentException if the parameter name or value are invalid

        NameValueList parameterList = header.getParameters();

        // Validating the name.
        if (name != null) {
            name = name.trim();
        }

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

        // Validating the value.
        if (value != null) {
            value = value.trim();
        }

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

        // If the value is null, the parameter is interpreted as a parameter
        // without value.
        if (value == null) {
            value = "";
        }

        header.setParameter(name, value);
    
public voidsetValue(java.lang.String value)
Sets the header value as String without parameters. For example <sip:UserB@192.168.200.201>. The existing (if any) header parameter values are not modified. For the authorization and authentication header this method sets the authentication scheme e.g. Digest.

param
value the header value
throws
IllegalArgumentException if the value is invalid or there is parameters included.

        if (value == null || value.equals("")) {
            throw new IllegalArgumentException(
                "Value must not be null or empty.");
        }

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

        String name = header.getName();

        if (Header.isAuthorization(name)) {
            // Authorization headers need additional validation.
            // An existing parser is used to check the validity of the value.
            try {
                Header h = StackConnector.headerFactory.
                    createHeader(name, value);
                if (((ParametersHeader)h).hasParameters()) {
                    throw new IllegalArgumentException("Value must not " +
                        "contain parameters.");
                }
            } catch (ParseException pe) {
                throw new IllegalArgumentException(pe.getMessage());
            }
        }

        header.setHeaderValue(value);
    
public java.lang.StringtoString()
Returns the String representation of the header according to header type. For example: From: Alice <sip:alice@atlanta.com>;tag=1928301774 WWW-Authenticate: Digest realm=atlanta.com, domain=sip:boxesbybob.com, qop=auth, nonce=f84f1cec41e6cbe5aea9c8e88d359, opaque=, stale=FALSE, algorithm=MD5

return
encoded string of object contents

        return header.toString();