Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I update an existing page attachment in Confluence?

Brian Alston April 30, 2015

We have a custom plug-in that allows people to upload documents through the GUI, and add metadata about those documents within our plug-in. Also, another function of the plug-in, is we have an XML file being pushed to us via the Confluence SOAP service. This XML file is being attached to a specific file within the plug-in space, by the SOAP service, once a day.

 

We have now been charged with turning the process around. We will now make our own SOAP call to get the same XML file and attach it to the same page.

 

I can successfully attach a new file to the page, but when I try to update (replace) that XML file with a newer version, I either get an error, or I end up just uploading another file, so I am left with 2 files with the same name, which is not what we want. Here is what I have that gives me an error:

 

page = pageManager.getPage(spaceKey, pageTitle);

currentAttachment = attachmentManager.getAttachment(page, attachmentFileName);
 
newAttachment = new Attachment(attachmentFileName, newAttachmentContentType, newAttachmentFileSize, attachmentComment);
newAttachment.setContent(page);
 
try{
    attachmentManager.saveAttachment(newAttachment, currentAttachment, fileInputStream);
} catch(IOException ioe){
}

 

The error I get is:

 

Caused by: net.sf.hibernate.HibernateException: identifier of an instance of com.atlassian.confluence.pages.Attachment altered from 2752514 to 0

 

Here is what I have that adds a new attachment, but does not update (replace) the attachment:

 

page = pageManager.getPage(spaceKey, pageTitle);
 
currentAttachment = attachmentManager.getAttachment(page, attachmentFileName);
previousAttachment = (Attachment) currentAttachment.clone();
previousAttachment.convertToHistoricalVersion();
 
newAttachment = new Attachment(attachmentFileName, newAttachmentContentType, newAttachmentFileSize, attachmentComment);
newAttachment.setContent(page);
 
attachmentManager.saveAttachment(newAttachment, previousAttachment, fileInputStream);

 

Any help with this will be greatly appreciated. Thank you, and have a great day. smile

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Midori
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 4, 2015

You should look at the source code of the Confluence "upload attachment" action. If you have a commercial license, you have the full Confluence source.

Note: you cannot just clone a Hibernate-managed entity. That will not be part of the underlying Hibernate session and will lead to hard to debug problems even if it succeeds.

Check the Confluence code.

Brian Alston May 11, 2015

Can you suggest a good place to start looking within the Confluence source code for this?

0 votes
Luiz Maia
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 1, 2015

Hi Brian,

Let me see if I understood the action you're trying to perform here: you created a custom plugin to upload attachments and you would like that this plugin replaced old files when you upload a new one with the same name, is that correct?

Well, if that's the case, the best solution I see here is using the Confluence bundled Attachments macro.
https://confluence.atlassian.com/display/DOC/Attachments+Macro

This macro understands when you are uploading a new version of a file. It doesn't replace the old one, but makes the new one more visible.

Please let me know if this helps you or if you have any other question.

Thanks!

Kind regards,

Luiz Maia
Atlassian Support

Brian Alston May 1, 2015

Not exactly. Currently, the other entity contacts our Confluence SOAP service, and attaches an xml file to a specific page. The xml file is indexed, and the data is used throughout our plug-in. Every day, the xml file will be a little different, to reflect changes in the data, but it has to have the same name attached to the same page because we use Confluences XML extractors to grab the file and extract the data. Now, we are charged with getting the xml file from their SOAP service, because they no longer want to push it to us. I plan on using the AttachmentManager to do the attaching, but so far I can only either get an error, or add an additional file to the page ... neither of which I want. I think I may have found a way to do it, though. I remove the current attachment, then save the new attachment. Not what I would prefer, but it appears to work. The only issue I can see is that, somehow, the current process keeps all of the old versions of the file for reference. I can't figure it out. Confluence is a weird piece of software.

TAGS
AUG Leaders

Atlassian Community Events