|
| 1 | +/* |
| 2 | + * Copyright 2020 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.net.URI; |
| 20 | +import java.time.ZonedDateTime; |
| 21 | +import java.util.UUID; |
| 22 | + |
| 23 | +import org.springframework.expression.EvaluationContext; |
| 24 | +import org.springframework.expression.Expression; |
| 25 | +import org.springframework.integration.StaticMessageHeaderAccessor; |
| 26 | +import org.springframework.integration.expression.ExpressionUtils; |
| 27 | +import org.springframework.integration.expression.FunctionExpression; |
| 28 | +import org.springframework.integration.support.cloudevents.Marshallers; |
| 29 | +import org.springframework.lang.Nullable; |
| 30 | +import org.springframework.messaging.Message; |
| 31 | +import org.springframework.messaging.MessageHeaders; |
| 32 | +import org.springframework.util.Assert; |
| 33 | +import org.springframework.util.MimeType; |
| 34 | + |
| 35 | +import io.cloudevents.CloudEvent; |
| 36 | +import io.cloudevents.extensions.ExtensionFormat; |
| 37 | +import io.cloudevents.format.Wire; |
| 38 | +import io.cloudevents.format.builder.EventStep; |
| 39 | +import io.cloudevents.v1.AttributesImpl; |
| 40 | +import io.cloudevents.v1.CloudEventBuilder; |
| 41 | +import io.cloudevents.v1.CloudEventImpl; |
| 42 | + |
| 43 | +/** |
| 44 | + * An {@link AbstractTransformer} implementation to build a cloud event |
| 45 | + * from the request message. |
| 46 | + * <p> |
| 47 | + * This transformer may produce a message according a {@link ToCloudEventTransformer.Result} option. |
| 48 | + * By default it is a {@link ToCloudEventTransformer.Result#RAW} |
| 49 | + * with the meaning to produce a {@link io.cloudevents.CloudEvent} |
| 50 | + * instance as a reply message payload. |
| 51 | + * <p> |
| 52 | + * A {@link ToCloudEventTransformer.Result#BINARY} mode produces a marshalled into a {@code byte[]} |
| 53 | + * a built {@link io.cloudevents.CloudEvent} body and respective cloud event headers. |
| 54 | + * <p> |
| 55 | + * A {@link ToCloudEventTransformer.Result#STRUCTURED} mode produces a marshalled into a {@code byte[]} |
| 56 | + * a whole {@link io.cloudevents.CloudEvent} and respective content type header |
| 57 | + * with the {@code "application/cloudevents+json"} value. |
| 58 | + * |
| 59 | + * @author Artem Bilan |
| 60 | + * |
| 61 | + * @since 5.3 |
| 62 | + */ |
| 63 | +public class ToCloudEventTransformer extends AbstractTransformer { |
| 64 | + |
| 65 | + public enum Result { |
| 66 | + |
| 67 | + RAW, BINARY, STRUCTURED |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + private final URI source; |
| 72 | + |
| 73 | + @Nullable |
| 74 | + private final EventStep<AttributesImpl, Object, byte[], String> wireBuilder; |
| 75 | + |
| 76 | + private Expression typeExpression = |
| 77 | + new FunctionExpression<Message<?>>((message) -> message.getPayload().getClass().getName()); |
| 78 | + |
| 79 | + @Nullable |
| 80 | + private Expression subjectExpression; |
| 81 | + |
| 82 | + @Nullable |
| 83 | + private Expression dataSchemaExpression; |
| 84 | + |
| 85 | + @Nullable |
| 86 | + private Expression extensionExpression; |
| 87 | + |
| 88 | + private EvaluationContext evaluationContext; |
| 89 | + |
| 90 | + public ToCloudEventTransformer(URI source) { |
| 91 | + this(source, Result.RAW); |
| 92 | + } |
| 93 | + |
| 94 | + public ToCloudEventTransformer(URI source, Result resultMode) { |
| 95 | + Assert.notNull(source, "'source' must not be null"); |
| 96 | + Assert.notNull(resultMode, "'resultMode' must not be null"); |
| 97 | + this.source = source; |
| 98 | + switch (resultMode) { |
| 99 | + case BINARY: |
| 100 | + this.wireBuilder = Marshallers.binary(); |
| 101 | + break; |
| 102 | + case STRUCTURED: |
| 103 | + this.wireBuilder = Marshallers.structured(); |
| 104 | + break; |
| 105 | + default: |
| 106 | + this.wireBuilder = null; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public void setTypeExpression(Expression typeExpression) { |
| 111 | + Assert.notNull(source, "'typeExpression' must not be null"); |
| 112 | + this.typeExpression = typeExpression; |
| 113 | + } |
| 114 | + |
| 115 | + public void setSubjectExpression(@Nullable Expression subjectExpression) { |
| 116 | + this.subjectExpression = subjectExpression; |
| 117 | + } |
| 118 | + |
| 119 | + public void setDataSchemaExpression(@Nullable Expression dataSchemaExpression) { |
| 120 | + this.dataSchemaExpression = dataSchemaExpression; |
| 121 | + } |
| 122 | + |
| 123 | + public void setExtensionExpression(@Nullable Expression extensionExpression) { |
| 124 | + this.extensionExpression = extensionExpression; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected void onInit() { |
| 129 | + super.onInit(); |
| 130 | + this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory()); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + protected Object doTransform(Message<?> message) { |
| 135 | + CloudEventImpl<Object> cloudEvent = buildCloudEvent(message); |
| 136 | + |
| 137 | + if (this.wireBuilder != null) { |
| 138 | + Wire<byte[], String, String> wire = |
| 139 | + this.wireBuilder.withEvent(() -> cloudEvent) |
| 140 | + .marshal(); |
| 141 | + |
| 142 | + return getMessageBuilderFactory() |
| 143 | + .withPayload(wire.getPayload().orElse(new byte[0])) |
| 144 | + .copyHeaders(wire.getHeaders()) |
| 145 | + .copyHeadersIfAbsent(message.getHeaders()) |
| 146 | + .build(); |
| 147 | + } |
| 148 | + else { |
| 149 | + return cloudEvent; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @SuppressWarnings("unchecked") |
| 154 | + private CloudEventImpl<Object> buildCloudEvent(Message<?> message) { |
| 155 | + MessageHeaders headers = message.getHeaders(); |
| 156 | + Object payload = message.getPayload(); |
| 157 | + |
| 158 | + CloudEventBuilder<Object> cloudEventBuilder = |
| 159 | + payload instanceof CloudEvent |
| 160 | + ? CloudEventBuilder.builder((CloudEvent<AttributesImpl, Object>) payload) |
| 161 | + : CloudEventBuilder.builder(); |
| 162 | + |
| 163 | + cloudEventBuilder.withId(headers.getId() != null |
| 164 | + ? headers.getId().toString() |
| 165 | + : UUID.randomUUID().toString()) |
| 166 | + .withTime(ZonedDateTime.now()) |
| 167 | + .withSource(this.source) |
| 168 | + .withType(this.typeExpression.getValue(this.evaluationContext, message, String.class)); |
| 169 | + |
| 170 | + if (!(payload instanceof CloudEvent)) { |
| 171 | + if (payload instanceof byte[]) { |
| 172 | + cloudEventBuilder.withDataBase64((byte[]) payload); |
| 173 | + } |
| 174 | + else { |
| 175 | + cloudEventBuilder.withData(payload); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + MimeType contentType = StaticMessageHeaderAccessor.getContentType(message); |
| 180 | + |
| 181 | + if (contentType != null) { |
| 182 | + cloudEventBuilder.withDataContentType(contentType.toString()); |
| 183 | + } |
| 184 | + |
| 185 | + if (this.subjectExpression != null) { |
| 186 | + cloudEventBuilder.withSubject( |
| 187 | + this.subjectExpression.getValue(this.evaluationContext, message, String.class)); |
| 188 | + } |
| 189 | + |
| 190 | + if (this.dataSchemaExpression != null) { |
| 191 | + cloudEventBuilder.withDataschema( |
| 192 | + this.dataSchemaExpression.getValue(this.evaluationContext, message, URI.class)); |
| 193 | + } |
| 194 | + |
| 195 | + if (this.extensionExpression != null) { |
| 196 | + cloudEventBuilder.withExtension( |
| 197 | + this.extensionExpression.getValue(this.evaluationContext, message, ExtensionFormat.class)); |
| 198 | + } |
| 199 | + |
| 200 | + return cloudEventBuilder.build(); |
| 201 | + } |
| 202 | + |
| 203 | +} |
0 commit comments