Skip to content

Commit

Permalink
Merge pull request #22722 from geoand/#22694
Browse files Browse the repository at this point in the history
Enable returning a Mutiny Buffer from RESTEasy Reactive Resource Method
  • Loading branch information
geoand authored Jan 7, 2022
2 parents 052d88e + 91ead07 commit 5ab0e00
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.jboss.resteasy.reactive.server.processor.scanning.ResponseStatusMethodScanner;
import org.jboss.resteasy.reactive.server.processor.util.ResteasyReactiveServerDotNames;
import org.jboss.resteasy.reactive.server.vertx.serializers.ServerMutinyAsyncFileMessageBodyWriter;
import org.jboss.resteasy.reactive.server.vertx.serializers.ServerMutinyBufferMessageBodyWriter;
import org.jboss.resteasy.reactive.server.vertx.serializers.ServerVertxAsyncFileMessageBodyWriter;
import org.jboss.resteasy.reactive.server.vertx.serializers.ServerVertxBufferMessageBodyWriter;
import org.jboss.resteasy.reactive.spi.BeanFactory;
Expand Down Expand Up @@ -216,7 +217,12 @@ MethodScannerBuildItem responseHeaderSupport() {
@BuildStep
void vertxIntegration(BuildProducer<MessageBodyWriterBuildItem> writerBuildItemBuildProducer) {
writerBuildItemBuildProducer.produce(new MessageBodyWriterBuildItem(ServerVertxBufferMessageBodyWriter.class.getName(),
Buffer.class.getName(), Collections.singletonList(MediaType.WILDCARD), RuntimeType.SERVER, true,
io.vertx.core.buffer.Buffer.class.getName(), Collections.singletonList(MediaType.WILDCARD), RuntimeType.SERVER,
true,
Priorities.USER));
writerBuildItemBuildProducer.produce(new MessageBodyWriterBuildItem(ServerMutinyBufferMessageBodyWriter.class.getName(),
io.vertx.mutiny.core.buffer.Buffer.class.getName(), Collections.singletonList(MediaType.WILDCARD),
RuntimeType.SERVER, true,
Priorities.USER));
writerBuildItemBuildProducer
.produce(new MessageBodyWriterBuildItem(ServerVertxAsyncFileMessageBodyWriter.class.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ public Buffer vertxBuffer() {
return Buffer.buffer("VERTX-BUFFER");
}

@GET
@Path("writer/mutiny-buffer")
public io.vertx.mutiny.core.buffer.Buffer mutinyBuffer() {
return io.vertx.mutiny.core.buffer.Buffer.buffer("MUTINY-BUFFER");
}

@GET
@Path("async/cs/ok")
public CompletionStage<String> asyncCompletionStageOK() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ public void testWriter() {

RestAssured.get("/simple/writer/vertx-buffer")
.then().body(Matchers.equalTo("VERTX-BUFFER"));

RestAssured.get("/simple/writer/mutiny-buffer")
.then().body(Matchers.equalTo("MUTINY-BUFFER"));
}

@DisabledOnOs(OS.WINDOWS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.jboss.resteasy.reactive.server.vertx.serializers;

import io.vertx.mutiny.core.buffer.Buffer;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo;
import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter;
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext;

@Provider
public class ServerMutinyBufferMessageBodyWriter implements ServerMessageBodyWriter<Buffer> {

public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return true;
}

public void writeTo(Buffer buffer, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
entityStream.write(buffer.getBytes());
}

@Override
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) {
return true;
}

@Override
public void writeResponse(Buffer buffer, Type genericType, ServerRequestContext context) throws WebApplicationException {
context.serverResponse().end(buffer.getBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ public Buffer vertxBuffer() {
return Buffer.buffer("VERTX-BUFFER");
}

@GET
@Path("writer/mutiny-buffer")
public io.vertx.mutiny.core.buffer.Buffer mutinyBuffer() {
return io.vertx.mutiny.core.buffer.Buffer.buffer("MUTINY-BUFFER");
}

@GET
@Path("async/cs/ok")
public CompletionStage<String> asyncCompletionStageOK() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ public void testWriter() {

RestAssured.get("/simple/writer/vertx-buffer")
.then().body(Matchers.equalTo("VERTX-BUFFER"));

RestAssured.get("/simple/writer/mutiny-buffer")
.then().body(Matchers.equalTo("MUTINY-BUFFER"));
}

@DisabledOnOs(OS.WINDOWS)
Expand Down

0 comments on commit 5ab0e00

Please sign in to comment.