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 package gov.nist.javax.sip.header;
30 
31 import javax.sip.InvalidArgumentException;
32 
33 /**
34  * Accept header : The top level header is actually AcceptList which is a list of
35  * Accept headers.
36  *
37  * @version 1.2 $Revision: 1.9 $ $Date: 2009/07/17 18:57:24 $
38  *
39  * @since 1.1
40  *
41  * @author M. Ranganathan   <br/>
42  *
43  *
44  *
45  */
46 public final class Accept
47     extends ParametersHeader
48     implements javax.sip.header.AcceptHeader {
49 
50     /**
51      * Comment for <code>serialVersionUID</code>
52      */
53     private static final long serialVersionUID = -7866187924308658151L;
54 
55     /** mediaRange field
56      */
57     protected MediaRange mediaRange;
58 
59     /** default constructor
60      */
Accept()61     public Accept() {
62         super(NAME);
63     }
64 
65     /** returns true if this header allows all ContentTypes,
66       * false otherwise.
67       * @return Boolean
68       */
allowsAllContentTypes()69     public boolean allowsAllContentTypes() {
70         if (mediaRange == null)
71             return false;
72         else
73             return mediaRange.type.compareTo(STAR) == 0;
74     }
75 
76     /**
77      * returns true if this header allows all ContentSubTypes,
78      * false otherwise.
79      * @return boolean
80      */
allowsAllContentSubTypes()81     public boolean allowsAllContentSubTypes() {
82         if (mediaRange == null) {
83             return false;
84         } else
85             return mediaRange.getSubtype().compareTo(STAR) == 0;
86     }
87 
88     /** Encode the value of this header into cannonical form.
89     *@return encoded value of the header as a string.
90     */
encodeBody()91     protected String encodeBody() {
92         return encodeBody(new StringBuffer()).toString();
93     }
94 
encodeBody(StringBuffer buffer)95     protected StringBuffer encodeBody(StringBuffer buffer) {
96         if (mediaRange != null)
97             mediaRange.encode(buffer);
98         if (parameters != null && !parameters.isEmpty()) {
99             buffer.append(';');
100             parameters.encode(buffer);
101         }
102         return buffer;
103     }
104 
105     /** get the MediaRange field
106      * @return MediaRange
107      */
getMediaRange()108     public MediaRange getMediaRange() {
109         return mediaRange;
110     }
111 
112     /** get the contentType field
113      * @return String
114      */
getContentType()115     public String getContentType() {
116         if (mediaRange == null)
117             return null;
118         else
119             return mediaRange.getType();
120     }
121 
122     /** get the ContentSubType fiels
123      * @return String
124      */
getContentSubType()125     public String getContentSubType() {
126         if (mediaRange == null)
127             return null;
128         else
129             return mediaRange.getSubtype();
130     }
131 
132     /**
133      * Get the q value.
134      * @return float
135      */
getQValue()136     public float getQValue() {
137         return getParameterAsFloat(ParameterNames.Q);
138     }
139 
140     /**
141      * Return true if the q value has been set.
142      * @return boolean
143      */
hasQValue()144     public boolean hasQValue() {
145         return super.hasParameter(ParameterNames.Q);
146 
147     }
148 
149     /**
150      *Remove the q value.
151      */
removeQValue()152     public void removeQValue() {
153         super.removeParameter(ParameterNames.Q);
154     }
155 
156     /** set the ContentSubType field
157      * @param subtype String to set
158      */
setContentSubType(String subtype)159     public void setContentSubType(String subtype) {
160         if (mediaRange == null)
161             mediaRange = new MediaRange();
162         mediaRange.setSubtype(subtype);
163     }
164 
165     /** set the ContentType field
166      * @param type String to set
167      */
setContentType(String type)168     public void setContentType(String type) {
169         if (mediaRange == null)
170             mediaRange = new MediaRange();
171         mediaRange.setType(type);
172     }
173 
174     /**
175      * Set the q value
176      * @param qValue float to set
177      * @throws IllegalArgumentException if qValue is <0.0 or >1.0
178      */
setQValue(float qValue)179     public void setQValue(float qValue) throws InvalidArgumentException {
180         if (qValue == -1)
181             super.removeParameter(ParameterNames.Q);
182         super.setParameter(ParameterNames.Q, qValue);
183 
184     }
185 
186     /**
187          * Set the mediaRange member
188          * @param m MediaRange field
189          */
setMediaRange(MediaRange m)190     public void setMediaRange(MediaRange m) {
191         mediaRange = m;
192     }
193 
clone()194     public Object clone() {
195         Accept retval = (Accept) super.clone();
196         if (this.mediaRange != null)
197             retval.mediaRange = (MediaRange) this.mediaRange.clone();
198         return retval;
199     }
200 
201 }
202