16
16
17
17
package org .springframework .cloud .function .cloudevent ;
18
18
19
+ import java .util .Collections ;
19
20
import java .util .HashMap ;
20
21
import java .util .Map ;
21
22
import java .util .Set ;
38
39
* Mainly for internal use within the framework;
39
40
*
40
41
* @author Oleg Zhurakousky
42
+ * @author Dave Syer
43
+ *
41
44
* @since 3.1
42
45
*/
43
46
public final class CloudEventMessageUtils {
@@ -162,20 +165,20 @@ private CloudEventMessageUtils() {
162
165
* Checks if {@link Message} represents cloud event in binary-mode.
163
166
*/
164
167
public static boolean isBinary (Map <String , Object > headers ) {
165
- CloudEventAttributesHelper attributes = new CloudEventAttributesHelper (headers );
168
+ CloudEventAttributes attributes = new CloudEventAttributes (headers );
166
169
return attributes .isValidCloudEvent ();
167
170
}
168
171
169
172
/**
170
- * Will construct instance of {@link CloudEventAttributesHelper } setting its required attributes.
173
+ * Will construct instance of {@link CloudEventAttributes } setting its required attributes.
171
174
*
172
175
* @param ce_id value for Cloud Event 'id' attribute
173
176
* @param ce_specversion value for Cloud Event 'specversion' attribute
174
177
* @param ce_source value for Cloud Event 'source' attribute
175
178
* @param ce_type value for Cloud Event 'type' attribute
176
- * @return instance of {@link CloudEventAttributesHelper }
179
+ * @return instance of {@link CloudEventAttributes }
177
180
*/
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 ) {
179
182
Assert .hasText (ce_id , "'ce_id' must not be null or empty" );
180
183
Assert .hasText (ce_specversion , "'ce_specversion' must not be null or empty" );
181
184
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
185
188
requiredAttributes .put (CloudEventMessageUtils .CANONICAL_SPECVERSION , ce_specversion );
186
189
requiredAttributes .put (CloudEventMessageUtils .CANONICAL_SOURCE , ce_source );
187
190
requiredAttributes .put (CloudEventMessageUtils .CANONICAL_TYPE , ce_type );
188
- return new CloudEventAttributesHelper (requiredAttributes );
191
+ return new CloudEventAttributes (requiredAttributes );
189
192
}
190
193
191
194
/**
192
- * Will construct instance of {@link CloudEventAttributesHelper }
195
+ * Will construct instance of {@link CloudEventAttributes }
193
196
* Should default/generate cloud event ID and SPECVERSION.
194
197
*
195
198
* @param ce_source value for Cloud Event 'source' attribute
196
199
* @param ce_type value for Cloud Event 'type' attribute
197
- * @return instance of {@link CloudEventAttributesHelper }
200
+ * @return instance of {@link CloudEventAttributes }
198
201
*/
199
- public static CloudEventAttributesHelper get (String ce_source , String ce_type ) {
202
+ public static CloudEventAttributes get (String ce_source , String ce_type ) {
200
203
return get (UUID .randomUUID ().toString (), "1.0" , ce_source , ce_type );
201
204
}
202
205
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
+ // }
215
218
216
219
217
220
@ SuppressWarnings ("unchecked" )
218
221
public static Message <?> toBinary (Message <?> inputMessage , MessageConverter messageConverter ) {
219
222
220
223
Map <String , Object > headers = inputMessage .getHeaders ();
221
- CloudEventAttributesHelper attributes = new CloudEventAttributesHelper (headers );
224
+ CloudEventAttributes attributes = new CloudEventAttributes (headers );
222
225
223
226
// first check the obvious and see if content-type is `cloudevents`
224
227
if (!attributes .isValidCloudEvent () && headers .containsKey (MessageHeaders .CONTENT_TYPE )) {
@@ -265,7 +268,7 @@ else if (structuredCloudEvent.containsKey(CloudEventMessageUtils.DATA)) {
265
268
}
266
269
Assert .notNull (data , "'data' must not be null" );
267
270
MessageBuilder <?> builder = MessageBuilder .withPayload (data );
268
- CloudEventAttributesHelper attributes = new CloudEventAttributesHelper (structuredCloudEvent );
271
+ CloudEventAttributes attributes = new CloudEventAttributes (structuredCloudEvent );
269
272
builder .setHeader (prefixToUse + CloudEventMessageUtils .ID , attributes .getId ());
270
273
builder .setHeader (prefixToUse + CloudEventMessageUtils .SOURCE , attributes .getSource ());
271
274
builder .setHeader (prefixToUse + CloudEventMessageUtils .TYPE , attributes .getType ());
@@ -282,4 +285,16 @@ public static String determinePrefixToUse(Message<?> inputMessage) {
282
285
return CloudEventMessageUtils .ATTR_PREFIX ;
283
286
}
284
287
}
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
+ }
285
300
}
0 commit comments