How to do server push with standalone Jetty

I am trying to test server push function on a static website with standalone Jetty.

My site consists of index.html  + 1 CSS + bunch of images. Directory structure:

/Album
   Â|
   Â|-----index.html
   Â|-----style.css
   Â|------/images
        Â|
        Â|-----image 1.png
        Â|---a set of 100 images
        Â|---image100.png

      

Since I just wanted to quickly test the server push function, I did not code this site as a Java web project and therefore did not have any web.xml file.

However, the Jetty documentation suggests adding PushCacheFilter to web.xml. So I created a file / Album / WEB -INF / web.xml in my project and added PushCacheFilter as the documentation mentioned.

First, I can't figure out from the documentation how PushCacheFilter works. Second, I want to control which files to push and which not. From the documentation it seems that PushCacheFilter doesn't give me that kind of control.

I've checked several examples on the web, but most of them are with Jetty built in. Someone please help me figure out how to test the server push function on my static website with standalone Jetty?

Also, I wanted to ask if the HTTP / 2 client example in the Jetty GitHub repository as such can be used? Sorry, I haven't tested the client myself. I just saw this in the vault and I was curious. I would be very grateful if someone could point me in the direction of an example using casewith Jetty HTTP and HTTP / 2 client.

0


source to share


1 answer


Too many questions and it is not entirely clear what you want to do :)

Jetty provides a Jetty-specific API on the server for pushing (these APIs will eventually be part of Servlet 4.0).

You can access this API with org.eclipse.jetty.server.Request.getPushBuilder()

, see http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/server/PushBuilder.html

The APIs PushBuilder

will then allow you to configure the resource to be clicked and clicked on.

PushCacheFilter

implements a cache of correlated resources. When a primary resource is requested that correlated secondary resources, PushCacheFilter

pushes those correlated resources using the API PushBuilder

.

If PushCacheFilter

doesn't suit your needs, you can write your own filter with your own logic and push using the API PushBuilder

.



On the client side, if you want to use Java API to make requests and receive clicks you should 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 make a request and receive clicks here .

If you need a complete example, similar to your (index.html + bunch of images), you can see the HTTP / 2 demo .

UPDATE: Simple example of use PushBuilder

.

public class MyPushFilter implements Filter {
@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        String uri = httpRequest.getRequestURI();
        switch (uri) {
            case "/index.html":
                // Jetty specific APIs for now.
                PushBuilder pushBuilder = Request.getBaseRequest(request).getPushBuilder();
                pushBuilder.path("/styles.css").push();
                pushBuilder.path("/background.png").push();
                break;
            default:
                break;
        }
        chain.doFilter(req, resp);
    }
}

      

The example above is very simple. It does not handle HTTP version, conditional headers, etc. Please see the implementation PushCacheFilter

here for a better implementation.

+2


source







All Articles