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