All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jetdrone.vertx.yoke.middleware.ResponseHeaders Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version

package com.jetdrone.vertx.yoke.middleware;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.jetbrains.annotations.NotNull;
import org.vertx.java.core.Handler;

import com.jetdrone.vertx.yoke.Middleware;

/**
 * 

* A simple {@link Middleware} that allows adding custom response headers to all * {@link YokeResponse}. *

*

* Example: *

* *
 * {@code
 * yoke.use(new ResponseHeaders()
 *          .with("X-Build-Meta", "1.0-SNAPSHOT")
 *          .with("X-Server-Id", "server-123"));
 * }
 * 
*/ public class ResponseHeaders extends Middleware { private final Map headers; public ResponseHeaders() { headers = new HashMap<>(); } public ResponseHeaders with(final CharSequence name, final CharSequence... values) { headers.put(name, values); return this; } @Override public void handle(@NotNull final YokeRequest request, @NotNull final Handler next) { for (final Entry header : headers.entrySet()) { request.response().headers().add(header.getKey(), Arrays.asList(header.getValue())); } next.handle(null); } }