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