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)