public void convert()

in library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/format/converter/pojo/JavaObjectDataType.java [51:85]


    public void convert(Exchange exchange) {
        ObjectHelper.notNull(camelContext, "camelContext");

        FormatSchema schema = exchange.getProperty(SchemaHelper.CONTENT_SCHEMA, FormatSchema.class);
        if (schema == null) {
            throw new CamelExecutionException("Missing proper schema for Java object data type processing", exchange);
        }

        String contentClass = SchemaHelper.resolveContentClass(exchange, null);
        if (contentClass == null) {
            throw new CamelExecutionException("Missing content class information for Java object data type processing",
                    exchange);
        }

        SchemaType schemaType = SchemaType.of(exchange.getProperty(SchemaHelper.CONTENT_SCHEMA_TYPE, SchemaType.JSON.type(), String.class));

        try {
            Class<?> contentType = camelContext.getClassResolver().resolveMandatoryClass(contentClass);
            Object unmarshalled;

            if (schemaType == SchemaType.AVRO) {
                unmarshalled = Avro.MAPPER.reader().forType(contentType).with(schema).readValue(getBodyAsStream(exchange));
            } else if (schemaType == SchemaType.JSON) {
                unmarshalled = Json.MAPPER.reader().forType(contentType).with(schema).readValue(getBodyAsStream(exchange));
            } else {
                throw new CamelExecutionException(String.format("Unsupported schema type '%s'", schemaType), exchange);
            }

            exchange.getMessage().setBody(unmarshalled);

            exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, MimeType.STRUCT.type());
        } catch (InvalidPayloadException | IOException | ClassNotFoundException e) {
            throw new CamelExecutionException("Failed to apply Java object data type on exchange", exchange, e);
        }
    }