package org.fracturedatlas.athena.reports.serialization;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import org.fracturedatlas.athena.reports.model.AthenaReport;
import org.fracturedatlas.athena.web.util.JsonUtil;
@Provider
@Produces({"application/json"})
public class AthenaReportSerializer implements MessageBodyWriter<AthenaReport>, MessageBodyReader<AthenaReport> {
@Override
public long getSize(AthenaReport t, Class<?> type, Type type1, Annotation[] annotations, MediaType mediaType) {
return -1L;
}
@Override
public boolean isWriteable(Class<?> type, Type type1, Annotation[] annotations, MediaType mediaType) {
return (AthenaReport.class.isAssignableFrom(type));
}
@Override
public boolean isReadable(java.lang.Class<?> type, java.lang.reflect.Type genericType, java.lang.annotation.Annotation[] annotations, MediaType mediaType) {
return (AthenaReport.class.isAssignableFrom(type));
}
@Override
public AthenaReport (java.lang.Class<AthenaReport> type, java.lang.reflect.Type genericType, java.lang.annotation.Annotation[] annotations, MediaType mediaType, MultivaluedMap<java.lang.String,java.lang.String> httpHeaders, java.io.InputStream entityStream) {
Gson gson = JsonUtil.getGson();
try{
AthenaReport tran = gson.fromJson(new InputStreamReader(entityStream), AthenaReport.class);
return tran;
} catch (JsonParseException e) {
e.printStackTrace();
throw e;
}
}
@Override
public void (AthenaReport t, Class<?> type, Type type1, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream out) throws IOException, WebApplicationException {
Gson gson = JsonUtil.getGsonWithoutNulls();
out.write(gson.toJson(t).getBytes());
}
}