Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] embedded welcomefiles+http2-push+gzip-static-content+rest-api configuration

There's a few shortcuts you are taking that are not helping you.
  • HttpConfiguration isn't 100% common between connectors, use the hierarchy.
  • ServerRequestCustomizer only belongs on secure connector
  • SecureScheme / SecurePort configuration belongs on non-secure connector
  • Constraints / ConstraintMappings are not needed if you use the SecureRedirectHandler
  • You shouldn't need to wrap handlers as much as you are doing.
  • ResourceHandler is a poor choice for within a ServletContextHandler (it's designed to be used with DefaultServlet, not a ResourceHandler)
  • ServletContextHandler should have a BaseResource setup, many things use it. (various Servlet APIs and also the DefaultServlet)
  • A BaseResource can be directly created from a java Path, use a PathResource
etc ...

Here's a broken down version of what you are attempting to do, with proper initialization and configuration.

        Server server = new Server();

        // HTTP Port

        HttpConfiguration httpConfig = new HttpConfiguration();
        httpConfig.setSecureScheme("https"); // needed on non-ssl connector for secure redirect
        httpConfig.setSecurePort(SSL_PORT); // needed on non-ssl connector for secure redirect

        HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory();
        ServerConnector httpConnector = new ServerConnector(server, httpConnectionFactory);

        // TLS (SSL) Port

        HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig); // extends from non-ssl
        httpsConfig.addCustomizer(new SecureRequestCustomizer()); // only valid on SSL/TLS connector

        HTTP2ServerConnectionFactory http2ConnectionFactory = new HTTP2ServerConnectionFactory(httpsConfig);
        ALPNServerConnectionFactory alpnConnectionFactory = new ALPNServerConnectionFactory();
        SslContextFactory sslContextFactory = new SslContextFactory();
        SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory, alpnConnectionFactory.getProtocol());
        ServerConnector httpsConnector = new ServerConnector(server, sslConnectionFactory, http2ConnectionFactory,  httpConnectionFactory);


        // Non Handler Configuration
        server.setRequestLog(new AsyncNCSARequestLog());

        // Servlet ROOT Context
        ServletContextHandler context = new ServletContextHandler();
        context.setWelcomeFiles(new String[]{"index.html"});
        context.setBaseResource(new PathResource(Paths.get("some","path", "somewhere")));

        // Push Cache Filter
        FilterHolder pushFilter = context.addFilter(PushCacheFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        pushFilter.setInitParameter("maxAssociations", "" + 32);
        pushFilter.setInitParameter("ports", "" + SSL_PORT);

        // Your servlet
        context.addServlet(AdapterServlet.class, "/*"); // won't serve static files if you use this url-pattern!

        // what serves static files from a ServletContext, always added last
        ServletHolder defHolder = new ServletHolder("default", DefaultServlet.class); // always named "default"
        defHolder.setInitParameter("dirAllowed", "false");
        context.addServlet(defHolder, "/"); // always at url-pattern "/"

        // Handler Tree
        HandlerList handlers = new HandlerList();
        handlers.addHandler(new SecuredRedirectHandler()); // no need for Constraints / Constraint Mappings
        handlers.addHandler(new GzipHandler()); // wrapping / nesting is optional
        handlers.addHandler(new DefaultHandler()); // always last in handler tree, to help with errors and configuration mistakes


Joakim Erdfelt / joakim@xxxxxxxxxxx

On Mon, Feb 18, 2019 at 9:38 AM James Northrup <jim@xxxxxxxxxxx> wrote:
hi Simone, 

just checking in.

i just wanted to pose a question:
the java code at the bottom of this email is somewhat hard to follow.  is this the most succinct programatic way to create the desired h2 server, static, api, and gzip ?

this still fails with pushcachefilter, as well highlighted in the previous gist..  

import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import org.apache.commons.lang3.ArrayUtils;
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
import org.eclipse.jetty.http2.HTTP2Cipher;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlets.PushCacheFilter;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import javax.servlet.FilterRegistration;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.TreeMap;
import static;
import static org.apache.commons.lang3.ArrayUtils.toArray;
public class JettyJavaStarter implements AutoCloseable {
private final Server server;
public JettyJavaStarter(String... args) {
server = new Server() {
HttpConnectionFactory httpFactory = new HttpConnectionFactory(new HttpConfiguration() {{
addCustomizer(new SecureRequestCustomizer());
HTTP2ServerConnectionFactory http2factory = new HTTP2ServerConnectionFactory(new HttpConfiguration() {{
addCustomizer(new SecureRequestCustomizer());
ALPNServerConnectionFactory alpn1 = new ALPNServerConnectionFactory() {{
setRequestLog(new AsyncNCSARequestLog());
new Connector[]{
new ServerConnector(this, httpFactory) {{
new ServerConnector(this, new SslConnectionFactory(new SslContextFactory() {{
}}, alpn1.getProtocol()), http2factory, httpFactory) {{
setHandler(new ConstraintSecurityHandler() {{
addConstraintMapping(new ConstraintMapping() {{
setConstraint(new Constraint() {{
setHandler(new GzipHandler() {{
setHandler(new ContextHandlerCollection(new ServletContextHandler() {{
setHandler(new ResourceHandler() {{
setBaseResource(Resource.newResource(Paths.get(args.length > 0 ? args[0] : AdapterServlet.Companion.getResourceBase().toString())));
//the main feature: pass or fail?
FilterRegistration.Dynamic push = getServletContext().addFilter("push", PushCacheFilter.class);
push.setInitParameters(new HashMap<String, String>() {{
put("maxAssociations", "" + 32);
put("ports", "" + SSL_PORT);
new ServletContextHandler() {{
addServlet(AdapterServlet.class, "/*");
public void start() throws Exception {
public void close() throws Exception {
public static void main(String... args) throws Exception {
AppKt.hz = Hazelcast.getOrCreateHazelcastInstance(new Config(args.length > 1 ? args[1] : "default"));
JettyJavaStarter it = new JettyJavaStarter(args);
synchronized (it) {

On Sun, Feb 17, 2019 at 1:28 PM James Northrup <jim@xxxxxxxxxxx> wrote:

Suppressed: java.lang.NullPointerException
at org.eclipse.jetty.servlets.PushCacheFilter.init(
at org.eclipse.jetty.servlet.FilterHolder.initialize(

again, whatever I'm doing is just uninformed guesswork from incomplete knowledge.  the reason for various features is to support 100/100 lighthouse score on hnpwa or thereabouts.

roughly speaking, jetty should be able to host e.g.  with http2-push as designed.  adding an additional rest-api is the tweak you need to be able to drive the static content with java backend frameworks .

On Sun, Feb 17, 2019 at 11:54 AM James Northrup <jim@xxxxxxxxxxx> wrote:

On Fri, Feb 15, 2019 at 2:59 PM Simone Bordet <sbordet@xxxxxxxxxxx> wrote:

On Fri, Feb 15, 2019 at 6:29 AM James Northrup <jim@xxxxxxxxxxx> wrote:
> revising the servletcontextholder changes the result:

It NPEs at, which is:

config.getServletContext().setAttribute(config.getFilterName(), this);

A NPE there is not possible, unless "config" is null, or the
ServletContext is null.
In both cases it is a major break of the implementation of the Servlet
specification, which I doubt it's the case (we would have a million
reports by now).

Have you tried to _not_ use coroutines, at least to initialize Jetty?

I would recommend that you start with the simplest possible Jetty
server configuration: a server, one connector, one
Try that and see if it works. Then start adding things until it breaks.

Since it's Kotlin, try also to write it in pure Java first and then
convert to Kotlin.

Simone Bordet
Developer advice, training, services and support
from the Jetty & CometD experts.
jetty-users mailing list
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
jetty-users mailing list
To change your delivery options, retrieve your password, or unsubscribe from this list, visit

Back to the top