Search in sources :

Example 16 with SerializerException

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());
    }
}
Also used : TException(org.apache.thrift.TException) CountingOutputStream(net.morimekta.util.io.CountingOutputStream) TProtocol(org.apache.thrift.protocol.TProtocol) TMessage(org.apache.thrift.protocol.TMessage) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport) SerializerException(net.morimekta.providence.serializer.SerializerException)

Example 17 with SerializerException

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();
}
Also used : TField(org.apache.thrift.protocol.TField) PField(net.morimekta.providence.descriptor.PField) TField(org.apache.thrift.protocol.TField) PMessage(net.morimekta.providence.PMessage) TMessage(org.apache.thrift.protocol.TMessage) PField(net.morimekta.providence.descriptor.PField) SerializerException(net.morimekta.providence.serializer.SerializerException)

Example 18 with SerializerException

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());
    }
}
Also used : TException(org.apache.thrift.TException) CountingOutputStream(net.morimekta.util.io.CountingOutputStream) TProtocol(org.apache.thrift.protocol.TProtocol) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport) SerializerException(net.morimekta.providence.serializer.SerializerException)

Example 19 with SerializerException

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;
    }
}
Also used : TException(org.apache.thrift.TException) PMessage(net.morimekta.providence.PMessage) TMessage(org.apache.thrift.protocol.TMessage) PServiceCallType(net.morimekta.providence.PServiceCallType) TTransportException(org.apache.thrift.transport.TTransportException) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) SerializerException(net.morimekta.providence.serializer.SerializerException) TField(org.apache.thrift.protocol.TField) PField(net.morimekta.providence.descriptor.PField) TMessage(org.apache.thrift.protocol.TMessage) TProtocol(org.apache.thrift.protocol.TProtocol) PApplicationException(net.morimekta.providence.PApplicationException) PServiceCall(net.morimekta.providence.PServiceCall) TTransport(org.apache.thrift.transport.TTransport) PServiceMethod(net.morimekta.providence.descriptor.PServiceMethod) Nonnull(javax.annotation.Nonnull)

Example 20 with SerializerException

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());
    }
}
Also used : TException(org.apache.thrift.TException) CountingOutputStream(net.morimekta.util.io.CountingOutputStream) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport) SerializerException(net.morimekta.providence.serializer.SerializerException) TTupleProtocol(org.apache.thrift.protocol.TTupleProtocol)

Aggregations

SerializerException (net.morimekta.providence.serializer.SerializerException)26 TException (org.apache.thrift.TException)8 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)8 TTransport (org.apache.thrift.transport.TTransport)8 IOException (java.io.IOException)7 PServiceCall (net.morimekta.providence.PServiceCall)6 File (java.io.File)5 UncheckedIOException (java.io.UncheckedIOException)5 PMessage (net.morimekta.providence.PMessage)5 PField (net.morimekta.providence.descriptor.PField)5 TMessage (org.apache.thrift.protocol.TMessage)5 Nonnull (javax.annotation.Nonnull)4 ArgumentException (net.morimekta.console.args.ArgumentException)4 PApplicationException (net.morimekta.providence.PApplicationException)4 Test (org.junit.Test)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ArgumentParser (net.morimekta.console.args.ArgumentParser)3 CountingOutputStream (net.morimekta.util.io.CountingOutputStream)3 TProtocol (org.apache.thrift.protocol.TProtocol)3 TTupleProtocol (org.apache.thrift.protocol.TTupleProtocol)3