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;
014
015import static org.apache.juneau.http.Constants.*;
016
017import org.apache.juneau.http.annotation.*;
018import org.apache.juneau.internal.*;
019
020/**
021 * Represents a parsed <l>Content-Type</l> HTTP request/response header.
022 *
023 * <p>
024 * The MIME type of this content.
025 *
026 * <h5 class='figure'>Example</h5>
027 * <p class='bcode w800'>
028 *    Content-Type: text/html; charset=utf-8
029 * </p>
030 *
031 * <h5 class='topic'>RFC2616 Specification</h5>
032 *
033 * The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient or, in the
034 * case of the HEAD method, the media type that would have been sent had the request been a GET.
035 * <p class='bcode w800'>
036 *    Content-Type   = "Content-Type" ":" media-type
037 * </p>
038 *
039 * <p>
040 * Media types are defined in section 3.7.
041 * An example of the field is...
042 * <p class='bcode w800'>
043 *    Content-Type: text/html; charset=ISO-8859-4
044 * </p>
045 *
046 * <ul class='seealso'>
047 *    <li class='extlink'>{@doc RFC2616}
048 * </ul>
049 */
050@Header("Content-Type")
051public class ContentType extends MediaType {
052
053   private static Cache<String,ContentType> cache = new Cache<>(NOCACHE, CACHE_MAX_SIZE);
054
055   /**
056    * Returns a parsed <c>Content-Type</c> header.
057    *
058    * @param value The <c>Content-Type</c> header string.
059    * @return The parsed <c>Content-Type</c> header, or <jk>null</jk> if the string was null.
060    */
061   public static ContentType forString(String value) {
062      if (value == null)
063         return null;
064      ContentType ct = cache.get(value);
065      if (ct == null)
066         ct = cache.put(value, new ContentType(value));
067      return ct;
068   }
069
070
071   private ContentType(String s) {
072      super(s);
073   }
074
075   /**
076    * Given a list of media types, returns the best match for this <c>Content-Type</c> header.
077    *
078    * <p>
079    * Note that fuzzy matching is allowed on the media types where the <c>Content-Types</c> header may
080    * contain additional subtype parts.
081    * <br>For example, given a <c>Content-Type</c> value of <js>"text/json+activity"</js>,
082    * the media type <js>"text/json"</js> will match if <js>"text/json+activity"</js> or <js>"text/activity+json"</js>
083    * isn't found.
084    * <br>The purpose for this is to allow parsers to match when artifacts such as <c>id</c> properties are
085    * present in the header.
086    *
087    * @param mediaTypes The media types to match against.
088    * @return The index into the array of the best match, or <c>-1</c> if no suitable matches could be found.
089    */
090   public int findMatch(MediaType[] mediaTypes) {
091      int matchQuant = 0, matchIndex = -1;
092
093      for (int i = 0; i < mediaTypes.length; i++) {
094         MediaType mt = mediaTypes[i];
095         int matchQuant2 = mt.match(this, true);
096         if (matchQuant2 > matchQuant) {
097            matchQuant = matchQuant2;
098            matchIndex = i;
099         }
100      }
101      return matchIndex;
102   }
103}