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