Download Strategy module

Available:

Confluence 9.3 and later

Purpose of this module

Confluence supports predefined URL paths for downloading attachments, thumbnails, exported pages and files inside plugin JARs.
For example:
<confluenceBaseURL>/download/attachments/<filename> to get attachments,
<confluenceBaseURL>/download/export/<pdf-filename> to get the PDF of an exported page, etc.

The Download Strategy module helps to extend this functionality to downloading other files. For example, app developers can define a custom strategy for accessing files that were generated and used by their app.

Download Strategy declaration

Here's an example of defining a new download strategy in the atlassian-plugin.xml file:

1
2
<atlassian-plugin name="Sample" key="com.atlassian.confluence.extra.sample" plugins-version="2">
 ...
    <download-strategy key="custom-downloader" class="com.atlassian.confluence.plugins.download.CustomDownloadStrategy" />
 ...
</atlassian-plugin>

Download Strategy Implementation

Create the download strategy class that was defined in the atlassian-plugin.xml file above.

1
2
package com.atlassian.confluence.plugins.download;

import com.atlassian.plugin.servlet.DownloadStrategy;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.atlassian.plugin.servlet.AbstractFileServerServlet.PATH_SEPARATOR;
import static com.atlassian.plugin.servlet.AbstractFileServerServlet.SERVLET_PATH;

public class CustomDownloadStrategy implements DownloadStrategy {

    public final static String CUSTOM_DOWNLOAD_URL_PATH = PATH_SEPARATOR + SERVLET_PATH + "/custom";

    @Override
    public boolean matches(String urlPath) {
        return urlPath.contains(CUSTOM_DOWNLOAD_URL_PATH);
    }

    @Override
    public void serveFile(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        // write the file data to the output stream of httpServletResponse
    }
}

Invoking the download strategy

  1. Upload the app to Confluence.
  2. Access your files using the URL GET <confluenceBaseURL>/download/custom/<your file path>

The matches() method in CustomDownloadStrategy returns true for URL containing "/download/custom". Hence the CustomDownloadStrategy.serveFile() method will be invoked to return the requested file.

Rate this page: