15
15
*/
16
16
package io .cloudevents .spring .http ;
17
17
18
- import java .util .function .Consumer ;
19
- import java .util .function .Supplier ;
20
-
21
18
import io .cloudevents .CloudEvent ;
22
19
import io .cloudevents .CloudEventContext ;
20
+ import io .cloudevents .SpecVersion ;
23
21
import io .cloudevents .core .CloudEventUtils ;
24
22
import io .cloudevents .core .builder .CloudEventBuilder ;
25
23
import io .cloudevents .core .message .MessageReader ;
26
24
import io .cloudevents .http .HttpMessageFactory ;
27
25
import io .cloudevents .http .impl .HttpMessageWriter ;
28
-
26
+ import io . cloudevents . rw . CloudEventRWException ;
29
27
import org .springframework .http .HttpHeaders ;
30
28
import org .springframework .http .ResponseEntity ;
31
29
30
+ import java .util .function .Consumer ;
31
+ import java .util .function .Supplier ;
32
+
32
33
/**
33
34
* Miscellaneous utility methods to assist with Cloud Events in the context of Spring Web
34
35
* frameworks. Primarily intended for the internal use within Spring-based frameworks or
@@ -42,54 +43,58 @@ public class CloudEventHttpUtils {
42
43
private CloudEventHttpUtils () {
43
44
}
44
45
45
- /**
46
- * Create a {@link MessageReader} to assist in conversion of an HTTP request to a
47
- * {@link CloudEvent}.
48
- * @param headers the HTTP request headers
49
- * @param body the HTTP request body as a byte array
50
- * @return a {@link MessageReader} representing the {@link CloudEvent}
51
- */
52
- public static MessageReader toReader (HttpHeaders headers , Supplier <byte []> body ) {
53
- return HttpMessageFactory .createReaderFromMultimap (headers , body .get ());
54
- }
46
+ /**
47
+ * Create a {@link MessageReader} to convert an HTTP request to a {@link CloudEvent}.
48
+ *
49
+ * @param headers the HTTP request headers
50
+ * @param body the HTTP request body as a byte array
51
+ * @return a {@link MessageReader} representing the {@link CloudEvent}
52
+ * @throws CloudEventRWException if something goes wrong while resolving the {@link SpecVersion} or if the message has unknown encoding
53
+ */
54
+ public static MessageReader toReader (HttpHeaders headers , Supplier <byte []> body ) throws CloudEventRWException {
55
+ return HttpMessageFactory .createReaderFromMultimap (headers , body .get ());
56
+ }
55
57
56
58
/**
57
59
* Create an {@link HttpMessageWriter} that can hand off a {@link CloudEvent} to an
58
60
* HTTP response. Mainly useful in a blocking (not async) setting because the response
59
61
* body has to be consumed directly.
62
+ *
60
63
* @param headers the response headers (will be mutated)
61
64
* @param sendBody a consumer for the response body that puts the bytes on the wire
62
65
*/
63
66
public static HttpMessageWriter toWriter (HttpHeaders headers , Consumer <byte []> sendBody ) {
64
67
return HttpMessageFactory .createWriter (headers ::set , sendBody );
65
- }
68
+ }
66
69
67
- /**
68
- * Helper method for extracting {@link HttpHeaders} from a {@link CloudEvent}. Can,
69
- * for instance, be used in a <code>@RequestMapping</code> to return a
70
- * {@link ResponseEntity} that has headers copied from a {@link CloudEvent}.
71
- * @param event the input {@link CloudEvent}
72
- * @return the response headers represented by the event
73
- */
74
- public static HttpHeaders toHttp (CloudEventContext event ) {
75
- HttpHeaders headers = new HttpHeaders ();
76
- CloudEventUtils .toReader (CloudEventBuilder .fromContext (event ).build ()).read (toWriter (headers , bytes -> {
77
- }));
78
- return headers ;
79
- }
70
+ /**
71
+ * Helper method for extracting {@link HttpHeaders} from a {@link CloudEvent}. Can,
72
+ * for instance, be used in a {@link org.springframework.web.bind.annotation.RequestMapping} to return a
73
+ * {@link ResponseEntity} that has headers copied from a {@link CloudEvent}.
74
+ *
75
+ * @param event the input {@link CloudEvent}
76
+ * @return the response headers represented by the event
77
+ * @throws CloudEventRWException if something goes wrong while writing the context to the http headers
78
+ */
79
+ public static HttpHeaders toHttp (CloudEventContext event ) throws CloudEventRWException {
80
+ HttpHeaders headers = new HttpHeaders ();
81
+ CloudEventUtils .toReader (CloudEventBuilder .fromContext (event ).build ()).read (toWriter (headers , bytes -> {
82
+ }));
83
+ return headers ;
84
+ }
80
85
81
- /**
82
- * Helper method for converting {@link HttpHeaders} to a {@link CloudEvent}. The input
83
- * headers must represent a valid event in "binary" form, i.e. it must have headers
84
- * " ce-id", " ce-specversion" etc.
85
- * @param headers the input request headers
86
- * @return a {@link CloudEventBuilder} that can be used to create a new
87
- * {@link CloudEvent}
88
- *
89
- */
90
- public static CloudEventBuilder fromHttp (HttpHeaders headers ) {
91
- return CloudEventBuilder
92
- .fromContext (CloudEventUtils .toEvent (CloudEventHttpUtils .toReader (headers , () -> null )));
86
+ /**
87
+ * Helper method for converting {@link HttpHeaders} to a {@link CloudEvent}. The input
88
+ * headers <b> must</b> represent a valid event in "binary" form, i.e. it must have headers
89
+ * {@code ce-id}, {@code ce-specversion} etc.
90
+ *
91
+ * @param headers the input request headers
92
+ * @return a {@link CloudEventBuilder} that can be used to create a new {@link CloudEvent}
93
+ * @throws CloudEventRWException if something goes wrong while reading the context from the http headers
94
+ */
95
+ public static CloudEventBuilder fromHttp (HttpHeaders headers ) throws CloudEventRWException {
96
+ return CloudEventBuilder
97
+ .fromContext (CloudEventUtils .toEvent (CloudEventHttpUtils .toReader (headers , () -> null )));
93
98
}
94
99
95
100
}
0 commit comments