diff --git a/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/HeadersFilter.java b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/HeadersFilter.java new file mode 100644 index 00000000000..751ed753b89 --- /dev/null +++ b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/HeadersFilter.java @@ -0,0 +1,44 @@ +package org.glassfish.grizzly.bm; + +import java.io.IOException; + +import org.glassfish.grizzly.filterchain.FilterChainContext; +import org.glassfish.grizzly.filterchain.NextAction; +import org.glassfish.grizzly.http.HttpBaseFilter; +import org.glassfish.grizzly.http.HttpContent; +import org.glassfish.grizzly.http.HttpPacket; +import org.glassfish.grizzly.http.HttpRequestPacket; +import org.glassfish.grizzly.http.HttpResponsePacket; +import org.glassfish.grizzly.http.util.FastHttpDateFormat; +import org.glassfish.grizzly.http.util.Header; + +/** + * Must be added just before the HttpServerFilter i.e. second to last in the filter chain. + * + * @author zloster + * + */ +public class HeadersFilter extends HttpBaseFilter { + + @Override + protected void bind(HttpRequestPacket request, HttpResponsePacket response) { + // This is never called. I don't know why. + super.bind(request, response); + } + + @Override + public NextAction handleRead(FilterChainContext ctx) throws IOException { + // Taken from HttpServerFilter + final Object message = ctx.getMessage(); + if (HttpPacket.isHttp(message)) { + // Otherwise cast message to a HttpContent + final HttpContent httpContent = (HttpContent) message; + final HttpRequestPacket request = (HttpRequestPacket) httpContent.getHttpHeader(); + final HttpResponsePacket response = request.getResponse(); + response.setHeader(Header.Server, Server.SERVER_VERSION); + response.setHeader(Header.Date, FastHttpDateFormat.getCurrentDate()); + } + return super.handleRead(ctx); + } + +} \ No newline at end of file diff --git a/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/JsonHttpHandler.java b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/JsonHttpHandler.java index 0d9adda7d91..6f458e5d374 100644 --- a/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/JsonHttpHandler.java +++ b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/JsonHttpHandler.java @@ -1,14 +1,12 @@ package org.glassfish.grizzly.bm; -import com.fasterxml.jackson.databind.*; - import org.glassfish.grizzly.http.server.HttpHandler; import org.glassfish.grizzly.http.server.Request; import org.glassfish.grizzly.http.server.RequestExecutorProvider; import org.glassfish.grizzly.http.server.Response; import org.glassfish.grizzly.http.util.ContentType; -import org.glassfish.grizzly.http.util.FastHttpDateFormat; -import org.glassfish.grizzly.http.util.Header; + +import com.fasterxml.jackson.databind.ObjectMapper; /** * JSON test @@ -27,9 +25,6 @@ public static class HelloMessage { @Override public void service(final Request request, final Response response) throws Exception { response.setContentType(CONTENT_TYPE); - response.setHeader(Header.Server, Server.SERVER_VERSION); - response.setHeader(Header.Date, FastHttpDateFormat.getCurrentDate()); - // Write JSON encoded message to the response. MAPPER.writeValue(response.getOutputStream(), new HelloMessage()); } diff --git a/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/PlainText2HttpHandler.java b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/PlainText2HttpHandler.java index 28c1b8ff96e..416025cbe22 100644 --- a/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/PlainText2HttpHandler.java +++ b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/PlainText2HttpHandler.java @@ -5,8 +5,6 @@ import org.glassfish.grizzly.http.server.RequestExecutorProvider; import org.glassfish.grizzly.http.server.Response; import org.glassfish.grizzly.http.util.ContentType; -import org.glassfish.grizzly.http.util.FastHttpDateFormat; -import org.glassfish.grizzly.http.util.Header; import org.glassfish.grizzly.utils.Charsets; /** @@ -22,9 +20,6 @@ public class PlainText2HttpHandler extends HttpHandler { @Override public void service(final Request request, final Response response) throws Exception { response.setContentType(CONTENT_TYPE); - response.setHeader(Header.Server, Server.SERVER_VERSION); - response.setHeader(Header.Date, FastHttpDateFormat.getCurrentDate()); - response.getOutputStream().write(HELLO_WORLD_BYTES); } diff --git a/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/PlainTextHttpHandler.java b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/PlainTextHttpHandler.java index 1093d6720d1..1fab9853e9a 100644 --- a/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/PlainTextHttpHandler.java +++ b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/PlainTextHttpHandler.java @@ -5,8 +5,6 @@ import org.glassfish.grizzly.http.server.RequestExecutorProvider; import org.glassfish.grizzly.http.server.Response; import org.glassfish.grizzly.http.util.ContentType; -import org.glassfish.grizzly.http.util.FastHttpDateFormat; -import org.glassfish.grizzly.http.util.Header; /** * Plaintext test case @@ -18,8 +16,6 @@ public class PlainTextHttpHandler extends HttpHandler { @Override public void service(final Request request, final Response response) throws Exception { response.setContentType(CONTENT_TYPE); - response.setHeader(Header.Server, Server.SERVER_VERSION); - response.setHeader(Header.Date, FastHttpDateFormat.getCurrentDate()); response.getWriter().write("Hello, World!"); } diff --git a/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/Server.java b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/Server.java index 7d736aec3f1..378c8719cfe 100644 --- a/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/Server.java +++ b/frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/Server.java @@ -59,6 +59,9 @@ public static void main(String[] args) throws Exception { try { httpServer.start(); + // This can't be done before the call to start(). Also note the + // positions + httpServer.getListener("http-listener").getFilterChain().add(3, new HeadersFilter()); synchronized (Server.class) { Server.class.wait(); }