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-Range</l> HTTP response header.
021 *
022 * <p>
023 * Where in a full body message this partial message belongs.
024 *
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode'>
027 *    Content-Range: bytes 21010-47021/47022
028 * </p>
029 *
030 * <h5 class='topic'>RFC2616 Specification</h5>
031 *
032 * The Content-Range entity-header is sent with a partial entity-body to specify where in the full entity-body the
033 * partial body should be applied.
034 * Range units are defined in section 3.12.
035 * <p class='bcode'>
036 *    Content-Range = "Content-Range" ":" content-range-spec
037 *    content-range-spec      = byte-content-range-spec
038 *    byte-content-range-spec = bytes-unit SP
039 *                              byte-range-resp-spec "/"
040 *                              ( instance-length | "*" )
041 *    byte-range-resp-spec = (first-byte-pos "-" last-byte-pos)
042 *                                   | "*"
043 *    instance-length           = 1*DIGIT
044 * </p>
045 *
046 * <p>
047 * The header SHOULD indicate the total length of the full entity-body, unless this length is unknown or difficult to
048 * determine.
049 * The asterisk "*" character means that the instance-length is unknown at the time when the response was generated.
050 *
051 * <p>
052 * Unlike byte-ranges-specifier values (see section 14.35.1), a byte- range-resp-spec MUST only specify one range, and
053 * MUST contain absolute byte positions for both the first and last byte of the range.
054 *
055 * <p>
056 * A byte-content-range-spec with a byte-range-resp-spec whose last- byte-pos value is less than its first-byte-pos
057 * value, or whose instance-length value is less than or equal to its last-byte-pos value, is invalid.
058 * The recipient of an invalid byte-content-range- spec MUST ignore it and any content transferred along with it.
059 *
060 * <p>
061 * A server sending a response with status code 416 (Requested range not satisfiable) SHOULD include a Content-Range
062 * field with a byte-range- resp-spec of "*".
063 * The instance-length specifies the current length of the selected resource.
064 * A response with status code 206 (Partial Content) MUST NOT include a Content-Range field with a byte-range-resp-spec
065 * of "*".
066 *
067 * <p>
068 * Examples of byte-content-range-spec values, assuming that the entity contains a total of 1234 bytes:
069 * <p class='bcode'>
070 *    The first 500 bytes:
071 *     bytes 0-499/1234
072 *    The second 500 bytes:
073 *     bytes 500-999/1234
074 *    All except for the first 500 bytes:
075 *     bytes 500-1233/1234
076 *    The last 500 bytes:
077 *     bytes 734-1233/1234
078 * </p>
079 *
080 * <p>
081 * When an HTTP message includes the content of a single range (for example, a response to a request for a single range,
082 * or to a request for a set of ranges that overlap without any holes), this content is transmitted with a Content-Range
083 * header, and a Content-Length header showing the number of bytes actually transferred.
084 * For example:
085 * <p class='bcode'>
086 *    HTTP/1.1 206 Partial content
087 *    Date: Wed, 15 Nov 1995 06:25:24 GMT
088 *    Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT
089 *    Content-Range: bytes 21010-47021/47022
090 *    Content-Length: 26012
091 *    Content-Type: image/gif
092 * </p>
093 *
094 * <p>
095 * When an HTTP message includes the content of multiple ranges (for example, a response to a request for multiple
096 * non-overlapping ranges), these are transmitted as a multipart message.
097 * The multipart media type used for this purpose is "multipart/byteranges" as defined in appendix 19.2.
098 * See appendix 19.6.3 for a compatibility issue.
099 *
100 * <p>
101 * A response to a request for a single range MUST NOT be sent using the multipart/byteranges media type.
102 * A response to a request for multiple ranges, whose result is a single range, MAY be sent as a multipart/byteranges
103 * media type with one part.
104 * A client that cannot decode a multipart/byteranges message MUST NOT ask for multiple byte-ranges in a single request.
105 *
106 * <p>
107 * When a client requests multiple byte-ranges in one request, the server SHOULD return them in the order that they
108 * appeared in the request.
109 *
110 * <p>
111 * If the server ignores a byte-range-spec because it is syntactically invalid, the server SHOULD treat the request as
112 * if the invalid Range header field did not exist.
113 * (Normally, this means return a 200 response containing the full entity).
114 *
115 * <p>
116 * If the server receives a request (other than one including an If- Range request-header field) with an unsatisfiable
117 * Range request- header field
118 * (that is, all of whose byte-range-spec values have a first-byte-pos value greater than the current length of the
119 * selected resource),
120 * it SHOULD return a response code of 416 (Requested range not satisfiable) (section 10.4.17).
121 *
122 * <p>
123 * Note: clients cannot depend on servers to send a 416 (Requested range not satisfiable) response instead of a 200 (OK)
124 * response for
125 * an unsatisfiable Range request-header, since not all servers implement this request-header.
126 *
127 * <h5 class='section'>See Also:</h5><ul>
128 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
129 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
130 * </ul>
131 *
132 * @serial exclude
133 */
134@Header("Content-Range")
135public class ContentRange extends BasicStringHeader {
136
137   //-----------------------------------------------------------------------------------------------------------------
138   // Static
139   //-----------------------------------------------------------------------------------------------------------------
140
141   private static final long serialVersionUID = 1L;
142   private static final String NAME = "Content-Range";
143
144   /**
145    * Static creator.
146    *
147    * @param value
148    *    The header value.
149    *    <br>Can be <jk>null</jk>.
150    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
151    */
152   public static ContentRange of(String value) {
153      return value == null ? null : new ContentRange(value);
154   }
155
156   /**
157    * Static creator with delayed value.
158    *
159    * <p>
160    * Header value is re-evaluated on each call to {@link #getValue()}.
161    *
162    * @param value
163    *    The supplier of the header value.
164    *    <br>Can be <jk>null</jk>.
165    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
166    */
167   public static ContentRange of(Supplier<String> value) {
168      return value == null ? null : new ContentRange(value);
169   }
170
171   //-----------------------------------------------------------------------------------------------------------------
172   // Instance
173   //-----------------------------------------------------------------------------------------------------------------
174
175   /**
176    * Constructor.
177    *
178    * @param value
179    *    The header value.
180    *    <br>Can be <jk>null</jk>.
181    */
182   public ContentRange(String value) {
183      super(NAME, value);
184   }
185
186   /**
187    * Constructor with delayed value.
188    *
189    * <p>
190    * Header value is re-evaluated on each call to {@link #getValue()}.
191    *
192    * @param value
193    *    The supplier of the header value.
194    *    <br>Can be <jk>null</jk>.
195    */
196   public ContentRange(Supplier<String> value) {
197      super(NAME, value);
198   }
199}