-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add support for JAX-RS StreamingOutput in RESTEasy Reactive
- Loading branch information
Showing
4 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...boss/resteasy/reactive/server/providers/serialisers/StreamingOutputMessageBodyWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.jboss.resteasy.reactive.server.providers.serialisers; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
import jakarta.ws.rs.core.StreamingOutput; | ||
|
||
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo; | ||
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; | ||
|
||
public class StreamingOutputMessageBodyWriter implements ServerMessageBodyWriter<StreamingOutput> { | ||
@Override | ||
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return doIsWriteable(type); | ||
} | ||
|
||
@Override | ||
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target, | ||
MediaType mediaType) { | ||
return doIsWriteable(type); | ||
} | ||
|
||
private static boolean doIsWriteable(Class<?> type) { | ||
return StreamingOutput.class.isAssignableFrom(type); | ||
} | ||
|
||
@Override | ||
public long getSize(StreamingOutput streamingOutput, Class<?> type, Type genericType, Annotation[] annotations, | ||
MediaType mediaType) { | ||
return -1; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamingOutput streamingOutput, Class<?> type, Type genericType, Annotation[] annotations, | ||
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) | ||
throws IOException, WebApplicationException { | ||
streamingOutput.write(entityStream); | ||
} | ||
|
||
@Override | ||
public void writeResponse(StreamingOutput o, Type genericType, ServerRequestContext context) | ||
throws WebApplicationException, IOException { | ||
o.write(context.getOrCreateOutputStream()); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...java/org/jboss/resteasy/reactive/server/vertx/test/providers/StreamingOutputTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.jboss.resteasy.reactive.server.vertx.test.providers; | ||
|
||
import static io.restassured.RestAssured.get; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.function.Supplier; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.StreamingOutput; | ||
|
||
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
public class StreamingOutputTestCase { | ||
|
||
@RegisterExtension | ||
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestResource.class); | ||
} | ||
}); | ||
|
||
@Test | ||
public void testWith() { | ||
get("/test") | ||
.then() | ||
.statusCode(200) | ||
.body(equalTo("hello world")); | ||
} | ||
|
||
@Path("test") | ||
public static class TestResource { | ||
|
||
@GET | ||
public StreamingOutput with() { | ||
return new StreamingOutput() { | ||
@Override | ||
public void write(OutputStream output) throws IOException, WebApplicationException { | ||
output.write("hello world".getBytes(StandardCharsets.UTF_8)); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters