Sometimes binary downloads (e.g. PDF files) open in the browser instead of directly downloaded to the hard disk. If the behaviour of opening in the browser is not wished, it is possible to force the download with a small filter.

This solutions achieve this with a ontent-disposition set in the response header and and a parameter in the URL. Following a URL to a PDF file in the website workspace:

With the parameter download the filter is triggered.

package ch.esense.example.filter;

import info.magnolia.cms.filters.AbstractMgnlFilter;
import info.magnolia.context.MgnlContext;


import javax.inject.Inject;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

public class DownloadFilter extends AbstractMgnlFilter {

    public DownloadFilter() {

    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {

        final String downloadParameter = MgnlContext.getParameter("download");

        if (downloadParameter != null) {
            final String requestUrl = request.getRequestURL().toString();
            final String filename = StringUtils.substringAfterLast(requestUrl, "/");
            response.addHeader("content-disposition", "attachment; filename=" + filename);

        chain.doFilter(request, response);


Place this filter after the uriSecurity filter.


  • No labels