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 w800'> 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 w800'> 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 w800'> 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 w800'> 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 * <ul class='seealso'> 128 * <li class='extlink'>{@doc ExtRFC2616} 129 * </ul> 130 */ 131@Header("Content-Range") 132public class ContentRange extends BasicStringHeader { 133 134 private static final long serialVersionUID = 1L; 135 136 /** 137 * Convenience creator. 138 * 139 * @param value 140 * The header value. 141 * <br>Can be any of the following: 142 * <ul> 143 * <li>{@link String} 144 * <li>Anything else - Converted to <c>String</c> then parsed. 145 * </ul> 146 * @return A new {@link ContentRange} object. 147 */ 148 public static ContentRange of(Object value) { 149 if (value == null) 150 return null; 151 return new ContentRange(value); 152 } 153 154 /** 155 * Convenience creator using supplier. 156 * 157 * <p> 158 * Header value is re-evaluated on each call to {@link #getValue()}. 159 * 160 * @param value 161 * The header value supplier. 162 * <br>Can be any of the following: 163 * <ul> 164 * <li>{@link String} 165 * <li>Anything else - Converted to <c>String</c> then parsed. 166 * </ul> 167 * @return A new {@link ContentRange} object. 168 */ 169 public static ContentRange of(Supplier<?> value) { 170 if (value == null) 171 return null; 172 return new ContentRange(value); 173 } 174 175 /** 176 * Constructor. 177 * 178 * @param value 179 * The header value. 180 * <br>Can be any of the following: 181 * <ul> 182 * <li>{@link String} 183 * <li>Anything else - Converted to <c>String</c> then parsed. 184 * <li>A {@link Supplier} of anything on this list. 185 * </ul> 186 */ 187 public ContentRange(Object value) { 188 super("Content-Range", value); 189 } 190 191 /** 192 * Constructor 193 * 194 * @param value 195 * The header value. 196 */ 197 public ContentRange(String value) { 198 this((Object)value); 199 } 200}