diff options
Diffstat (limited to 'plugins/plugins-context/context-schema')
-rw-r--r-- | plugins/plugins-context/context-schema/context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java | 82 |
1 files changed, 48 insertions, 34 deletions
diff --git a/plugins/plugins-context/context-schema/context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java b/plugins/plugins-context/context-schema/context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java index 831962042..b4cc38602 100644 --- a/plugins/plugins-context/context-schema/context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java +++ b/plugins/plugins-context/context-schema/context-schema-avro/src/main/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaHelper.java @@ -127,19 +127,42 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { return object; } - // Check that the incoming object is a string, the incoming object must be a string - // containing Json - String objectString; + String objectString = getStringObject(object); + + // Translate illegal characters in incoming JSON keys to legal Avro values + objectString = AvroSchemaKeyTranslationUtilities.translateIllegalKeys(objectString, false); + + // Decode the object + Object decodedObject; try { - if (object == null) { - objectString = null; - } - if (object != null && avroSchema.getType().equals(Schema.Type.STRING)) { - objectString = object.toString().trim(); + final JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(avroSchema, objectString); + decodedObject = new GenericDatumReader<GenericRecord>(avroSchema).read(null, jsonDecoder); + } catch (final Exception e) { + final String returnString = getUserKey().getID() + ": object \"" + objectString + + "\" Avro unmarshalling failed: " + e.getMessage(); + LOGGER.warn(returnString, e); + throw new ContextRuntimeException(returnString, e); + } + + // Now map the decoded object into something we can handle + return avroObjectMapper.mapFromAvro(decodedObject); + } + + /** + * Check that the incoming object is a string, the incoming object must be a string containing + * Json + * + * @param object incoming object + * @return object as String + */ + private String getStringObject(final Object object) { + try { + if (isObjectString(object)) { + String objectString = object.toString().trim(); if (objectString.length() == 0) { - objectString = "\"\""; + return "\"\""; } else if (objectString.length() == 1) { - objectString = "\"" + objectString + "\""; + return "\"" + objectString + "\""; } else { // All strings must be quoted for decoding if (objectString.charAt(0) != '"') { @@ -149,35 +172,22 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { objectString += '"'; } } + return objectString; } else { - objectString = (String) object; + return (String) object; } } catch (final ClassCastException e) { final String returnString = getUserKey().getID() + ": object \"" + object + "\" of type \"" - + object.getClass().getCanonicalName() + "\" must be assignable to \"" + + (object != null ? object.getClass().getCanonicalName() : "null") + "\" must be assignable to \"" + getSchemaClass().getCanonicalName() + "\" or be a Json string representation of it for Avro unmarshalling"; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } + } - // Translate illegal characters in incoming JSON keys to legal Avro values - objectString = AvroSchemaKeyTranslationUtilities.translateIllegalKeys(objectString, false); - - // Decode the object - Object decodedObject; - try { - final JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(avroSchema, objectString); - decodedObject = new GenericDatumReader<GenericRecord>(avroSchema).read(null, jsonDecoder); - } catch (final Exception e) { - final String returnString = getUserKey().getID() + ": object \"" + objectString - + "\" Avro unmarshalling failed: " + e.getMessage(); - LOGGER.warn(returnString, e); - throw new ContextRuntimeException(returnString, e); - } - - // Now map the decoded object into something we can handle - return avroObjectMapper.mapFromAvro(decodedObject); + private boolean isObjectString(final Object object) { + return object != null && avroSchema.getType().equals(Schema.Type.STRING); } @Override @@ -185,21 +195,25 @@ public class AvroSchemaHelper extends AbstractSchemaHelper { // Condition the object for Avro encoding final Object conditionedObject = avroObjectMapper.mapToAvro(object); - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - try { + final String jsonString = getJsonString(object, conditionedObject); + + return AvroSchemaKeyTranslationUtilities.translateIllegalKeys(jsonString, true); + } + + private String getJsonString(final Object object, final Object conditionedObject) { + + try (final ByteArrayOutputStream output = new ByteArrayOutputStream();) { final DatumWriter<Object> writer = new GenericDatumWriter<>(avroSchema); final JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(avroSchema, output, true); writer.write(conditionedObject, jsonEncoder); jsonEncoder.flush(); - output.close(); + return new String(output.toByteArray()); } catch (final Exception e) { final String returnString = getUserKey().getID() + ": object \"" + object + "\" Avro marshalling failed: " + e.getMessage(); LOGGER.warn(returnString); throw new ContextRuntimeException(returnString, e); } - - return AvroSchemaKeyTranslationUtilities.translateIllegalKeys(new String(output.toByteArray()), true); } @Override |