Search in sources :

Example 1 with DynamicSchema

use of com.github.os72.protobuf.dynamic.DynamicSchema in project druid by druid-io.

the class FileBasedProtobufBytesDecoder method getDescriptor.

private Descriptors.Descriptor getDescriptor(String descriptorFilePath) {
    InputStream fin;
    fin = this.getClass().getClassLoader().getResourceAsStream(descriptorFilePath);
    if (fin == null) {
        URL url;
        try {
            url = new URL(descriptorFilePath);
        } catch (MalformedURLException e) {
            throw new ParseException(descriptorFilePath, e, "Descriptor not found in class path or malformed URL:" + descriptorFilePath);
        }
        try {
            fin = url.openConnection().getInputStream();
        } catch (IOException e) {
            throw new ParseException(url.toString(), e, "Cannot read descriptor file: " + url);
        }
    }
    DynamicSchema dynamicSchema;
    try {
        dynamicSchema = DynamicSchema.parseFrom(fin);
    } catch (Descriptors.DescriptorValidationException e) {
        throw new ParseException(null, e, "Invalid descriptor file: " + descriptorFilePath);
    } catch (IOException e) {
        throw new ParseException(null, e, "Cannot read descriptor file: " + descriptorFilePath);
    }
    Set<String> messageTypes = dynamicSchema.getMessageTypes();
    if (messageTypes.size() == 0) {
        throw new ParseException(null, "No message types found in the descriptor: " + descriptorFilePath);
    }
    String messageType = protoMessageType == null ? (String) messageTypes.toArray()[0] : protoMessageType;
    Descriptors.Descriptor desc = dynamicSchema.getMessageDescriptor(messageType);
    if (desc == null) {
        throw new ParseException(null, StringUtils.format("Protobuf message type %s not found in the specified descriptor.  Available messages types are %s", protoMessageType, messageTypes));
    }
    return desc;
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) ParseException(org.apache.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) Descriptors(com.google.protobuf.Descriptors) ByteString(com.google.protobuf.ByteString) URL(java.net.URL)

Aggregations

DynamicSchema (com.github.os72.protobuf.dynamic.DynamicSchema)1 ByteString (com.google.protobuf.ByteString)1 Descriptors (com.google.protobuf.Descriptors)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ParseException (org.apache.druid.java.util.common.parsers.ParseException)1