HTML Export Plugin Edited

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? 

1 answer

2 votes

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();
}

}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 11, 2018 in Confluence

What are your project planning tips?

Hello Community,  Jessica here from the Confluence product marketing team! Today I wanted to get your takes on project planning –– what works, what doesn’t, how do you know if you’re doing it r...

376 views 2 4
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you