1
+ package org .glassfish .grizzly .bm ;
2
+
3
+ import java .io .IOException ;
4
+
5
+ import org .glassfish .grizzly .filterchain .FilterChainContext ;
6
+ import org .glassfish .grizzly .filterchain .NextAction ;
7
+ import org .glassfish .grizzly .http .HttpBaseFilter ;
8
+ import org .glassfish .grizzly .http .HttpContent ;
9
+ import org .glassfish .grizzly .http .HttpPacket ;
10
+ import org .glassfish .grizzly .http .HttpRequestPacket ;
11
+ import org .glassfish .grizzly .http .HttpResponsePacket ;
12
+ import org .glassfish .grizzly .http .util .FastHttpDateFormat ;
13
+ import org .glassfish .grizzly .http .util .Header ;
14
+
15
+ /**
16
+ * Must be added just before the HttpServerFilter i.e. second to last in the filter chain.
17
+ *
18
+ * @author zloster
19
+ *
20
+ */
21
+ public class HeadersFilter extends HttpBaseFilter {
22
+
23
+ @ Override
24
+ protected void bind (HttpRequestPacket request , HttpResponsePacket response ) {
25
+ // This is never called. I don't know why.
26
+ super .bind (request , response );
27
+ }
28
+
29
+ @ Override
30
+ public NextAction handleRead (FilterChainContext ctx ) throws IOException {
31
+ // Taken from HttpServerFilter
32
+ final Object message = ctx .getMessage ();
33
+ if (HttpPacket .isHttp (message )) {
34
+ // Otherwise cast message to a HttpContent
35
+ final HttpContent httpContent = (HttpContent ) message ;
36
+ final HttpRequestPacket request = (HttpRequestPacket ) httpContent .getHttpHeader ();
37
+ final HttpResponsePacket response = request .getResponse ();
38
+ response .setHeader (Header .Server , Server .SERVER_VERSION );
39
+ response .setHeader (Header .Date , FastHttpDateFormat .getCurrentDate ());
40
+ }
41
+ return super .handleRead (ctx );
42
+ }
43
+
44
+ }
0 commit comments