Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support programmatic multipart/form-data responses #28631

Closed
knutwannheden opened this issue Oct 17, 2022 · 4 comments · Fixed by #28821
Closed

Support programmatic multipart/form-data responses #28631

knutwannheden opened this issue Oct 17, 2022 · 4 comments · Fixed by #28821
Labels
area/rest kind/enhancement New feature or request
Milestone

Comments

@knutwannheden
Copy link
Contributor

Description

With RESTEasy Classic there is the MultipartFormDataOutput API which allows programmatic creation of multipart/form-data responses, when the actual parts need to be created dynamically. In RESTEasy Reactive there is no such counterpart and I could not find any easy way to do this without basically implementing everything from scratch.

Also note the OutputPart#getHeaders() API which would allow to programmatically add additional headers to the individual parts. This currently isn't possible with org.jboss.resteasy.reactive.server.core.multipart.PartItem.

Implementation ideas

No response

@knutwannheden knutwannheden added the kind/enhancement New feature or request label Oct 17, 2022
@geoand
Copy link
Contributor

geoand commented Oct 18, 2022

Can you show an example use of RESTEasy Classic's MultipartFormDataOutput?

Thanks

@knutwannheden
Copy link
Contributor Author

The Kotlin code we have which uses this API looks like this:

    fun toMultipartFormDataOutput(): MultipartFormDataOutput = MultipartFormDataOutput().apply {
        for ((key, result) in results) {
            val part = addFormData(key, result.content, result.mediaType)
            result.pageCount?.let {
                part.headers[Param.CONTENT_RANGE] = listOf("pages 0-${it - 1}/$it")
                part.headers["x-page-count"] = listOf(it)
            }
        }
    }

@quarkus-bot
Copy link

quarkus-bot bot commented Oct 18, 2022

/cc @FroMage, @stuartwdouglas

@geoand
Copy link
Contributor

geoand commented Oct 19, 2022

cc @Sgitario who did the multipart output stuff in RR

Sgitario added a commit to Sgitario/quarkus that referenced this issue Oct 25, 2022
With these changes, users can also manually append the parts of the form using the class `MultipartFormDataOutput` as:

```java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.server.core.multipart.MultipartFormDataOutput;

@path("multipart")
public class Endpoint {

    @get
    @produces(MediaType.MULTIPART_FORM_DATA)
    @path("file")
    public MultipartFormDataOutput getFile() {
        MultipartFormDataOutput form = new MultipartFormDataOutput();
        form.addFormData("person", new Person("John"), MediaType.APPLICATION_JSON_TYPE);
        form.addFormData("status", "a status", MediaType.TEXT_PLAIN_TYPE)
                .getHeaders().putSingle("extra-header", "extra-value");
        return form;
    }
}
```

Fix quarkusio#28631

This last approach allows you adding extra headers to the output part.
Sgitario added a commit to Sgitario/quarkus that referenced this issue Oct 26, 2022
With these changes, users can also manually append the parts of the form using the class `MultipartFormDataOutput` as:

```java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.server.core.multipart.MultipartFormDataOutput;

@path("multipart")
public class Endpoint {

    @get
    @produces(MediaType.MULTIPART_FORM_DATA)
    @path("file")
    public MultipartFormDataOutput getFile() {
        MultipartFormDataOutput form = new MultipartFormDataOutput();
        form.addFormData("person", new Person("John"), MediaType.APPLICATION_JSON_TYPE);
        form.addFormData("status", "a status", MediaType.TEXT_PLAIN_TYPE)
                .getHeaders().putSingle("extra-header", "extra-value");
        return form;
    }
}
```

Fix quarkusio#28631

This last approach allows you adding extra headers to the output part.
@quarkus-bot quarkus-bot bot added this to the 2.15 - main milestone Oct 26, 2022
tmihalac pushed a commit to tmihalac/quarkus that referenced this issue Oct 27, 2022
With these changes, users can also manually append the parts of the form using the class `MultipartFormDataOutput` as:

```java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.server.core.multipart.MultipartFormDataOutput;

@path("multipart")
public class Endpoint {

    @get
    @produces(MediaType.MULTIPART_FORM_DATA)
    @path("file")
    public MultipartFormDataOutput getFile() {
        MultipartFormDataOutput form = new MultipartFormDataOutput();
        form.addFormData("person", new Person("John"), MediaType.APPLICATION_JSON_TYPE);
        form.addFormData("status", "a status", MediaType.TEXT_PLAIN_TYPE)
                .getHeaders().putSingle("extra-header", "extra-value");
        return form;
    }
}
```

Fix quarkusio#28631

This last approach allows you adding extra headers to the output part.
@gsmet gsmet modified the milestones: 2.15 - main, 2.14.0.Final Oct 31, 2022
gsmet pushed a commit to gsmet/quarkus that referenced this issue Oct 31, 2022
With these changes, users can also manually append the parts of the form using the class `MultipartFormDataOutput` as:

```java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.server.core.multipart.MultipartFormDataOutput;

@path("multipart")
public class Endpoint {

    @get
    @produces(MediaType.MULTIPART_FORM_DATA)
    @path("file")
    public MultipartFormDataOutput getFile() {
        MultipartFormDataOutput form = new MultipartFormDataOutput();
        form.addFormData("person", new Person("John"), MediaType.APPLICATION_JSON_TYPE);
        form.addFormData("status", "a status", MediaType.TEXT_PLAIN_TYPE)
                .getHeaders().putSingle("extra-header", "extra-value");
        return form;
    }
}
```

Fix quarkusio#28631

This last approach allows you adding extra headers to the output part.

(cherry picked from commit 6967fbc)
@gsmet gsmet modified the milestones: 2.14.0.Final, 2.13.4.Final Oct 31, 2022
gsmet pushed a commit to gsmet/quarkus that referenced this issue Oct 31, 2022
With these changes, users can also manually append the parts of the form using the class `MultipartFormDataOutput` as:

```java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.server.core.multipart.MultipartFormDataOutput;

@path("multipart")
public class Endpoint {

    @get
    @produces(MediaType.MULTIPART_FORM_DATA)
    @path("file")
    public MultipartFormDataOutput getFile() {
        MultipartFormDataOutput form = new MultipartFormDataOutput();
        form.addFormData("person", new Person("John"), MediaType.APPLICATION_JSON_TYPE);
        form.addFormData("status", "a status", MediaType.TEXT_PLAIN_TYPE)
                .getHeaders().putSingle("extra-header", "extra-value");
        return form;
    }
}
```

Fix quarkusio#28631

This last approach allows you adding extra headers to the output part.

(cherry picked from commit 6967fbc)
zakkak pushed a commit to zakkak/quarkus that referenced this issue Nov 15, 2022
With these changes, users can also manually append the parts of the form using the class `MultipartFormDataOutput` as:

```java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.server.core.multipart.MultipartFormDataOutput;

@path("multipart")
public class Endpoint {

    @get
    @produces(MediaType.MULTIPART_FORM_DATA)
    @path("file")
    public MultipartFormDataOutput getFile() {
        MultipartFormDataOutput form = new MultipartFormDataOutput();
        form.addFormData("person", new Person("John"), MediaType.APPLICATION_JSON_TYPE);
        form.addFormData("status", "a status", MediaType.TEXT_PLAIN_TYPE)
                .getHeaders().putSingle("extra-header", "extra-value");
        return form;
    }
}
```

Fix quarkusio#28631

This last approach allows you adding extra headers to the output part.

(cherry picked from commit 6967fbc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/rest kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants