I am trying to test the server push feature on a static website with standalone Jetty.
My website consists of an index.html Â+ 1 CSS + a bunch of images. The directory structure is:
/Album
   Â|
   Â|-----index.html
   Â|-----style.css
   Â|------/images
        Â|
        Â|-----image 1.png
        Â|---a set of 100 images
        Â|---image100.png
As I just wanted to quickly test out the server push feature I did not code this website as a Java Web project and, therefore, didn't have any web.xml file.
However, the Jetty documentation asks to add PushCacheFilter to web.xml. Therefore, I created a /Album/WEB-INF/web.xml in my project and added the PushCacheFilter as the documentation specified.Â
First, I am unable to understand from the documentation how exactly PushCacheFilter works. Secondly, I want to control which files get pushed and which do not. From the documentation, it seems PushCacheFilter doesn't give me that kind of control.
I have checked a few examples on Internet but most of them are with embedded Jetty. Will someone please help me to figure out how to test server push feature in my static website with standalone Jetty?
Also, I wanted to ask whether the HTTP/2 client example in Jetty's GitHub repository directly usable as such? Sorry, haven't tested the client myself. I just saw it in the repository and was curious. I'll be really thankful if someone can point me in the direction of example use casewith Jetty's HTTP and HTTP/2 client.
Too many questions and it's not really clear what you want to do :)
Jetty exposes a Jetty-specific API on the server to perform pushes (eventually, these APIs will be part of Servlet 4.0).
You can access this API using
org.eclipse.jetty.server.Request.getPushBuilder()
, see http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/server/PushBuilder.htmlThe
PushBuilder
APIs will then allow you to setup a resource to push, and to push it.PushCacheFilter
implements a cache of correlated resources. When a primary resource that has correlated secondary resources is being requested,PushCacheFilter
pushes those correlated resources using thePushBuilder
APIs.If
PushCacheFilter
does not fit your needs, you can write your own filter with your own logic and perform pushes using thePushBuilder
APIs.On the client side, if you want to use Java APIs to perform requests and receive pushes, you have to use
HTTP2Client
, see http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/http2/client/HTTP2Client.html.You can find examples of how to perform a request and receive pushes here.
If you want a full fledged example similar to yours (index.html + bunch of images), you can look at the HTTP/2 demo.
UPDATE: Simple example of how to use
PushBuilder
.The example above is very very simple. It does not handle HTTP version, conditional headers, etc. Please have a look at the implementation of
PushCacheFilter
here for a better implementation.