Confluence rest plugin upload file?

Hi!

I am trying to create a Confluence rest Plugin which offers the possibility to upload a file to the server,. currently I am using atlas-run for testing.

My attempts to use parts of the many samples on how to achieve this with jax-rs and jersey,
didnt work out with fulfilling the packaging dependencies.

@FormDataParam seems to need jersey-multipart package:

<dependency>
	<groupId>com.sun.jersey.contribs</groupId>
	<artifactId>jersey-multipart</artifactId>
	<version>1.9</version>
</dependency> 

using this in my pom.xml, leads to more needed com.sun.packages and ongoing “missing requirements” messages like missing requirement [261.0] osgi.wiring.package; (osgi.wiring.package=com.sun.net.server) and more …

but looking at the packages from atlassian:

import com.atlassian.plugins.rest.common.multipart.MultipartForm;
import com.atlassian.plugins.rest.common.multipart.MultipartFormParam;
import com.atlassian.plugins.rest.common.multipart.fileupload;
import com.atlassian.plugins.rest.common.multipart.jersey;

everything should already be there by using

<dependency>
   <groupId>com.atlassian.plugins.rest</groupId>
   <artifactId>atlassian-rest-common</artifactId>
   <version>3.0.14</version>
   <scope>provided</scope>
</dependency>

at least the apache felix web management console display all necessary packages.

I am pretty much stuck here and I could use some help on how to achieve this.

there must be a way …

Thanx in advance!

Regards,
Hans

This is how it works for me:

@Override
    @Path("/somepath")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @POST
    public Response requestDevice(@Context HttpServletRequest request){
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
     // Parse the form file here
   if (!item.isFormField()) {
   }
   else {}// Parse the form data here
}
...}

and as dependency

<dependency>
            <groupId>com.atlassian.plugins.rest</groupId>
            <artifactId>atlassian-rest-common</artifactId>
            <version>2.8.0-confluence3</version>
            <scope>provided</scope>
        </dependency>
2 Likes

Hi Pannos!

Thanx for writing!

I suppose You are using a RestResource function which aligns to a rest-entry in atlassian-plugin.xml ?

Why are You using @Override with it ?

I seem to be able to call my rest resource from a vm template but I am receiving XSRF check failed.

googling around shows that I would have to use a specific velocity macro in my vm file: #form_xsrfToken()

unfortunately this doesnt get rendered … it appears in the text. I am loadning the vm file from within a servlet using TemplateRenderer.

any ideas on that ?

Thanx and Happy Easter!

Hans

The xsrf is answered in your other question. The override annotation is a leftover from copy paste please ignore. And yes it aligns with the rest entry in the xml. You can also explicitly ignore the lack of xsrf token using some annotation which i cant remember atm

Good morning!

https://developer.atlassian.com/confdev/confluence-plugin-guide/writing-confluence-plugins/form-token-handling

an intersting and helpful link.

additionally apache.commons fileupload package also needs to be added, which itself needs javax.portlet.

it is a miracle how this all works with all those packages, dependencies,spring beans, modules, annotations … sometimes it feels like rocket science to me :wink:

Best regards,
Hans