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.header.ContentTypeHeader;
     32 import java.text.ParseException;
     33 
     34 /**
     35 *  ContentType SIP Header
     36 * <pre>
     37 *14.17 Content-Type
     38 *
     39 *   The Content-Type entity-header field indicates the media type of the
     40 *   entity-body sent to the recipient or, in the case of the HEAD method,
     41 *   the media type that would have been sent had the request been a GET.
     42 *
     43 *   Content-Type   = "Content-Type" ":" media-type
     44 *
     45 *   Media types are defined in section 3.7. An example of the field is
     46 *
     47 *       Content-Type: text/html; charset=ISO-8859-4
     48 *
     49 *   Further discussion of methods for identifying the media type of an
     50 *   entity is provided in section 7.2.1.
     51 *
     52 * From  HTTP RFC 2616
     53 * </pre>
     54 *
     55 *
     56 *@version 1.2
     57 *
     58 *@author M. Ranganathan   <br/>
     59 *@author Olivier Deruelle <br/>
     60 *@version 1.2 $Revision: 1.7 $ $Date: 2009/07/17 18:57:29 $
     61 *@since 1.1
     62 *
     63 */
     64 public class ContentType
     65     extends ParametersHeader
     66     implements javax.sip.header.ContentTypeHeader {
     67 
     68     /**
     69      * Comment for <code>serialVersionUID</code>
     70      */
     71     private static final long serialVersionUID = 8475682204373446610L;
     72     /** mediaRange field.
     73      */
     74     protected MediaRange mediaRange;
     75 
     76     /** Default constructor.
     77      */
     78     public ContentType() {
     79         super(CONTENT_TYPE);
     80     }
     81 
     82     /** Constructor given a content type and subtype.
     83     *@param contentType is the content type.
     84     *@param contentSubtype is the content subtype
     85     */
     86     public ContentType(String contentType, String contentSubtype) {
     87         this();
     88         this.setContentType(contentType, contentSubtype);
     89     }
     90 
     91     /** compare two MediaRange headers.
     92      * @param media String to set
     93      * @return int.
     94      */
     95     public int compareMediaRange(String media) {
     96         return (
     97             mediaRange.type + "/" + mediaRange.subtype).compareToIgnoreCase(
     98             media);
     99     }
    100 
    101     /**
    102      * Encode into a canonical string.
    103      * @return String.
    104      */
    105     public String encodeBody() {
    106         return encodeBody(new StringBuffer()).toString();
    107     }
    108 
    109     protected StringBuffer encodeBody(StringBuffer buffer) {
    110         mediaRange.encode(buffer);
    111         if (hasParameters()) {
    112             buffer.append(SEMICOLON);
    113             parameters.encode(buffer);
    114         }
    115         return buffer;
    116     }
    117 
    118     /** get the mediaRange field.
    119      * @return MediaRange.
    120      */
    121     public MediaRange getMediaRange() {
    122         return mediaRange;
    123     }
    124 
    125     /** get the Media Type.
    126      * @return String.
    127      */
    128     public String getMediaType() {
    129         return mediaRange.type;
    130     }
    131 
    132     /** get the MediaSubType field.
    133      * @return String.
    134      */
    135     public String getMediaSubType() {
    136         return mediaRange.subtype;
    137     }
    138 
    139     /** Get the content subtype.
    140     *@return the content subtype string (or null if not set).
    141     */
    142     public String getContentSubType() {
    143         return mediaRange == null ? null : mediaRange.getSubtype();
    144     }
    145 
    146     /** Get the content subtype.
    147     *@return the content tyep string (or null if not set).
    148     */
    149 
    150     public String getContentType() {
    151         return mediaRange == null ? null : mediaRange.getType();
    152     }
    153 
    154     /** Get the charset parameter.
    155     */
    156     public String getCharset() {
    157         return this.getParameter("charset");
    158     }
    159 
    160     /**
    161      * Set the mediaRange member
    162      * @param m mediaRange field.
    163      */
    164     public void setMediaRange(MediaRange m) {
    165         mediaRange = m;
    166     }
    167 
    168     /**
    169     * set the content type and subtype.
    170     *@param contentType Content type string.
    171     *@param contentSubType content subtype string
    172     */
    173     public void setContentType(String contentType, String contentSubType) {
    174         if (mediaRange == null)
    175             mediaRange = new MediaRange();
    176         mediaRange.setType(contentType);
    177         mediaRange.setSubtype(contentSubType);
    178     }
    179 
    180     /**
    181     * set the content type.
    182     *@param contentType Content type string.
    183     */
    184 
    185     public void setContentType(String contentType) throws ParseException {
    186         if (contentType == null)
    187             throw new NullPointerException("null arg");
    188         if (mediaRange == null)
    189             mediaRange = new MediaRange();
    190         mediaRange.setType(contentType);
    191 
    192     }
    193 
    194     /** Set the content subtype.
    195          * @param contentType String to set
    196          */
    197     public void setContentSubType(String contentType) throws ParseException {
    198         if (contentType == null)
    199             throw new NullPointerException("null arg");
    200         if (mediaRange == null)
    201             mediaRange = new MediaRange();
    202         mediaRange.setSubtype(contentType);
    203     }
    204 
    205     public Object clone() {
    206         ContentType retval = (ContentType) super.clone();
    207         if (this.mediaRange != null)
    208             retval.mediaRange = (MediaRange) this.mediaRange.clone();
    209         return retval;
    210     }
    211 
    212     public boolean equals(Object other) {
    213         if (other instanceof ContentTypeHeader) {
    214             final ContentTypeHeader o = (ContentTypeHeader) other;
    215             return this.getContentType().equalsIgnoreCase( o.getContentType() )
    216                 && this.getContentSubType().equalsIgnoreCase( o.getContentSubType() )
    217                 && equalParameters( o );
    218         }
    219         return false;
    220     }
    221 }
    222 
    223