Installing the Java Module as a Jetty Handler

Requirements

Jetty 9.2 or higher

Supported Application Types

For Jetty specific implementations, we support a HandlerWrapper-based install on Jetty 9.2 or higher.

We also provide a lower-level agent RPC communication API if you are interested in writing an implementation for another Java platform. If you are interested in writing an implementation for another Java platform, please reach out to our support team.

Agent Configuration

Like other Next-Gen WAF modules, the Jetty Handler supports both Unix domain sockets and TCP sockets for communication with the Next-Gen WAF agent. By default, the agent uses Unix domain sockets with the address set to unix:/var/run/sigsci.sock. It is possible to override this or specify a TCP socket instead by configuring the rpc-address parameter in the Agent.

Additionally, ensure the agent is configured to use the default RPC version: rpc-version=0. This can be done by verifying the parameter rpc-version is not specified in the agent configuration or if it is specified, ensure that is specified with a value of 0. Below is an example Agent configuration that overrides the default Unix domain socket value:

1accesskeyid = "YOUR AGENT ACCESSKEYID"
2secretaccesskey = "YOUR AGENT SECRETACCESSKEY"
3rpc-address = "127.0.0.1:9999"

Download

Download the Next-Gen WAF Java module manually or access it with Maven.

Download manually

  1. Download the Java module archive from https://dl.signalsciences.net/sigsci-module-java/sigsci-module-java_latest.tar.gz.
  2. Extract sigsci-module-java_latest.tar.gz.
  3. Deploy the jars using one of the following options:
    • Copy sigsci-module-java-{version}-shaded.jar (an uber jar with all the dependencies bundled) to your application’s classpath (e.g., %CATALINA_HOME%\webbapps\<APP_FOLDER>\WEB-INF\lib).
    • Copy sigsci-module-java-{version}.jar and its dependencies in the lib folder to your application’s classpath (e.g., %CATALINA_HOME%\webbapps\<APP_FOLDER>\WEB-INF\lib). If you already have any of the dependency jar files in your application classpath folder (i.e., for Tomcat in the WEB-INF\lib) then it is not necessary to copy them, even if the version numbers are different. The logging jars are optional based on how slf4j is configured.

Access with Maven

For projects using Maven for build or deployment, the latest version of Next-Gen WAF Java modules can be installed by adding XML to the project pom.xml file. For example:

1<repositories>
2 <repository>
3 <id>sigsci-stable</id>
4 <url>https://packages.signalsciences.net/release/maven2</url>
5 </repository>
6</repositories>
7
8<dependency>
9 <groupId>com.signalsciences</groupId>
10 <artifactId>sigsci-module-java</artifactId>
11 <version>LATEST_MODULE_VERSION</version>
12</dependency>

Be sure to replace LATEST_MODULE_VERSION with the latest release of the Java module. You can find the latest version in our version file at https://dl.signalsciences.net/sigsci-module-java/VERSION.

Install

The installation of the Jetty module varies slightly depending upon whether you deployed Jetty as an embedded or stand alone application.

If you are embedding Jetty within your web application, follow the instructions for Embedded Jetty.

Alternatively, if you are deploying your web application to a Jetty instance, follow the instructions for Standalone Jetty.

Embedded Jetty

The Next-Gen WAF Jetty module is currently implemented as a Handler. Edit your application to wrap your existing Handlers with the Next-Gen WAF Handler.

A typical Jetty based application will add all of the Handlers to a HandlerList, similar to this:

1Server server = new Server(InetSocketAddress.createUnresolved("0.0.0.0", 8800));
2ServletContextHandler context = new ServletContextHandler();
3ServletHolder defHolder = new ServletHolder("default", DefaultServlet.class);
4HandlerList handlers = new HandlerList();
5
6// Servlet: /
7defHolder.setInitParameter("pathInfoOnly", "true");
8defHolder.setInitParameter("dirAllowed", "true");
9defHolder.setInitParameter("acceptRanges", "true");
10context.addServlet(defHolder, "/*");
11context.addServlet(defHolder, "/");
12
13// Existing App Handlers
14handlers.addHandler(context);
15handlers.addHandler(new DefaultHandler());
16
17// Add the existing handlers as the server handler
18server.setHandler(handlers);
19
20try {
21server.start();
22server.join();
23} catch (Exception e) {
24e.printStackTrace();
25} finally {
26server.stop();
27}

Add the Next-Gen WAF Handler around your primary handler. For example:

1Server server = new Server(InetSocketAddress.createUnresolved("0.0.0.0", 8800));
2ServletContextHandler context = new ServletContextHandler();
3ServletHolder defHolder = new ServletHolder("default", DefaultServlet.class);
4HandlerList handlers = new HandlerList();
5
6// Servlet: /
7defHolder.setInitParameter("pathInfoOnly", "true");
8defHolder.setInitParameter("dirAllowed", "true");
9defHolder.setInitParameter("acceptRanges", "true");
10context.addServlet(defHolder, "/*");
11context.addServlet(defHolder, "/");
12
13// Existing App Handlers
14handlers.addHandler(context);
15handlers.addHandler(new DefaultHandler());
16
17// REMOVED: This is replaced by wrapping with the sigsci handler below
18//server.setHandler(handlers);
19
20//////////////////////////////////////////////////////////////////////////
21// BEGIN ADDITION: Next-Gen WAF Handler
22// Need to also add these imports for SignalSciencesHandler and Timeout:
23// import com.signalsciences.jetty.SignalSciencesHandler;
24// import com.signalsciences.rpc.util.Timeout;
25//////////////////////////////////////////////////////////////////////////
26// 1. Create a new SignalSciencesHandler
27SignalSciencesHandler sigsciHandler = new SignalSciencesHandler();
28// 2. Specify the URI of the sigsci-agent rpc-address (Unix)
29sigsciHandler.getSigSciConfig().setRpcServerURI(URI.create("unix:/var/run/sigsci.sock"));
30// 3. Specify a timeout
31sigsciHandler.getSigSciConfig().setRpcTimeout(new Timeout(300, TimeUnit.MILLISECONDS));
32// 4. Set rpcVersion to 0
33sigsciHandler.getSigSciConfig().setRpcVersion(0);
34// 5. Wrap the other handlers
35sigsciHandler.setHandler(handlers);
36// 6. Set the SignalSciencesHandler (wrapper) as the server handler
37server.setHandler(sigsciHandler);
38//////////////////////////////////////////////////////////////////////////
39// END ADDITION
40//////////////////////////////////////////////////////////////////////////
41
42try {
43server.start();
44server.join();
45} catch (Exception e) {
46e.printStackTrace();
47} finally {
48server.stop();
49}

Standalone Jetty

The Next-Gen WAF Jetty module is currently implemented as a handler. To use this, you will need to follow the steps below to update your server configuration.

Update Jetty Server Configuration File

In a default Jetty installation, the server configuration file can be found under {jetty.base}/etc/jetty.xml. You will need to update the configuration file to wrap the existing Handlers with the Next-Gen WAF Handler. Modify the stanza in the file that specifies the handler collection to include the Next-Gen WAF Handler. Below is an example using the out of the box jetty.xml file:

1<Set name="handler">
2 <New id="Wrapper" class="com.signalsciences.jetty.SignalSciencesHandler">
3 <Call name="setRpcServerURI">
4 <Arg>
5 <New class="java.net.URI">
6 <Arg>unix:/var/run/sigsci.sock</Arg>
7 </New>
8 </Arg>
9 </Call>
10 <Call name="setRpcTimeout">
11 <Arg>
12 <New class="com.signalsciences.rpc.util.Timeout">
13 <Arg type="long">300</Arg>
14 <Arg>
15 <Get class="java.util.concurrent.TimeUnit" name="MILLISECONDS"/>
16 </Arg>
17 </New>
18 </Arg>
19 </Call>
20 <Set name="handler">
21 <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
22 <Set name="handlers">
23 <Array type="org.eclipse.jetty.server.Handler">
24 <Item>
25 <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
26 </Item>
27 <Item>
28 <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
29 </Item>
30 </Array>
31 </Set>
32 </New>
33 </Set>
34 </New>
35</Set>

Deploy Signal Sciences Library to the Server ClassPath

There are two options for deploying the jars:

  • Copy sigsci-module-java-{version}-shaded.jar (an uber jar with all the dependencies bundled) to your server classpath.
  • Copy sigsci-module-java-{version}.jar and its dependencies in the lib folder to your server classpath.

Although optional, we recommended adding this library to {jetty.base}/lib/ext, as Jetty automatically loads libraries in this path to the server classpath.

Simple Example Server

For a more complete example, see the sigsci-jetty-simple-example JAR files included in the distribution. This consists of the binaries, source, and javadoc for a simple working example. The binary JAR is executable and can be run with commands similar to the following. These commands will start the simple server and point it at an agent running on TCP port 5000 on the local host, which require an agent started with rpc-address = "127.0.0.1:5000":

$ java -jar examples/sigsci-jetty-simple-example-{version}.jar

That command will produce the following output:

tcp://127.0.0.1:5000
00:00:00.384 [main] INFO c.s.example.SimpleExampleServer - WebRoot is jar:file:/x/sigsci-jetty-simple-example-0.1.3.jar!/webroot/
00:00:00.403 [main] INFO c.s.example.SimpleExampleServer - Signal Sciences WAF: enabled
00:00:00.501 [main] INFO c.s.example.SimpleExampleServer - Signal Sciences Simple Example Server started (http://0.0.0.0:8800/)
00:00:00.986 [qtp123456789-12] INFO c.s.example.RequestLogger - "GET /test/ HTTP/1.1" 302

This example test server will respond with a simple HTML page on the root directory. It can also be used to do basic tests using the /test/ context. In this test context the following parameters are interpreted:

  • response_time: Time in milliseconds to delay the response - to test timeouts.
  • response_code: The HTTP response code to return in the response.
  • size: The size of the response body in bytes.

For example:

$ curl -D- "http://127.0.0.1:8800/test/?response_code=302&response_time=10&size=86"

That command will produce the following output:

HTTP/1.1 302 Found
Date: Sat, 01 Sep 2016 00:00:00 GMT
Location: /
Content-Length: 86
Server: Jetty(9.2.z-SNAPSHOT)
Was this guide helpful?

Do not use this form to send sensitive information. If you need assistance, contact support. This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.