Custom HTTP Headers - Resource Seeking

I maintain an addon (
http://vaadin.com/addon/mediaelementjs-player
) and have been struggling with a bug for a while. Seeking in the media player does not function correctly in Chrome and Firefox, but works in IE 11. You can see what I’m talking about in the online demo (
http://bdunn.jelastic.servint.net/Vaadin-Addon-Demo/?a=MejsPlayer
).

First, a little about how the addon works. It simply registers resources using:

AbstractClientConnector.setResource(key, resource)

It then sets the resource path and MIME type in the shared state for the JS connector to read, which sets the source of the MEJS player. Simple enough.

According to a StackOverflow question (
http://stackoverflow.com/questions/18369735/enable-audio-seeking-html5-audio-and-mediaelement-js-players-with-php-served-sou
) I need to set an HTTP header to enable seeking:

Accept-Ranges: bytes

I’d like to try this fix but can’t figure out how to set HTTP headers for the VaadinResponse, specifically targeting responses to requests for resources my connector registers. Is this possible? Any other ideas on what’s going on here?

Very much appreciate the collaboration. You can check out the source code of the addon at
https://github.com/bdunn44/MEJS-Vaadin
.

-Bryson

For those interested I did this by implementing a simple Filter. It isn’t very targeted and adds the header to all requests for any connector resource, but it does fix the problem.

@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        final HttpServletRequest httpRequest = (HttpServletRequest) request;
        final HttpServletResponse httpResponse = (HttpServletResponse) response;
        
        final String requestUri = httpRequest.getRequestURI();
        if (requestUri.matches("/.*APP/connector/[0-9]
+/[0-9]
+/0/.*")) {
            httpResponse.addHeader("Accept-Ranges", "bytes");
        }
        
        chain.doFilter(request, response);
    }

Could there be any harmful side effects? I’m also not sure if it’s a good idea to package this in with the addon or provide sample code to users needing this functionality.

After a closer look at the methods exposed by AbstractJavaScriptComponent I found the best solution is to override handleConnectorRequest. Wish I would have found this sonner!

@Override
public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path) {
    try {
        if (path.startsWith("0")) {
            response.setHeader("Accept-Ranges", "bytes");
        }
        return super.handleConnectorRequest(request, response, path);
    } catch (IOException e) {
        return true;
    }
}

You can obviously target specific connector resources with this. In my implementation I also catch IOExceptions which occur when the client aborts connections to a resource - these were thrown each time the source of the player changed. Hope this helps someone.

Bryson

Hi Bryson

I which class did you override doFIlter() and handleConnectorRequest()? Did you have to configure any web.xml for this purpose?

Thanks.