io.quarkus.resteasy.runtime.vertx.JsonObjectReader 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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
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.core.NoContentException;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.Provider;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
/**
* A body reader that allows to get a Vert.x {@link JsonObject} as JAX-RS request content.
*/
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JsonObjectReader implements MessageBodyReader {
@Override
public boolean isReadable(Class> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == JsonObject.class;
}
@Override
public JsonObject readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
byte[] bytes = getBytes(entityStream);
if (bytes.length == 0) {
throw new NoContentException("Cannot create JsonObject");
}
return new JsonObject(Buffer.buffer(bytes));
}
private static byte[] getBytes(InputStream entityStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int len;
while ((len = entityStream.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
return baos.toByteArray();
}
}