use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException in project registry by hortonworks.
the class ConfluentAvroSerDesHandler method handlePayloadSerialization.
@Override
public void handlePayloadSerialization(OutputStream outputStream, Object input) {
try {
Schema schema = AvroUtils.computeSchema(input);
if (input instanceof byte[]) {
outputStream.write((byte[]) input);
} else {
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
DatumWriter<Object> writer;
if (input instanceof SpecificRecord) {
writer = new SpecificDatumWriter<>(schema);
} else {
writer = new GenericDatumWriter<>(schema);
}
writer.write(input, encoder);
encoder.flush();
}
} catch (IOException e) {
throw new AvroRetryableException("Error serializing Avro message", e);
} catch (RuntimeException e) {
// ClassCastException, etc
throw new AvroException("Error serializing Avro message", e);
}
}
use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException in project registry by hortonworks.
the class ConfluentAvroSerDesHandler method handlePayloadDeserialization.
@Override
public Object handlePayloadDeserialization(InputStream payloadInputStream, Schema writerSchema, Schema readerSchema, boolean useSpecificAvroReader) {
Object deserializedObj;
try {
if (Schema.Type.BYTES.equals(writerSchema.getType())) {
// serializer writes byte array directly without going through avro encoder layers.
deserializedObj = IOUtils.toByteArray(payloadInputStream);
} else {
DatumReader datumReader = getDatumReader(writerSchema, readerSchema, useSpecificAvroReader);
deserializedObj = datumReader.read(null, DecoderFactory.get().binaryDecoder(payloadInputStream, null));
}
} catch (IOException e) {
throw new AvroRetryableException("Error deserializing Avro message for id " + writerSchema, e);
} catch (RuntimeException e) {
// avro deserialization may throw AvroRuntimeException, NullPointerException, etc
throw new AvroException("Error deserializing Avro message for id " + writerSchema, e);
}
return deserializedObj;
}
use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException in project registry by hortonworks.
the class DefaultAvroSerDesHandler method handlePayloadDeserialization.
@Override
public Object handlePayloadDeserialization(InputStream payloadInputStream, Schema writerSchema, Schema readerSchema, boolean useSpecificAvroReader) {
Object deserializedObj;
Schema.Type writerSchemaType = writerSchema.getType();
try {
if (Schema.Type.BYTES.equals(writerSchemaType)) {
// serializer writes byte array directly without going through avro encoder layers.
deserializedObj = IOUtils.toByteArray(payloadInputStream);
} else if (Schema.Type.STRING.equals(writerSchemaType)) {
// generate UTF-8 string object from the received bytes.
deserializedObj = new String(IOUtils.toByteArray(payloadInputStream), AvroUtils.UTF_8);
} else {
DatumReader datumReader = getDatumReader(writerSchema, readerSchema, useSpecificAvroReader);
deserializedObj = datumReader.read(null, DecoderFactory.get().binaryDecoder(payloadInputStream, null));
}
} catch (IOException e) {
throw new AvroRetryableException(e);
} catch (Exception e) {
throw new AvroException(e);
}
return deserializedObj;
}
use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException in project registry by hortonworks.
the class AvroSnapshotDeserializer method retrieveProtocolId.
protected byte retrieveProtocolId(InputStream inputStream) throws SerDesException {
// first byte is protocol version/id.
// protocol format:
// 1 byte : protocol version
byte protocolId;
try {
protocolId = (byte) inputStream.read();
} catch (IOException e) {
throw new AvroRetryableException(e);
}
if (protocolId == -1) {
throw new AvroException("End of stream reached while trying to read protocol id");
}
checkProtocolHandlerExists(protocolId);
return protocolId;
}
use of com.hortonworks.registries.schemaregistry.serdes.avro.exceptions.AvroException in project registry by hortonworks.
the class ConfluentAvroSerDesHandler method getReaderSchema.
private Schema getReaderSchema(Schema writerSchema) {
Schema readerSchema = this.readerSchemaCache.get(writerSchema.getFullName());
if (readerSchema == null) {
Class readerClass = SpecificData.get().getClass(writerSchema);
if (readerClass == null) {
throw new AvroException("Could not find class " + writerSchema.getFullName() + " specified in writer\'s schema whilst finding reader\'s schema for a SpecificRecord.");
}
try {
readerSchema = ((SpecificRecord) readerClass.newInstance()).getSchema();
} catch (InstantiationException e) {
throw new AvroException(writerSchema.getFullName() + " specified by the " + "writers schema could not be instantiated to find the readers schema.");
} catch (IllegalAccessException e) {
throw new AvroException(writerSchema.getFullName() + " specified by the " + "writers schema is not allowed to be instantiated to find the readers schema.");
}
this.readerSchemaCache.put(writerSchema.getFullName(), readerSchema);
}
return readerSchema;
}
Aggregations