001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http.header;
014
015import java.util.function.*;
016
017import org.apache.juneau.http.annotation.*;
018
019/**
020 * Represents a parsed <l>Content-Length</l> HTTP request/response header.
021 *
022 * <p>
023 * The length of the response body in octets (8-bit bytes).
024 *
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode'>
027 *    Content-Length: 348
028 * </p>
029 *
030 * <h5 class='topic'>RFC2616 Specification</h5>
031 *
032 * The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to
033 * the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the
034 * request been a GET.
035 * <p class='bcode'>
036 *    Content-Length    = "Content-Length" ":" 1*DIGIT
037 * </p>
038 *
039 * <p>
040 * An example is...
041 * <p class='bcode'>
042 *    Content-Length: 3495
043 * </p>
044 *
045 * <p>
046 * Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by
047 * the rules in section 4.4.
048 *
049 * <p>
050 * Any Content-Length greater than or equal to zero is a valid value.
051 * Section 4.4 describes how to determine the length of a message-body if a Content-Length is not given.
052 *
053 * <p>
054 * Note that the meaning of this field is significantly different from the corresponding definition in MIME, where it is
055 * an optional field used within the "message/external-body" content-type.
056 * In HTTP, it SHOULD be sent whenever the message's length can be determined prior to being transferred, unless this is
057 * prohibited by the rules in section 4.4.
058 *
059 * <h5 class='section'>See Also:</h5><ul>
060 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
061 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
062 * </ul>
063 *
064 * @serial exclude
065 */
066@Header("Content-Length")
067public class ContentLength extends BasicLongHeader {
068
069   //-----------------------------------------------------------------------------------------------------------------
070   // Static
071   //-----------------------------------------------------------------------------------------------------------------
072
073   private static final long serialVersionUID = 1L;
074   private static final String NAME = "Content-Length";
075
076   /**
077    * Static creator.
078    *
079    * @param value
080    *    The header value.
081    *    <br>Must be parsable using {@link Long#parseLong(String)}.
082    *    <br>Can be <jk>null</jk>.
083    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
084    */
085   public static ContentLength of(String value) {
086      return value == null ? null : new ContentLength(value);
087   }
088
089   /**
090    * Static creator.
091    *
092    * @param value
093    *    The header value.
094    *    <br>Can be <jk>null</jk>.
095    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
096    */
097   public static ContentLength of(Long value) {
098      return value == null ? null : new ContentLength(value);
099   }
100
101   /**
102    * Static creator with delayed value.
103    *
104    * <p>
105    * Header value is re-evaluated on each call to {@link #getValue()}.
106    *
107    * @param value
108    *    The supplier of the header value.
109    *    <br>Can be <jk>null</jk>.
110    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
111    */
112   public static ContentLength of(Supplier<Long> value) {
113      return value == null ? null : new ContentLength(value);
114   }
115
116   //-----------------------------------------------------------------------------------------------------------------
117   // Instance
118   //-----------------------------------------------------------------------------------------------------------------
119
120   /**
121    * Constructor.
122    *
123    * @param value
124    *    The header value.
125    *    <br>Must be parsable using {@link Long#parseLong(String)}.
126    *    <br>Can be <jk>null</jk>.
127    */
128   public ContentLength(String value) {
129      super(NAME, value);
130   }
131
132   /**
133    * Constructor.
134    *
135    * @param value
136    *    The header value.
137    *    <br>Can be <jk>null</jk>.
138    */
139   public ContentLength(Long value) {
140      super(NAME, value);
141   }
142
143   /**
144    * Constructor with delayed value.
145    *
146    * <p>
147    * Header value is re-evaluated on each call to {@link #getValue()}.
148    *
149    * @param value
150    *    The supplier of the header value.
151    *    <br>Can be <jk>null</jk>.
152    */
153   public ContentLength(Supplier<Long> value) {
154      super(NAME, value);
155   }
156}