use of io.scalecube.services.exceptions.MessageCodecException in project scalecube by scalecube.
the class ProtostuffCodec method decode.
@Override
public Object decode(InputStream stream, Type type) throws IOException {
try {
Class<?> clazz = null;
if (type instanceof Class<?>) {
clazz = (Class<?>) type;
} else if (type instanceof ParameterizedType) {
clazz = Class.forName(((ParameterizedType) type).getRawType().getTypeName());
}
// noinspection rawtypes
Schema schema = RuntimeSchema.getSchema(clazz);
Object result = schema.newMessage();
// noinspection unchecked
ProtobufIOUtil.mergeFrom(stream, result, schema, LinkedBuffer.allocate());
return result;
} catch (ClassNotFoundException e) {
throw new MessageCodecException("Couldn't decode message", e);
}
}
use of io.scalecube.services.exceptions.MessageCodecException in project scalecube by scalecube.
the class ServiceMessageCodec method encodeAndTransform.
/**
* Encode a message, transform it to T.
*
* @param message the message to transform
* @param transformer a function that accepts data and header {@link ByteBuf} and return the
* required T
* @return the object (transformed message)
* @throws MessageCodecException when encoding cannot be done.
*/
public <T> T encodeAndTransform(ServiceMessage message, BiFunction<ByteBuf, ByteBuf, T> transformer) throws MessageCodecException {
ByteBuf dataBuffer = Unpooled.EMPTY_BUFFER;
ByteBuf headersBuffer = Unpooled.EMPTY_BUFFER;
if (message.hasData(ByteBuf.class)) {
dataBuffer = message.data();
} else if (message.hasData()) {
dataBuffer = ByteBufAllocator.DEFAULT.buffer();
try {
DataCodec dataCodec = getDataCodec(message.dataFormatOrDefault());
dataCodec.encode(new ByteBufOutputStream(dataBuffer), message.data());
} catch (Throwable ex) {
ReferenceCountUtil.safestRelease(dataBuffer);
LOGGER.error("Failed to encode service message data on: {}, cause: {}", message, ex.toString());
throw new MessageCodecException("Failed to encode service message data", ex);
}
}
if (!message.headers().isEmpty()) {
headersBuffer = ByteBufAllocator.DEFAULT.buffer();
try {
headersCodec.encode(new ByteBufOutputStream(headersBuffer), message.headers());
} catch (Throwable ex) {
ReferenceCountUtil.safestRelease(headersBuffer);
// release data buf as well
ReferenceCountUtil.safestRelease(dataBuffer);
LOGGER.error("Failed to encode service message headers on: {}, cause: {}", message, ex.toString());
throw new MessageCodecException("Failed to encode service message headers", ex);
}
}
return transformer.apply(dataBuffer, headersBuffer);
}
use of io.scalecube.services.exceptions.MessageCodecException in project scalecube by scalecube.
the class ServiceMessageCodec method decodeData.
/**
* Decode message.
*
* @param message the original message (with {@link ByteBuf} data)
* @param dataType the type of the data.
* @return a new Service message that upon {@link ServiceMessage#data()} returns the actual data
* (of type data type)
* @throws MessageCodecException when decode fails
*/
public static ServiceMessage decodeData(ServiceMessage message, Type dataType) throws MessageCodecException {
if (dataType == null || !message.hasData(ByteBuf.class) || ((ByteBuf) message.data()).readableBytes() == 0) {
return message;
}
Object data;
Type targetType = message.isError() ? ErrorData.class : dataType;
ByteBuf dataBuffer = message.data();
try (ByteBufInputStream inputStream = new ByteBufInputStream(dataBuffer, true)) {
DataCodec dataCodec = DataCodec.getInstance(message.dataFormatOrDefault());
data = dataCodec.decode(inputStream, targetType);
} catch (Throwable ex) {
throw new MessageCodecException("Failed to decode service message data", ex);
}
return ServiceMessage.from(message).data(data).build();
}
Aggregations