Hi im trying to figure out how to upload an existing File to Confluence using the primary confluenceLink
This is my actual code (groovy):
<span>import</span> org.apache.log4j.Logger
<span>import</span> org.apache.log4j.Level
<span>import</span> com.onresolve.scriptrunner.runner.customisers.PluginModule
<span>import</span> com.atlassian.applinks.api.ApplicationLinkService
<span>import</span> com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
<span>import</span> org.apache.http.entity.FileEntity
<span>import</span> com.atlassian.sal.api.net.Request
<span>import</span> com.atlassian.sal.api.net.Response
<span>import</span> com.atlassian.sal.api.net.ResponseException
<span>import</span> com.atlassian.sal.api.net.ResponseHandler
def logger = Logger.getLogger(<span>"com.nolddor"</span>)
logger.setLevel(Level.DEBUG)
<span>@PluginModule</span>
ApplicationLinkService applicationLinkService
<span>//--------------------------------------------------------------</span>
<span>// You must change the following variables as per your needs</span>
<span>//--------------------------------------------------------------</span>
def pageId = <span>"6060557"</span> <span>// Confluence page</span>
def documentPath = <span>"/var/atlassian/application-data/jirasd/dbconfig.xml"</span> <span>// Attachment path</span>
<span>//--------------------------------------------------------------</span>
<span>// Ensure the existence of a working Confluence application link</span>
def confluenceLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class)
<span>assert</span> confluenceLink : <span>"Unable to retrieve primary Confluence application link."</span>
<span>// Grab the attachment from the filesystem</span>
def file = <span>new</span> File(documentPath)
<span>// REST Call</span>
confluenceLink.createAuthenticatedRequestFactory()
.createRequest(Request.MethodType.POST, <span>"rest/api/content/${pageId}/child/attachment"</span>)
.addHeader(<span>"X-Atlassian-Token"</span>, <span>"no-check"</span>)
.addHeader(<span>"Content-Type"</span>, <span>"multipart/form-data"</span>)
.addHeader(<span>"Accept"</span>, <span>"application/json"</span>)
.setEntity(<span>new</span> FileEntity(file))
.execute(<span>new</span> ResponseHandler<Response>() {
<span>@Override</span>
<span>void handle(Response response) throws ResponseException </span>{
def responseBodyAsString = response.getResponseBodyAsString();
<span>if</span>(!response.successful) {
<span>throw</span> <span>new</span> Exception(<span>"Unable to attach file to Conflunce page (id=${pageId}).\n Http-Response: ${responseBodyAsString}"</span>);
}
}
})
However it seems the already created authenticatedRequest is not able to handle a FileEntity because some misconfiguration on Jersey client. Any help?
java.lang.<span>RuntimeException:</span> Unable to find a message body writer <span>for</span> <span>class org.apache.http.entity.FileEntity</span>
at com.atlassian.plugins.rest.<span>module</span>.jersey.JerseyEntityHandler.marshall(JerseyEntityHandler.<span>java:</span><span>170</span>)
at com.atlassian.plugins.rest.<span>module</span>.jersey.JerseyRequest.marshallEntity(JerseyRequest.<span>java:</span><span>154</span>)
at com.atlassian.plugins.rest.<span>module</span>.jersey.JerseyRequest.executeAndReturn(JerseyRequest.<span>java:</span><span>130</span>)
at com.atlassian.plugins.rest.<span>module</span>.jersey.JerseyRequest.execute(JerseyRequest.<span>java:</span><span>113</span>)
at com.atlassian.applinks.core.auth.ApplicationLinkRequestAdaptor.execute(ApplicationLinkRequestAdaptor.<span>java:</span><span>50</span>)
at com.atlassian.applinks.oauth.auth.OAuthRequest.execute(OAuthRequest.<span>java:</span><span>71</span>)
at com.atlassian.sal.api.net.Request$execute$1.call(Unknown Source)
at Script405.run(Script405.<span>groovy:</span><span>43</span>)