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