Search in sources :

Example 21 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class TTupleProtocolSerializer method readMessage.

private <Message extends PMessage<Message, Field>, Field extends PField> Message readMessage(TTupleProtocol protocol, PMessageDescriptor<Message, Field> descriptor) throws SerializerException, TException {
    PMessageBuilder<Message, Field> builder = descriptor.builder();
    if (descriptor.getVariant() == PMessageVariant.UNION) {
        int fieldId = protocol.readI16();
        PField fld = descriptor.findFieldById(fieldId);
        if (fld != null) {
            builder.set(fld.getId(), readTypedValue(fld.getDescriptor(), protocol));
        } else {
            throw new SerializerException("Unable to read unknown union field " + fieldId + " in " + descriptor.getQualifiedName());
        }
    } else {
        PField[] fields = descriptor.getFields();
        int numOptionals = countOptionals(fields);
        BitSet optionals = null;
        int optionalPos = 0;
        for (PField fld : fields) {
            if (fld.getRequirement() == PRequirement.REQUIRED) {
                builder.set(fld.getId(), readTypedValue(fld.getDescriptor(), protocol));
            } else {
                if (optionals == null) {
                    optionals = protocol.readBitSet(numOptionals);
                }
                if (optionals.get(optionalPos)) {
                    builder.set(fld.getId(), readTypedValue(fld.getDescriptor(), protocol));
                }
                ++optionalPos;
            }
        }
    }
    if (strict) {
        try {
            builder.validate();
        } catch (IllegalStateException e) {
            throw new SerializerException(e, e.getMessage());
        }
    }
    return builder.build();
}
Also used : PField(net.morimekta.providence.descriptor.PField) PMessage(net.morimekta.providence.PMessage) TMessage(org.apache.thrift.protocol.TMessage) PField(net.morimekta.providence.descriptor.PField) BitSet(java.util.BitSet) SerializerException(net.morimekta.providence.serializer.SerializerException)

Example 22 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class TTupleProtocolSerializer 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 {
    TMessage tm = null;
    PServiceCallType type = null;
    try {
        TTransport transport = new TIOStreamTransport(input);
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        tm = protocol.readMessageBegin();
        type = PServiceCallType.findById(tm.type);
        if (type == null) {
            throw new SerializerException("Unknown call type for id " + tm.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());
        }
        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, "Unable to serialize into transport protocol").setExceptionType(PApplicationExceptionType.findById(e.getType())).setCallType(type).setMethodName(tm != null ? tm.name : "").setSequenceNo(tm != null ? tm.seqid : 0);
    } catch (TException e) {
        throw new SerializerException(e, "Transport exception in protocol").setExceptionType(PApplicationExceptionType.PROTOCOL_ERROR).setCallType(type).setMethodName(tm != null ? tm.name : "").setSequenceNo(tm != null ? tm.seqid : 0);
    }
}
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) TTupleProtocol(org.apache.thrift.protocol.TTupleProtocol) PField(net.morimekta.providence.descriptor.PField) TMessage(org.apache.thrift.protocol.TMessage) 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 23 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class TTupleProtocolSerializer method deserialize.

@Nonnull
@Override
public <Message extends PMessage<Message, Field>, Field extends PField> Message deserialize(@Nonnull InputStream input, @Nonnull PMessageDescriptor<Message, Field> descriptor) throws IOException {
    try {
        TTransport transport = new TIOStreamTransport(input);
        TTupleProtocol protocol = (TTupleProtocol) protocolFactory.getProtocol(transport);
        return readMessage(protocol, descriptor);
    } catch (TTransportException e) {
        throw new SerializerException(e, "Unable to serialize into transport protocol");
    } catch (TException e) {
        throw new SerializerException(e, "Transport exception in protocol");
    }
}
Also used : TException(org.apache.thrift.TException) TTransportException(org.apache.thrift.transport.TTransportException) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TTransport(org.apache.thrift.transport.TTransport) SerializerException(net.morimekta.providence.serializer.SerializerException) TTupleProtocol(org.apache.thrift.protocol.TTupleProtocol) Nonnull(javax.annotation.Nonnull)

Example 24 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class FormatUtils method collectConfigIncludes.

public static void collectConfigIncludes(File rc, Map<String, File> includes) throws IOException {
    if (!rc.exists()) {
        return;
    }
    rc = rc.getCanonicalFile();
    if (!rc.isFile()) {
        throw new ProvidenceConfigException("Rc file is not a file " + rc.getPath());
    }
    try {
        SimpleTypeRegistry registry = new SimpleTypeRegistry();
        registry.registerRecursively(ProvidenceTools.kDescriptor);
        ProvidenceConfig loader = new ProvidenceConfig(registry);
        ProvidenceTools config = loader.getConfig(rc);
        if (config.hasIncludes()) {
            File basePath = rc.getParentFile();
            if (config.hasIncludesBasePath()) {
                String base = config.getIncludesBasePath();
                if (base.charAt(0) == '~') {
                    base = System.getenv("HOME") + base.substring(1);
                }
                basePath = new File(base);
                if (!basePath.exists() || !basePath.isDirectory()) {
                    throw new ProvidenceConfigException("Includes Base path in " + rc.getPath() + " is not a directory: " + basePath);
                }
            }
            for (String path : config.getIncludes()) {
                File include = new File(basePath, path);
                collectIncludes(include, includes);
            }
        }
    } catch (SerializerException e) {
        System.err.println("Config error: " + e.getMessage());
        System.err.println(e.asString());
        System.err.println();
        throw new ArgumentException(e, "Exception when parsing " + rc.getCanonicalFile());
    }
}
Also used : SimpleTypeRegistry(net.morimekta.providence.util.SimpleTypeRegistry) ProvidenceTools(net.morimekta.providence.tools.common.ProvidenceTools) ArgumentException(net.morimekta.console.args.ArgumentException) ProvidenceConfig(net.morimekta.providence.config.ProvidenceConfig) File(java.io.File) SerializerException(net.morimekta.providence.serializer.SerializerException) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException)

Example 25 with SerializerException

use of net.morimekta.providence.serializer.SerializerException in project providence by morimekta.

the class JsonGenerator method generate.

@Override
public void generate(ProgramTypeRegistry registry) throws IOException, GeneratorException {
    ProgramType doc = registry.getProgramType();
    if (doc.hasIncludes()) {
        doc = doc.mutate().setIncludes(doc.getIncludes().stream().map(path -> path.replaceAll("(\\.thrift)$", ".json")).collect(Collectors.toList())).build();
    }
    OutputStream out = getFileManager().create(null, doc.getProgramName() + ".json");
    try {
        serializer.serialize(out, doc);
        out.write('\n');
    } catch (SerializerException e) {
        throw new GeneratorException("Unable to serialize document.", e);
    }
    getFileManager().finalize(out);
}
Also used : OutputStream(java.io.OutputStream) ProgramType(net.morimekta.providence.model.ProgramType) GeneratorException(net.morimekta.providence.generator.GeneratorException) SerializerException(net.morimekta.providence.serializer.SerializerException)

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