Using the Java API in ScriptRunner/groovy I am trying to retrieve all attachments for a page and then create a new one if it currently does not exists.
Everything is working so far, except the creation of the new attachment. Clearly, I'm missing something and community insight would be super helpful! I'm fairly new to Java/ScriptRunner/Groovy programming.
Code:
Attachment newFile = new Attachment("filename.csv", "text/csv", 6014, "new file");
Per: https://docs.atlassian.com/atlassian-confluence/6.0.3/com/atlassian/confluence/pages/Attachment.html, that should be all that is needed to construct a new Attachment object.
Once the attachment is created I would use AttachmentManager to save the file to the page with content from InputStream/ByteArray.
Does the file/content actually need to exist on the server somewhere before I can create the attachment and save to the page?
However, I receive the following errors in the log. <snippet>
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:873) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:710) at jdk.internal.reflect.GeneratedMethodAccessor80.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205) at com.sun.proxy.$Proxy75.commit(Unknown Source)
Hi @Matt , if you've not already worked this out, the issue would be that the Attachment needs to have a container reference (that is, a reference to the page it's being saved on) set to the page that you're uploading it to - see the exception note on the api
IllegalArgumentException
- if attachment's content is not set
The code I've used that successfully uploads an attachment looks like
def final pageTitle = 'myPage';
def final spaceKey = 'KEY';
def destinationPage = pageManager.getPage spaceKey, pageTitle;
def newAttachment = new Attachment();
newAttachment.with {
fileName = 'my file name'
mediaType = 'mime type'
fileSize = content.length
versionComment = 'a comment'
container = destinationPage
};
attachmentManager.saveAttachment(newAttachment, null, new ByteArrayInputStream(content)
where content is a byte array.
Note this doesn't enable you to view the attachment on the page, but if you go to the page Attachments section, it'll be listed there. If you want to overwrite an existing attachment, you need to instead search for the existing attachment and pass it in as the previousVersionOfAttachment.
Hello @Matt
Not sure what is going on there, but I usually use `AttachmentService` from our java-api over the `AttachmentManager`
Some questions to help me understand what's going on with that specific API usage :
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Hasnae, thank you very much for you reply!
A bit of background. This is Confluence Server, 7.3.1 and this is being written through ScriptRunner so I am not sure what JDK but I'd assume it's in the 8.x family.
As stated in this article, the error thrown before the "org.springframework.transaction.UnexpectedRollbackException" exception may be the culprit.
Looking at the logs, I see this before the "UnexpectedRollbackException":
2020-05-19 21:09:53,309 INFO [http-nio-8090-exec-454 url:/rest/scriptrun...heduledJob/preview username:] [onresolve.scriptrunner.runner.ScriptBindingsManager] call Attachment: GroupAdmin_audit_2020.csv v.1 (0) null
2020-05-19 21:09:53,314 ERROR [http-nio-8090-exec-454 url:/rest/scriptrun...heduledJob/preview ] [onresolve.scriptrunner.runner.ScriptBindingsManager] call Attachment content
-- referer: /plugins/servlet/scriptrunner/admin/jobs/edit/07a91bc6-58be-4aea-b31a-0acc9f2e5c8d?REF=EDACA9A69B5BE5EA19B534CAA0CB76C490997B3BD7A0F23D3E9100000004 | url: /rest/scriptrunner/latest/scheduled-jobs/com.onresolve.scriptrunner.canned.confluence.jobs.CustomScheduledJob/preview | traceId: 4b20be416167a207 | userName: | sr.execution.id: [814937b7-1d59-49a8-9f66-06963a8e334d]
2020-05-19 21:09:53,317 WARN [http-nio-8090-exec-454 url:/rest/scriptrun...heduledJob/preview username:] [confluence.impl.hibernate.ConfluenceHibernateTransactionManager] doRollback Performing rollback. Transactions:
->[PluginReadWriteTx]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT (Session #1888224564)
-- referer: /plugins/servlet/scriptrunner/admin/jobs/edit/07a91bc6-58be-4aea-b31a-0acc9f2e5c8d?REF=EDACA9A69B5BE5EA19B534CAA0CB76C490997B3BD7A0F23D3E9100000004 | url: /rest/scriptrunner/latest/scheduled-jobs/com.onresolve.scriptrunner.canned.confluence.jobs.CustomScheduledJob/preview | traceId: 4b20be416167a207 | userName: | sr.execution.id: [814937b7-1d59-49a8-9f66-06963a8e334d]
I've bolded the part which I believe is causing the issue. It appears the attachment is null. So now I'm back to the drawing board of creating a new file attachment for a page without first writing the file to the filesystem.
Can the AttachmentService accomplish this?
Many Thanks!
-Matt
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
AHA !
I was hoping for this attachment being null error to surface again, so I can reproduce it consistently.
I have vague memories of "something somewhere" requiring a new hibernate transaction, which in this case means some objects are null. I will make some time this week/next to help come up with a workaround
Thank you so much for the clarification of how the code is executed, extremely useful to my team !
:thinking: I don't think the `AttachmentService` allows for that at the moment, looks like a good improvement to add to it.
Cheers
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Any help formulating a work around would be wonderful! I tried to do this again with saving the file to the Confluence temp dir first and then uploading but I'm still seeing "org.springframework.transaction.UnexpectedRollbackException" error but it does not show the attachment being null.
2020-05-20 21:11:05,963 ERROR [http-nio-8090-exec-472 url:/rest/scriptrun...heduledJob/preview ] [onresolve.scriptrunner.runner.ScriptBindingsManager] call Attachment content
-- referer: https://confluence-test-aws..com/plugins/servlet/scriptrunner/admin/jobs/edit/07a91bc6-58be-4aea-b31a-0acc9f2e5c8d?REF=1DE612E880BB9A7862649FC39A75860773AE5264966C670D5B3600000001 | url: /rest/scriptrunner/latest/scheduled-jobs/com.onresolve.scriptrunner.canned.confluence.jobs.CustomScheduledJob/preview | traceId: 97633fdaa713cb21 | userName: | sr.execution.id: [75ae8dc5-02c3-4272-9d0b-d052f95377d1]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Alright, I am starting my Friday just now, busy morning, but making time for it this afternoon.
Thanks for your patience !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.