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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to upload a file using existing confluenceLink

Hi im trying to figure out how to upload an existing File to Confluence using the primary confluenceLink

This is my actual code (groovy):

 

import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import org.apache.http.entity.FileEntity
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import com.atlassian.sal.api.net.ResponseHandler


def logger = Logger.getLogger("com.nolddor")
logger.setLevel(Level.DEBUG)


@PluginModule
ApplicationLinkService applicationLinkService


//--------------------------------------------------------------
// You must change the following variables as per your needs
//--------------------------------------------------------------
def pageId = "6060557" 														// Confluence page
def documentPath = "/var/atlassian/application-data/jirasd/dbconfig.xml" 	// Attachment path
//--------------------------------------------------------------


// Ensure the existence of a working Confluence application link
def confluenceLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class)
assert confluenceLink : "Unable to retrieve primary Confluence application link."

// Grab the attachment from the filesystem
def file = new File(documentPath)

// REST Call
confluenceLink.createAuthenticatedRequestFactory()
	.createRequest(Request.MethodType.POST, "rest/api/content/${pageId}/child/attachment")
    .addHeader("X-Atlassian-Token", "no-check")
    .addHeader("Content-Type", "multipart/form-data")
	.addHeader("Accept", "application/json")
    .setEntity(new FileEntity(file))
    .execute(new ResponseHandler<Response>() {
        @Override
        void handle(Response response) throws ResponseException {
            def responseBodyAsString = response.getResponseBodyAsString();
            if(!response.successful) {
                throw new Exception("Unable to attach file to Conflunce page (id=${pageId}).\n Http-Response: ${responseBodyAsString}");
            }
        }
    })                               

 

However it seems the already created authenticatedRequest is not able to handle a FileEntity because some misconfiguration on Jersey client. Any help?

java.lang.RuntimeException: Unable to find a message body writer for class org.apache.http.entity.FileEntity
	at com.atlassian.plugins.rest.module.jersey.JerseyEntityHandler.marshall(JerseyEntityHandler.java:170)
	at com.atlassian.plugins.rest.module.jersey.JerseyRequest.marshallEntity(JerseyRequest.java:154)
	at com.atlassian.plugins.rest.module.jersey.JerseyRequest.executeAndReturn(JerseyRequest.java:130)
	at com.atlassian.plugins.rest.module.jersey.JerseyRequest.execute(JerseyRequest.java:113)
	at com.atlassian.applinks.core.auth.ApplicationLinkRequestAdaptor.execute(ApplicationLinkRequestAdaptor.java:50)
	at com.atlassian.applinks.oauth.auth.OAuthRequest.execute(OAuthRequest.java:71)
	at com.atlassian.sal.api.net.Request$execute$1.call(Unknown Source)
	at Script405.run(Script405.groovy:43)

 

 

 

 

 

 

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you