HTML Export Plugin
Asked by Benny Joe Villiger:

I'm currently writing an HTML Export Plugin for Cofluence using the ImportExportManager Class.

```
 @Override
    public String execute() throws ImportExportException, IOException {
        DefaultExportContext defaultExportContext = new MyExportContext(space);

        TreeBuilder treeBuilder = new TreeBuilder(AuthenticatedUserThreadLocal.get(), contentPermissionManager, pageManager);
        ContentTree contentTree = treeBuilder.createPageTree(space);

        defaultExportContext.setContentTree(contentTree);
        ProgressMeter progress = new ProgressMeter();
        importExportManager.exportAs(defaultExportContext, progress);

        return SUCCESS;
    }
```

when running the program on my Confluence instance, I only get empty .html files. (Well at least something) But when I run the program in the debug mode, everything works fine. How can that be? And what can I do there?


Answers:
author: Hasnae
body: Not sure what went wrong in there, perhaps some data missing from the export context.

I have implemented the same as a rest endpoint, and made sure the export context has what it needs (granted it is not the most intuitive API ... in fact it's not entirely API as far as I am concerned)

Below is a very naive implementation for the happy path without any error handling .

Hope this helps

```
package com.atlassian.confluence.community.rest;

import com.atlassian.annotations.security.XsrfProtectionExcluded;
import com.atlassian.confluence.core.ContentPermissionManager;
import com.atlassian.confluence.importexport.DefaultExportContext;
import com.atlassian.confluence.importexport.ImportExportException;
import com.atlassian.confluence.importexport.ImportExportManager;
import com.atlassian.confluence.importexport.impl.ExportScope;
import com.atlassian.confluence.pages.ContentTree;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.pages.TreeBuilder;
import com.atlassian.confluence.spaces.Space;
import com.atlassian.confluence.spaces.SpaceManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.ConfluenceUser;
import com.atlassian.core.util.ProgressMeter;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import static java.util.Collections.singletonMap;

@Path("/export")
public class ExportResource {

    private final ImportExportManager importExportManager;
    private final ContentPermissionManager contentPermissionManager;
    private final PageManager pageManager;
    private final SpaceManager spaceManager;

    public ExportResource(
            final @ComponentImport ImportExportManager importExportManager,
            final @ComponentImport ContentPermissionManager contentPermissionManager,
            final @ComponentImport PageManager pageManager,
            final @ComponentImport SpaceManager spaceManager) {
        this.importExportManager = importExportManager;
        this.contentPermissionManager = contentPermissionManager;
        this.pageManager = pageManager;
        this.spaceManager = spaceManager;
    }

    @GET
    @Path("/{spaceKey}")
    @Produces("application/json")
    @XsrfProtectionExcluded
    public Response exportSpace(final @PathParam("spaceKey") String spaceKey) throws ImportExportException {
        final Space space = spaceManager.getSpace(spaceKey);
        final ConfluenceUser currentUser = AuthenticatedUserThreadLocal.get();

        final TreeBuilder treeBuilder = new TreeBuilder(currentUser, contentPermissionManager, pageManager);
        final ContentTree contentTree = treeBuilder.createPageTree(space);

        final DefaultExportContext defaultExportContext = new DefaultExportContext();
        defaultExportContext.setType(ImportExportManager.TYPE_HTML);
        defaultExportContext.setContentTree(contentTree);
        defaultExportContext.addWorkingEntity(space);
        defaultExportContext.setSpaceKey(spaceKey);
        defaultExportContext.setExportScope(ExportScope.SPACE);

        final ProgressMeter progress = new ProgressMeter();
        final String exportPath = importExportManager.exportAs(defaultExportContext, progress);

        return Response.ok(singletonMap("path", exportPath)).build();
    }

}
```


Tags: development