use of io.protostuff.Message in project datawave by NationalSecurityAgency.
the class ProtostuffMessageBodyWriter method writeTo.
@SuppressWarnings("unchecked")
@Override
public void writeTo(Object message, Class<?> clazz, Type type, Annotation[] annotations, MediaType media, MultivaluedMap<String, Object> httpHeaders, OutputStream out) throws IOException, WebApplicationException {
// TODO: Figure out a method to add the proto file location in the response headers.
// This map must be mofified before any data is written to out,
// since at that time the response headers will be flushed.
Schema<Object> schema = null;
if (message instanceof Message) {
Message<Object> msg = (Message<Object>) message;
schema = msg.cachedSchema();
} else {
schema = (Schema<Object>) RuntimeSchema.getSchema(clazz);
}
try {
if (MediaType.APPLICATION_XML_TYPE.equals(media) || MediaType.TEXT_XML_TYPE.equals(media)) {
XmlIOUtil.writeTo(out, message, schema);
} else if ("text/yaml".equals(media.toString()) || "text/x-yaml".equals(media.toString()) || "application/x-yaml".equals(media.toString())) {
YamlIOUtil.writeTo(out, message, schema, buffer);
} else if ("application/x-protobuf".equals(media.toString())) {
ProtobufIOUtil.writeTo(out, message, schema, buffer);
} else if ("application/x-protostuff".equals(media.toString())) {
ProtostuffIOUtil.writeTo(out, message, schema, buffer);
} else if (MediaType.APPLICATION_JSON_TYPE.equals(media)) {
IOContext ctx = new IOContext(JsonIOUtil.DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false);
UTF8JsonGenerator generator = new UTF8JsonGenerator(ctx, JsonIOUtil.DEFAULT_JSON_FACTORY.getGeneratorFeatures(), JsonIOUtil.DEFAULT_JSON_FACTORY.getCodec(), out);
try {
JsonIOUtil.writeTo(generator, message, schema, false);
} finally {
generator.close();
}
}
} finally {
buffer.clear();
}
}
use of io.protostuff.Message in project datawave-spring-boot-starter by NationalSecurityAgency.
the class ProtostuffHttpMessageConverter method readInternal.
@Override
@NonNull
protected Message<?> readInternal(@NonNull Class<? extends Message<?>> clazz, @NonNull HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
MediaType contentType = inputMessage.getHeaders().getContentType();
if (contentType == null) {
contentType = PROTOSTUFF;
}
if (PROTOSTUFF.isCompatibleWith(contentType)) {
try {
// noinspection unchecked
Message<Object> msg = (Message<Object>) clazz.newInstance();
ProtostuffIOUtil.mergeFrom(inputMessage.getBody(), msg, msg.cachedSchema(), buffer.get());
return msg;
} catch (InstantiationException | IllegalAccessException e) {
throw new HttpMessageNotReadableException("Unable to read protostuff message: " + e.getMessage(), e, inputMessage);
}
}
throw new UnsupportedOperationException("Reading protostuff messages from media types other than " + PROTOSTUFF + " is not supported.");
}
Aggregations