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.*;
     32 import javax.sip.header.ContentLengthHeader;
     33 
     34 /**
     35 * ContentLength SIPHeader (of which there can be only one in a SIPMessage).
     36 *<pre>
     37 *Fielding, et al.            Standards Track                   [Page 119]
     38 *RFC 2616                        HTTP/1.1                       June 1999
     40 *
     41 *
     42 *      14.13 Content-Length
     43 *
     44 *   The Content-Length entity-header field indicates the size of the
     45 *   entity-body, in decimal number of OCTETs, sent to the recipient or,
     46 *   in the case of the HEAD method, the size of the entity-body that
     47 *   would have been sent had the request been a GET.
     48 *
     49 *       Content-Length    = "Content-Length" ":" 1*DIGIT
     50 *
     51 *   An example is
     52 *
     53 *       Content-Length: 3495
     54 *
     55 *   Applications SHOULD use this field to indicate the transfer-length of
     56 *   the message-body, unless this is prohibited by the rules in section
     57 *   4.4.
     58 *
     59 *   Any Content-Length greater than or equal to zero is a valid value.
     60 *   Section 4.4 describes how to determine the length of a message-body
     61 *   if a Content-Length is not given.
     62 *
     63 *   Note that the meaning of this field is significantly different from
     64 *   the corresponding definition in MIME, where it is an optional field
     65 *   used within the "message/external-body" content-type. In HTTP, it
     66 *   SHOULD be sent whenever the message's length can be determined prior
     67 *   to being transferred, unless this is prohibited by the rules in
     68 *   section 4.4.
     69 * </pre>
     70 *
     71 *@author M. Ranganathan   <br/>
     72 *@author Olivier Deruelle <br/>
     73 *@version 1.2 $Revision: 1.7 $ $Date: 2009/10/18 13:46:34 $
     74 *@since 1.1
     75 */
     76 public class ContentLength
     77     extends SIPHeader
     78     implements javax.sip.header.ContentLengthHeader {
     79 
     80     /**
     81      * Comment for <code>serialVersionUID</code>
     82      */
     83     private static final long serialVersionUID = 1187190542411037027L;
     84     /**
     85      * contentLength field.
     86      */
     87     protected Integer contentLength;
     88 
     89     /**
     90      * Default constructor.
     91      */
     92     public ContentLength() {
     93         super(NAME);
     94     }
     95 
     96     /**
     97      * Constructor given a length.
     98      */
     99     public ContentLength(int length) {
    100         super(NAME);
    101         this.contentLength = Integer.valueOf(length);
    102     }
    103 
    104     /**
    105      * get the ContentLength field.
    106      * @return int
    107      */
    108     public int getContentLength() {
    109         return contentLength.intValue();
    110     }
    111 
    112     /**
    113      * Set the contentLength member
    114      * @param contentLength int to set
    115      */
    116     public void setContentLength(int contentLength)
    117         throws InvalidArgumentException {
    118         if (contentLength < 0)
    119             throw new InvalidArgumentException(
    120                 "JAIN-SIP Exception"
    121                     + ", ContentLength, setContentLength(), the contentLength parameter is <0");
    122         this.contentLength = Integer.valueOf(contentLength);
    123     }
    124 
    125     /**
    126      * Encode into a canonical string.
    127      * @return String
    128      */
    129     public String encodeBody() {
    130         return encodeBody(new StringBuffer()).toString();
    131     }
    132 
    133     protected StringBuffer encodeBody(StringBuffer buffer) {
    134         if (contentLength == null)
    135             buffer.append("0");
    136         else
    137             buffer.append(contentLength.toString());
    138         return buffer;
    139     }
    140 
    141     /**
    142      * Pattern matcher ignores content length.
    143      */
    144     public boolean match(Object other) {
    145         if (other instanceof ContentLength)
    146             return true;
    147         else
    148             return false;
    149     }
    150 
    151     public boolean equals(Object other) {
    152         if (other instanceof ContentLengthHeader) {
    153             final ContentLengthHeader o = (ContentLengthHeader) other;
    154             return this.getContentLength() == o.getContentLength();
    155         }
    156         return false;
    157     }
    158 }
    159