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 * <h5 class='section'>See Also:</h5> 047 * <ul class='doctree'> 048 * <li class='extlink'>{@doc RFC2616} 049 * </ul> 050 */ 051@Header("Content-Type") 052public class ContentType extends MediaType { 053 054 private static Cache<String,ContentType> cache = new Cache<>(NOCACHE, CACHE_MAX_SIZE); 055 056 /** 057 * Returns a parsed <code>Content-Type</code> header. 058 * 059 * @param value The <code>Content-Type</code> header string. 060 * @return The parsed <code>Content-Type</code> header, or <jk>null</jk> if the string was null. 061 */ 062 public static ContentType forString(String value) { 063 if (value == null) 064 return null; 065 ContentType ct = cache.get(value); 066 if (ct == null) 067 ct = cache.put(value, new ContentType(value)); 068 return ct; 069 } 070 071 072 private ContentType(String s) { 073 super(s); 074 } 075 076 /** 077 * Given a list of media types, returns the best match for this <code>Content-Type</code> header. 078 * 079 * <p> 080 * Note that fuzzy matching is allowed on the media types where the <code>Content-Types</code> header may 081 * contain additional subtype parts. 082 * <br>For example, given a <code>Content-Type</code> value of <js>"text/json+activity"</js>, 083 * the media type <js>"text/json"</js> will match if <js>"text/json+activity"</js> or <js>"text/activity+json"</js> 084 * isn't found. 085 * <br>The purpose for this is to allow parsers to match when artifacts such as <code>id</code> properties are 086 * present in the header. 087 * 088 * @param mediaTypes The media types to match against. 089 * @return The index into the array of the best match, or <code>-1</code> if no suitable matches could be found. 090 */ 091 public int findMatch(MediaType[] mediaTypes) { 092 int matchQuant = 0, matchIndex = -1; 093 094 for (int i = 0; i < mediaTypes.length; i++) { 095 MediaType mt = mediaTypes[i]; 096 int matchQuant2 = mt.match(this, true); 097 if (matchQuant2 > matchQuant) { 098 matchQuant = matchQuant2; 099 matchIndex = i; 100 } 101 } 102 return matchIndex; 103 } 104}