Skip to content

Commit

Permalink
Add filter adding HTTP headers (#4824)
Browse files Browse the repository at this point in the history
  • Loading branch information
Radoslav Petrov authored and NateBrady23 committed May 31, 2019
1 parent d65ec6e commit 12b6802
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 12b6802

Please sign in to comment.