Skip to content

Commit 4ebeab0

Browse files
Refactor to Facilitate Decoupling from Concrete Implementations of EventFormat (cloudevents#539)
- Introduce ContentType enum - Resolve formats by using the ContentType enum Signed-off-by: Randi Sheaffer-Klass <[email protected]>
1 parent 4c81f3e commit 4ebeab0

File tree

9 files changed

+95
-8
lines changed

9 files changed

+95
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.core.format;
19+
20+
import io.cloudevents.CloudEvent;
21+
import io.cloudevents.CloudEventData;
22+
import io.cloudevents.rw.CloudEventDataMapper;
23+
24+
import javax.annotation.ParametersAreNonnullByDefault;
25+
import java.util.Collections;
26+
import java.util.Set;
27+
28+
/**
29+
* <p>A construct that aggregates a two-part identifier of file formats and format contents transmitted on the Internet.
30+
*
31+
* <p>The two parts of a {@code ContentType} are its <em>type</em> and a <em>subtype</em>; separated by a forward slash ({@code /}).
32+
*
33+
* <p>The constants enumerated by {@code ContentType} correspond <em>only</em> to the specialized formats supported by the Java™ SDK for CloudEvents.
34+
*
35+
* @see io.cloudevents.core.format.EventFormat
36+
*/
37+
@ParametersAreNonnullByDefault
38+
public enum ContentType {
39+
40+
/**
41+
* Content type associated with the JSON event format
42+
*/
43+
JSON("application/cloudevents+json"),
44+
/**
45+
* The content type for transports sending cloudevents in the protocol buffer format.
46+
*/
47+
PROTO("application/cloudevents+protobuf"),
48+
/**
49+
* The content type for transports sending cloudevents in XML format.
50+
*/
51+
XML("application/cloudevents+xml");
52+
53+
private String value;
54+
55+
private ContentType(String value) { this.value = value; }
56+
57+
/**
58+
* Return a string consisting of the slash-delimited ({@code /}) two-part identifier for this {@code enum} constant.
59+
*/
60+
public String value() { return value; }
61+
62+
/**
63+
* Return a string consisting of the slash-delimited ({@code /}) two-part identifier for this {@code enum} constant.
64+
*/
65+
@Override
66+
public String toString() { return value(); }
67+
68+
}

core/src/main/java/io/cloudevents/core/provider/EventFormatProvider.java

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import javax.annotation.ParametersAreNonnullByDefault;
2727

28+
import io.cloudevents.core.format.ContentType;
2829
import io.cloudevents.core.format.EventFormat;
2930
import io.cloudevents.lang.Nullable;
3031

@@ -98,4 +99,14 @@ public EventFormat resolveFormat(String contentType) {
9899
return this.formats.get(contentType);
99100
}
100101

102+
/**
103+
* Resolve an event format starting from the content type.
104+
*
105+
* @param contentType the content type to resolve the event format
106+
* @return null if no format was found for the provided content type
107+
*/
108+
@Nullable
109+
public EventFormat resolveFormat(ContentType contentType) {
110+
return this.formats.get(contentType.value());
111+
}
101112
}

docs/json-jackson.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ adding the dependency to your project:
2828

2929
```java
3030
import io.cloudevents.CloudEvent;
31+
import io.cloudevents.core.format.ContentType;
3132
import io.cloudevents.core.format.EventFormatProvider;
3233
import io.cloudevents.core.builder.CloudEventBuilder;
33-
import io.cloudevents.jackson.JsonFormat;
3434

3535
CloudEvent event = CloudEventBuilder.v1()
3636
.withId("hello")
@@ -40,7 +40,7 @@ CloudEvent event = CloudEventBuilder.v1()
4040

4141
byte[]serialized = EventFormatProvider
4242
.getInstance()
43-
.resolveFormat(JsonFormat.CONTENT_TYPE)
43+
.resolveFormat(ContentType.JSON)
4444
.serialize(event);
4545
```
4646

docs/protobuf.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ No further configuration is required is use the module.
3030

3131
```java
3232
import io.cloudevents.CloudEvent;
33+
import io.cloudevents.core.format.ContentType;
3334
import io.cloudevents.core.format.EventFormatProvider;
3435
import io.cloudevents.core.builder.CloudEventBuilder;
35-
import io.cloudevents.protobuf.ProtobufFormat;
3636

3737
CloudEvent event = CloudEventBuilder.v1()
3838
.withId("hello")
@@ -42,7 +42,7 @@ CloudEvent event = CloudEventBuilder.v1()
4242

4343
byte[]serialized = EventFormatProvider
4444
.getInstance()
45-
.resolveFormat(ProtobufFormat.CONTENT_TYPE)
45+
.resolveFormat(ContentType.PROTO)
4646
.serialize(event);
4747
```
4848

docs/xml.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ adding the dependency to your project:
2929

3030
```java
3131
import io.cloudevents.CloudEvent;
32+
import io.cloudevents.core.format.ContentType;
3233
import io.cloudevents.core.format.EventFormatProvider;
3334
import io.cloudevents.core.builder.CloudEventBuilder;
34-
import io.cloudevents.xml.XMLFormat;
3535

3636
CloudEvent event = CloudEventBuilder.v1()
3737
.withId("hello")
@@ -41,7 +41,7 @@ CloudEvent event = CloudEventBuilder.v1()
4141

4242
byte[] serialized = EventFormatProvider
4343
.getInstance()
44-
.resolveFormat(XMLFormat.CONTENT_TYPE)
44+
.resolveFormat(ContentType.XML)
4545
.serialize(event);
4646
```
4747

formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.cloudevents.CloudEvent;
2323
import io.cloudevents.CloudEventData;
2424
import io.cloudevents.core.builder.CloudEventBuilder;
25+
import io.cloudevents.core.format.ContentType;
2526
import io.cloudevents.core.format.EventDeserializationException;
2627
import io.cloudevents.core.format.EventFormat;
2728
import io.cloudevents.core.format.EventSerializationException;

formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.cloudevents.CloudEvent;
2525
import io.cloudevents.SpecVersion;
2626
import io.cloudevents.core.builder.CloudEventBuilder;
27+
import io.cloudevents.core.format.ContentType;
2728
import io.cloudevents.core.format.EventDeserializationException;
2829
import io.cloudevents.core.provider.EventFormatProvider;
2930
import io.cloudevents.rw.CloudEventRWException;
@@ -40,6 +41,7 @@
4041
import java.util.Objects;
4142
import java.util.stream.Stream;
4243

44+
import static io.cloudevents.core.format.ContentType.*;
4345
import static io.cloudevents.core.test.Data.*;
4446
import static org.assertj.core.api.Assertions.*;
4547

@@ -185,7 +187,10 @@ static Stream<Arguments> jsonContentTypes() {
185187
//https://www.rfc-editor.org/rfc/rfc2045#section-5.1
186188
// any us-ascii char can be part of parameters (except CTRLs and tspecials)
187189
Arguments.of("text/json; char-set = $!#$%&'*+.^_`|"),
188-
Arguments.of((Object) null)
190+
Arguments.of((Object) null),
191+
Arguments.of(JSON + ""),
192+
Arguments.of(JSON.value()),
193+
Arguments.of(JSON.toString())
189194
);
190195
}
191196

@@ -307,7 +312,7 @@ public static Stream<String> badJsonContent() {
307312
}
308313

309314
private JsonFormat getFormat() {
310-
return (JsonFormat) EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE);
315+
return (JsonFormat) EventFormatProvider.getInstance().resolveFormat(JSON);
311316
}
312317

313318
private static byte[] loadFile(String input) {

formats/protobuf/src/main/java/io/cloudevents/protobuf/ProtobufFormat.java

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.cloudevents.CloudEvent;
2121
import io.cloudevents.CloudEventData;
2222
import io.cloudevents.core.builder.CloudEventBuilder;
23+
import io.cloudevents.core.format.ContentType;
2324
import io.cloudevents.core.format.EventDeserializationException;
2425
import io.cloudevents.core.format.EventFormat;
2526
import io.cloudevents.core.format.EventSerializationException;

formats/xml/src/main/java/io/cloudevents/xml/XMLFormat.java

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.cloudevents.CloudEvent;
2020
import io.cloudevents.CloudEventData;
2121
import io.cloudevents.core.builder.CloudEventBuilder;
22+
import io.cloudevents.core.format.ContentType;
2223
import io.cloudevents.core.format.EventDeserializationException;
2324
import io.cloudevents.core.format.EventFormat;
2425
import io.cloudevents.core.format.EventSerializationException;

0 commit comments

Comments
 (0)