Skip to content

Commit

Permalink
Using optimized Vertx HTTP header names
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 committed Dec 8, 2023
1 parent 87eb294 commit f6e9240
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.vertx.web.runtime;

import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;

import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
Expand All @@ -9,15 +11,14 @@ public final class RouteHandlers {
private RouteHandlers() {
}

static final String CONTENT_TYPE = "content-type";

public static void setContentType(RoutingContext context, String defaultContentType) {
HttpServerResponse response = context.response();
context.addHeadersEndHandler(new Handler<Void>() {
@Override
public void handle(Void aVoid) {
var headers = response.headers();
//use a listener to set the content type if it has not been set
if (response.headers().get(CONTENT_TYPE) == null) {
if (!headers.contains(CONTENT_TYPE)) {
String acceptableContentType = context.getAcceptableContentType();
if (acceptableContentType != null) {
response.putHeader(CONTENT_TYPE, acceptableContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.validation.Validator;

import io.quarkus.arc.ArcContainer;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
Expand Down Expand Up @@ -70,7 +71,7 @@ private static JsonObject generateJsonResponse(Set<ConstraintViolation<?>> viola
public static void handleViolationException(ConstraintViolationException ex, RoutingContext rc, boolean forceJsonEncoding) {
String accept = rc.request().getHeader(ACCEPT_HEADER);
if (forceJsonEncoding || accept != null && accept.contains(APPLICATION_JSON)) {
rc.response().putHeader(RouteHandlers.CONTENT_TYPE, APPLICATION_JSON);
rc.response().putHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
JsonObject json = generateJsonResponse(ex.getConstraintViolations(), false);
rc.response().setStatusCode(json.getInteger(PROBLEM_STATUS));
rc.response().end(json.encode());
Expand Down

0 comments on commit f6e9240

Please sign in to comment.