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 = new Comparator<>() {
143      @Override
144      public int compare(StringRange o1, StringRange o2) {
145         // Compare q-values.
146         int qCompare = Float.compare(o2.getQValue(), o1.getQValue());
147         if (qCompare != 0)
148            return qCompare;
149
150         // Compare media-types.
151         // Note that '*' comes alphabetically before letters, so just do a reverse-alphabetical comparison.
152         int i = o2.toString().compareTo(o1.toString());
153         return i;
154      }
155   };
156
157   /**
158    * Given a list of media types, returns the best match for this string range header.
159    *
160    * <p>
161    * Note that fuzzy matching is allowed on the media types where the string range header may
162    * contain additional subtype parts.
163    * <br>For example, given identical q-values and an string range value of <js>"text/json+activity"</js>,
164    * the media type <js>"text/json"</js> will match if <js>"text/json+activity"</js> or <js>"text/activity+json"</js>
165    * isn't found.
166    * <br>The purpose for this is to allow serializers to match when artifacts such as <c>id</c> properties are
167    * present in the header.
168    *
169    * <p>
170    * See <a class="doclink" href="https://www.w3.org/TR/activitypub/#retrieving-objects">ActivityPub / Retrieving Objects</a>
171    *
172    * @param names The names to match against.
173    * @return The index into the array of the best match, or <c>-1</c> if no suitable matches could be found.
174    */
175   public int match(List<String> names) {
176      if (string.isEmpty())
177         return -1;
178
179      int matchQuant = 0, matchIndex = -1;
180      float q = 0f;
181
182      // Media ranges are ordered by 'q'.
183      // So we only need to search until we've found a match.
184      for (StringRange mr : value) {
185         float q2 = mr.getQValue();
186
187         if (q2 < q || q2 == 0)
188            break;
189
190         for (int i = 0; i < names.size(); i++) {
191            String mt = names.get(i);
192            int matchQuant2 = mr.match(mt);
193
194            if (matchQuant2 > matchQuant) {
195               matchIndex = i;
196               matchQuant = matchQuant2;
197               q = q2;
198            }
199         }
200      }
201
202      return matchIndex;
203   }
204
205   /**
206    * Returns the {@link MediaRange} at the specified index.
207    *
208    * @param index The index position of the media range.
209    * @return The {@link MediaRange} at the specified index or <jk>null</jk> if the index is out of range.
210    */
211   public StringRange getRange(int index) {
212      if (index < 0 || index >= value.length)
213         return null;
214      return value[index];
215   }
216
217   /**
218    * Returns the string ranges that make up this object.
219    *
220    * @return The string ranges that make up this object.
221    */
222   public List<StringRange> toList() {
223      return ulist(value);
224   }
225
226   /**
227    * Performs an action on the string ranges that make up this object.
228    *
229    * @param action The action to perform.
230    * @return This object.
231    */
232   public StringRanges forEachRange(Consumer<StringRange> action) {
233      for (StringRange r : value)
234         action.accept(r);
235      return this;
236   }
237
238   private static HeaderElement[] parse(String value) {
239      return value == null ? null : BasicHeaderValueParser.parseElements(emptyIfNull(trim(value)), null);
240   }
241
242   @Override /* Object */
243   public String toString() {
244      return string;
245   }
246}