io.quarkus.resteasy.runtime.vertx.JsonObjectWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-resteasy Show documentation
Show all versions of quarkus-resteasy Show documentation
REST endpoint framework implementing JAX-RS and more
package io.quarkus.resteasy.runtime.vertx;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.concurrent.CompletionStage;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.Provider;
import org.jboss.resteasy.spi.AsyncMessageBodyWriter;
import org.jboss.resteasy.spi.AsyncOutputStream;
import io.vertx.core.json.JsonObject;
/**
* A body writer that allows to return a Vert.x {@link JsonObject} as JAX-RS response content.
*
* @author Thomas Segismont
*/
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JsonObjectWriter implements AsyncMessageBodyWriter {
@Override
public boolean isWriteable(Class> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == JsonObject.class;
}
@Override
public void writeTo(JsonObject jsonObject, Class> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
entityStream.write(jsonObject.toBuffer().getBytes());
entityStream.flush();
entityStream.close();
}
@Override
public CompletionStage asyncWriteTo(JsonObject jsonObject, Class> type, Type genericType, Annotation[] annotations,
MediaType mediaType,
MultivaluedMap httpHeaders, AsyncOutputStream entityStream) {
return entityStream.asyncWrite(jsonObject.toBuffer().getBytes());
}
}