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