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