-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add filter adding HTTP headers (#4824)
- Loading branch information
1 parent
d65ec6e
commit 12b6802
Showing
5 changed files
with
49 additions
and
16 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
frameworks/Java/grizzly/src/main/java/org/glassfish/grizzly/bm/HeadersFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters