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*/
26/******************************************************************************
27 * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).      *
28 ******************************************************************************/
29package gov.nist.javax.sip.header;
30
31import gov.nist.javax.sip.message.SIPRequest;
32
33import javax.sip.InvalidArgumentException;
34import javax.sip.header.CSeqHeader;
35import java.text.ParseException;
36
37/**
38 *  CSeq SIP Header.
39 *
40 * @author M. Ranganathan    <br/>
41 * @version 1.2 $Revision: 1.10 $ $Date: 2009/10/18 13:46:33 $
42 * @since 1.1
43 *
44 */
45
46public class CSeq extends SIPHeader implements javax.sip.header.CSeqHeader {
47
48    /**
49     * Comment for <code>serialVersionUID</code>
50     */
51    private static final long serialVersionUID = -5405798080040422910L;
52
53    /**
54     * seqno field
55     */
56    protected Long seqno;
57
58    /**
59     * method field
60     */
61    protected String method;
62
63    /**
64     * Constructor.
65     */
66    public CSeq() {
67        super(CSEQ);
68    }
69
70    /**
71     * Constructor given the sequence number and method.
72     *
73     * @param seqno is the sequence number to assign.
74     * @param method is the method string.
75     */
76    public CSeq(long seqno, String method) {
77        this();
78        this.seqno = Long.valueOf(seqno);
79        this.method = SIPRequest.getCannonicalName(method);
80    }
81
82    /**
83     * Compare two cseq headers for equality.
84     * @param other Object to compare against.
85     * @return true if the two cseq headers are equals, false
86     * otherwise.
87     */
88    public boolean equals(Object other) {
89
90        if (other instanceof CSeqHeader) {
91            final CSeqHeader o = (CSeqHeader) other;
92            return this.getSeqNumber() == o.getSeqNumber()
93                && this.getMethod().equals( o.getMethod() );
94        }
95        return false;
96    }
97
98    /**
99     * Return canonical encoded header.
100     * @return String with canonical encoded header.
101     */
102    public String encode() {
103        return headerName + COLON + SP + encodeBody() + NEWLINE;
104    }
105
106    /**
107     * Return canonical header content. (encoded header except headerName:)
108     *
109     * @return encoded string.
110     */
111    public String encodeBody() {
112        return encodeBody(new StringBuffer()).toString();
113    }
114
115    protected StringBuffer encodeBody(StringBuffer buffer) {
116        return buffer.append(seqno).append(SP).append(method.toUpperCase());
117    }
118
119    /**
120     * Get the method.
121     * @return String the method.
122     */
123    public String getMethod() {
124        return method;
125    }
126
127    /*
128     * (non-Javadoc)
129     * @see javax.sip.header.CSeqHeader#setSequenceNumber(long)
130     */
131    public void setSeqNumber(long sequenceNumber)
132        throws InvalidArgumentException {
133        if (sequenceNumber < 0 )
134            throw new InvalidArgumentException(
135                "JAIN-SIP Exception, CSeq, setSequenceNumber(), "
136                    + "the sequence number parameter is < 0 : " + sequenceNumber);
137        else if ( sequenceNumber >  ((long)1)<<32 - 1)
138            throw new InvalidArgumentException(
139                    "JAIN-SIP Exception, CSeq, setSequenceNumber(), "
140                        + "the sequence number parameter is too large : " + sequenceNumber);
141
142        seqno = Long.valueOf(sequenceNumber);
143    }
144
145    /**
146     * For backwards compatibility
147     */
148    public void setSequenceNumber(int sequenceNumber) throws InvalidArgumentException {
149        this.setSeqNumber( (long) sequenceNumber );
150    }
151
152    /*
153     * (non-Javadoc)
154     * @see javax.sip.header.CSeqHeader#setMethod(java.lang.String)
155     */
156    public void setMethod(String meth) throws ParseException {
157        if (meth == null)
158            throw new NullPointerException(
159                "JAIN-SIP Exception, CSeq"
160                    + ", setMethod(), the meth parameter is null");
161        this.method = SIPRequest.getCannonicalName(meth);
162    }
163
164    /*
165     * (non-Javadoc)
166     * @see javax.sip.header.CSeqHeader#getSequenceNumber()
167     */
168    public int getSequenceNumber() {
169        if (this.seqno == null)
170            return 0;
171        else
172            return this.seqno.intValue();
173    }
174
175
176
177
178    public long getSeqNumber() {
179        return this.seqno.longValue();
180    }
181
182
183}
184
185