|
| 1 | +/* |
| 2 | + * Copyright 2018-Present The CloudEvents Authors |
| 3 | + * <p> |
| 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 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 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 | + |
| 18 | +package io.cloudevents.rocketmq; |
| 19 | + |
| 20 | +import io.cloudevents.core.message.MessageReader; |
| 21 | +import io.cloudevents.core.message.MessageWriter; |
| 22 | +import io.cloudevents.core.message.impl.GenericStructuredMessageReader; |
| 23 | +import io.cloudevents.core.message.impl.MessageUtils; |
| 24 | +import io.cloudevents.rw.CloudEventWriter; |
| 25 | +import java.nio.ByteBuffer; |
| 26 | +import java.util.Map; |
| 27 | +import org.apache.rocketmq.client.apis.message.Message; |
| 28 | +import org.apache.rocketmq.client.apis.message.MessageView; |
| 29 | + |
| 30 | +/** |
| 31 | + * A factory class providing convenience methods for creating {@link MessageReader} and {@link MessageWriter} instances |
| 32 | + * based on RocketMQ {@link MessageView} and {@link Message}. |
| 33 | + */ |
| 34 | +public class RocketMqMessageFactory { |
| 35 | + private RocketMqMessageFactory() { |
| 36 | + // prevent instantiation |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a {@link MessageReader} to read a RocketMQ {@link MessageView}. |
| 41 | + * |
| 42 | + * @param message The RocketMQ {@link MessageView} to read from. |
| 43 | + * @return A {@link MessageReader} that can read the given {@link MessageView} to a {@link io.cloudevents.CloudEvent} representation. |
| 44 | + */ |
| 45 | + public static MessageReader createReader(final MessageView message) { |
| 46 | + final ByteBuffer byteBuffer = message.getBody(); |
| 47 | + byte[] body = new byte[byteBuffer.remaining()]; |
| 48 | + byteBuffer.get(body); |
| 49 | + final Map<String, String> properties = message.getProperties(); |
| 50 | + final String contentType = properties.get(RocketmqConstants.PROPERTY_CONTENT_TYPE); |
| 51 | + return createReader(contentType, properties, body); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Creates a {@link MessageReader} using the content type, properties, and body of a RocketMQ {@link MessageView}. |
| 56 | + * |
| 57 | + * @param contentType The content type of the message payload. |
| 58 | + * @param properties The properties of the RocketMQ message containing CloudEvent metadata (attributes and/or extensions). |
| 59 | + * @param body The message body as byte array. |
| 60 | + * @return A {@link MessageReader} capable of parsing a {@link io.cloudevents.CloudEvent} from the content-type, properties, and payload of a RocketMQ message. |
| 61 | + */ |
| 62 | + public static MessageReader createReader(final String contentType, final Map<String, String> properties, final byte[] body) { |
| 63 | + return MessageUtils.parseStructuredOrBinaryMessage( |
| 64 | + () -> contentType, |
| 65 | + format -> new GenericStructuredMessageReader(format, body), |
| 66 | + () -> properties.get(RocketmqConstants.MESSAGE_PROPERTY_SPEC_VERSION), |
| 67 | + sv -> new RocketmqBinaryMessageReader(sv, properties, contentType, body) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates a {@link MessageWriter} instance capable of translating a {@link io.cloudevents.CloudEvent} to a RocketMQ {@link Message}. |
| 73 | + * |
| 74 | + * @param topic The topic to which the created RocketMQ message will be sent. |
| 75 | + * @return A {@link MessageWriter} capable of converting a {@link io.cloudevents.CloudEvent} to a RocketMQ {@link Message}. |
| 76 | + */ |
| 77 | + public static MessageWriter<CloudEventWriter<Message>, Message> createWriter(final String topic) { |
| 78 | + return new RocketmqMessageWriter(topic); |
| 79 | + } |
| 80 | +} |
0 commit comments