}
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final List<String> res = exchange.getRequestHeaders().get(Headers.ACCEPT_ENCODING);
HttpHandler nextHandler = this.next;
if (res == null || res.isEmpty()) {
if (nextHandler != null) {
nextHandler.handleRequest(exchange);
} else {
//we don't have an identity handler
noEncodingHandler.handleRequest(exchange);
}
return;
}
final List<EncodingMapping> resultingMappings = new ArrayList<EncodingMapping>();
final List<List<QValueParser.QValueResult>> found = QValueParser.parse(res);
for (List<QValueParser.QValueResult> result : found) {
List<EncodingMapping> available = new ArrayList<EncodingMapping>();
boolean includesIdentity = false;
boolean isQValue0 = false;
for (final QValueParser.QValueResult value : result) {
EncodingMapping encoding;
if (value.getValue().equals("*")) {
includesIdentity = true;
encoding = new EncodingMapping(IDENTITY, ContentEncodingProvider.IDENTITY, 0, Predicates.truePredicate());
} else {
encoding = encodingMap.get(value.getValue());
}
if (value.isQValueZero()) {
isQValue0 = true;
}
if (encoding != null) {
available.add(encoding);
}
}
if (isQValue0) {
if (resultingMappings.isEmpty()) {
if (includesIdentity) {
noEncodingHandler.handleRequest(exchange);
return;
} else {
nextHandler.handleRequest(exchange);
return;
}
}
} else if (!available.isEmpty()) {
Collections.sort(available, Collections.reverseOrder());
resultingMappings.addAll(available);
}
}
if (!resultingMappings.isEmpty()) {
final ContentEncoding contentEncoding = new ContentEncoding(exchange, resultingMappings);
exchange.addResponseWrapper(contentEncoding);
exchange.putAttachment(ContentEncoding.CONENT_ENCODING, contentEncoding);
}
nextHandler.handleRequest(exchange);
}