|
| 1 | +/* |
| 2 | + * Copyright 2023 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 org.springframework.beans.factory.BeanClassLoaderAware; |
| 20 | +import org.springframework.expression.EvaluationContext; |
| 21 | +import org.springframework.expression.Expression; |
| 22 | +import org.springframework.integration.context.IntegrationContextUtils; |
| 23 | +import org.springframework.integration.expression.FunctionExpression; |
| 24 | +import org.springframework.integration.expression.ValueExpression; |
| 25 | +import org.springframework.integration.transformer.support.ProtoHeaders; |
| 26 | +import org.springframework.messaging.Message; |
| 27 | +import org.springframework.messaging.converter.ProtobufMessageConverter; |
| 28 | +import org.springframework.util.Assert; |
| 29 | +import org.springframework.util.ClassUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * A Protocol Buffer transformer to instantiate {@link com.google.protobuf.Message} objects |
| 33 | + * from either {@code byte[]} if content type is {@code application/x-protobuf} |
| 34 | + * or from {@code String} in case of {@code application/json} content type. |
| 35 | + * |
| 36 | + * @author Christian Tzolov |
| 37 | + * |
| 38 | + * @since 6.1 |
| 39 | + */ |
| 40 | +public class FromProtobufTransformer extends AbstractTransformer implements BeanClassLoaderAware { |
| 41 | + |
| 42 | + private final ProtobufMessageConverter protobufMessageConverter; |
| 43 | + |
| 44 | + private ClassLoader beanClassLoader; |
| 45 | + |
| 46 | + private Expression expectedTypeExpression = |
| 47 | + new FunctionExpression<Message<?>>((message) -> message.getHeaders().get(ProtoHeaders.TYPE)); |
| 48 | + |
| 49 | + private EvaluationContext evaluationContext; |
| 50 | + |
| 51 | + /** |
| 52 | + * Construct an instance with the supplied default type to create. |
| 53 | + */ |
| 54 | + public FromProtobufTransformer() { |
| 55 | + this(new ProtobufMessageConverter()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Construct an instance with the supplied default type and ProtobufMessageConverter instance. |
| 60 | + * @param protobufMessageConverter the message converter used. |
| 61 | + */ |
| 62 | + public FromProtobufTransformer(ProtobufMessageConverter protobufMessageConverter) { |
| 63 | + Assert.notNull(protobufMessageConverter, "'protobufMessageConverter' must not be null"); |
| 64 | + this.protobufMessageConverter = protobufMessageConverter; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void setBeanClassLoader(ClassLoader classLoader) { |
| 69 | + this.beanClassLoader = classLoader; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Set an expected protobuf class type. |
| 74 | + * Mutually exclusive with {@link #setExpectedTypeExpression} and |
| 75 | + * {@link #setExpectedTypeExpressionString}. |
| 76 | + * @param expectedType expected protobuf class type. |
| 77 | + * @return updated FromProtobufTransformer instance. |
| 78 | + */ |
| 79 | + public FromProtobufTransformer setExpectedType(Class<? extends com.google.protobuf.Message> expectedType) { |
| 80 | + return setExpectedTypeExpression(new ValueExpression<>(expectedType)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Set an expression to evaluate against the message to determine the type id. |
| 85 | + * Defaults to{@code headers['proto_type']}. Mutually exclusive with |
| 86 | + * {@link #setExpectedType} and {@link #setExpectedTypeExpression}. |
| 87 | + * @param expression the expression. |
| 88 | + * @return updated FromProtobufTransformer instance. |
| 89 | + */ |
| 90 | + public FromProtobufTransformer setExpectedTypeExpressionString(String expression) { |
| 91 | + return setExpectedTypeExpression(EXPRESSION_PARSER.parseExpression(expression)); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Set an expression to evaluate against the message to determine the type. |
| 96 | + * Default {@code headers['proto_type']}. |
| 97 | + * Mutually exclusive with {@link #setExpectedType} and |
| 98 | + * {@link #setExpectedTypeExpressionString}. |
| 99 | + * @param expression the expression. |
| 100 | + * @return updated FromProtobufTransformer instance. |
| 101 | + */ |
| 102 | + public FromProtobufTransformer setExpectedTypeExpression(Expression expression) { |
| 103 | + Assert.notNull(expression, "'expression' must not be null"); |
| 104 | + this.expectedTypeExpression = expression; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected void onInit() { |
| 110 | + this.evaluationContext = IntegrationContextUtils.getEvaluationContext(getBeanFactory()); |
| 111 | + } |
| 112 | + |
| 113 | + @SuppressWarnings("unchecked") |
| 114 | + @Override |
| 115 | + protected Object doTransform(Message<?> message) { |
| 116 | + Class<? extends com.google.protobuf.Message> targetClass = null; |
| 117 | + Object value = this.expectedTypeExpression.getValue(this.evaluationContext, message); |
| 118 | + if (value instanceof Class<?>) { |
| 119 | + targetClass = (Class<? extends com.google.protobuf.Message>) value; |
| 120 | + } |
| 121 | + else if (value instanceof String) { |
| 122 | + try { |
| 123 | + targetClass = |
| 124 | + (Class<? extends com.google.protobuf.Message>) |
| 125 | + ClassUtils.forName((String) value, this.beanClassLoader); |
| 126 | + } |
| 127 | + catch (ClassNotFoundException | LinkageError e) { |
| 128 | + throw new IllegalStateException(e); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (targetClass == null) { |
| 133 | + throw new MessageTransformationException(message, |
| 134 | + "The 'expectedTypeExpression' (" + this.expectedTypeExpression + ") returned 'null'."); |
| 135 | + } |
| 136 | + |
| 137 | + return this.protobufMessageConverter.fromMessage(message, targetClass); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments