Class CloudEventHttpUtils

java.lang.Object
io.cloudevents.spring.http.CloudEventHttpUtils

public class CloudEventHttpUtils extends Object
Miscellaneous utility methods to assist with Cloud Events in the context of Spring Web frameworks. Primarily intended for the internal use within Spring-based frameworks or integrations.
Since:
2.0
Author:
Dave Syer
  • Method Details

    • toReader

      public static io.cloudevents.core.message.MessageReader toReader(HttpHeaders headers, Supplier<byte[]> body) throws io.cloudevents.rw.CloudEventRWException
      Create a MessageReader to convert an HTTP request to a CloudEvent.
      Parameters:
      headers - the HTTP request headers
      body - the HTTP request body as a byte array
      Returns:
      a MessageReader representing the CloudEvent
      Throws:
      io.cloudevents.rw.CloudEventRWException - if something goes wrong while resolving the SpecVersion or if the message has unknown encoding
    • toWriter

      public static io.cloudevents.http.impl.HttpMessageWriter toWriter(HttpHeaders headers, Consumer<byte[]> sendBody)
      Create an HttpMessageWriter that can hand off a CloudEvent to an HTTP response. Mainly useful in a blocking (not async) setting because the response body has to be consumed directly.
      Parameters:
      headers - the response headers (will be mutated)
      sendBody - a consumer for the response body that puts the bytes on the wire
    • toHttp

      public static HttpHeaders toHttp(io.cloudevents.CloudEventContext event) throws io.cloudevents.rw.CloudEventRWException
      Helper method for extracting HttpHeaders from a CloudEvent. Can, for instance, be used in a RequestMapping to return a ResponseEntity that has headers copied from a CloudEvent.
      Parameters:
      event - the input CloudEvent
      Returns:
      the response headers represented by the event
      Throws:
      io.cloudevents.rw.CloudEventRWException - if something goes wrong while writing the context to the http headers
    • fromHttp

      public static io.cloudevents.core.builder.CloudEventBuilder fromHttp(HttpHeaders headers) throws io.cloudevents.rw.CloudEventRWException
      Helper method for converting HttpHeaders to a CloudEvent. The input headers must represent a valid event in "binary" form, i.e. it must have headers ce-id, ce-specversion etc.
      Parameters:
      headers - the input request headers
      Returns:
      a CloudEventBuilder that can be used to create a new CloudEvent
      Throws:
      io.cloudevents.rw.CloudEventRWException - if something goes wrong while reading the context from the http headers