Skip to content

Commit ada1607

Browse files
committed
GH-422 GH-606 Add support for normalizing structure-mode CE message
Normalizing in this context means converting it to binary-mode so the rest of the processing logic is the same. Added support for canonical attribute names. Now, internally any attribute can be set as 'ce_' regardless where it came from are where it goes to as the frameork will be able to recognize both Removed CloudEventMessageConverter Renamed CloudEventAttributes to CloudEventAttributesHelperas it is better suited to what it actually does
1 parent c31b7f1 commit ada1607

File tree

15 files changed

+325
-362
lines changed

15 files changed

+325
-362
lines changed

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

-84
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2019-2019 the original author or authors.
3+
*
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+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
package org.springframework.cloud.function.cloudevent;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.util.StringUtils;
23+
24+
25+
/**
26+
*
27+
* @author Oleg Zhurakousky
28+
* @since 3.1
29+
*/
30+
public class CloudEventAttributesHelper extends HashMap<String, Object> {
31+
32+
/**
33+
*
34+
*/
35+
private static final long serialVersionUID = 5393610770855366497L;
36+
37+
38+
39+
CloudEventAttributesHelper(Map<String, Object> headers) {
40+
super(headers);
41+
}
42+
43+
@SuppressWarnings("unchecked")
44+
public <A> A getId() {
45+
if (this.containsKey(CloudEventMessageUtils.CANONICAL_ID)) {
46+
return (A) this.get(CloudEventMessageUtils.CANONICAL_ID);
47+
}
48+
else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.ID)) {
49+
return (A) this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.ID);
50+
}
51+
return null;
52+
}
53+
54+
String getAttributeName(String attributeName) {
55+
if (this.containsKey(CloudEventMessageUtils.ATTR_PREFIX + attributeName)) {
56+
return CloudEventMessageUtils.ATTR_PREFIX + attributeName;
57+
}
58+
else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + attributeName)) {
59+
return CloudEventMessageUtils.HTTP_ATTR_PREFIX + attributeName;
60+
}
61+
return attributeName;
62+
}
63+
64+
@SuppressWarnings("unchecked")
65+
public <A> A getSource() {
66+
if (this.containsKey(CloudEventMessageUtils.CANONICAL_SOURCE)) {
67+
return (A) this.get(CloudEventMessageUtils.CANONICAL_SOURCE);
68+
}
69+
else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE)) {
70+
return (A) this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SOURCE);
71+
}
72+
return (A) this.get(CloudEventMessageUtils.SOURCE);
73+
}
74+
75+
@SuppressWarnings("unchecked")
76+
public <A> A getSpecversion() {
77+
if (this.containsKey(CloudEventMessageUtils.CANONICAL_SPECVERSION)) {
78+
return (A) this.get(CloudEventMessageUtils.CANONICAL_SPECVERSION);
79+
}
80+
else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SPECVERSION)) {
81+
return (A) this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.SPECVERSION);
82+
}
83+
return (A) this.get(CloudEventMessageUtils.SPECVERSION);
84+
}
85+
86+
@SuppressWarnings("unchecked")
87+
public <A> A getType() {
88+
if (this.containsKey(CloudEventMessageUtils.CANONICAL_TYPE)) {
89+
return (A) this.get(CloudEventMessageUtils.CANONICAL_TYPE);
90+
}
91+
else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE)) {
92+
return (A) this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.TYPE);
93+
}
94+
return (A) this.get(CloudEventMessageUtils.TYPE);
95+
}
96+
97+
@SuppressWarnings("unchecked")
98+
public <A> A getDataContentType() {
99+
Object dataContentType;
100+
if (this.containsKey(CloudEventMessageUtils.CANONICAL_DATACONTENTTYPE)) {
101+
dataContentType = this.get(CloudEventMessageUtils.CANONICAL_DATACONTENTTYPE);
102+
}
103+
else if (this.containsKey(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATACONTENTTYPE)) {
104+
dataContentType = this.get(CloudEventMessageUtils.HTTP_ATTR_PREFIX + CloudEventMessageUtils.DATACONTENTTYPE);
105+
}
106+
dataContentType = this.get(CloudEventMessageUtils.DATACONTENTTYPE);
107+
return (A) dataContentType;
108+
}
109+
110+
public void setDataContentType(String datacontenttype) {
111+
this.put(CloudEventMessageUtils.CANONICAL_DATACONTENTTYPE, datacontenttype);
112+
}
113+
114+
@SuppressWarnings("unchecked")
115+
public <A> A getAtttribute(String name) {
116+
return (A) this.get(name);
117+
}
118+
119+
public boolean isValidCloudEvent() {
120+
return StringUtils.hasText(this.getId())
121+
&& StringUtils.hasText(this.getSource())
122+
&& StringUtils.hasText(this.getSpecversion())
123+
&& StringUtils.hasText(this.getType());
124+
}
125+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public interface CloudEventAttributesProvider {
3131
*
3232
* @param inputMessage input message used to invoke user functionality (e.g., function)
3333
* @param result result of the invocation of user functionality (e.g., function)
34-
* @return instance of {@link CloudEventAttributes}
34+
* @return instance of {@link CloudEventAttributesHelper}
3535
*/
3636
Map<String, Object> generateDefaultCloudEventHeaders(Message<?> inputMessage, Object result);
3737
}

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

-130
This file was deleted.

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

-39
This file was deleted.

0 commit comments

Comments
 (0)