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.internal.CollectionUtils.*;
016
017import java.util.*;
018import java.util.function.*;
019
020import org.apache.http.*;
021import org.apache.http.message.*;
022
023
024/**
025 * Describes a single type used in content negotiation between an HTTP client and server, as described in
026 * Section 14.1 and 14.7 of RFC2616 (the HTTP/1.1 specification).
027 *
028 * <h5 class='section'>See Also:</h5><ul>
029 *    <li class='link'><a class="doclink" href="../../../index.html#juneau-rest-common">juneau-rest-common</a>
030 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
031 * </ul>
032 */
033public class MediaRange extends MediaType {
034
035   private final NameValuePair[] extensions;
036   private final Float qValue;
037   private final String string;
038
039   /**
040    * Constructor.
041    *
042    * @param e The parsed media range element.
043    */
044   public MediaRange(HeaderElement e) {
045      super(e);
046
047      Float qValue = 1f;
048
049      // The media type consists of everything up to the q parameter.
050      // The q parameter and stuff after is part of the range.
051      List<NameValuePair> extensions = list();
052      boolean foundQ = false;
053      for (NameValuePair p : e.getParameters()) {
054         if (p.getName().equals("q")) {
055            qValue = Float.parseFloat(p.getValue());
056            foundQ = true;
057         } else if (foundQ) {
058            extensions.add(new BasicNameValuePair(p.getName(), p.getValue()));
059         }
060      }
061
062      this.qValue = qValue;
063      this.extensions = extensions.toArray(new NameValuePair[extensions.size()]);
064
065      StringBuffer sb = new StringBuffer().append(super.toString());
066
067      // '1' is equivalent to specifying no qValue. If there's no extensions, then we won't include a qValue.
068      if (qValue.floatValue() == 1.0) {
069         if (this.extensions.length > 0) {
070            sb.append(";q=").append(qValue);
071            extensions.forEach(x -> sb.append(';').append(x.getName()).append('=').append(x.getValue()));
072         }
073      } else {
074         sb.append(";q=").append(qValue);
075         extensions.forEach(x -> sb.append(';').append(x.getName()).append('=').append(x.getValue()));
076      }
077      string = sb.toString();
078   }
079
080   /**
081    * Returns the <js>'q'</js> (quality) value for this type, as described in Section 3.9 of RFC2616.
082    *
083    * <p>
084    * The quality value is a float between <c>0.0</c> (unacceptable) and <c>1.0</c> (most acceptable).
085    *
086    * <p>
087    * If 'q' value doesn't make sense for the context (e.g. this range was extracted from a <js>"content-*"</js>
088    * header, as opposed to <js>"accept-*"</js> header, its value will always be <js>"1"</js>.
089    *
090    * @return The 'q' value for this type, never <jk>null</jk>.
091    */
092   public Float getQValue() {
093      return qValue;
094   }
095
096   /**
097    * Returns the optional set of custom extensions defined for this type.
098    *
099    * <p>
100    * Values are lowercase and never <jk>null</jk>.
101    *
102    * @return The optional list of extensions, never <jk>null</jk>.
103    */
104   public List<NameValuePair> getExtensions() {
105      return ulist(extensions);
106   }
107
108   /**
109    * Performs an action on the optional set of custom extensions defined for this type.
110    *
111    * <p>
112    * Values are lowercase and never <jk>null</jk>.
113    *
114    * @param action The action to perform.
115    * @return This object.
116    */
117   public MediaRange forEachExtension(Consumer<NameValuePair> action) {
118      for (NameValuePair r : extensions)
119         action.accept(r);
120      return this;
121   }
122
123   @Override /* Object */
124   public String toString() {
125      return string;
126   }
127}