Skip to content

Instantly share code, notes, and snippets.

@bbrowning
Last active May 12, 2017
Embed
What would you like to do?
An example of using Undertow ResponseHeader filters with WildFly Swarm
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.wildfly.swarm.config.undertow.FilterConfiguration;
import org.wildfly.swarm.config.undertow.configuration.ResponseHeader;
import org.wildfly.swarm.config.undertow.server.host.FilterRef;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.undertow.UndertowFraction;
import org.wildfly.swarm.undertow.WARArchive;
public class Main {
public static void main(String... args) throws Exception {
Container container = new Container();
// Create a ResponseHeader identified by a key with the desired HTTP
// header name and value
ResponseHeader responseHeader = new ResponseHeader("foo-header")
.headerName("foo")
.headerValue("bar");
// Create a FilterRef referencing the response header's key. The
// predicate is optional to restrict the application of this filter
// to specific requests.
// http://undertow.io/undertow-docs/undertow-docs-1.2.0/predicates-attributes-handlers.html
FilterRef filterRef = new FilterRef("foo-header")
.predicate("path-suffix['.html'] or path-prefix['/js']");
// Modify the default UndertowFraction to include our ResponseHeader
UndertowFraction fraction = UndertowFraction.createDefaultFraction()
.filterConfiguration(new FilterConfiguration()
.responseHeader(responseHeader));
// Modify the default host of the default server to use our FilterRef
fraction.subresources().server("default-server").subresources()
.host("default-host").filterRef(filterRef);
// Tell the Container to use our customized UndertowFraction
container.fraction(fraction);
// Your application may have different code below to finish
// configuring and booting WildFly Swarm
container.start();
WARArchive war = ShrinkWrap.create(WARArchive.class);
war.staticContent();
war.addAllDependencies();
container.deploy(war);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment