Skip to content

Commit 87c6915

Browse files
AMQP javadocs (cloudevents#322)
* Javadocs Signed-off-by: Francesco Guardiani <[email protected]> * Removed createReader for structured mode and refactored createReader(String, ApplicationProperties, byte[]) to createReader(String, ApplicationProperties, Section) Signed-off-by: Francesco Guardiani <[email protected]>
1 parent a7f87cf commit 87c6915

File tree

3 files changed

+50
-58
lines changed

3 files changed

+50
-58
lines changed

amqp/src/main/java/io/cloudevents/amqp/ProtonAmqpMessageFactory.java

+33-44
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@
1717

1818
package io.cloudevents.amqp;
1919

20-
20+
import io.cloudevents.SpecVersion;
2121
import io.cloudevents.amqp.impl.AmqpConstants;
2222
import io.cloudevents.amqp.impl.ProtonAmqpBinaryMessageReader;
2323
import io.cloudevents.amqp.impl.ProtonAmqpMessageWriter;
2424
import io.cloudevents.core.message.MessageReader;
2525
import io.cloudevents.core.message.MessageWriter;
2626
import io.cloudevents.core.message.impl.GenericStructuredMessageReader;
2727
import io.cloudevents.core.message.impl.MessageUtils;
28+
import io.cloudevents.lang.Nullable;
29+
import io.cloudevents.rw.CloudEventRWException;
2830
import io.cloudevents.rw.CloudEventWriter;
2931
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
32+
import org.apache.qpid.proton.amqp.messaging.Section;
3033
import org.apache.qpid.proton.message.Message;
3134

3235
import javax.annotation.ParametersAreNonnullByDefault;
36+
3337
/**
34-
* A factory class providing convenience methods for creating MessageReader and MessageWriter instances based on Qpid Proton.
38+
* A factory class providing convenience methods for creating {@link MessageReader} and {@link MessageWriter} instances based on Qpid Proton {@link Message}.
3539
*/
3640
@ParametersAreNonnullByDefault
3741
public final class ProtonAmqpMessageFactory {
@@ -41,62 +45,47 @@ private ProtonAmqpMessageFactory() {
4145
}
4246

4347
/**
44-
* Creates a MessageReader to read a proton-based {@link Message}.
45-
* <p>
46-
* This implementation simply calls {@link #createReader(String, ApplicationProperties, byte[])}.
48+
* Creates a {@link MessageReader} to read a proton-based {@link Message}.
49+
* This reader is able to read both binary and structured encoded {@link io.cloudevents.CloudEvent}.
4750
*
48-
* @param message The proton message to read from.
49-
*
50-
* @return A message reader that can read the given proton message to a cloud event representation.
51+
* @param message The proton {@link Message} to read from.
52+
* @return A {@link MessageReader} that can read the given proton {@link Message} to a {@link io.cloudevents.CloudEvent} representation.
53+
* @throws CloudEventRWException if something goes wrong while resolving the {@link SpecVersion} or if the message has unknown encoding
54+
* @see #createReader(String, ApplicationProperties, Section)
5155
*/
52-
public static MessageReader createReader(final Message message) {
53-
54-
final byte[] payload = AmqpConstants.getPayloadAsByteArray(message.getBody());
55-
return createReader(message.getContentType(), message.getApplicationProperties(), payload);
56+
public static MessageReader createReader(final Message message) throws CloudEventRWException {
57+
return createReader(message.getContentType(), message.getApplicationProperties(), message.getBody());
5658
}
5759

5860
/**
59-
* Creates a MessageReader using the content-type property and payload of a proton-based message.
60-
* <p>
61-
* This method simply calls {@link #createReader(String, ApplicationProperties, byte[])}.
61+
* Creates a MessageReader to read using the {@code content-type} property, {@code application-properties} and data payload
62+
* of a proton-based {@link Message}. This reader is able to read both binary and structured encoded {@link io.cloudevents.CloudEvent}.
6263
*
63-
* @param contentType The content-type of the message payload.
64-
* @param payload The message payload in bytes.
65-
* @return A message reader capable of representing a CloudEvent from
66-
* a message <em>content-type</em> property and <em>application-data</em>.
64+
* @param contentType The {@code content-type} of the message payload.
65+
* @param props The {@code application-properties} section of the proton-message containing cloud event metadata (attributes and/or extensions).
66+
* @param body The message body or {@code null} if the message does not contain any body.
67+
* @return A {@link MessageReader} capable of representing a {@link io.cloudevents.CloudEvent} from the {@code application-properties},
68+
* {@code content-type} and payload of a proton message.
69+
* @throws CloudEventRWException if something goes wrong while resolving the {@link SpecVersion} or if the message has unknown encoding
6770
*/
68-
public static MessageReader createReader(final String contentType, final byte[] payload) {
69-
return createReader(contentType, null, payload);
71+
public static MessageReader createReader(final String contentType, final ApplicationProperties props, @Nullable final Section body) throws CloudEventRWException {
72+
final byte[] payload = AmqpConstants.getPayloadAsByteArray(body);
73+
return MessageUtils.parseStructuredOrBinaryMessage(
74+
() -> contentType,
75+
format -> new GenericStructuredMessageReader(format, payload),
76+
() -> AmqpConstants.getApplicationProperty(props, AmqpConstants.APP_PROPERTY_SPEC_VERSION, String.class),
77+
sv -> new ProtonAmqpBinaryMessageReader(sv, props, contentType, payload)
78+
);
7079
}
7180

7281
/**
73-
* Creates a MessageWriter capable of translating both a structured and binary CloudEvent
74-
* to a proton-based AMQP 1.0 representation.
82+
* Creates a {@link MessageWriter} capable of translating both a structured and binary CloudEvent
83+
* to a proton-based AMQP 1.0 {@link Message}.
7584
*
76-
* @return A message writer to read structured and binary cloud event from a proton-based message.
85+
* @return A {@link MessageWriter} to write a {@link io.cloudevents.CloudEvent} to Proton {@link Message} using structured or binary encoding.
7786
*/
7887
public static MessageWriter<CloudEventWriter<Message>, Message> createWriter() {
7988
return new ProtonAmqpMessageWriter<>();
8089
}
8190

82-
/**
83-
* Creates a MessageReader to read using the content-type property, application-propeties and data payload
84-
* of a proton-based message.
85-
*
86-
* @param contentType The content-type of the message payload.
87-
* @param props The application-properties section of the proton-message containing cloud event metadata (attributes and/or extensions).
88-
* @param payload The message payload in bytes or {@code null} if the message does not contain any payload.
89-
* @return A message reader capable of representing a CloudEvent from the application-properties,
90-
* content-type and payload of a proton message.
91-
*/
92-
public static MessageReader createReader(final String contentType, final ApplicationProperties props, final byte[] payload) {
93-
94-
return MessageUtils.parseStructuredOrBinaryMessage(
95-
() -> contentType,
96-
format -> new GenericStructuredMessageReader(format, payload),
97-
() -> AmqpConstants.getApplicationProperty(props, AmqpConstants.APP_PROPERTY_SPEC_VERSION, String.class),
98-
sv -> new ProtonAmqpBinaryMessageReader(sv, props, contentType, payload));
99-
100-
}
101-
10291
}

amqp/src/test/java/io/cloudevents/amqp/ProtonAmqpMessageFactoryTest.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@
1717

1818
package io.cloudevents.amqp;
1919

20-
import static org.assertj.core.api.Assertions.assertThat;
21-
22-
import java.util.AbstractMap.SimpleEntry;
23-
import java.util.Map;
24-
import java.util.stream.Collectors;
25-
import java.util.stream.Stream;
26-
27-
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
28-
import org.junit.jupiter.params.ParameterizedTest;
29-
import org.junit.jupiter.params.provider.Arguments;
30-
import org.junit.jupiter.params.provider.MethodSource;
31-
3220
import io.cloudevents.CloudEvent;
3321
import io.cloudevents.SpecVersion;
3422
import io.cloudevents.amqp.impl.AmqpConstants;
@@ -39,6 +27,19 @@
3927
import io.cloudevents.core.v03.CloudEventV03;
4028
import io.cloudevents.core.v1.CloudEventV1;
4129
import io.cloudevents.types.Time;
30+
import org.apache.qpid.proton.amqp.Binary;
31+
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
32+
import org.apache.qpid.proton.amqp.messaging.Section;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.Arguments;
35+
import org.junit.jupiter.params.provider.MethodSource;
36+
37+
import java.util.AbstractMap.SimpleEntry;
38+
import java.util.Map;
39+
import java.util.stream.Collectors;
40+
import java.util.stream.Stream;
41+
42+
import static org.assertj.core.api.Assertions.assertThat;
4243

4344
/**
4445
* Tests verifying the behavior of the {@code ProtonAmqpMessageFactory}.
@@ -53,7 +54,8 @@ public class ProtonAmqpMessageFactoryTest {
5354
@MethodSource("binaryTestArguments")
5455
public void readBinary(final Map<String, Object> props, final String contentType, final byte[] body,
5556
final CloudEvent event) {
56-
final MessageReader amqpReader = ProtonAmqpMessageFactory.createReader(contentType, new ApplicationProperties(props), body);
57+
final Section bodySection = body != null ? new org.apache.qpid.proton.amqp.messaging.Data(new Binary(body)) : null;
58+
final MessageReader amqpReader = ProtonAmqpMessageFactory.createReader(contentType, new ApplicationProperties(props), bodySection);
5759
assertThat(amqpReader.getEncoding()).isEqualTo(Encoding.BINARY);
5860
assertThat(amqpReader.toEvent()).isEqualTo(event);
5961
}
@@ -64,7 +66,7 @@ public void readStructured(final CloudEvent event) {
6466
final String contentType = CSVFormat.INSTANCE.serializedContentType() + "; charset=utf8";
6567
final byte[] contentPayload = CSVFormat.INSTANCE.serialize(event);
6668

67-
final MessageReader amqpReader = ProtonAmqpMessageFactory.createReader(contentType, null, contentPayload);
69+
final MessageReader amqpReader = ProtonAmqpMessageFactory.createReader(contentType, null, new org.apache.qpid.proton.amqp.messaging.Data(new Binary(contentPayload)));
6870
assertThat(amqpReader.getEncoding()).isEqualTo(Encoding.STRUCTURED);
6971
assertThat(amqpReader.toEvent()).isEqualTo(event);
7072
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
<link>https://docs.spring.io/spring-framework/docs/current/javadoc-api/</link>
163163
<link>https://vertx.io/docs/apidocs/</link>
164164
<link>https://jakarta.ee/specifications/platform/8/apidocs/</link>
165+
<link>https://qpid.apache.org/releases/qpid-proton-j-0.33.7/api/</link>
165166
</links>
166167
</configuration>
167168
<executions>

0 commit comments

Comments
 (0)