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 java.util.*; 016 017import org.apache.http.*; 018import org.apache.juneau.annotation.*; 019import org.apache.juneau.collections.*; 020 021 022/** 023 * Describes a single type used in content negotiation between an HTTP client and server, as described in 024 * Section 14.1 and 14.7 of RFC2616 (the HTTP/1.1 specification). 025 * 026 * <ul class='seealso'> 027 * <li class='extlink'>{@doc ExtRFC2616} 028 * </ul> 029 */ 030@BeanIgnore 031public class MediaRange extends MediaType { 032 033 private final NameValuePair[] extensions; 034 private final Float qValue; 035 private final String string; 036 037 /** 038 * Constructor. 039 * 040 * @param e The parsed media range element. 041 */ 042 public MediaRange(HeaderElement e) { 043 super(e); 044 045 Float qValue = 1f; 046 047 // The media type consists of everything up to the q parameter. 048 // The q parameter and stuff after is part of the range. 049 List<NameValuePair> extensions = AList.of(); 050 boolean foundQ = false; 051 for (NameValuePair p : e.getParameters()) { 052 if (p.getName().equals("q")) { 053 qValue = Float.parseFloat(p.getValue()); 054 foundQ = true; 055 } else if (foundQ) { 056 extensions.add(BasicNameValuePair.of(p.getName(), p.getValue())); 057 } 058 } 059 060 this.qValue = qValue; 061 this.extensions = extensions.toArray(new NameValuePair[extensions.size()]); 062 063 StringBuffer sb = new StringBuffer().append(super.toString()); 064 065 // '1' is equivalent to specifying no qValue. If there's no extensions, then we won't include a qValue. 066 if (qValue.floatValue() == 1.0) { 067 if (this.extensions.length > 0) { 068 sb.append(";q=").append(qValue); 069 for (NameValuePair p : extensions) 070 sb.append(';').append(p.getName()).append('=').append(p.getValue()); 071 } 072 } else { 073 sb.append(";q=").append(qValue); 074 for (NameValuePair p : extensions) 075 sb.append(';').append(p.getName()).append('=').append(p.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 Collections.unmodifiableList(Arrays.asList(extensions)); 106 } 107 108 @Override /* Object */ 109 public String toString() { 110 return string; 111 } 112}