use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TProtocolSerializer method serialize.
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull PServiceCall<Message, Field> call) throws IOException {
CountingOutputStream wrapper = new CountingOutputStream(output);
TTransport transport = new TIOStreamTransport(wrapper);
try {
TProtocol protocol = protocolFactory.getProtocol(transport);
TMessage tm = new TMessage(call.getMethod(), (byte) call.getType().asInteger(), call.getSequence());
protocol.writeMessageBegin(tm);
writeMessage(call.getMessage(), protocol);
protocol.writeMessageEnd();
transport.flush();
wrapper.flush();
return wrapper.getByteCount();
} catch (TException e) {
throw new SerializerException(e, e.getMessage());
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TProtocolSerializer method readMessage.
private <Message extends PMessage<Message, Field>, Field extends PField> Message readMessage(TProtocol protocol, PMessageDescriptor<Message, Field> descriptor) throws SerializerException, TException {
TField f;
PMessageBuilder<Message, Field> builder = descriptor.builder();
// ignored.
protocol.readStructBegin();
while ((f = protocol.readFieldBegin()) != null) {
if (f.type == BinaryType.STOP) {
break;
}
PField field;
// f.name is never fulled out, rely on f.id being correct.
field = descriptor.findFieldById(f.id);
if (field != null) {
if (f.type != forType(field.getDescriptor().getType())) {
throw new SerializerException("Incompatible serialized type " + asString(f.type) + " for field " + field.getName() + ", expected " + asString(forType(field.getDescriptor().getType())));
}
Object value = readTypedValue(f.type, field.getDescriptor(), protocol, true);
if (value != null) {
builder.set(field.getId(), value);
}
} else {
TProtocolUtil.skip(protocol, f.type);
}
protocol.readFieldEnd();
}
protocol.readStructEnd();
if (readStrict) {
try {
builder.validate();
} catch (IllegalStateException e) {
throw new SerializerException(e, e.getMessage());
}
}
return builder.build();
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TProtocolSerializer method serialize.
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull Message message) throws IOException {
CountingOutputStream wrapper = new CountingOutputStream(output);
TTransport transport = new TIOStreamTransport(wrapper);
try {
TProtocol protocol = protocolFactory.getProtocol(transport);
writeMessage(message, protocol);
transport.flush();
wrapper.flush();
return wrapper.getByteCount();
} catch (TException e) {
throw new SerializerException(e, e.getMessage());
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TProtocolSerializer method deserialize.
@Nonnull
@Override
@SuppressWarnings("unchecked")
public <Message extends PMessage<Message, Field>, Field extends PField> PServiceCall<Message, Field> deserialize(@Nonnull InputStream input, @Nonnull PService service) throws SerializerException {
PServiceCallType type = null;
TMessage tm = null;
try {
TTransport transport = new TIOStreamTransport(input);
TProtocol protocol = protocolFactory.getProtocol(transport);
tm = protocol.readMessageBegin();
type = PServiceCallType.findById(tm.type);
if (type == null) {
throw new SerializerException("Unknown call type for id " + tm.type).setExceptionType(PApplicationExceptionType.INVALID_MESSAGE_TYPE);
} else if (type == PServiceCallType.EXCEPTION) {
PApplicationException exception = readMessage(protocol, PApplicationException.kDescriptor);
return new PServiceCall(tm.name, type, tm.seqid, exception);
}
PServiceMethod method = service.getMethod(tm.name);
if (method == null) {
throw new SerializerException("No such method " + tm.name + " on " + service.getQualifiedName()).setExceptionType(PApplicationExceptionType.UNKNOWN_METHOD);
}
@SuppressWarnings("unchecked") PMessageDescriptor<Message, Field> descriptor = isRequestCallType(type) ? method.getRequestType() : method.getResponseType();
Message message = readMessage(protocol, descriptor);
protocol.readMessageEnd();
return new PServiceCall<>(tm.name, type, tm.seqid, message);
} catch (TTransportException e) {
throw new SerializerException(e, e.getMessage()).setExceptionType(PApplicationExceptionType.findById(e.getType())).setCallType(type).setSequenceNo(tm != null ? tm.seqid : 0).setMethodName(tm != null ? tm.name : null);
} catch (TException e) {
throw new SerializerException(e, e.getMessage()).setExceptionType(PApplicationExceptionType.PROTOCOL_ERROR).setCallType(type).setSequenceNo(tm != null ? tm.seqid : 0).setMethodName(tm != null ? tm.name : null);
} catch (SerializerException e) {
e.setMethodName(tm.name).setSequenceNo(tm.seqid).setCallType(type);
throw e;
}
}
use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.
the class TTupleProtocolSerializer method serialize.
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> int serialize(@Nonnull OutputStream output, @Nonnull Message message) throws IOException {
CountingOutputStream wrapper = new CountingOutputStream(output);
TTransport transport = new TIOStreamTransport(wrapper);
try {
TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
writeMessage(message, protocol);
transport.flush();
wrapper.flush();
return wrapper.getByteCount();
} catch (TException e) {
throw new SerializerException(e, e.getMessage());
}
}
Aggregations