List<MediaType> acceptableMediaTypes =
msgContext.getHttpHeaders().getAcceptableMediaTypes();
List<String> acceptableLanguages =
msgContext.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT_LANGUAGE);
AcceptLanguage languages = null;
if (acceptableLanguages != null) {
StringBuilder acceptLanguageTemp = new StringBuilder();
acceptLanguageTemp.append(acceptableLanguages.get(0));
for (int c = 1; c < acceptableLanguages.size(); ++c) {
acceptLanguageTemp.append(","); //$NON-NLS-1$
acceptLanguageTemp.append(acceptableLanguages.get(c));
}
String acceptLanguage = acceptLanguageTemp.toString();
languages = AcceptLanguage.valueOf(acceptLanguage);
}
List<String> acceptableEncodings =
msgContext.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT_ENCODING);
AcceptEncoding encodings = null;
if (acceptableEncodings != null) {
StringBuilder acceptEncodingsTemp = new StringBuilder();
acceptEncodingsTemp.append(acceptableEncodings.get(0));
for (int c = 1; c < acceptableEncodings.size(); ++c) {
acceptEncodingsTemp.append(","); //$NON-NLS-1$
acceptEncodingsTemp.append(acceptableEncodings.get(c));
}
String acceptEncodings = acceptEncodingsTemp.toString();
encodings = AcceptEncoding.valueOf(acceptEncodings);
}
List<String> acceptableCharsets =
msgContext.getHttpHeaders().getRequestHeader(HttpHeaders.ACCEPT_CHARSET);
AcceptCharset charsets = null;
if (acceptableCharsets != null) {
StringBuilder acceptCharsetsTemp = new StringBuilder();
acceptCharsetsTemp.append(acceptableCharsets.get(0));
for (int c = 1; c < acceptableCharsets.size(); ++c) {
acceptCharsetsTemp.append(","); //$NON-NLS-1$
acceptCharsetsTemp.append(acceptableCharsets.get(c));
}
String acceptCharsets = acceptCharsetsTemp.toString();
charsets = AcceptCharset.valueOf(acceptCharsets);
}
VariantQChecked bestVariant = null;
boolean isIdentityEncodingChecked = false;
for (Iterator<Variant> iter = variants.iterator(); iter.hasNext();) {
double acceptQFactor = -1.0d;
Variant v = iter.next();
logger.debug("Variant being evaluated is: {}", v); //$NON-NLS-1$
MediaType vMediaType = v.getMediaType();
if (vMediaType != null && acceptableMediaTypes != null) {
boolean isCompatible = false;
boolean isAcceptable = true; // explicitly denied by the client
for (MediaType mt : acceptableMediaTypes) {
logger.debug("Checking variant media type {} against Accept media type {}", //$NON-NLS-1$
vMediaType,
mt);
if (mt.isCompatible(vMediaType)) {
Map<String, String> params = mt.getParameters();
String q = params.get("q"); //$NON-NLS-1$
if (q != null) {
try {
Double qAsDouble = Double.valueOf(q);
if (qAsDouble.equals(0.0)) {
isAcceptable = false;
logger
.debug("Accept Media Type: {} is NOT compatible with q-factor {}", //$NON-NLS-1$
mt,
qAsDouble);
break;
}
acceptQFactor = qAsDouble;
} catch (NumberFormatException e) {
logger
.debug("NumberFormatException during MediaType q-factor evaluation: {}", //$NON-NLS-1$
e);
}
} else {
acceptQFactor = 1.0d;
}
isCompatible = true;
logger.debug("Accept Media Type: {} is compatible with q-factor {}", //$NON-NLS-1$
mt,
acceptQFactor);
}
}
if (!isCompatible || !isAcceptable) {
logger.debug("Variant {} is not compatible or not acceptable", vMediaType); //$NON-NLS-1$
continue;
}
}
if (bestVariant != null) {
if (acceptQFactor < bestVariant.acceptMediaTypeQFactor) {
logger
.debug("Best variant's media type {} q-factor {} is greater than current variant {} q-factor {}", //$NON-NLS-1$
new Object[] {bestVariant.variant,
bestVariant.acceptMediaTypeQFactor, vMediaType, acceptQFactor});
continue;
}
}
double acceptLanguageQFactor = -1.0d;
Locale vLocale = v.getLanguage();
if (vLocale != null && languages != null) {
boolean isCompatible = false;
logger.debug("Checking variant locale {}", vLocale); //$NON-NLS-1$
if (languages.getBannedLanguages().contains(vLocale)) {
logger.debug("Variant locale {} was in unacceptable languages", vLocale); //$NON-NLS-1$
continue;
}
for (AcceptLanguage.ValuedLocale locale : languages.getValuedLocales()) {
logger
.debug("Checking against Accept-Language locale {} with quality factor {}", //$NON-NLS-1$
locale.locale,
locale.qValue);
if (locale.isWildcard() || vLocale.equals(locale.locale)) {