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 javax.sip.InvalidArgumentException;
32import javax.sip.header.SubscriptionStateHeader;
33import java.text.ParseException;
34
35/**
36 *SubscriptionState header
37 *
38 * @version 1.2 $Revision: 1.7 $ $Date: 2009/07/17 18:57:39 $
39 *
40 * @author Olivier Deruelle <br/>
41 *
42 *
43 */
44public class SubscriptionState
45    extends ParametersHeader
46    implements SubscriptionStateHeader {
47
48    /**
49     * Comment for <code>serialVersionUID</code>
50     */
51    private static final long serialVersionUID = -6673833053927258745L;
52    protected int expires;
53    protected int retryAfter;
54    protected String reasonCode;
55    protected String state;
56
57    /** Creates a new instance of SubscriptionState */
58    public SubscriptionState() {
59        super(SIPHeaderNames.SUBSCRIPTION_STATE);
60        expires = -1;
61        retryAfter = -1;
62    }
63
64    /**
65    * Sets the relative expires value of the SubscriptionStateHeader. The
66    * expires value MUST be greater than zero and MUST be less than 2**31.
67    *
68    * @param expires - the new expires value of this SubscriptionStateHeader.
69    * @throws InvalidArgumentException if supplied value is less than zero.
70    */
71    public void setExpires(int expires) throws InvalidArgumentException {
72        if (expires < 0)
73            throw new InvalidArgumentException(
74                "JAIN-SIP "
75                    + "Exception, SubscriptionState, setExpires(), the expires parameter is  < 0");
76        this.expires = expires;
77    }
78
79    /**
80     * Gets the expires value of the SubscriptionStateHeader. This expires value is
81     * relative time.
82     *
83     * @return the expires value of the SubscriptionStateHeader.
84     */
85    public int getExpires() {
86        return expires;
87    }
88
89    /**
90     * Sets the retry after value of the SubscriptionStateHeader. The retry after value
91     * MUST be greater than zero and MUST be less than 2**31.
92     *
93     * @param retryAfter - the new retry after value of this SubscriptionStateHeader
94     * @throws InvalidArgumentException if supplied value is less than zero.
95     */
96    public void setRetryAfter(int retryAfter) throws InvalidArgumentException {
97        if (retryAfter <= 0)
98            throw new InvalidArgumentException(
99                "JAIN-SIP "
100                    + "Exception, SubscriptionState, setRetryAfter(), the retryAfter parameter is <=0");
101        this.retryAfter = retryAfter;
102    }
103
104    /**
105     * Gets the retry after value of the SubscriptionStateHeader. This retry after
106     * value is relative time.
107     *
108     * @return the retry after value of the SubscriptionStateHeader.
109     */
110    public int getRetryAfter() {
111        return retryAfter;
112    }
113
114    /**
115     * Gets the reason code of SubscriptionStateHeader.
116     *
117     * @return the comment of this SubscriptionStateHeader, return null if no reason code
118     * is available.
119     */
120    public String getReasonCode() {
121        return reasonCode;
122    }
123
124    /**
125     * Sets the reason code value of the SubscriptionStateHeader.
126     *
127     * @param reasonCode - the new reason code string value of the SubscriptionStateHeader.
128     * @throws ParseException which signals that an error has been reached
129     * unexpectedly while parsing the reason code.
130     */
131    public void setReasonCode(String reasonCode) throws ParseException {
132        if (reasonCode == null)
133            throw new NullPointerException(
134                "JAIN-SIP "
135                    + "Exception, SubscriptionState, setReasonCode(), the reasonCode parameter is null");
136        this.reasonCode = reasonCode;
137    }
138
139    /**
140     * Gets the state of SubscriptionStateHeader.
141     *
142     * @return the state of this SubscriptionStateHeader.
143     */
144    public String getState() {
145        return state;
146    }
147
148    /**
149     * Sets the state value of the SubscriptionStateHeader.
150     *
151     * @param state - the new state string value of the SubscriptionStateHeader.
152     * @throws ParseException which signals that an error has been reached
153     * unexpectedly while parsing the state.
154     */
155    public void setState(String state) throws ParseException {
156        if (state == null)
157            throw new NullPointerException(
158                "JAIN-SIP "
159                    + "Exception, SubscriptionState, setState(), the state parameter is null");
160        this.state = state;
161    }
162
163    /** Just the encoded body of the header.
164     * @return the string encoded header body.
165     */
166    public String encodeBody() {
167        return encodeBody(new StringBuffer()).toString();
168    }
169
170    protected StringBuffer encodeBody(StringBuffer buffer) {
171        if (state != null)
172            buffer.append(state);
173        if (reasonCode != null)
174            buffer.append(";reason=").append(reasonCode);
175        if (expires != -1)
176            buffer.append(";expires=").append(expires);
177        if (retryAfter != -1)
178            buffer.append(";retry-after=").append(retryAfter);
179
180        if (!parameters.isEmpty()) {
181            buffer.append(SEMICOLON);
182            parameters.encode(buffer);
183        }
184        return buffer;
185    }
186}
187
188