Skip to content

Commit 42e684a

Browse files
Javadoc'ed more and more the api module
Cleanup the CloudEventRWException More tests on the API module Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
1 parent 3440823 commit 42e684a

File tree

17 files changed

+193
-57
lines changed

17 files changed

+193
-57
lines changed

api/src/main/java/io/cloudevents/CloudEventData.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@
1919

2020
/**
2121
* Interface that defines a wrapper for CloudEvent data.
22+
* <p>
23+
* This interface can be overridden to include any type of data inside a {@link CloudEvent}, given it has a method to convert back to bytes.
2224
*/
2325
public interface CloudEventData {
2426

2527
/**
26-
* @return this CloudEventData, represented as bytes. Note: this operation may be expensive, depending on the internal representation of data
28+
* Returns the bytes representation of this data instance.
29+
* <p>
30+
* Note: depending on the implementation, this operation may be expensive.
31+
*
32+
* @return this data, represented as bytes.
2733
*/
2834
byte[] toBytes();
2935

api/src/main/java/io/cloudevents/CloudEventExtensions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Set;
2424

2525
/**
26-
* The event extensions
26+
* The event extensions.
2727
* <p>
2828
* Extensions values could be String/Number/Boolean
2929
*/

api/src/main/java/io/cloudevents/SpecVersion.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package io.cloudevents;
1919

20+
import io.cloudevents.rw.CloudEventRWException;
21+
2022
import javax.annotation.ParametersAreNonnullByDefault;
2123
import java.util.*;
2224
import java.util.stream.Collectors;
@@ -62,7 +64,7 @@ public String toString() {
6264
*
6365
* @param sv String representing the spec version
6466
* @return The parsed spec version
65-
* @throws IllegalArgumentException When the spec version string is unrecognized
67+
* @throws CloudEventRWException When the spec version string is unrecognized
6668
*/
6769
public static SpecVersion parse(String sv) {
6870
switch (sv) {
@@ -71,7 +73,7 @@ public static SpecVersion parse(String sv) {
7173
case "1.0":
7274
return SpecVersion.V1;
7375
default:
74-
throw new IllegalArgumentException("Unrecognized SpecVersion " + sv);
76+
throw CloudEventRWException.newInvalidSpecVersion(sv);
7577
}
7678
}
7779

api/src/main/java/io/cloudevents/rw/CloudEventAttributesWriter.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@ public interface CloudEventAttributesWriter {
3232
* Set attribute with type {@link String}. This setter should not be invoked for specversion, because the built Visitor already
3333
* has the information through the {@link CloudEventWriterFactory}.
3434
*
35-
* @param name
36-
* @param value
37-
* @throws CloudEventRWException
35+
* @param name name of the attribute
36+
* @param value value of the attribute
37+
* @return self
38+
* @throws CloudEventRWException if anything goes wrong while writing this attribute.
3839
*/
3940
CloudEventAttributesWriter withAttribute(String name, @Nullable String value) throws CloudEventRWException;
4041

4142
/**
4243
* Set attribute with type {@link URI}.
4344
*
44-
* @param name
45-
* @param value
46-
* @throws CloudEventRWException
45+
* @param name name of the attribute
46+
* @param value value of the attribute
47+
* @throws CloudEventRWException if anything goes wrong while writing this attribute.
48+
* @return self
4749
*/
4850
default CloudEventAttributesWriter withAttribute(String name, @Nullable URI value) throws CloudEventRWException {
4951
return withAttribute(name, value == null ? null : value.toString());
@@ -52,9 +54,10 @@ default CloudEventAttributesWriter withAttribute(String name, @Nullable URI valu
5254
/**
5355
* Set attribute with type {@link OffsetDateTime} attribute.
5456
*
55-
* @param name
56-
* @param value
57-
* @throws CloudEventRWException
57+
* @param name name of the attribute
58+
* @param value value of the attribute
59+
* @throws CloudEventRWException if anything goes wrong while writing this attribute.
60+
* @return self
5861
*/
5962
default CloudEventAttributesWriter withAttribute(String name, @Nullable OffsetDateTime value) throws CloudEventRWException {
6063
return withAttribute(name, value == null ? null : Time.writeTime(value));

api/src/main/java/io/cloudevents/rw/CloudEventExtensionsWriter.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ public interface CloudEventExtensionsWriter {
2929
/**
3030
* Set an extension with type {@link String}.
3131
*
32-
* @param name
33-
* @param value
34-
* @throws CloudEventRWException
32+
* @param name name of the extension
33+
* @param value value of the extension
34+
* @return self
35+
* @throws CloudEventRWException if anything goes wrong while writing this extension.
3536
*/
3637
CloudEventExtensionsWriter withExtension(String name, @Nullable String value) throws CloudEventRWException;
3738

3839
/**
3940
* Set attribute with type {@link URI}.
4041
*
41-
* @param name
42-
* @param value
43-
* @throws CloudEventRWException
42+
* @param name name of the extension
43+
* @param value value of the extension
44+
* @throws CloudEventRWException if anything goes wrong while writing this extension.
45+
* @return self
4446
*/
4547
default CloudEventExtensionsWriter withExtension(String name, @Nullable Number value) throws CloudEventRWException {
4648
return withExtension(name, value == null ? null : value.toString());
@@ -49,9 +51,10 @@ default CloudEventExtensionsWriter withExtension(String name, @Nullable Number v
4951
/**
5052
* Set attribute with type {@link Boolean} attribute.
5153
*
52-
* @param name
53-
* @param value
54-
* @throws CloudEventRWException
54+
* @param name name of the extension
55+
* @param value value of the extension
56+
* @throws CloudEventRWException if anything goes wrong while writing this extension.
57+
* @return self
5558
*/
5659
default CloudEventExtensionsWriter withExtension(String name, @Nullable Boolean value) throws CloudEventRWException {
5760
return withExtension(name, value == null ? null : value.toString());

api/src/main/java/io/cloudevents/rw/CloudEventRWException.java

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,64 @@
1717

1818
package io.cloudevents.rw;
1919

20+
/**
21+
* This class is the exception Protocol Binding and Event Format implementers can use to signal errors while serializing/deserializing CloudEvent.
22+
*/
2023
public class CloudEventRWException extends RuntimeException {
2124

25+
/**
26+
* The kind of error that happened while serializing/deserializing
27+
*/
2228
public enum CloudEventRWExceptionKind {
29+
/**
30+
* Spec version string is not recognized by this particular SDK version.
31+
*/
2332
INVALID_SPEC_VERSION,
33+
/**
34+
* The attribute name is not a valid/known context attribute.
35+
*/
2436
INVALID_ATTRIBUTE_NAME,
37+
/**
38+
* The extension name is not valid,
39+
* because it doesn't follow the <a href="https://github.com/cloudevents/spec/blob/v1.0/spec.md#attribute-naming-convention">naming convention</a>
40+
* enforced by the CloudEvents spec.
41+
*/
42+
INVALID_EXTENSION_NAME,
43+
/**
44+
* The attribute/extension type is not valid.
45+
*/
2546
INVALID_ATTRIBUTE_TYPE,
47+
/**
48+
* The attribute/extension value is not valid.
49+
*/
2650
INVALID_ATTRIBUTE_VALUE,
27-
INVALID_EXTENSION_TYPE,
51+
/**
52+
* The data type is not valid.
53+
*/
54+
INVALID_DATA_TYPE,
55+
/**
56+
* Error while converting CloudEventData.
57+
*/
2858
DATA_CONVERSION,
59+
/**
60+
* Other error.
61+
*/
2962
OTHER
3063
}
3164

3265
private final CloudEventRWExceptionKind kind;
3366

34-
public CloudEventRWException(CloudEventRWExceptionKind kind, Throwable cause) {
67+
private CloudEventRWException(CloudEventRWExceptionKind kind, Throwable cause) {
3568
super(cause);
3669
this.kind = kind;
3770
}
3871

39-
public CloudEventRWException(CloudEventRWExceptionKind kind, String message) {
72+
private CloudEventRWException(CloudEventRWExceptionKind kind, String message) {
4073
super(message);
4174
this.kind = kind;
4275
}
4376

44-
public CloudEventRWException(CloudEventRWExceptionKind kind, String message, Throwable cause) {
77+
private CloudEventRWException(CloudEventRWExceptionKind kind, String message, Throwable cause) {
4578
super(message, cause);
4679
this.kind = kind;
4780
}
@@ -52,7 +85,7 @@ public CloudEventRWExceptionKind getKind() {
5285

5386
public static CloudEventRWException newInvalidSpecVersion(String specVersion) {
5487
return new CloudEventRWException(
55-
CloudEventRWExceptionKind.INVALID_ATTRIBUTE_TYPE,
88+
CloudEventRWExceptionKind.INVALID_SPEC_VERSION,
5689
"Invalid specversion: " + specVersion
5790
);
5891
}
@@ -64,32 +97,45 @@ public static CloudEventRWException newInvalidAttributeName(String attributeName
6497
);
6598
}
6699

100+
public static CloudEventRWException newInvalidExtensionName(String extensionName) {
101+
return new CloudEventRWException(
102+
CloudEventRWExceptionKind.INVALID_EXTENSION_NAME,
103+
"Invalid extensions name: " + extensionName
104+
);
105+
}
106+
67107
public static CloudEventRWException newInvalidAttributeType(String attributeName, Class<?> clazz) {
68108
return new CloudEventRWException(
69109
CloudEventRWExceptionKind.INVALID_ATTRIBUTE_TYPE,
70-
"Invalid attribute type for \"" + attributeName + "\": " + clazz.getCanonicalName()
110+
"Invalid attribute/extension type for \"" + attributeName + "\": " + clazz.getCanonicalName()
71111
);
72112
}
73113

74114
public static CloudEventRWException newInvalidAttributeValue(String attributeName, Object value, Throwable cause) {
75115
return new CloudEventRWException(
76116
CloudEventRWExceptionKind.INVALID_ATTRIBUTE_VALUE,
77-
"Invalid attribute value for \"" + attributeName + "\": " + value,
117+
"Invalid attribute/extension value for \"" + attributeName + "\": " + value,
78118
cause
79119
);
80120
}
81121

82-
public static CloudEventRWException newInvalidExtensionType(String extensionName, Class<?> clazz) {
122+
public static CloudEventRWException newInvalidDataType(String actual, String... allowed) {
123+
String message;
124+
if (allowed.length == 0) {
125+
message = "Invalid data type: " + actual;
126+
} else {
127+
message = "Invalid data type: " + actual + ". Allowed: " + String.join(", ", allowed);
128+
}
83129
return new CloudEventRWException(
84-
CloudEventRWExceptionKind.INVALID_EXTENSION_TYPE,
85-
"Invalid extension type for \"" + extensionName + "\": " + clazz.getCanonicalName()
130+
CloudEventRWExceptionKind.INVALID_DATA_TYPE,
131+
message
86132
);
87133
}
88134

89-
public static CloudEventRWException newDataConversion(Throwable cause, String to) {
135+
public static CloudEventRWException newDataConversion(Throwable cause, String from, String to) {
90136
return new CloudEventRWException(
91137
CloudEventRWExceptionKind.DATA_CONVERSION,
92-
"Error while trying to convert data to " + to,
138+
"Error while trying to convert data from " + from + " to " + to,
93139
cause
94140
);
95141
}

api/src/main/java/io/cloudevents/rw/CloudEventReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public interface CloudEventReader {
3333
* Visit self using the provided visitor factory
3434
*
3535
* @param writerFactory a factory that generates a visitor starting from the SpecVersion of the event
36-
* @throws CloudEventRWException if something went wrong during the visit.
36+
* @throws CloudEventRWException if something went wrong during the read.
3737
*/
3838
default <V extends CloudEventWriter<R>, R> R read(CloudEventWriterFactory<V, R> writerFactory) throws CloudEventRWException {
3939
return read(writerFactory, null);

api/src/main/java/io/cloudevents/rw/CloudEventWriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ public interface CloudEventWriter<R> extends CloudEventAttributesWriter, CloudEv
3131
* End the visit with a data field
3232
*
3333
* @return an eventual return value
34+
* @throws CloudEventRWException if the message writer cannot be ended.
3435
*/
3536
R end(CloudEventData data) throws CloudEventRWException;
3637

3738
/**
3839
* End the visit
3940
*
4041
* @return an eventual return value
42+
* @throws CloudEventRWException if the message writer cannot be ended.
4143
*/
42-
R end();
44+
R end() throws CloudEventRWException;
4345

4446
}

api/src/main/java/io/cloudevents/rw/CloudEventWriterFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public interface CloudEventWriterFactory<V extends CloudEventWriter<R>, R> {
2525
/**
2626
* Create a {@link CloudEventWriter} starting from the provided {@link SpecVersion}
2727
*
28-
* @param version
29-
* @return
28+
* @param version the spec version to create the writer
29+
* @return the new writer
30+
* @throws CloudEventRWException if the spec version is invalid or the writer cannot be instantiated.
3031
*/
31-
V create(SpecVersion version);
32+
V create(SpecVersion version) throws CloudEventRWException;
3233
}

api/src/main/java/io/cloudevents/types/Time.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package io.cloudevents.types;
1919

20+
import io.cloudevents.rw.CloudEventRWException;
21+
22+
import java.time.DateTimeException;
2023
import java.time.OffsetDateTime;
2124
import java.time.format.DateTimeParseException;
2225

@@ -31,16 +34,56 @@ private Time() {
3134
}
3235

3336
/**
34-
* Parse a {@link String} RFC3339 compliant as {@link OffsetDateTime}
37+
* Parse a {@link String} RFC3339 compliant as {@link OffsetDateTime}.
38+
*
39+
* @param time the value to parse as time
40+
* @return the parsed {@link OffsetDateTime}
41+
* @throws DateTimeParseException if something went wrong when parsing the provided time.
3542
*/
3643
public static OffsetDateTime parseTime(String time) throws DateTimeParseException {
3744
return OffsetDateTime.parse(time);
3845
}
3946

4047
/**
41-
* Convert a {@link OffsetDateTime} to a RFC3339 compliant {@link String}
48+
* Parse an attribute/extension with RFC3339 compliant {@link String} value as {@link OffsetDateTime}.
49+
*
50+
* @param attributeName the attribute/extension name
51+
* @param time the value to parse as time
52+
* @return the parsed {@link OffsetDateTime}
53+
* @throws CloudEventRWException if something went wrong when parsing the attribute/extension.
54+
*/
55+
public static OffsetDateTime parseTime(String attributeName, String time) throws CloudEventRWException {
56+
try {
57+
return parseTime(time);
58+
} catch (DateTimeParseException e) {
59+
throw CloudEventRWException.newInvalidAttributeValue(attributeName, time, e);
60+
}
61+
}
62+
63+
/**
64+
* Convert a {@link OffsetDateTime} to a RFC3339 compliant {@link String}.
65+
*
66+
* @param time the time to write as {@link String}
67+
* @return the serialized time
68+
* @throws DateTimeException if something went wrong when serializing the provided time.
4269
*/
43-
public static String writeTime(OffsetDateTime time) throws DateTimeParseException {
70+
public static String writeTime(OffsetDateTime time) throws DateTimeException {
4471
return ISO_OFFSET_DATE_TIME.format(time);
4572
}
73+
74+
/**
75+
* Convert an attribute/extension {@link OffsetDateTime} to a RFC3339 compliant {@link String}.
76+
*
77+
* @param attributeName the attribute/extension name
78+
* @param time the time to write as {@link String}
79+
* @return the serialized time
80+
* @throws CloudEventRWException if something went wrong when serializing the attribute/extension.
81+
*/
82+
public static String writeTime(String attributeName, OffsetDateTime time) throws DateTimeException {
83+
try {
84+
return writeTime(time);
85+
} catch (DateTimeParseException e) {
86+
throw CloudEventRWException.newInvalidAttributeValue(attributeName, time, e);
87+
}
88+
}
4689
}

0 commit comments

Comments
 (0)