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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,558,752
Community Members
 
Community Events
184
Community Groups

I am trying to create a new Attachment for a Confluence page but have been unsuccessful

Edited
Matt
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 13, 2020

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)

 

 

2 answers

1 accepted

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

(at https://docs.atlassian.com/atlassian-confluence/6.0.3/index.html?com/atlassian/confluence/api/service/content/AttachmentService.html)

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.

0 votes
Hasnae
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 17, 2020

Hello @Matt 

Not sure what is going on there, but I usually use `AttachmentService` from our java-api over the `AttachmentManager` 

https://docs.atlassian.com/atlassian-confluence/6.0.3/com/atlassian/confluence/api/service/content/AttachmentService.html#addAttachments-com.atlassian.confluence.api.model.content.id.ContentId-java.util.Collection-

Some questions to help me understand what's going on with that specific API usage :

  • is there anything else in the logs around that transaction rollback ? a `caused by` stacktrace ?
  • which JDK and Confluence versions are you on ?
Matt
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 19, 2020 • edited May 21, 2020

@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



Hasnae
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 19, 2020

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

Like Matt likes this
Matt
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 21, 2020

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]
Hasnae
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 21, 2020

Alright, I am starting my Friday just now, busy morning, but making time for it this afternoon.

Thanks for your patience !

Like Matt likes this
Matt
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.
Jun 05, 2020

Hi @Hasnae, any luck on this? Thanks! 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events