Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Inconsistent state in AttachmentManager after saveAttachment

Christian Koehle December 26, 2018

 

 

Hi,

I create attachments using a groovy script, executed by Adaptavist ScriptRunner. It is registered as event script and listening to LabelAddEvent events.

Below is a highly simplified version of the script used to create the attachment, omitting a lot of boilerplate code, the event filter code, but it contains the API calls used to create the attachment:

Attachment createAttachment(ContentEntityObject attachToPage, String filename) {
    AttachmentManager attachmentManager = ...
    LabelManager labelManager = ...
    // Dummy content
    InputStream stream = new ByteArrayInputStream('dummy content'.getBytes(StandardCharsets.UTF_8))
def data = stream.bytes
def dataLength = data.length
// Check if an attachment exists
Attachment att = attachmentManager.getAttachment(attachToPage, filename)
if (att) return null // do not overwrite existing attachment
att = new Attachment(filename,'text/plain',dataLength,'comment',true)
att.setContainer(attachToPage)

// Save the attachment, this sets user, data and ensures proper association with given container
ByteArrayInputStream attContent = new ByteArrayInputStream(data)
attachmentManager.saveAttachment(att, null, attContent) // no previous attachment -> null
return att
}

// Code checking for correct event, label, page has been omitted here
AbstractPage page = ...
Attachment att = createAttachment(page, "snapshot-v${page.version}.txt")

When the script is executed, an attachment is created: It is visible in the UI, it is accessible, everything looks fine at a first glance. But when I access the attachments using the AttachmentManager, I get an inconsistent state:


attachmentManager.getLatestVersionsOfAttachments(testPage)

--> Contains the new attachment


attachmentManager.getAllVersionsOfAttachments(testPage)

--> Does *not* contain the new attachment

This may be the root cause for some unexpected behavior in other addons.

After a Confluence restart (not sure, may be also just after a certain amount of time, but we frequently restart the test instance), getAllVersionsOfAttachments also returns the new attachment, which may be an indication for some caching/indexing issue.

Question:

  • Is there some obvious mistake in the way I create the attachments, e.g. something which causes problems in the backend (index, cache...)?
  • Is there a way to enforce an update of the attachments returned in getAllVersionsOfAttachments?

I appreciate any little hint that helps me solving this issue/identifying the root cause.

Christian

1 answer

1 accepted

0 votes
Answer accepted
Christian Koehle December 29, 2018

Hi,

found a solution by myself. For anyone with the same issue: After adding 

attachToPage.addAttachment(att)

the inconsistencies are gone.

Looks like that 

att.setContainer(attachToPage)

is not sufficient and leads to unexpected side-effects.

Christian

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events