17
17
18
18
package io .cloudevents .amqp ;
19
19
20
-
20
+ import io . cloudevents . SpecVersion ;
21
21
import io .cloudevents .amqp .impl .AmqpConstants ;
22
22
import io .cloudevents .amqp .impl .ProtonAmqpBinaryMessageReader ;
23
23
import io .cloudevents .amqp .impl .ProtonAmqpMessageWriter ;
24
24
import io .cloudevents .core .message .MessageReader ;
25
25
import io .cloudevents .core .message .MessageWriter ;
26
26
import io .cloudevents .core .message .impl .GenericStructuredMessageReader ;
27
27
import io .cloudevents .core .message .impl .MessageUtils ;
28
+ import io .cloudevents .lang .Nullable ;
29
+ import io .cloudevents .rw .CloudEventRWException ;
28
30
import io .cloudevents .rw .CloudEventWriter ;
29
31
import org .apache .qpid .proton .amqp .messaging .ApplicationProperties ;
32
+ import org .apache .qpid .proton .amqp .messaging .Section ;
30
33
import org .apache .qpid .proton .message .Message ;
31
34
32
35
import javax .annotation .ParametersAreNonnullByDefault ;
36
+
33
37
/**
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} .
35
39
*/
36
40
@ ParametersAreNonnullByDefault
37
41
public final class ProtonAmqpMessageFactory {
@@ -41,62 +45,47 @@ private ProtonAmqpMessageFactory() {
41
45
}
42
46
43
47
/**
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}.
47
50
*
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)
51
55
*/
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 ());
56
58
}
57
59
58
60
/**
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}.
62
63
*
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
67
70
*/
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
+ );
70
79
}
71
80
72
81
/**
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} .
75
84
*
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 .
77
86
*/
78
87
public static MessageWriter <CloudEventWriter <Message >, Message > createWriter () {
79
88
return new ProtonAmqpMessageWriter <>();
80
89
}
81
90
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
-
102
91
}
0 commit comments