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.*;
018import org.apache.juneau.http.annotation.Header;
019import org.apache.juneau.internal.*;
020
021/**
022 * Represents a parsed <l>Content-Type</l> HTTP request/response header.
023 *
024 * <p>
025 * The MIME type of this content.
026 *
027 * <h5 class='figure'>Example</h5>
028 * <p class='bcode'>
029 *    Content-Type: text/html; charset=utf-8
030 * </p>
031 *
032 * <h5 class='topic'>RFC2616 Specification</h5>
033 *
034 * The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient or, in the
035 * case of the HEAD method, the media type that would have been sent had the request been a GET.
036 * <p class='bcode'>
037 *    Content-Type   = "Content-Type" ":" media-type
038 * </p>
039 *
040 * <p>
041 * Media types are defined in section 3.7.
042 * An example of the field is...
043 * <p class='bcode'>
044 *    Content-Type: text/html; charset=ISO-8859-4
045 * </p>
046 *
047 * <h5 class='section'>See Also:</h5><ul>
048 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
049 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
050 * </ul>
051 *
052 * @serial exclude
053 */
054@Header("Content-Type")
055public class ContentType extends BasicMediaTypeHeader {
056
057   //-----------------------------------------------------------------------------------------------------------------
058   // Static
059   //-----------------------------------------------------------------------------------------------------------------
060
061   private static final long serialVersionUID = 1L;
062   private static final String NAME = "Content-Type";
063
064   private static Cache<String,ContentType> CACHE = Cache.of(String.class, ContentType.class).build();
065
066   // Constants
067   @SuppressWarnings("javadoc")
068   public static final ContentType
069      APPLICATION_ATOM_XML = of("application/atom+xml"),
070      APPLICATION_FORM_URLENCODED = of("application/x-www-form-urlencoded"),
071      APPLICATION_JSON = of("application/json"),
072      APPLICATION_OCTET_STREAM = of("application/octet-stream"),
073      APPLICATION_SOAP_XML = of("application/soap+xml"),
074      APPLICATION_SVG_XML = of("application/svg+xml"),
075      APPLICATION_XHTML_XML = of("application/xhtml+xml"),
076      APPLICATION_XML = of("application/xml"),
077      IMAGE_BMP = of("image/bmp"),
078      IMAGE_GIF = of("image/gif"),
079      IMAGE_JPEG = of("image/jpeg"),
080      IMAGE_PNG = of("image/png"),
081      IMAGE_SVG = of("image/svg+xml"),
082      IMAGE_TIFF = of("image/tiff"),
083      IMAGE_WEBP = of("image/webp"),
084      MULTIPART_FORM_DATA = of("multipart/form-data"),
085      TEXT_HTML = of("text/html"),
086      TEXT_OPENAPI = of("text/openapi"),
087      TEXT_PLAIN = of("text/plain"),
088      TEXT_XML = of("text/xml"),
089      WILDCARD = of("*/*"),
090      NULL = new ContentType((String)null);
091
092   /**
093    * Static creator.
094    *
095    * @param value
096    *    The header value.
097    *    <br>Must be parsable by {@link MediaType#of(String)}.
098    *    <br>Can be <jk>null</jk>.
099    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
100    */
101   public static ContentType of(String value) {
102      return value == null ? null : CACHE.get(value, ()->new ContentType(value));
103   }
104
105   /**
106    * Static creator.
107    *
108    * @param value
109    *    The header value.
110    *    <br>Can be <jk>null</jk>.
111    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
112    */
113   public static ContentType of(MediaType value) {
114      return value == null ? null : new ContentType(value);
115   }
116
117   /**
118    * Static creator with delayed value.
119    *
120    * <p>
121    * Header value is re-evaluated on each call to {@link #getValue()}.
122    *
123    * @param value
124    *    The header value.
125    *    <br>Can be <jk>null</jk>.
126    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
127    */
128   public static ContentType of(Supplier<MediaType> value) {
129      return value == null ? null : new ContentType(value);
130   }
131
132   //-----------------------------------------------------------------------------------------------------------------
133   // Instance
134   //-----------------------------------------------------------------------------------------------------------------
135
136   /**
137    * Constructor.
138    *
139    * @param value
140    *    The header value.
141    *    <br>Must be parsable by {@link MediaType#of(String)}.
142    *    <br>Can be <jk>null</jk>.
143    */
144   public ContentType(String value) {
145      super(NAME, value);
146   }
147
148   /**
149    * Constructor.
150    *
151    * @param value
152    *    The header value.
153    *    <br>Can be <jk>null</jk>.
154    */
155   public ContentType(MediaType value) {
156      super(NAME, value);
157   }
158
159   /**
160    * Constructor with delayed value.
161    *
162    * <p>
163    * Header value is re-evaluated on each call to {@link #getValue()}.
164    *
165    * @param value
166    *    The supplier of the header value.
167    *    <br>Can be <jk>null</jk>.
168    */
169   public ContentType(Supplier<MediaType> value) {
170      super(NAME, value);
171   }
172}