I’m trying to copy an attachment from one page to another with the onBlueprintCreateEvent however I keep seeing the error “Caused by: java.lang.NullPointerException” in the console.
I’m fairly certain it’s related to the PageManager but I’m not sure how to fix it. I’m setting the PageManager as described in Accessing Confluence Components.
Anyone have suggestions? Here’s my code:
import com.atlassian.confluence.plugins.createcontent.api.events.BlueprintPageCreateEvent;
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.confluence.pages.Page;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.pages.AbstractPage;
import com.atlassian.confluence.pages.Attachment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PiBlueprintListener {
private static final Logger log = LoggerFactory.getLogger(PiBlueprintListener.class);
public PiBlueprintListener(EventPublisher eventPublisher) {
eventPublisher.register(this); //demonstration only
}
public PageManager pageManager;
public void setPageManager(PageManager pageManager)
{
this.pageManager = pageManager;
}
@EventListener
public void onBlueprintCreateEvent(BlueprintPageCreateEvent event){
Page targetPage = event.getPage();
String tempPageId = (String) event.getContext().get("tempPageId");
System.out.println("tempPageId: " + tempPageId);
AbstractPage attachmentPage = this.pageManager.getAbstractPage(Long.parseLong(tempPageId));
Attachment attachment = attachmentPage.getAttachments().iterator().next();
targetPage.addAttachment(attachment);
}
}
FYI, the tempPageId is the id of the page where the attachment is stored and the targetPage is where I want the attachment to go.
you are trying to wire the pagemanager through setter, this is deprecated. Change the constructor to
public PiBlueprintListener(EventPublisher eventPublisher,PageManager pageManager) {
eventPublisher.register(this); //demonstration only
this.pageManager=pageManager;
}
Thanks Panos, this worked but it’s only copying the attachment name, not the actual data so it appears to copy but when I click on the attachment it says there is no file. Any suggestions?
Thanks, but honestly, I can’t figure out how to use it. I don’t know java so I’ve spent hours trying to figure it out but I have no idea what I need to do.
Caused by: com.atlassian.confluence.pages.attachments.AttachmentDataStreamSizeMismatchException: Attachment data stream contains a different number of bytes to the declared size of the attachment. Expected: 0, actual: 33
Hi @sameer.v
I haven’t seen that error before. Is the attachment correctly saved in confluence and fetched? Because it expects 0 (???) bytes.
Post a little more of your code around the copyAttachment, e.g. how the attachment is fetched, how the page is fetched etc.
Also do some trivial debugging to see if that attachment is valid:
Compare size using getFileSize(), check if exists and displayed with getDownloadPath(), same with page.
I have an event listener listening to “AttachmentCreateEvent”. Once that event is fired, I need to be able to move / “copy to new - delete from old” the attachment (based on some conditions in its contents) to another Space’s page. That’s the code in my listener and its failing.
I also tried
This does the copy. But the problem is copyLatestVersion does not make a copy of the attachment with all its contents and metadata. Seems like it does only so much:
> Attachment attachmentCopy = new Attachment();