/** * @return bets matching {@link MediaType} for 'reference' between 'candidate1' and 'candidate2'. If both candidate have the same matching average, return 'candidate1'. */ MediaType getBestMatchingCandidate(MediaType candidate1, MediaType candidate2, MediaType reference) { if(reference.equals(candidate1)) { return candidate1; } if(reference.equals(candidate2)) { return candidate2; } if (voteOnMediaType(candidate1, reference) >= voteOnMediaType(candidate2, reference)) { return candidate1; } return candidate2; }
/** * Get the preferred content media type that is acceptable for the client. For instance, in Accept: text/*;q=0.3, * text/html;q=0.7, text/html;level=1,text/html;level=2;q=0.4, text/html is returned. * <p/> * The Accept request-header field can be used to specify certain media * types which are acceptable for the response. Accept headers can be used * to indicate that the request is specifically limited to a small set of * desired types, as in the case of a request for an in-line image. * * @return a MediaType that is acceptable for the * client or {@see MediaType#HTML_UTF_8} if not set * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html" * >http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html</a> */ @Override public MediaType mediaType() { Collection<MediaType> types = mediaTypes(); if (types == null || types.isEmpty()) { return MediaType.ANY_TEXT_TYPE; } else if (types.size() == 1 && types.iterator().next().equals(MediaType.ANY_TYPE)) { return MediaType.ANY_TEXT_TYPE; } else { return types.iterator().next(); } }
/** * Get the preferred content media type that is acceptable for the client. For instance, in Accept: text/*;q=0.3, * text/html;q=0.7, text/html;level=1,text/html;level=2;q=0.4, text/html is returned. * <p/> * The Accept request-header field can be used to specify certain media * types which are acceptable for the response. Accept headers can be used * to indicate that the request is specifically limited to a small set of * desired types, as in the case of a request for an in-line image. * * @return a MediaType that is acceptable for the * client or {@see MediaType#HTML_UTF_8} if not set * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html" * >http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html</a> */ @Override public MediaType mediaType() { Collection<MediaType> types = mediaTypes(); if (types == null || types.isEmpty()) { return MediaType.ANY_TEXT_TYPE; } else if (types.size() == 1 && types.iterator().next().equals(MediaType.ANY_TYPE)) { return MediaType.ANY_TEXT_TYPE; } else { return types.iterator().next(); } }