Search in sources :

Example 1 with Message

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();
    }
}
Also used : UTF8JsonGenerator(com.fasterxml.jackson.core.json.UTF8JsonGenerator) Message(io.protostuff.Message) IOContext(com.fasterxml.jackson.core.io.IOContext)

Example 2 with Message

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.");
}
Also used : HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpInputMessage(org.springframework.http.HttpInputMessage) Message(io.protostuff.Message) HttpOutputMessage(org.springframework.http.HttpOutputMessage) MediaType(org.springframework.http.MediaType) NonNull(org.springframework.lang.NonNull)

Aggregations

Message (io.protostuff.Message)2 IOContext (com.fasterxml.jackson.core.io.IOContext)1 UTF8JsonGenerator (com.fasterxml.jackson.core.json.UTF8JsonGenerator)1 HttpInputMessage (org.springframework.http.HttpInputMessage)1 HttpOutputMessage (org.springframework.http.HttpOutputMessage)1 MediaType (org.springframework.http.MediaType)1 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)1 NonNull (org.springframework.lang.NonNull)1