Skip to content

Commit b89f452

Browse files
Simplify the Reader/Writer implementations, reducing the knowledge of spec details (#309)
* Messing up stuff Signed-off-by: Francesco Guardiani <[email protected]> * Collapse CloudEventAttributesWriter and CloudEventAttributesWriter into CloudEventContextWriter Signed-off-by: Francesco Guardiani <[email protected]> * Rebase fix Signed-off-by: Francesco Guardiani <[email protected]>
1 parent a14f5ea commit b89f452

File tree

30 files changed

+739
-544
lines changed

30 files changed

+739
-544
lines changed

amqp/src/main/java/io/cloudevents/amqp/impl/ProtonAmqpMessageWriter.java

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,20 @@
1717

1818
package io.cloudevents.amqp.impl;
1919

20-
import java.util.HashMap;
21-
22-
import org.apache.qpid.proton.amqp.Binary;
23-
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
24-
import org.apache.qpid.proton.amqp.messaging.Data;
25-
import org.apache.qpid.proton.message.Message;
26-
2720
import io.cloudevents.CloudEventData;
2821
import io.cloudevents.SpecVersion;
2922
import io.cloudevents.core.format.EventFormat;
3023
import io.cloudevents.core.message.MessageWriter;
3124
import io.cloudevents.core.v1.CloudEventV1;
32-
import io.cloudevents.rw.CloudEventAttributesWriter;
33-
import io.cloudevents.rw.CloudEventExtensionsWriter;
25+
import io.cloudevents.rw.CloudEventContextWriter;
3426
import io.cloudevents.rw.CloudEventRWException;
3527
import io.cloudevents.rw.CloudEventWriter;
28+
import org.apache.qpid.proton.amqp.Binary;
29+
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
30+
import org.apache.qpid.proton.amqp.messaging.Data;
31+
import org.apache.qpid.proton.message.Message;
32+
33+
import java.util.HashMap;
3634

3735
/**
3836
* A proton-based MessageWriter capable of writing both structured and binary CloudEvent messages to an AMQP 1.0 representation as
@@ -53,33 +51,28 @@ public ProtonAmqpMessageWriter() {
5351
}
5452

5553
@Override
56-
public CloudEventAttributesWriter withAttribute(final String name, final String value) throws CloudEventRWException {
54+
public CloudEventContextWriter withContextAttribute(String name, String value) throws CloudEventRWException {
5755
if (name.equals(CloudEventV1.DATACONTENTTYPE)) {
5856
message.setContentType(value);
5957
} else {
58+
// for now, extensions are mapped to application-properties
59+
// see https://github.com/cloudevents/sdk-java/issues/30#issuecomment-723982190
6060
if (applicationProperties == null) {
6161
throw new IllegalStateException("This Writer is not initialized");
6262
}
63-
applicationProperties.getValue().put(AmqpConstants.ATTRIBUTES_TO_PROPERTYNAMES.get(name), value);
64-
}
65-
return null;
66-
}
67-
68-
@Override
69-
public CloudEventExtensionsWriter withExtension(final String name, final String value) throws CloudEventRWException {
70-
// for now, extensions are mapped to application-properties
71-
// see https://github.com/cloudevents/sdk-java/issues/30#issuecomment-723982190
72-
if (applicationProperties == null) {
73-
throw new IllegalStateException("This Writer is not initialized");
63+
String propName = AmqpConstants.ATTRIBUTES_TO_PROPERTYNAMES.get(name);
64+
if (propName == null) {
65+
propName = name;
66+
}
67+
applicationProperties.getValue().put(propName, value);
7468
}
75-
applicationProperties.getValue().put(name, value);
76-
return null;
69+
return this;
7770
}
7871

7972
@Override
8073
public ProtonAmqpMessageWriter<R> create(final SpecVersion version) {
8174
if (applicationProperties == null) {
82-
applicationProperties = new ApplicationProperties(new HashMap<String, Object>());
75+
applicationProperties = new ApplicationProperties(new HashMap<>());
8376
}
8477
applicationProperties.getValue().put(AmqpConstants.APP_PROPERTY_SPEC_VERSION, version.toString());
8578
return this;
@@ -105,5 +98,4 @@ public Message end() {
10598
message.setApplicationProperties(applicationProperties);
10699
return message;
107100
}
108-
109101
}

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

Lines changed: 0 additions & 65 deletions
This file was deleted.

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,11 @@
2828
public interface CloudEventContextReader {
2929

3030
/**
31-
* Visit self attributes using the provided writer
31+
* Read the context attributes and extensions using the provided writer
3232
*
33-
* @param writer Attributes writer
34-
* @throws CloudEventRWException if something went wrong during the visit.
33+
* @param writer context writer
34+
* @throws CloudEventRWException if something went wrong during the read.
3535
*/
36-
void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException;
37-
38-
/**
39-
* Visit self extensions using the provided writer
40-
*
41-
* @param visitor Extensions writer
42-
* @throws CloudEventRWException if something went wrong during the visit.
43-
*/
44-
void readExtensions(CloudEventExtensionsWriter visitor) throws CloudEventRWException;
36+
void readContext(CloudEventContextWriter writer) throws CloudEventRWException;
4537

4638
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.rw;
19+
20+
import io.cloudevents.types.Time;
21+
22+
import javax.annotation.ParametersAreNonnullByDefault;
23+
import java.net.URI;
24+
import java.time.OffsetDateTime;
25+
26+
/**
27+
* Interface to write the context attributes/extensions from a {@link io.cloudevents.rw.CloudEventContextReader} to a new representation.
28+
*/
29+
@ParametersAreNonnullByDefault
30+
public interface CloudEventContextWriter {
31+
32+
/**
33+
* Set attribute with type {@link String}.
34+
* This setter should not be invoked for specversion, because the writer should
35+
* already know the specversion or because it doesn't need it to correctly write the value.
36+
*
37+
* @param name name of the attribute
38+
* @param value value of the attribute
39+
* @return self
40+
* @throws CloudEventRWException if anything goes wrong while writing this attribute.
41+
* @throws IllegalArgumentException if you're trying to set the specversion attribute.
42+
*/
43+
CloudEventContextWriter withContextAttribute(String name, String value) throws CloudEventRWException;
44+
45+
/**
46+
* Set attribute with type {@link URI}.
47+
* This setter should not be invoked for specversion, because the writer should
48+
* already know the specversion or because it doesn't need it to correctly write the value.
49+
*
50+
* @param name name of the attribute
51+
* @param value value of the attribute
52+
* @return self
53+
* @throws CloudEventRWException if anything goes wrong while writing this attribute.
54+
* @throws IllegalArgumentException if you're trying to set the specversion attribute.
55+
*/
56+
default CloudEventContextWriter withContextAttribute(String name, URI value) throws CloudEventRWException {
57+
return withContextAttribute(name, value.toString());
58+
}
59+
60+
/**
61+
* Set attribute with type {@link OffsetDateTime} attribute.
62+
* This setter should not be invoked for specversion, because the writer should
63+
* already know the specversion or because it doesn't need it to correctly write the value.
64+
*
65+
* @param name name of the attribute
66+
* @param value value of the attribute
67+
* @return self
68+
* @throws CloudEventRWException if anything goes wrong while writing this attribute.
69+
* @throws IllegalArgumentException if you're trying to set the specversion attribute.
70+
*/
71+
default CloudEventContextWriter withContextAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
72+
return withContextAttribute(name, Time.writeTime(name, value));
73+
}
74+
75+
/**
76+
* Set attribute with type {@link URI}.
77+
* This setter should not be invoked for specversion, because the writer should
78+
* already know the specversion or because it doesn't need it to correctly write the value.
79+
*
80+
* @param name name of the attribute
81+
* @param value value of the attribute
82+
* @return self
83+
* @throws CloudEventRWException if anything goes wrong while writing this extension.
84+
* @throws IllegalArgumentException if you're trying to set the specversion attribute.
85+
*/
86+
default CloudEventContextWriter withContextAttribute(String name, Number value) throws CloudEventRWException {
87+
return withContextAttribute(name, value.toString());
88+
}
89+
90+
/**
91+
* Set attribute with type {@link Boolean} attribute.
92+
* This setter should not be invoked for specversion, because the writer should
93+
* already know the specversion or because it doesn't need it to correctly write the value.
94+
*
95+
* @param name name of the attribute
96+
* @param value value of the attribute
97+
* @return self
98+
* @throws CloudEventRWException if anything goes wrong while writing this extension.
99+
* @throws IllegalArgumentException if you're trying to set the specversion attribute.
100+
*/
101+
default CloudEventContextWriter withContextAttribute(String name, Boolean value) throws CloudEventRWException {
102+
return withContextAttribute(name, value.toString());
103+
}
104+
105+
}

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

Lines changed: 0 additions & 61 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @param <R> return value at the end of the write process
2727
*/
28-
public interface CloudEventWriter<R> extends CloudEventAttributesWriter, CloudEventExtensionsWriter {
28+
public interface CloudEventWriter<R> extends CloudEventContextWriter {
2929

3030
/**
3131
* End the visit with a data field

0 commit comments

Comments
 (0)