Skip to content
Permalink
Browse files
Add filter adding HTTP headers (#4824)
  • Loading branch information
zloster authored and nbrady-techempower committed May 31, 2019
1 parent d65ec6e commit 12b68023e5d406680af745d34b2984741bc7c198
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
@@ -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);
}

}
@@ -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());
}
@@ -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);
}

@@ -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!");
}

@@ -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();
}

0 comments on commit 12b6802

Please sign in to comment.