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: package gov.nist.siplite.header;
027:
028: import gov.nist.core.*;
029: import gov.nist.siplite.header.*;
030:
031: /**
032: * <pre>Subscription-State Header class.
033: *
034: * RFC 3265, p. 36:
035: * Subscription-State = "Subscription-State" HCOLON substate-value
036: * *( SEMI subexp-params )
037: * substate-value = "active" / "pending" / "terminated"
038: * / extension-substate
039: * extension-substate = token
040: * subexp-params = ("reason" EQUAL event-reason-value)
041: * / ("expires" EQUAL delta-seconds)
042: * / ("retry-after" EQUAL delta-seconds)
043: * / generic-param
044: * event-reason-value = "deactivated"
045: * / "probation"
046: * / "rejected"
047: * / "timeout"
048: * / "giveup"
049: * / "noresource"
050: * / event-reason-extension
051: * event-reason-extension = token</pre>
052: */
053: public class SubscriptionStateHeader extends ParametersHeader {
054: /** Class handle. */
055: public static Class clazz;
056:
057: /** SubscriptionState header field label. */
058: public static final String NAME = Header.SUBSCRIPTION_STATE;
059:
060: /** 'active' state label. */
061: public static final String STATE_ACTIVE = "active";
062:
063: /** 'pending' state label. */
064: public static final String STATE_PENDING = "pending";
065:
066: /** 'terminated' state label. */
067: public static final String STATE_TERMINATED = "terminated";
068:
069: /** 'reason' parameter label. */
070: public static final String PARAM_REASON = "reason";
071:
072: /** 'expires' parameter label. */
073: public static final String PARAM_EXPIRES = "expires";
074:
075: /** 'retry-after' parameter label. */
076: public static final String PARAM_RETRY_AFTER = "retry-after";
077:
078: /** Current subscription state. */
079: private String state = null;
080:
081: /**
082: * Static initializer.
083: */
084: static {
085: clazz = new SubscriptionStateHeader().getClass();
086: }
087:
088: /**
089: * Default constructor.
090: */
091: public SubscriptionStateHeader() {
092: super (NAME);
093: }
094:
095: /**
096: * Constructor given a state.
097: * @param subscriptionState state of the subscription
098: * @throws IllegalArgumentException if the subscriptionState is invalid.
099: */
100: public SubscriptionStateHeader(String subscriptionState)
101: throws IllegalArgumentException {
102: super (NAME);
103: setState(subscriptionState);
104: }
105:
106: /**
107: * Encode this into a cannonical String.
108: * @return String
109: */
110: public String encodeBody() {
111: if (state == null) {
112: return "";
113: }
114:
115: return state + encodeWithSep();
116: }
117:
118: /**
119: * Checks if the current subscription state is "active".
120: * @return true if the subscription is active, false otherwise.
121: */
122: public boolean isActive() {
123: if (state == null) {
124: return false;
125: }
126: return state.equalsIgnoreCase(STATE_ACTIVE);
127: }
128:
129: /**
130: * Checks if the current subscription state is "terminated".
131: * @return true if the subscription is terminated, false otherwise.
132: */
133: public boolean isTerminated() {
134: if (state == null) {
135: return false;
136: }
137: return state.equalsIgnoreCase(STATE_TERMINATED);
138: }
139:
140: /**
141: * Returns the current subscription state.
142: * @return subscription state.
143: */
144: public String getState() {
145: return state;
146: }
147:
148: /**
149: * Sets the subscription state.
150: * @param newState subscription state to set.
151: * @throws IllegalArgumentException if the newState value is invalid.
152: */
153: public void setState(String newState)
154: throws IllegalArgumentException {
155: if (newState == null || newState.equals("")) {
156: throw new IllegalArgumentException("Invalid state value: "
157: + newState);
158: }
159: state = newState.trim();
160: }
161:
162: /**
163: * Returns the value of the current object, i.e., the subscription state.
164: * @return subscription state.
165: */
166: public Object getValue() {
167: return state;
168: }
169:
170: /**
171: * Returns the subscription expiration time in seconds.
172: * @return subscription expiration time.
173: */
174: public String getExpires() {
175: return getParameter(PARAM_EXPIRES);
176: }
177:
178: /**
179: * Sets the subscription expiration time in seconds.
180: * @param expires expiration time to set.
181: * @throws IllegalArgumentException if the expires parameter is invalid.
182: */
183: public void setExpires(String expires)
184: throws IllegalArgumentException {
185: int intExpires;
186:
187: try {
188: intExpires = Integer.parseInt(expires);
189: } catch (NumberFormatException e) {
190: intExpires = -1;
191: }
192:
193: if (intExpires < 0) {
194: throw new IllegalArgumentException("Invalid 'expires': "
195: + expires);
196: }
197:
198: setParameter(PARAM_EXPIRES, expires);
199: }
200:
201: /**
202: * Sets the subscription expiration time in seconds.
203: * @param expires expiration time to set.
204: * @throws IllegalArgumentException if the expires parameter is invalid.
205: */
206: public void setExpires(int expires) throws IllegalArgumentException {
207: if (expires < 0) {
208: throw new IllegalArgumentException("Invalid 'expires': "
209: + expires);
210: }
211: setParameter(PARAM_EXPIRES, new Integer(expires).toString());
212: }
213: }
|