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:
I appreciate any little hint that helps me solving this issue/identifying the root cause.
Christian
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.