Skip to content

Commit

Permalink
Merge pull request #30945 from geoand/#28385
Browse files Browse the repository at this point in the history
Add support for JAX-RS StreamingOutput in RESTEasy Reactive
  • Loading branch information
geoand authored Feb 7, 2023
2 parents dce88e8 + bbbd2da commit 5b8475d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.StreamingOutput;
import jakarta.ws.rs.core.Variant;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.MessageBodyWriter;
Expand Down Expand Up @@ -63,6 +64,7 @@
import org.jboss.resteasy.reactive.server.providers.serialisers.ServerPathPartBodyHandler;
import org.jboss.resteasy.reactive.server.providers.serialisers.ServerReaderBodyHandler;
import org.jboss.resteasy.reactive.server.providers.serialisers.ServerStringMessageBodyHandler;
import org.jboss.resteasy.reactive.server.providers.serialisers.StreamingOutputMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerHttpRequest;
import org.jboss.resteasy.reactive.server.spi.ServerHttpResponse;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
Expand Down Expand Up @@ -130,6 +132,8 @@ public void accept(ResteasyReactiveRequestContext context) {
MediaType.APPLICATION_FORM_URLENCODED),
new Serialisers.BuiltinWriter(InputStream.class, ServerInputStreamMessageBodyHandler.class,
MediaType.WILDCARD),
new Serialisers.BuiltinWriter(StreamingOutput.class, StreamingOutputMessageBodyWriter.class,
MediaType.WILDCARD),
new Serialisers.BuiltinWriter(Reader.class, ServerReaderBodyHandler.class,
MediaType.WILDCARD),
new Serialisers.BuiltinWriter(File.class, ServerFileBodyHandler.class,
Expand Down
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());
}
}
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));
}
};
}
}
}
2 changes: 1 addition & 1 deletion tcks/resteasy-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<properties>

<!-- to avoid sudden surprises, checkout is pinned to a specific commit -->
<resteasy-reactive-testsuite.repo.ref>34795c1d2159c420298916d640df239f249f4935</resteasy-reactive-testsuite.repo.ref>
<resteasy-reactive-testsuite.repo.ref>8937bc59acc21658d1c9acf59dc3e8960fd98869</resteasy-reactive-testsuite.repo.ref>

<exec.skip>${skipTests}</exec.skip>
<resteasy-reactive-testsuite.clone.skip>${exec.skip}</resteasy-reactive-testsuite.clone.skip>
Expand Down

0 comments on commit 5b8475d

Please sign in to comment.