How to modify response header of a binary file?

Hi!

Currently I’m looking into a way to modify the response headers of a binary file such as a PDF file or image file stored in Hippo. My goal is to change the response header ‘filename’ to the binary node ‘displayName’.

I know it’s possible to set headers for all binaries on the hst:default sitemap using the property ‘hst:responseheader’. The problem is that my header value is file specific, so this wont work, unless it’s possible to use parameters of course. But that’s not possible as far as I know (might be a nice feature though :wink:)

Or Is the only solution to create my own BinariesServlet? I’ve tried to extend the already exiting ‘org.hippoecm.hst.servlet.BinariesServlet::doGet()’ but this didn’t really work out. If this is the only way to achieve my goal, how could I do this?

Looking forward to your suggestions!
Thanks in advance :slight_smile:

Cheers,
Jesper

Hi Jesper,

I think you can extend org.hippoecm.hst.servlet.BinariesServlet to override #createBinaryPageFactory() like the following:

    @Override
    protected BinaryPageFactory createBinaryPageFactory() {
        return new BinariesServlet.DefaultBinaryPageFactory() {
            @Override
            public BinaryPage createBinaryPage(final String resourcePath, final Session session) throws RepositoryException{
                final BinaryPage binaryPage = super.createBinaryPage(resourcePath, session);
                // find fileName somehow, and change the file name here...
                binaryPage.setFileName(fileName);
                return binaryPage;
            }
        };
    }

Regards,

Woonsan

There is a init property for binary servlet (org.hippoecm.hst.servlet.BinariesServlet) which also could be used:
contentDispositionFilenameProperty

Woonsan, thanks for your super fast reply!

I’ve been diving deeper into the BinariesServlet code and what you say is correct. Although you have to do a bit more, since there is this check in the BinaryServlet::writeResponse()

if (ContentDispositionUtils.isContentDispositionType(pageMimeType, contentDispositionContentTypes) ||
            (request.getParameter(forceContentDispositionRequestParamName) != null &&
                    Boolean.parseBoolean(request.getParameter(forceContentDispositionRequestParamName)))) {
        setExpiresNeeded = false;
        ContentDispositionUtils.addContentDispositionHeader(request, response, page.getFileName(),
                contentDispositionFileNameEncoding);
    }

So for our project we actually want to force download, so this is perfect for us. Although we didn’t know of this feature, it’s really useful, might be good to put this somewhere on the documentation website.

Cheers,
Jesper