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; 018 019import static org.apache.juneau.commons.utils.CollectionUtils.*; 020import static org.apache.juneau.commons.utils.StringUtils.*; 021import static org.apache.juneau.commons.utils.Utils.*; 022 023import java.util.*; 024import java.util.function.*; 025 026import org.apache.http.*; 027import org.apache.http.message.*; 028import org.apache.juneau.annotation.*; 029import org.apache.juneau.commons.collections.*; 030 031/** 032 * A parsed <c>Accept-Encoding</c> or similar header value. 033 * 034 * <p> 035 * The returned ranges are sorted such that the most acceptable value is available at ordinal position 036 * <js>'0'</js>, and the least acceptable at position n-1. 037 * 038 * <h5 class='topic'>RFC2616 Specification</h5> 039 * 040 * The Accept-Encoding request-header field is similar to Accept, but restricts the content-codings (section 3.5) that 041 * are acceptable in the response. 042 * 043 * <p class='bcode'> 044 * Accept-Encoding = "Accept-Encoding" ":" 045 * 1#( codings [ ";" "q" "=" qvalue ] ) 046 * codings = ( content-coding | "*" ) 047 * </p> 048 * 049 * <p> 050 * Examples of its use are: 051 * <p class='bcode'> 052 * Accept-Encoding: compress, gzip 053 * Accept-Encoding: 054 * Accept-Encoding: * 055 * Accept-Encoding: compress;q=0.5, gzip;q=1.0 056 * Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0 057 * </p> 058 * 059 * <h5 class='section'>See Also:</h5><ul> 060 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a> 061 * <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a> 062 * </ul> 063 */ 064@BeanIgnore 065public class StringRanges { 066 /** Represents an empty string ranges object. */ 067 public static final StringRanges EMPTY = new StringRanges(""); 068 069 private static final Cache<String,StringRanges> CACHE = Cache.of(String.class, StringRanges.class).build(); 070 071 /** 072 * Compares two StringRanges for equality. 073 * 074 * <p> 075 * The values are first compared according to <c>qValue</c> values. 076 * Should those values be equal, the <c>type</c> is then lexicographically compared (case-insensitive) in 077 * ascending order, with the <js>"*"</js> type demoted last in that order. 078 */ 079 private static final Comparator<StringRange> RANGE_COMPARATOR = (o1, o2) -> { 080 // Compare q-values. 081 var qCompare = Float.compare(o2.getQValue(), o1.getQValue()); 082 if (qCompare != 0) 083 return qCompare; 084 085 // Compare media-types. 086 // Note that '*' comes alphabetically before letters, so just do a reverse-alphabetical comparison. 087 return o2.toString().compareTo(o1.toString()); 088 }; 089 090 /** 091 * Returns a parsed string range header value. 092 * 093 * @param value The raw header value. 094 * @return A parsed header value. 095 */ 096 public static StringRanges of(String value) { 097 return isEmpty(value) ? EMPTY : CACHE.get(value, () -> new StringRanges(value)); 098 } 099 100 /** 101 * Returns a parsed string range header value. 102 * 103 * @param value The raw header value. 104 * @return A parsed header value. 105 */ 106 public static StringRanges of(StringRange...value) { 107 return value == null ? null : new StringRanges(value); 108 } 109 110 private static HeaderElement[] parse(String value) { 111 return value == null ? null : BasicHeaderValueParser.parseElements(emptyIfNull(trim(value)), null); 112 } 113 114 private final StringRange[] value; 115 116 private final String string; 117 118 /** 119 * Constructor. 120 * 121 * @param e The parsed string range header value. 122 */ 123 public StringRanges(HeaderElement...e) { 124 125 value = new StringRange[e.length]; 126 for (var i = 0; i < e.length; i++) 127 value[i] = new StringRange(e[i]); 128 Arrays.sort(value, RANGE_COMPARATOR); 129 130 this.string = value.length == 1 ? value[0].toString() : join(value, ", "); 131 } 132 133 /** 134 * Constructor. 135 * 136 * @param value The string range header value. 137 */ 138 public StringRanges(String value) { 139 this(parse(value)); 140 } 141 142 /** 143 * Constructor. 144 * 145 * @param value The string range header value. 146 */ 147 public StringRanges(StringRange...value) { 148 this.string = join(value, ", "); 149 this.value = copyOf(value); 150 } 151 152 /** 153 * Performs an action on the string ranges that make up this object. 154 * 155 * @param action The action to perform. 156 * @return This object. 157 */ 158 public StringRanges forEachRange(Consumer<StringRange> action) { 159 for (var r : value) 160 action.accept(r); 161 return this; 162 } 163 164 /** 165 * Returns the {@link MediaRange} at the specified index. 166 * 167 * @param index The index position of the media range. 168 * @return The {@link MediaRange} at the specified index or <jk>null</jk> if the index is out of range. 169 */ 170 public StringRange getRange(int index) { 171 if (index < 0 || index >= value.length) 172 return null; 173 return value[index]; 174 } 175 176 /** 177 * Given a list of media types, returns the best match for this string range header. 178 * 179 * <p> 180 * Note that fuzzy matching is allowed on the media types where the string range header may 181 * contain additional subtype parts. 182 * <br>For example, given identical q-values and an string range value of <js>"text/json+activity"</js>, 183 * the media type <js>"text/json"</js> will match if <js>"text/json+activity"</js> or <js>"text/activity+json"</js> 184 * isn't found. 185 * <br>The purpose for this is to allow serializers to match when artifacts such as <c>id</c> properties are 186 * present in the header. 187 * 188 * <p> 189 * See <a class="doclink" href="https://www.w3.org/TR/activitypub/#retrieving-objects">ActivityPub / Retrieving Objects</a> 190 * 191 * @param names The names to match against. 192 * @return The index into the array of the best match, or <c>-1</c> if no suitable matches could be found. 193 */ 194 public int match(List<String> names) { 195 if (string.isEmpty()) 196 return -1; 197 198 int matchQuant = 0; 199 int matchIndex = -1; 200 var q = 0f; 201 202 // Media ranges are ordered by 'q'. 203 // So we only need to search until we've found a match. 204 for (var mr : value) { 205 var q2 = mr.getQValue(); 206 207 if (q2 < q || q2 == 0) 208 break; 209 210 for (var i = 0; i < names.size(); i++) { 211 var mt = names.get(i); 212 var matchQuant2 = mr.match(mt); 213 214 if (matchQuant2 > matchQuant) { 215 matchIndex = i; 216 matchQuant = matchQuant2; 217 q = q2; 218 } 219 } 220 } 221 222 return matchIndex; 223 } 224 225 /** 226 * Returns the string ranges that make up this object. 227 * 228 * @return The string ranges that make up this object. 229 */ 230 public List<StringRange> toList() { 231 return l(value); 232 } 233 234 @Override /* Overridden from Object */ 235 public String toString() { 236 return string; 237 } 238}