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 static org.apache.juneau.http.Constants.*;
016
017import java.util.function.*;
018
019import org.apache.juneau.annotation.*;
020import org.apache.juneau.http.*;
021import org.apache.juneau.http.annotation.Header;
022import org.apache.juneau.internal.*;
023
024/**
025 * Represents a parsed <l>Content-Type</l> HTTP request/response header.
026 *
027 * <p>
028 * The MIME type of this content.
029 *
030 * <h5 class='figure'>Example</h5>
031 * <p class='bcode w800'>
032 *    Content-Type: text/html; charset=utf-8
033 * </p>
034 *
035 * <h5 class='topic'>RFC2616 Specification</h5>
036 *
037 * The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient or, in the
038 * case of the HEAD method, the media type that would have been sent had the request been a GET.
039 * <p class='bcode w800'>
040 *    Content-Type   = "Content-Type" ":" media-type
041 * </p>
042 *
043 * <p>
044 * Media types are defined in section 3.7.
045 * An example of the field is...
046 * <p class='bcode w800'>
047 *    Content-Type: text/html; charset=ISO-8859-4
048 * </p>
049 *
050 * <ul class='seealso'>
051 *    <li class='extlink'>{@doc ExtRFC2616}
052 * </ul>
053 */
054@Header("Content-Type")
055@BeanIgnore
056public class ContentType extends BasicMediaTypeHeader {
057
058   private static final long serialVersionUID = 1L;
059
060   private static Cache<String,ContentType> CACHE = new Cache<>(NOCACHE, CACHE_MAX_SIZE);
061
062   /**
063    * Returns a parsed and cached <c>Content-Type</c> header.
064    *
065    * @param value The <c>Content-Type</c> header string.
066    * @return The parsed <c>Content-Type</c> header, or <jk>null</jk> if the string was null.
067    */
068   public static ContentType of(String value) {
069      if (value == null)
070         return null;
071      ContentType ct = CACHE.get(value);
072      if (ct == null)
073         ct = CACHE.put(value, new ContentType(value));
074      return ct;
075   }
076
077   /**
078    * Returns a parsed and cached <c>Content-Type</c> header.
079    *
080    * @param value The <c>Content-Type</c> header value.
081    * @return The parsed <c>Content-Type</c> header, or <jk>null</jk> if the string was null.
082    */
083   public static ContentType of(MediaType value) {
084      if (value == null)
085         return null;
086      return of(value.toString());
087   }
088
089   /**
090    * Convenience creator.
091    *
092    * @param value
093    *    The header value.
094    *    <br>Can be any of the following:
095    *    <ul>
096    *       <li>{@link String} - Converted using {@link MediaType#of(String)}.
097    *       <li>{@link MediaType}.
098    *       <li>Anything else - Converted to <c>String</c> then parsed.
099    *    </ul>
100    * @return A new {@link AcceptEncoding} object.
101    */
102   public static ContentType of(Object value) {
103      return new ContentType(value);
104   }
105
106   /**
107    * Convenience creator using supplier.
108    *
109    * <p>
110    * Header value is re-evaluated on each call to {@link #getValue()}.
111    *
112    * @param value
113    *    The header value supplier.
114    *    <br>Can be any of the following:
115    *    <ul>
116    *       <li>{@link String} - Converted using {@link MediaType#of(String)}.
117    *       <li>{@link MediaType}.
118    *       <li>Anything else - Converted to <c>String</c> then parsed.
119    *    </ul>
120    * @return A new {@link AcceptEncoding} object.
121    */
122   public static ContentType of(Supplier<?> value) {
123      return new ContentType(value);
124   }
125
126   /**
127    * Constructor.
128    *
129    * @param value The value for this header.
130    */
131   public ContentType(Object value) {
132      super("Content-Type", value);
133   }
134
135   /**
136    * Constructor.
137    *
138    * @param value The value for this header.
139    */
140   public ContentType(String value) {
141      this((Object)value);
142   }
143}