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.classic.header; 018 019import java.util.function.*; 020 021import org.apache.juneau.commons.collections.*; 022import org.apache.juneau.commons.http.*; 023import org.apache.juneau.http.*; 024 025/** 026 * Represents a parsed <l>Accept</l> HTTP request header. 027 * 028 * <p> 029 * Content-Types that are acceptable for the response. 030 * 031 * <h5 class='figure'>Example</h5> 032 * <p class='bcode'> 033 * Accept: text/plain 034 * </p> 035 * 036 * <h5 class='topic'>RFC2616 Specification</h5> 037 * 038 * The Accept request-header field can be used to specify certain media types which are acceptable for the response. 039 * Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as 040 * in the case of a request for an in-line image. 041 * 042 * <p class='bcode'> 043 * Accept = "Accept" ": 044 * #( media-range [ accept-params ] ) 045 * 046 * media-range = ( "* /*" 047 * | ( type "/" "*" ) 048 * | ( type "/" subtype ) 049 * ) *( ";" parameter ) 050 * accept-params = ";" "q" "=" qvalue *( accept-extension ) 051 * accept-extension = ";" token [ "=" ( token | quoted-string ) ] 052 * </p> 053 * 054 * <p> 055 * The asterisk "*" character is used to group media types into ranges, with "* /*" indicating all media types and 056 * "type/*" indicating all subtypes of that type. 057 * The media-range MAY include media type parameters that are applicable to that range. 058 * 059 * <p> 060 * Each media-range MAY be followed by one or more accept-params, beginning with the "q" parameter for indicating a 061 * relative quality factor. 062 * The first "q" parameter (if any) separates the media-range parameter(s) from the accept-params. 063 * Quality factors allow the user or user agent to indicate the relative degree of preference for that media-range, 064 * using the qvalue scale from 0 to 1 (section 3.9). 065 * The default value is q=1. 066 * 067 * <p> 068 * Note: Use of the "q" parameter name to separate media type parameters from Accept extension parameters is due to 069 * historical practice. 070 * Although this prevents any media type parameter named "q" from being used with a media range, such an event is 071 * believed to be unlikely given the lack of any "q" parameters in the IANA 072 * media type registry and the rare usage of any media type parameters in Accept. 073 * Future media types are discouraged from registering any parameter named "q". 074 * 075 * <p> 076 * The example 077 * <p class='bcode'> 078 * Accept: audio/*; q=0.2, audio/basic 079 * </p> 080 * <p> 081 * SHOULD be interpreted as "I prefer audio/basic, but send me any audio type if it is the best available after an 80% 082 * mark-down in quality." 083 * 084 * <p> 085 * If no Accept header field is present, then it is assumed that the client accepts all media types. 086 * 087 * <p> 088 * If an Accept header field is present, and if the server cannot send a response which is acceptable according to the 089 * combined Accept field value, then the server SHOULD send a 406 (not acceptable) response. 090 * 091 * <p> 092 * A more elaborate example is 093 * <p class='bcode'> 094 * Accept: text/plain; q=0.5, text/html, 095 * text/x-dvi; q=0.8, text/x-c 096 * </p> 097 * 098 * <p> 099 * Verbally, this would be interpreted as "text/html and text/x-c are the preferred media types, but if they do not 100 * exist, then send the 101 * text/x-dvi entity, and if that does not exist, send the text/plain entity." 102 * 103 * <p> 104 * Media ranges can be overridden by more specific media ranges or specific media types. 105 * If more than one media range applies to a given type, the most specific reference has precedence. 106 * For example, 107 * <p class='bcode'> 108 * Accept: text/ *, text/html, text/html;level=1, * /* 109 * </p> 110 * <p> 111 * have the following precedence: 112 * <ol> 113 * <li>text/html;level=1 114 * <li>text/html 115 * <li>text/* 116 * <li>* /* 117 * </ol> 118 * 119 * <p> 120 * The media type quality factor associated with a given type is determined by finding the media range with the highest 121 * precedence which matches that type. 122 * For example, 123 * <p class='bcode'> 124 * Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1, 125 * text/html;level=2;q=0.4, * /*;q=0.5 126 * </p> 127 * <p> 128 * would cause the following values to be associated: 129 * <p class='bcode'> 130 * text/html;level=1 = 1 131 * text/html = 0.7 132 * text/plain = 0.3 133 * image/jpeg = 0.5 134 * text/html;level=2 = 0.4 135 * text/html;level=3 = 0.7 136 * </p> 137 * 138 * <p> 139 * Note: A user agent might be provided with a default set of quality values for certain media ranges. 140 * However, unless the user agent is a closed system which cannot interact with other rendering agents, this default 141 * set ought to be configurable by the user. 142 * 143 * <h5 class='section'>See Also:</h5><ul> 144 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a> 145 * <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a> 146 * </ul> 147 * 148 * @serial exclude 149 */ 150@Header("Accept") 151public class Accept extends BasicMediaRangesHeader { 152 private static final long serialVersionUID = 1L; 153 private static final String NAME = "Accept"; 154 155 private static final Cache<String,Accept> CACHE = Cache.of(String.class, Accept.class).build(); 156 157 // Constants 158 /** Accept header for <c>application/atom+xml</c>. */ 159 public static final Accept APPLICATION_ATOM_XML = of("application/atom+xml"); 160 /** Accept header for <c>application/x-www-form-urlencoded</c>. */ 161 public static final Accept APPLICATION_FORM_URLENCODED = of("application/x-www-form-urlencoded"); 162 /** Accept header for <c>application/json</c>. */ 163 public static final Accept APPLICATION_JSON = of("application/json"); 164 /** Accept header for <c>application/octet-stream</c>. */ 165 public static final Accept APPLICATION_OCTET_STREAM = of("application/octet-stream"); 166 /** Accept header for <c>application/soap+xml</c>. */ 167 public static final Accept APPLICATION_SOAP_XML = of("application/soap+xml"); 168 /** Accept header for <c>application/svg+xml</c>. */ 169 public static final Accept APPLICATION_SVG_XML = of("application/svg+xml"); 170 /** Accept header for <c>application/xhtml+xml</c>. */ 171 public static final Accept APPLICATION_XHTML_XML = of("application/xhtml+xml"); 172 /** Accept header for <c>application/xml</c>. */ 173 public static final Accept APPLICATION_XML = of("application/xml"); 174 /** Accept header for <c>image/bmp</c>. */ 175 public static final Accept IMAGE_BMP = of("image/bmp"); 176 /** Accept header for <c>image/gif</c>. */ 177 public static final Accept IMAGE_GIF = of("image/gif"); 178 /** Accept header for <c>image/jpeg</c>. */ 179 public static final Accept IMAGE_JPEG = of("image/jpeg"); 180 /** Accept header for <c>image/png</c>. */ 181 public static final Accept IMAGE_PNG = of("image/png"); 182 /** Accept header for <c>image/svg+xml</c>. */ 183 public static final Accept IMAGE_SVG = of("image/svg+xml"); 184 /** Accept header for <c>image/tiff</c>. */ 185 public static final Accept IMAGE_TIFF = of("image/tiff"); 186 /** Accept header for <c>image/webp</c>. */ 187 public static final Accept IMAGE_WEBP = of("image/webp"); 188 /** Accept header for <c>multipart/form-data</c>. */ 189 public static final Accept MULTIPART_FORM_DATA = of("multipart/form-data"); 190 /** Accept header for <c>text/html</c>. */ 191 public static final Accept TEXT_HTML = of("text/html"); 192 /** Accept header for <c>text/plain</c>. */ 193 public static final Accept TEXT_PLAIN = of("text/plain"); 194 /** Accept header for <c>text/xml</c>. */ 195 public static final Accept TEXT_XML = of("text/xml"); 196 /** Accept header for <c>*/*</c> (all media types). */ 197 public static final Accept WILDCARD = of("*/*"); 198 /** Constant for <jk>null</jk> value. */ 199 public static final Accept NULL = new Accept((String)null); 200 201 /** 202 * Static creator. 203 * 204 * @param value 205 * The header value. 206 * <br>Can be <jk>null</jk>. 207 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 208 */ 209 public static Accept of(MediaRanges value) { 210 return value == null ? null : new Accept(value); 211 } 212 213 /** 214 * Static creator. 215 * 216 * @param value 217 * The header value. 218 * <br>Can be <jk>null</jk>. 219 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 220 */ 221 public static Accept of(MediaType value) { 222 return value == null ? null : new Accept(value.toString()); 223 } 224 225 /** 226 * Static creator. 227 * 228 * @param value 229 * The header value. 230 * <br>Must be parsable by {@link MediaRanges#of(String)}. 231 * <br>Can be <jk>null</jk>. 232 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 233 */ 234 public static Accept of(String value) { 235 return value == null ? null : CACHE.get(value, () -> new Accept(value)); 236 } 237 238 /** 239 * Static creator with delayed value. 240 * 241 * <p> 242 * Header value is re-evaluated on each call to {@link #getValue()}. 243 * 244 * @param value 245 * The header value. 246 * <br>Can be <jk>null</jk>. 247 * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>. 248 */ 249 public static Accept of(Supplier<MediaRanges> value) { 250 return value == null ? null : new Accept(value); 251 } 252 253 /** 254 * Constructor. 255 * 256 * @param value 257 * The header value. 258 * <br>Can be <jk>null</jk> to create a header with no value. 259 */ 260 public Accept(MediaRanges value) { 261 super(NAME, value); 262 } 263 264 /** 265 * Constructor. 266 * 267 * @param value 268 * The header value. 269 * <br>Must be parsable by {@link MediaRanges#of(String)}. 270 * <br>Can be <jk>null</jk> to create a header with no value. 271 */ 272 public Accept(String value) { 273 super(NAME, value); 274 } 275 276 /** 277 * Constructor with delayed value. 278 * 279 * <p> 280 * Header value is re-evaluated on each call to {@link #getValue()}. 281 * 282 * @param value 283 * The supplier of the header value. 284 * <br>Can be <jk>null</jk> to create a header with no value. 285 */ 286 public Accept(Supplier<MediaRanges> value) { 287 super(NAME, value); 288 } 289}