001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027: package gov.nist.siplite.header;
028:
029: import gov.nist.core.*;
030:
031: /**
032: * CSeqHeader SIP Header.
033: *
034: *
035: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
036: *
037: * @version JAIN-SIP-1.1
038: *
039: */
040:
041: public class CSeqHeader extends ParameterLessHeader {
042: /** Class handle. */
043: public static Class clazz;
044:
045: /** Sequence header. */
046: public static final String NAME = Header.CSEQ;
047:
048: static {
049: clazz = new CSeqHeader().getClass();
050: }
051:
052: /**
053: * seqno field
054: */
055: protected Integer seqno;
056:
057: /**
058: * method field
059: */
060: protected String method;
061:
062: /**
063: * Constructor.
064: */
065: public CSeqHeader() {
066: super (CSEQ);
067: }
068:
069: /**
070: * Constructor given the sequence number and method.
071: *
072: * @param seqno is the sequence number to assign.
073: * @param method is the method string.
074: */
075: public CSeqHeader(int seqno, String method) {
076: this ();
077: this .seqno = new Integer(seqno);
078: this .method = method;
079: }
080:
081: /**
082: * Compare two cseq headers for equality.
083: * @param other Object to compare against.
084: * @return true if the two cseq headers are equals, false
085: * otherwise.
086: */
087: public boolean equals(Object other) {
088: if (!other.getClass().equals(this .getClass())) {
089: return false;
090: }
091: CSeqHeader that = (CSeqHeader) other;
092: if (!this .seqno.equals(that.seqno)) {
093: return false;
094: }
095: if (!equalsIgnoreCase(this .method, that.method)) {
096: return false;
097: }
098: return true;
099: }
100:
101: /**
102: * Return canonical header content. (encoded header except headerName:)
103: *
104: * @return encoded string.
105: */
106: public String encodeBody() {
107: return seqno + Separators.SP + method.toUpperCase();
108: }
109:
110: /**
111: * Get the method.
112: * @return String the method.
113: */
114: public String getMethod() {
115: return method.toUpperCase();
116: }
117:
118: /**
119: * Sets the sequence number of this CSeqHeaderHeader. The sequence number
120: * MUST be expressible as a 32-bit unsigned integer and MUST be less than
121: * 2**31.
122: *
123: * @param sequenceNumber - the sequence number to set.
124: * @throws InvalidArgumentException -- if the seq number is <= 0
125: */
126: public void setSequenceNumber(int sequenceNumber) {
127: if (sequenceNumber < 0)
128: throw new IllegalArgumentException(
129: "the sequence number parameter is < 0");
130: seqno = new Integer(sequenceNumber);
131: }
132:
133: /**
134: * Set the method member
135: *
136: * @param meth -- String to set
137: */
138: public void setMethod(String meth) {
139: if (meth == null)
140: throw new NullPointerException("parameter is null");
141: method = meth;
142: }
143:
144: /**
145: * Gets the sequence number of this CSeqHeaderHeader.
146: *
147: * @return sequence number of the CSeqHeaderHeader
148: */
149:
150: public int getSequenceNumber() {
151: if (this .seqno == null)
152: return 0;
153: else
154: return this .seqno.intValue();
155: }
156:
157: /**
158: * Copies the current instance.
159: * @return copy of current object
160: */
161: public Object clone() {
162: CSeqHeader retval = new CSeqHeader();
163:
164: if (this .seqno != null)
165: retval.seqno = new Integer(this .seqno.intValue());
166: retval.method = this .method;
167:
168: return retval;
169: }
170:
171: /**
172: * Gets the header value.
173: * @return the header value
174: */
175: public Object getValue() {
176: return seqno + Separators.SP + method.toUpperCase();
177: }
178:
179: /**
180: * Sets the header value field.
181: * @param value is the value field to set.
182: * @throws IllegalArgumentException if the value is invalid.
183: */
184: public void setHeaderValue(String value)
185: throws IllegalArgumentException {
186: int newSeqNo;
187: String newMethod;
188:
189: value = value.trim();
190:
191: // Check if the sequence number presents
192: int delimIndex = value.indexOf(' ');
193:
194: if (delimIndex == -1) {
195: // IMPL_NOTE: set some flag to indicate that
196: // the Sequence Number is not set.
197: setMethod(value.trim());
198: return;
199: }
200:
201: try {
202: String strNum = value.substring(0, delimIndex).trim();
203: newSeqNo = Integer.parseInt(strNum);
204: setSequenceNumber(newSeqNo);
205: } catch (IllegalArgumentException iae) {
206: throw iae;
207: }
208:
209: newMethod = value.substring(delimIndex, value.length()).trim();
210: setMethod(newMethod);
211: }
212:
213: /**
214: * Gets the parameters.
215: * @return name value list
216: */
217: public NameValueList getParameters() {
218: return null;
219: }
220:
221: }
|