org.openntf.xsp.jakartaee icon indicating copy to clipboard operation
org.openntf.xsp.jakartaee copied to clipboard

Investigate options for attachments in JAX-RS

Open jesse-gallagher opened this issue 3 years ago • 6 comments

Jakarta REST is gaining proper multipart support in the next release, but that doesn't help us much. In the mean time, you can declare a single argument of type MimeMultipart, which works, but is tedious to process. Ideally, there'd be a good way to declare a single parameter to handle incoming attachments.

One option would be to look for an internal RESTEasy implementation for this, which presumably exists, but then it tightly binds code to the specific REST implementation. Another may be to investigate adding support for jakarta.servlet.http.Part parameters with a custom reader. I'm not sure if that reader would get enough information to make that work, though.

jesse-gallagher avatar Jun 20 '22 13:06 jesse-gallagher

When might it be possible to process in multipart/form-data or how to upload file and json now?

esvechnikov avatar Aug 30 '23 23:08 esvechnikov

For JSON you just need to specify the class you want your JSON to be mapped to. For multipart/form-data I'm currently also trying to figure it out.

public class TestClass {
    private String _name;
    private int _age;

    public getName() {
        return this._name;
    }

    public setName(String name) {
        this._name = name;
    }

    public getAge() {
        return this._age;
    }

    public setAge(int age) {
        this._age = age;
    }
}

@POST
@Path("/json")
@Consumes(MediaType.APPLICATION_JSON)
public Response postJson(TestClass obj) {
    /* Do whatever you want with obj. */
}

DISCLAIMER: I didn't test this specific piece of code.

monstermichl avatar Sep 20 '23 05:09 monstermichl

@esvechnikov I also just found an example for multipart/form-data. https://github.com/OpenNTF/org.openntf.xsp.jakartaee/blob/a983f5d0edfbc7175d29419a528034069d49d5de/eclipse/nsfs/nsf-jakartaee-example/odp/Code/Java/rest/NoSQLExample.java#L167

monstermichl avatar Sep 20 '23 06:09 monstermichl

MimeMultipart is basically the "workaround" for this for now - it's not the greatest API, but it does work and it's idiomatic enough.

Fortunately, the time since I originally made this issue has made it so that we'll have a real fix by default when Domino 14 comes out. We'll be able to move to the JEE 10 version of JAX-RS that has native support for splitting out parts without forcing you to also parse through the whole multipart yourself.

jesse-gallagher avatar Sep 20 '23 18:09 jesse-gallagher

I'm currently limited to Domino 9.0.1. The solution with MimeMultipart works, thanks @monstermichl. RestEasy has a Multipart provider, it is described in the services https://github.com/OpenNTF/org.openntf.xsp.jakartaee/blob/a983f5d0edfbc7175d29419a528034069d49d5de/eclipse/bundles/org.openntf.xsp.jaxrs.providers/META-INF/services/jakarta.ws.rs.ext.Providers#L8 ,but there is no way to use it. How do I enable it for my build?

esvechnikov avatar Sep 26 '23 14:09 esvechnikov

I've tried to studiously avoid exposing implementation-specific classes like that to the NSFs, especially for "core" specs like JAX-RS. I already changed the JAX-RS implementation once a while back, which would have broken any code relying on the previous provider, so it's not just a theoretical issue.

Still, it's a sticky spot at the moment. I'll consider options... maybe I could have a "backport" variant of the newer-era Part that's special to this toolkit but marked as deprecated immediately. It could be present specifically for the 2.x -> 3.x transition and then removed in 3.1.0 or thereabouts.

jesse-gallagher avatar Sep 26 '23 14:09 jesse-gallagher