Skip to content

Commit ac54a83

Browse files
committed
Clean up and simplifications around CloudEvent processing
This commit effectively a merge of work with #607 and simplifies the following - CloudEventAttributesProvider now provides CloudEventAttributes initialized with defaults to be set by the user - In HTTP RequestProcessor the logic of sanitizing headers was improved to ensure that correct prefix is applied Resolves #607
1 parent a0208ce commit ac54a83

File tree

11 files changed

+152
-206
lines changed

11 files changed

+152
-206
lines changed
Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,72 @@
2525
/**
2626
*
2727
* @author Oleg Zhurakousky
28+
* @author Dave Syer
29+
*
2830
* @since 3.1
2931
*/
30-
public class CloudEventAttributesHelper extends HashMap<String, Object> {
32+
public class CloudEventAttributes extends HashMap<String, Object> {
3133

3234
/**
3335
*
3436
*/
3537
private static final long serialVersionUID = 5393610770855366497L;
3638

3739

40+
private final String prefixToUse;
3841

39-
CloudEventAttributesHelper(Map<String, Object> headers) {
42+
public CloudEventAttributes(Map<String, Object> headers, String prefixToUse) {
4043
super(headers);
44+
this.prefixToUse = prefixToUse;
45+
}
46+
47+
48+
public CloudEventAttributes(Map<String, Object> headers) {
49+
this(headers, null);
50+
}
51+
52+
public CloudEventAttributes setId(String id) {
53+
if (StringUtils.hasText(this.prefixToUse)) {
54+
this.remove(this.getAttributeName(CloudEventMessageUtils.ID));
55+
this.put(this.prefixToUse + CloudEventMessageUtils.ID, id);
56+
}
57+
else {
58+
this.put(this.getAttributeName(CloudEventMessageUtils.ID), id);
59+
}
60+
return this;
61+
}
62+
63+
public CloudEventAttributes setSource(String source) {
64+
if (StringUtils.hasText(this.prefixToUse)) {
65+
this.remove(this.getAttributeName(CloudEventMessageUtils.SOURCE));
66+
this.put(this.prefixToUse + CloudEventMessageUtils.SOURCE, source);
67+
}
68+
else {
69+
this.put(this.getAttributeName(CloudEventMessageUtils.SOURCE), source);
70+
}
71+
return this;
72+
}
73+
74+
public CloudEventAttributes setSpecversion(String specversion) {
75+
if (StringUtils.hasText(this.prefixToUse)) {
76+
this.remove(this.getAttributeName(CloudEventMessageUtils.SPECVERSION));
77+
this.put(this.prefixToUse + CloudEventMessageUtils.SPECVERSION, specversion);
78+
}
79+
else {
80+
this.put(this.getAttributeName(CloudEventMessageUtils.SPECVERSION), specversion);
81+
}
82+
return this;
83+
}
84+
85+
public CloudEventAttributes setType(String type) {
86+
if (StringUtils.hasText(this.prefixToUse)) {
87+
this.remove(this.getAttributeName(CloudEventMessageUtils.TYPE));
88+
this.put(this.prefixToUse + CloudEventMessageUtils.TYPE, type);
89+
}
90+
else {
91+
this.put(this.getAttributeName(CloudEventMessageUtils.TYPE), type);
92+
}
93+
return this;
4194
}
4295

4396
@SuppressWarnings("unchecked")

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventAttributesProvider.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,19 @@
1616

1717
package org.springframework.cloud.function.cloudevent;
1818

19-
import java.util.Map;
20-
21-
import org.springframework.messaging.Message;
2219

2320
/**
2421
*
2522
* @author Oleg Zhurakousky
23+
* @author Dave Syer
24+
*
2625
* @since 3.1
2726
*/
2827
@FunctionalInterface
2928
public interface CloudEventAttributesProvider {
3029
/**
3130
*
32-
* @param inputMessage input message used to invoke user functionality (e.g., function)
33-
* @param result result of the invocation of user functionality (e.g., function)
34-
* @return instance of {@link CloudEventAttributesHelper}
31+
* @param attributes instance of {@link CloudEventAttributes}
3532
*/
36-
Map<String, Object> generateDefaultCloudEventHeaders(Message<?> inputMessage, Object result);
33+
void generateDefaultCloudEventHeaders(CloudEventAttributes attributes);
3734
}

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/CloudEventMessageUtils.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.function.cloudevent;
1818

19+
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122
import java.util.Set;
@@ -38,6 +39,8 @@
3839
* Mainly for internal use within the framework;
3940
*
4041
* @author Oleg Zhurakousky
42+
* @author Dave Syer
43+
*
4144
* @since 3.1
4245
*/
4346
public final class CloudEventMessageUtils {
@@ -162,20 +165,20 @@ private CloudEventMessageUtils() {
162165
* Checks if {@link Message} represents cloud event in binary-mode.
163166
*/
164167
public static boolean isBinary(Map<String, Object> headers) {
165-
CloudEventAttributesHelper attributes = new CloudEventAttributesHelper(headers);
168+
CloudEventAttributes attributes = new CloudEventAttributes(headers);
166169
return attributes.isValidCloudEvent();
167170
}
168171

169172
/**
170-
* Will construct instance of {@link CloudEventAttributesHelper} setting its required attributes.
173+
* Will construct instance of {@link CloudEventAttributes} setting its required attributes.
171174
*
172175
* @param ce_id value for Cloud Event 'id' attribute
173176
* @param ce_specversion value for Cloud Event 'specversion' attribute
174177
* @param ce_source value for Cloud Event 'source' attribute
175178
* @param ce_type value for Cloud Event 'type' attribute
176-
* @return instance of {@link CloudEventAttributesHelper}
179+
* @return instance of {@link CloudEventAttributes}
177180
*/
178-
public static CloudEventAttributesHelper get(String ce_id, String ce_specversion, String ce_source, String ce_type) {
181+
public static CloudEventAttributes get(String ce_id, String ce_specversion, String ce_source, String ce_type) {
179182
Assert.hasText(ce_id, "'ce_id' must not be null or empty");
180183
Assert.hasText(ce_specversion, "'ce_specversion' must not be null or empty");
181184
Assert.hasText(ce_source, "'ce_source' must not be null or empty");
@@ -185,40 +188,40 @@ public static CloudEventAttributesHelper get(String ce_id, String ce_specversion
185188
requiredAttributes.put(CloudEventMessageUtils.CANONICAL_SPECVERSION, ce_specversion);
186189
requiredAttributes.put(CloudEventMessageUtils.CANONICAL_SOURCE, ce_source);
187190
requiredAttributes.put(CloudEventMessageUtils.CANONICAL_TYPE, ce_type);
188-
return new CloudEventAttributesHelper(requiredAttributes);
191+
return new CloudEventAttributes(requiredAttributes);
189192
}
190193

191194
/**
192-
* Will construct instance of {@link CloudEventAttributesHelper}
195+
* Will construct instance of {@link CloudEventAttributes}
193196
* Should default/generate cloud event ID and SPECVERSION.
194197
*
195198
* @param ce_source value for Cloud Event 'source' attribute
196199
* @param ce_type value for Cloud Event 'type' attribute
197-
* @return instance of {@link CloudEventAttributesHelper}
200+
* @return instance of {@link CloudEventAttributes}
198201
*/
199-
public static CloudEventAttributesHelper get(String ce_source, String ce_type) {
202+
public static CloudEventAttributes get(String ce_source, String ce_type) {
200203
return get(UUID.randomUUID().toString(), "1.0", ce_source, ce_type);
201204
}
202205

203-
/**
204-
* Will construct instance of {@link CloudEventAttributesHelper} from {@link MessageHeaders}.
205-
*
206-
* Should copy Cloud Event related headers into an instance of {@link CloudEventAttributesHelper}
207-
* NOTE: Certain headers must not be copied.
208-
*
209-
* @param headers instance of {@link MessageHeaders}
210-
* @return modifiable instance of {@link CloudEventAttributesHelper}
211-
*/
212-
public static RequiredAttributeAccessor get(MessageHeaders headers) {
213-
return new RequiredAttributeAccessor(headers);
214-
}
206+
// /**
207+
// * Will construct instance of {@link CloudEventAttributes} from {@link MessageHeaders}.
208+
// *
209+
// * Should copy Cloud Event related headers into an instance of {@link CloudEventAttributes}
210+
// * NOTE: Certain headers must not be copied.
211+
// *
212+
// * @param headers instance of {@link MessageHeaders}
213+
// * @return modifiable instance of {@link CloudEventAttributes}
214+
// */
215+
// public static CloudEventAttributes get(MessageHeaders headers) {
216+
// return new CloudEventAttributes(headers);
217+
// }
215218

216219

217220
@SuppressWarnings("unchecked")
218221
public static Message<?> toBinary(Message<?> inputMessage, MessageConverter messageConverter) {
219222

220223
Map<String, Object> headers = inputMessage.getHeaders();
221-
CloudEventAttributesHelper attributes = new CloudEventAttributesHelper(headers);
224+
CloudEventAttributes attributes = new CloudEventAttributes(headers);
222225

223226
// first check the obvious and see if content-type is `cloudevents`
224227
if (!attributes.isValidCloudEvent() && headers.containsKey(MessageHeaders.CONTENT_TYPE)) {
@@ -265,7 +268,7 @@ else if (structuredCloudEvent.containsKey(CloudEventMessageUtils.DATA)) {
265268
}
266269
Assert.notNull(data, "'data' must not be null");
267270
MessageBuilder<?> builder = MessageBuilder.withPayload(data);
268-
CloudEventAttributesHelper attributes = new CloudEventAttributesHelper(structuredCloudEvent);
271+
CloudEventAttributes attributes = new CloudEventAttributes(structuredCloudEvent);
269272
builder.setHeader(prefixToUse + CloudEventMessageUtils.ID, attributes.getId());
270273
builder.setHeader(prefixToUse + CloudEventMessageUtils.SOURCE, attributes.getSource());
271274
builder.setHeader(prefixToUse + CloudEventMessageUtils.TYPE, attributes.getType());
@@ -282,4 +285,16 @@ public static String determinePrefixToUse(Message<?> inputMessage) {
282285
return CloudEventMessageUtils.ATTR_PREFIX;
283286
}
284287
}
288+
289+
public static Map<String, Object> generateDefaultCloudEventHeaders(Message<?> inputMessage, Object result, String applicationName) {
290+
CloudEventAttributes attributes = new CloudEventAttributes(inputMessage.getHeaders(), CloudEventMessageUtils.determinePrefixToUse(inputMessage));
291+
if (attributes.isValidCloudEvent()) {
292+
return attributes
293+
.setSpecversion("1.0")
294+
.setId(UUID.randomUUID().toString())
295+
.setType(result.getClass().getName())
296+
.setSource(applicationName);
297+
}
298+
return Collections.emptyMap();
299+
}
285300
}

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/DefaultCloudEventAttributesProvider.java

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

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/cloudevent/RequiredAttributeAccessor.java

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

0 commit comments

Comments
 (0)