package io.vertx.ext.web.handler.impl;
import io.vertx.core.MultiMap;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.ResponseContentTypeHandler;
import static io.vertx.core.http.HttpHeaders.*;
public class ResponseContentTypeHandlerImpl implements ResponseContentTypeHandler {
private final String disableFlag;
public ResponseContentTypeHandlerImpl(String disableFlag) {
this.disableFlag = disableFlag;
}
@Override
public void handle(RoutingContext rc) {
rc.addHeadersEndHandler(v -> {
if (rc.get(disableFlag) != null) {
return;
}
String acceptableContentType = rc.getAcceptableContentType();
if (acceptableContentType == null) {
return;
}
MultiMap headers = rc.response().headers();
if (headers.contains(CONTENT_TYPE)) {
return;
}
if (!"0".equals(headers.get(CONTENT_LENGTH))) {
headers.add(CONTENT_TYPE, acceptableContentType);
}
});
rc.next();
}
}