1
1
/*
2
- * Copyright 2019-2019 the original author or authors.
2
+ * Copyright 2020-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import java .util .Map ;
21
21
import java .util .UUID ;
22
22
23
+ import org .springframework .messaging .MessageHeaders ;
23
24
import org .springframework .util .StringUtils ;
24
25
25
26
26
27
/**
28
+ * Utility class to assist with accessing and setting Cloud Events attributes from {@link MessageHeaders}.
29
+ * <br><br>
30
+ * It is effectively a wrapper over {@link MessageHeaders} which is a {@link Map}.
31
+ * It also provides best effort to both discover the actual attribute name (regardless of the prefix)
32
+ * as well as set appropriate attribute name.
33
+ * <br><br>
34
+ * For example, If there is an attribute `ce-source` or `ce_source` or 'source`, by simply calling getSource()
35
+ * we'll discover it and will return its value.
36
+ * <br>
37
+ * Similar effort will happen during the setting of the attribute. If you provide {@link #prefixToUse} we will
38
+ * use it otherwise we'll attempt to determine based on current execution context which prefix to use.
27
39
*
28
40
* @author Oleg Zhurakousky
29
41
* @author Dave Syer
@@ -45,139 +57,131 @@ public CloudEventAttributes(Map<String, Object> headers, String prefixToUse) {
45
57
this .prefixToUse = prefixToUse ;
46
58
}
47
59
48
-
49
60
public CloudEventAttributes (Map <String , Object > headers ) {
50
61
this (headers , null );
51
62
}
52
63
53
64
public CloudEventAttributes setId (String id ) {
54
- if (StringUtils .hasText (this .prefixToUse )) {
55
- this .remove (this .getAttributeName (CloudEventMessageUtils .ID ));
56
- this .put (this .prefixToUse + CloudEventMessageUtils .ID , id );
57
- }
58
- else {
59
- this .put (this .getAttributeName (CloudEventMessageUtils .ID ), id );
60
- }
65
+ this .setAtttribute (CloudEventMessageUtils .ID , id );
61
66
return this ;
62
67
}
63
68
64
- public CloudEventAttributes setSource (String source ) {
65
- if (StringUtils .hasText (this .prefixToUse )) {
66
- this .remove (this .getAttributeName (CloudEventMessageUtils .SOURCE ));
67
- this .put (this .prefixToUse + CloudEventMessageUtils .SOURCE , source );
68
- }
69
- else {
70
- this .put (this .getAttributeName (CloudEventMessageUtils .SOURCE ), source );
69
+ public <A > A getId () {
70
+ A id = this .getAtttribute (CloudEventMessageUtils .ID );
71
+ if (id instanceof UUID ) {
72
+ id = null ;
71
73
}
74
+ return id ;
75
+ }
76
+
77
+ public CloudEventAttributes setSource (String source ) {
78
+ this .setAtttribute (CloudEventMessageUtils .SOURCE , source );
72
79
return this ;
73
80
}
74
81
82
+ public <A > A getSource () {
83
+ return this .getAtttribute (CloudEventMessageUtils .SOURCE );
84
+ }
85
+
75
86
public CloudEventAttributes setSpecversion (String specversion ) {
76
- if (StringUtils .hasText (this .prefixToUse )) {
77
- this .remove (this .getAttributeName (CloudEventMessageUtils .SPECVERSION ));
78
- this .put (this .prefixToUse + CloudEventMessageUtils .SPECVERSION , specversion );
79
- }
80
- else {
81
- this .put (this .getAttributeName (CloudEventMessageUtils .SPECVERSION ), specversion );
82
- }
87
+ this .setAtttribute (CloudEventMessageUtils .SPECVERSION , specversion );
83
88
return this ;
84
89
}
85
90
91
+ public <A > A getSpecversion () {
92
+ return this .getAtttribute (CloudEventMessageUtils .SPECVERSION );
93
+ }
94
+
86
95
public CloudEventAttributes setType (String type ) {
87
- if (StringUtils .hasText (this .prefixToUse )) {
88
- this .remove (this .getAttributeName (CloudEventMessageUtils .TYPE ));
89
- this .put (this .prefixToUse + CloudEventMessageUtils .TYPE , type );
90
- }
91
- else {
92
- this .put (this .getAttributeName (CloudEventMessageUtils .TYPE ), type );
93
- }
96
+ this .setAtttribute (CloudEventMessageUtils .TYPE , type );
94
97
return this ;
95
98
}
96
99
97
- @ SuppressWarnings ("unchecked" )
98
- public <A > A getId () {
99
- if (this .containsKey (CloudEventMessageUtils .CANONICAL_ID )) {
100
- return (A ) this .get (CloudEventMessageUtils .CANONICAL_ID );
101
- }
102
- else if (this .containsKey (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .ID )) {
103
- return (A ) this .get (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .ID );
104
- }
105
- Object id = this .get (CloudEventMessageUtils .ID );
106
- if (!(id instanceof UUID )) {
107
- return (A ) id ;
108
- }
109
- return null ;
100
+ public <A > A getType () {
101
+ return this .getAtttribute (CloudEventMessageUtils .TYPE );
110
102
}
111
103
112
- String getAttributeName (String attributeName ) {
113
- if (this .containsKey (CloudEventMessageUtils .ATTR_PREFIX + attributeName )) {
114
- return CloudEventMessageUtils .ATTR_PREFIX + attributeName ;
115
- }
116
- else if (this .containsKey (CloudEventMessageUtils .HTTP_ATTR_PREFIX + attributeName )) {
117
- return CloudEventMessageUtils .HTTP_ATTR_PREFIX + attributeName ;
118
- }
119
- return attributeName ;
104
+ public CloudEventAttributes setDataContentType (String datacontenttype ) {
105
+ this .setAtttribute (CloudEventMessageUtils .DATACONTENTTYPE , datacontenttype );
106
+ return this ;
120
107
}
121
108
122
- @ SuppressWarnings ("unchecked" )
123
- public <A > A getSource () {
124
- if (this .containsKey (CloudEventMessageUtils .CANONICAL_SOURCE )) {
125
- return (A ) this .get (CloudEventMessageUtils .CANONICAL_SOURCE );
126
- }
127
- else if (this .containsKey (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .SOURCE )) {
128
- return (A ) this .get (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .SOURCE );
129
- }
130
- return (A ) this .get (CloudEventMessageUtils .SOURCE );
109
+ public <A > A getDataContentType () {
110
+ return this .getAtttribute (CloudEventMessageUtils .DATACONTENTTYPE );
131
111
}
132
112
133
- @ SuppressWarnings ("unchecked" )
134
- public <A > A getSpecversion () {
135
- if (this .containsKey (CloudEventMessageUtils .CANONICAL_SPECVERSION )) {
136
- return (A ) this .get (CloudEventMessageUtils .CANONICAL_SPECVERSION );
137
- }
138
- else if (this .containsKey (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .SPECVERSION )) {
139
- return (A ) this .get (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .SPECVERSION );
140
- }
141
- return (A ) this .get (CloudEventMessageUtils .SPECVERSION );
113
+ public CloudEventAttributes setDataSchema (String dataschema ) {
114
+ this .setAtttribute (CloudEventMessageUtils .DATASCHEMA , dataschema );
115
+ return this ;
142
116
}
143
117
144
- @ SuppressWarnings ("unchecked" )
145
- public <A > A getType () {
146
- if (this .containsKey (CloudEventMessageUtils .CANONICAL_TYPE )) {
147
- return (A ) this .get (CloudEventMessageUtils .CANONICAL_TYPE );
148
- }
149
- else if (this .containsKey (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .TYPE )) {
150
- return (A ) this .get (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .TYPE );
151
- }
152
- return (A ) this .get (CloudEventMessageUtils .TYPE );
118
+ public <A > A getDataSchema () {
119
+ return this .getAtttribute (CloudEventMessageUtils .DATASCHEMA );
153
120
}
154
121
155
- @ SuppressWarnings ("unchecked" )
156
- public <A > A getDataContentType () {
157
- Object dataContentType ;
158
- if (this .containsKey (CloudEventMessageUtils .CANONICAL_DATACONTENTTYPE )) {
159
- dataContentType = this .get (CloudEventMessageUtils .CANONICAL_DATACONTENTTYPE );
160
- }
161
- else if (this .containsKey (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .DATACONTENTTYPE )) {
162
- dataContentType = this .get (CloudEventMessageUtils .HTTP_ATTR_PREFIX + CloudEventMessageUtils .DATACONTENTTYPE );
163
- }
164
- dataContentType = this .get (CloudEventMessageUtils .DATACONTENTTYPE );
165
- return (A ) dataContentType ;
122
+ public CloudEventAttributes setSubject (String subject ) {
123
+ this .setAtttribute (CloudEventMessageUtils .SUBJECT , subject );
124
+ return this ;
166
125
}
167
126
168
- public void setDataContentType ( String datacontenttype ) {
169
- this .put (CloudEventMessageUtils .CANONICAL_DATACONTENTTYPE , datacontenttype );
127
+ public < A > A getSubect ( ) {
128
+ return this .getAtttribute (CloudEventMessageUtils .SUBJECT );
170
129
}
171
130
131
+ public CloudEventAttributes setTime (String time ) {
132
+ this .setAtttribute (CloudEventMessageUtils .TIME , time );
133
+ return this ;
134
+ }
135
+
136
+ public <A > A getTime () {
137
+ return this .getAtttribute (CloudEventMessageUtils .TIME );
138
+ }
139
+
140
+ /**
141
+ * Will delegate to the underlying {@link Map} returning the value for the requested attribute or null.
142
+ */
172
143
@ SuppressWarnings ("unchecked" )
173
- public <A > A getAtttribute (String name ) {
174
- return (A ) this .get (name );
144
+ public <A > A getAtttribute (String attrName ) {
145
+ if (this .containsKey (CloudEventMessageUtils .ATTR_PREFIX + attrName )) {
146
+ return (A ) this .get (CloudEventMessageUtils .ATTR_PREFIX + attrName );
147
+ }
148
+ else if (this .containsKey (CloudEventMessageUtils .HTTP_ATTR_PREFIX + attrName )) {
149
+ return (A ) this .get (CloudEventMessageUtils .HTTP_ATTR_PREFIX + attrName );
150
+ }
151
+ return (A ) this .get (attrName );
175
152
}
176
153
154
+ /**
155
+ * Determines if this instance of {@link CloudEventAttributes} represents valid Cloud Event.
156
+ * This implies that it contains all 4 required attributes (id, source, type & specversion)
157
+ *
158
+ * @return true if this instance represents a valid Cloud Event
159
+ */
177
160
public boolean isValidCloudEvent () {
178
161
return StringUtils .hasText (this .getId ())
179
162
&& StringUtils .hasText (this .getSource ())
180
163
&& StringUtils .hasText (this .getSpecversion ())
181
164
&& StringUtils .hasText (this .getType ());
182
165
}
166
+
167
+ String getAttributeName (String attributeName ) {
168
+ if (this .containsKey (CloudEventMessageUtils .ATTR_PREFIX + attributeName )) {
169
+ return CloudEventMessageUtils .ATTR_PREFIX + attributeName ;
170
+ }
171
+ else if (this .containsKey (CloudEventMessageUtils .HTTP_ATTR_PREFIX + attributeName )) {
172
+ return CloudEventMessageUtils .HTTP_ATTR_PREFIX + attributeName ;
173
+ }
174
+ return attributeName ;
175
+ }
176
+
177
+ private CloudEventAttributes setAtttribute (String attrName , String attrValue ) {
178
+ if (StringUtils .hasText (this .prefixToUse )) {
179
+ this .remove (this .getAttributeName (attrName ));
180
+ this .put (this .prefixToUse + attrName , attrValue );
181
+ }
182
+ else {
183
+ this .put (this .getAttributeName (attrName ), attrValue );
184
+ }
185
+ return this ;
186
+ }
183
187
}
0 commit comments