org.openprovenance.prov.service.core.readers.GenericJsonMessageBodyReader Maven / Gradle / Ivy
package org.openprovenance.prov.service.core.readers;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyReader;
import jakarta.ws.rs.ext.Provider;
import org.openprovenance.prov.model.interop.InteropMediaType;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
@Provider
@Consumes({InteropMediaType.MEDIA_APPLICATION_JSON})
public class GenericJsonMessageBodyReader implements MessageBodyReader {
private final Class clazz;
public GenericJsonMessageBodyReader(Class clazz) {
this.clazz=clazz;
}
ObjectMapper om=new ObjectMapper();
@Override
public boolean isReadable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return (mediaType.toString().startsWith(InteropMediaType.MEDIA_APPLICATION_JSON));
}
@Override
public T readFrom(Class aClass,
Type type,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap multivaluedMap,
InputStream inputStream) throws IOException, WebApplicationException {
return om.readValue(inputStream, clazz);
}
}