|
| 1 | +/* |
| 2 | + * Copyright 2025-present 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.cloudevents.transformer; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import io.cloudevents.CloudEventData; |
| 23 | +import io.cloudevents.SpecVersion; |
| 24 | +import io.cloudevents.core.format.EventFormat; |
| 25 | +import io.cloudevents.core.message.MessageWriter; |
| 26 | +import io.cloudevents.rw.CloudEventContextWriter; |
| 27 | +import io.cloudevents.rw.CloudEventRWException; |
| 28 | +import io.cloudevents.rw.CloudEventWriter; |
| 29 | + |
| 30 | +import org.springframework.integration.support.MessageBuilder; |
| 31 | +import org.springframework.messaging.Message; |
| 32 | + |
| 33 | +/** |
| 34 | + * Adapt CloudEvents to Spring Integration {@link Message}s using the CloudEvents SDK |
| 35 | + * {@link MessageWriter} abstraction. |
| 36 | + * Write CloudEvent attributes as message headers with a configurable prefix for |
| 37 | + * binary content mode serialization. Used internally by {@link CloudEventMessageConverter} |
| 38 | + * to convert CloudEvent objects into Spring Integration messages. |
| 39 | + * |
| 40 | + * @author Glenn Renfro |
| 41 | + * |
| 42 | + * @since 7.0 |
| 43 | + * |
| 44 | + * @see CloudEventMessageConverter |
| 45 | + */ |
| 46 | +class MessageBuilderMessageWriter |
| 47 | + implements CloudEventWriter<Message<byte[]>>, MessageWriter<MessageBuilderMessageWriter, Message<byte[]>> { |
| 48 | + |
| 49 | + private final String cloudEventPrefix; |
| 50 | + |
| 51 | + private final String specVersionKey; |
| 52 | + |
| 53 | + private final String dataContentTypeKey; |
| 54 | + |
| 55 | + private final Map<String, Object> headers = new HashMap<>(); |
| 56 | + |
| 57 | + /** |
| 58 | + * Construct a MessageBuilderMessageWriter with the specified configuration. |
| 59 | + * @param cloudEventPrefix the prefix to prepend to CloudEvent attribute names in message headers |
| 60 | + * @param specVersionKey the header name for the CloudEvent specification version |
| 61 | + * @param dataContentTypeKey the header name for the data content type |
| 62 | + * @param headers the base message headers to include in the output message |
| 63 | + */ |
| 64 | + MessageBuilderMessageWriter(String cloudEventPrefix, String specVersionKey, String dataContentTypeKey, Map<String, Object> headers) { |
| 65 | + this.headers.putAll(headers); |
| 66 | + this.cloudEventPrefix = cloudEventPrefix; |
| 67 | + this.specVersionKey = specVersionKey; |
| 68 | + this.dataContentTypeKey = dataContentTypeKey; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Set the event in structured content mode. |
| 73 | + * Create a message with the serialized CloudEvent as the payload and set the |
| 74 | + * data content type header to the format's serialized content type. |
| 75 | + * @param format the event format used to serialize the CloudEvent |
| 76 | + * @param value the serialized CloudEvent bytes |
| 77 | + * @return the Spring Integration message containing the serialized CloudEvent |
| 78 | + * @throws CloudEventRWException if an error occurs during message creation |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public Message<byte[]> setEvent(EventFormat format, byte[] value) throws CloudEventRWException { |
| 82 | + this.headers.put(this.dataContentTypeKey, format.serializedContentType()); |
| 83 | + return MessageBuilder.withPayload(value).copyHeaders(this.headers).build(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Complete the message creation with CloudEvent data. |
| 88 | + * Create a message with the CloudEvent data as the payload. CloudEvent attributes |
| 89 | + * are already set as headers via {@link #withContextAttribute(String, String)}. |
| 90 | + * @param value the CloudEvent data to use as the message payload |
| 91 | + * @return the Spring Integration message with CloudEvent data and attributes |
| 92 | + * @throws CloudEventRWException if an error occurs during message creation |
| 93 | + */ |
| 94 | + @Override |
| 95 | + public Message<byte[]> end(CloudEventData value) throws CloudEventRWException { |
| 96 | + return MessageBuilder.withPayload(value.toBytes()).copyHeaders(this.headers).build(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Complete the message creation without CloudEvent data. |
| 101 | + * Create a message with an empty payload when the CloudEvent contains no data. |
| 102 | + * CloudEvent attributes are set as headers via {@link #withContextAttribute(String, String)}. |
| 103 | + * @return the Spring Integration message with an empty payload and CloudEvent attributes as headers |
| 104 | + */ |
| 105 | + @Override |
| 106 | + public Message<byte[]> end() { |
| 107 | + return MessageBuilder.withPayload(new byte[0]).copyHeaders(this.headers).build(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Add a CloudEvent context attribute to the message headers. |
| 112 | + * Map the CloudEvent attribute to a message header by prepending the configured prefix |
| 113 | + * to the attribute name (e.g., "id" becomes "ce-id" with default prefix). |
| 114 | + * @param name the CloudEvent attribute name |
| 115 | + * @param value the CloudEvent attribute value |
| 116 | + * @return this writer for method chaining |
| 117 | + * @throws CloudEventRWException if an error occurs while setting the attribute |
| 118 | + */ |
| 119 | + @Override |
| 120 | + public CloudEventContextWriter withContextAttribute(String name, String value) throws CloudEventRWException { |
| 121 | + this.headers.put(this.cloudEventPrefix + name, value); |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Initialize the writer with the CloudEvent specification version. |
| 127 | + * Set the specification version as a message header using the configured version key. |
| 128 | + * @param version the CloudEvent specification version |
| 129 | + * @return this writer for method chaining |
| 130 | + */ |
| 131 | + @Override |
| 132 | + public MessageBuilderMessageWriter create(SpecVersion version) { |
| 133 | + this.headers.put(this.specVersionKey, version.toString()); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments