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 *******************************************************************************/
29 
30 package gov.nist.javax.sip.header;
31 
32 import javax.sip.*;
33 import java.text.ParseException;
34 import javax.sip.header.*;
35 
36 /**
37  * Retry-After SIP Header.
38  *
39  * @version 1.2 $Revision: 1.9 $ $Date: 2009/11/04 17:35:55 $
40  *
41  * @author M. Ranganathan   <br/>
42  * @author Olivier Deruelle <br/>
43  *
44  *
45  */
46 public class RetryAfter extends ParametersHeader implements RetryAfterHeader {
47 
48     /**
49      * Comment for <code>serialVersionUID</code>
50      */
51     private static final long serialVersionUID = -1029458515616146140L;
52 
53     /** constant DURATION parameter.
54      */
55     public static final String DURATION = ParameterNames.DURATION;
56 
57     /** duration field
58      */
59     protected Integer retryAfter = new Integer(0);
60 
61     /** comment field
62      */
63     protected String comment;
64 
65     /** Default constructor
66      */
RetryAfter()67     public RetryAfter() {
68         super(NAME);
69     }
70 
71     /** Encode body of this into cannonical form.
72      * @return encoded body
73      */
encodeBody()74     public String encodeBody() {
75         StringBuffer s = new StringBuffer();
76 
77         if (retryAfter != null)
78             s.append(retryAfter);
79 
80         if (comment != null)
81             s.append(SP + LPAREN + comment + RPAREN);
82 
83         if (!parameters.isEmpty()) {
84             s.append(SEMICOLON + parameters.encode());
85         }
86 
87         return s.toString();
88     }
89 
90     /** Boolean function
91      * @return true if comment exist, false otherwise
92      */
hasComment()93     public boolean hasComment() {
94         return comment != null;
95     }
96 
97     /** remove comment field
98      */
removeComment()99     public void removeComment() {
100         comment = null;
101     }
102 
103     /** remove duration field
104      */
removeDuration()105     public void removeDuration() {
106         super.removeParameter(DURATION);
107     }
108 
109     /**
110      * Sets the retry after value of the RetryAfterHeader.
111      * The retry after value MUST be greater than zero and
112      * MUST be less than 2**31.
113      *
114      * @param retryAfter - the new retry after value of this RetryAfterHeader
115      * @throws InvalidArgumentException if supplied value is less than zero.
116      *
117      */
118 
setRetryAfter(int retryAfter)119     public void setRetryAfter(int retryAfter) throws InvalidArgumentException {
120         if (retryAfter < 0)
121             throw new InvalidArgumentException(
122                 "invalid parameter " + retryAfter);
123         this.retryAfter = Integer.valueOf(retryAfter);
124     }
125 
126     /**
127      * Gets the retry after value of the RetryAfterHeader. This retry after
128      * value is relative time.
129      *
130      * @return the retry after value of the RetryAfterHeader.
131      *
132      */
133 
getRetryAfter()134     public int getRetryAfter() {
135         return retryAfter.intValue();
136     }
137 
138     /**
139      * Gets the comment of RetryAfterHeader.
140      *
141      * @return the comment of this RetryAfterHeader, return null if no comment
142      * is available.
143      */
144 
getComment()145     public String getComment() {
146         return comment;
147     }
148 
149     /**
150      * Sets the comment value of the RetryAfterHeader.
151      *
152      * @param comment - the new comment string value of the RetryAfterHeader.
153      * @throws ParseException which signals that an error has been reached
154      * unexpectedly while parsing the comment.
155      */
156 
setComment(String comment)157     public void setComment(String comment) throws ParseException {
158         if (comment == null)
159             throw new NullPointerException("the comment parameter is null");
160         this.comment = comment;
161     }
162 
163     /**
164      * Sets the duration value of the RetryAfterHeader. The retry after value
165      * MUST be greater than zero and MUST be less than 2**31.
166      *
167      * @param duration - the new duration value of this RetryAfterHeader
168      * @throws InvalidArgumentException if supplied value is less than zero.
169      *
170      */
171 
setDuration(int duration)172     public void setDuration(int duration) throws InvalidArgumentException {
173         if (duration < 0)
174             throw new InvalidArgumentException("the duration parameter is <0");
175         this.setParameter(DURATION, duration);
176     }
177 
178     /**
179      * Gets the duration value of the RetryAfterHeader. This duration value
180      * is relative time.
181      *
182      * @return the duration value of the RetryAfterHeader, return zero if not
183      * set.
184      *
185      */
186 
getDuration()187     public int getDuration() {
188       if (this.getParameter(DURATION) == null) return -1;
189       else return super.getParameterAsInt(DURATION);
190     }
191 }
192