|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.transformer; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.UncheckedIOException; |
| 21 | + |
| 22 | +import org.apache.avro.io.DatumReader; |
| 23 | +import org.apache.avro.io.DecoderFactory; |
| 24 | +import org.apache.avro.specific.SpecificDatumReader; |
| 25 | +import org.apache.avro.specific.SpecificRecord; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.BeanClassLoaderAware; |
| 28 | +import org.springframework.expression.EvaluationContext; |
| 29 | +import org.springframework.expression.Expression; |
| 30 | +import org.springframework.integration.context.IntegrationContextUtils; |
| 31 | +import org.springframework.integration.expression.FunctionExpression; |
| 32 | +import org.springframework.integration.transformer.support.AvroHeaders; |
| 33 | +import org.springframework.messaging.Message; |
| 34 | +import org.springframework.util.Assert; |
| 35 | +import org.springframework.util.ClassUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * An Apache Avro transformer to create generated {@link SpecificRecord} objects |
| 39 | + * from {@code byte[]}. |
| 40 | + * |
| 41 | + * @author Gary Russell |
| 42 | + * @since 5.2 |
| 43 | + * |
| 44 | + */ |
| 45 | +public class SimpleFromAvroTransformer extends AbstractTransformer implements BeanClassLoaderAware { |
| 46 | + |
| 47 | + private final Class<? extends SpecificRecord> defaultType; |
| 48 | + |
| 49 | + private final DecoderFactory decoderFactory = new DecoderFactory(); |
| 50 | + |
| 51 | + private Expression typeIdExpression = new FunctionExpression<Message<?>>( |
| 52 | + msg -> msg.getHeaders().get(AvroHeaders.TYPE)); |
| 53 | + |
| 54 | + private EvaluationContext evaluationContext; |
| 55 | + |
| 56 | + private ClassLoader beanClassLoader; |
| 57 | + |
| 58 | + /** |
| 59 | + * Construct an instance with the supplied default type to create. |
| 60 | + * @param defaultType the type. |
| 61 | + */ |
| 62 | + public SimpleFromAvroTransformer(Class<? extends SpecificRecord> defaultType) { |
| 63 | + Assert.notNull(defaultType, "'defaultType' must not be null"); |
| 64 | + this.defaultType = defaultType; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void setBeanClassLoader(ClassLoader classLoader) { |
| 69 | + this.beanClassLoader = classLoader; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Set the expression to evaluate against the message to determine the type. |
| 74 | + * Default {@code headers['avro_type']}. |
| 75 | + * @param expression the expression. |
| 76 | + * @return the transformer |
| 77 | + */ |
| 78 | + public SimpleFromAvroTransformer typeExpression(Expression expression) { |
| 79 | + Assert.notNull(expression, "'expression' must not be null"); |
| 80 | + this.typeIdExpression = expression; |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Set the expression to evaluate against the message to determine the type id. |
| 86 | + * Default {@code headers['avro_type']}. |
| 87 | + * @param expression the expression. |
| 88 | + * @return the transformer |
| 89 | + */ |
| 90 | + public SimpleFromAvroTransformer typeExpression(String expression) { |
| 91 | + Assert.notNull(expression, "'expression' must not be null"); |
| 92 | + this.typeIdExpression = EXPRESSION_PARSER.parseExpression(expression); |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Set the expression to evaluate against the message to determine the type. |
| 98 | + * Default {@code headers['avro_type']}. |
| 99 | + * @param expression the expression. |
| 100 | + */ |
| 101 | + public void setTypeExpression(Expression expression) { |
| 102 | + Assert.notNull(expression, "'expression' must not be null"); |
| 103 | + this.typeIdExpression = expression; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Set the expression to evaluate against the message to determine the type id. |
| 108 | + * Default {@code headers['avro_type']}. |
| 109 | + * @param expression the expression. |
| 110 | + */ |
| 111 | + public void setTypeExpression(String expression) { |
| 112 | + Assert.notNull(expression, "'expression' must not be null"); |
| 113 | + this.typeIdExpression = EXPRESSION_PARSER.parseExpression(expression); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected void onInit() { |
| 118 | + this.evaluationContext = IntegrationContextUtils.getEvaluationContext(getBeanFactory()); |
| 119 | + } |
| 120 | + |
| 121 | + @SuppressWarnings("unchecked") |
| 122 | + @Override |
| 123 | + protected Object doTransform(Message<?> message) { |
| 124 | + Assert.state(message.getPayload() instanceof byte[], "Payload must be a byte[]"); |
| 125 | + Class<? extends SpecificRecord> type = null; |
| 126 | + Object value = this.typeIdExpression.getValue(this.evaluationContext, message); |
| 127 | + if (value instanceof Class) { |
| 128 | + type = (Class<? extends SpecificRecord>) value; |
| 129 | + } |
| 130 | + else if (value instanceof String) { |
| 131 | + try { |
| 132 | + type = (Class<? extends SpecificRecord>) ClassUtils.forName((String) value, this.beanClassLoader); |
| 133 | + } |
| 134 | + catch (ClassNotFoundException | LinkageError e) { |
| 135 | + throw new IllegalStateException(e); |
| 136 | + } |
| 137 | + } |
| 138 | + if (type == null) { |
| 139 | + type = this.defaultType; |
| 140 | + } |
| 141 | + DatumReader<?> reader = new SpecificDatumReader<>(type); |
| 142 | + try { |
| 143 | + return reader.read(null, this.decoderFactory.binaryDecoder((byte[]) message.getPayload(), null)); |
| 144 | + } |
| 145 | + catch (IOException e) { |
| 146 | + throw new UncheckedIOException(e); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | +} |
0 commit comments