Home | History | Annotate | Download | only in header
      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 import javax.sip.header.*;
     33 import java.text.ParseException;
     34 
     35 /**
     36  * Accept-Encoding SIP (HTTP) Header.
     37  *
     38  * @author M. Ranganathan
     39  * @author Olivier Deruelle <br/>
     40  * @version 1.2 $Revision: 1.7 $ $Date: 2009/07/17 18:57:24 $
     41  * @since 1.1
     42  *
     43  * <pre>
     44  *  From HTTP RFC 2616
     45  *
     46  *
     47  *    The Accept-Encoding request-header field is similar to Accept, but
     48  *    restricts the content-codings (section 3.5) that are acceptable in
     49  *    the response.
     50  *
     51  *
     52  *        Accept-Encoding  = &quot;Accept-Encoding&quot; &quot;:&quot;
     53  *
     54  *
     55  *                           1#( codings [ &quot;;&quot; &quot;q&quot; &quot;=&quot; qvalue ] )
     56  *        codings          = ( content-coding | &quot;*&quot; )
     57  *
     58  *    Examples of its use are:
     59  *
     60  *        Accept-Encoding: compress, gzip
     61  *        Accept-Encoding:
     62  *        Accept-Encoding: *
     63  *        Accept-Encoding: compress;q=0.5, gzip;q=1.0
     64  *        Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0
     65  * </pre>
     66  *
     67  */
     68 public final class AcceptEncoding extends ParametersHeader implements
     69         AcceptEncodingHeader {
     70 
     71     /**
     72      * Comment for <code>serialVersionUID</code>
     73      */
     74     private static final long serialVersionUID = -1476807565552873525L;
     75 
     76     /**
     77      * contentEncoding field
     78      */
     79     protected String contentCoding;
     80 
     81     /**
     82      * default constructor
     83      */
     84     public AcceptEncoding() {
     85         super(NAME);
     86     }
     87 
     88     /**
     89      * Encode the value of this header.
     90      *
     91      * @return the value of this header encoded into a string.
     92      */
     93     protected String encodeBody() {
     94         return encode(new StringBuffer()).toString();
     95     }
     96 
     97     protected StringBuffer encodeBody(StringBuffer buffer) {
     98         if (contentCoding != null) {
     99             buffer.append(contentCoding);
    100         }
    101         if (parameters != null && !parameters.isEmpty()) {
    102             buffer.append(SEMICOLON).append(parameters.encode());
    103         }
    104         return buffer;
    105     }
    106 
    107     /**
    108      * get QValue field
    109      *
    110      * @return float
    111      */
    112     public float getQValue() {
    113         return getParameterAsFloat("q");
    114     }
    115 
    116     /**
    117      * get ContentEncoding field
    118      *
    119      * @return String
    120      */
    121     public String getEncoding() {
    122         return contentCoding;
    123     }
    124 
    125     /**
    126      * Set the qvalue member
    127      *
    128      * @param q
    129      *            double to set
    130      */
    131     public void setQValue(float q) throws InvalidArgumentException {
    132         if (q < 0.0 || q > 1.0)
    133             throw new InvalidArgumentException("qvalue out of range!");
    134         super.setParameter("q", q);
    135     }
    136 
    137     /**
    138      * Sets the encoding of an EncodingHeader.
    139      *
    140      * @param encoding -
    141      *            the new string value defining the encoding.
    142      * @throws ParseException
    143      *             which signals that an error has been reached unexpectedly
    144      *             while parsing the encoding value.
    145      */
    146 
    147     public void setEncoding(String encoding) throws ParseException {
    148         if (encoding == null)
    149             throw new NullPointerException(" encoding parameter is null");
    150         contentCoding = encoding;
    151     }
    152 
    153 }
    154