Class HttpMessageFactory

java.lang.Object
io.cloudevents.http.HttpMessageFactory

public final class HttpMessageFactory extends Object
This class provides a collection of methods to create MessageReader and MessageWriter for various HTTP APIs.
  • Method Details

    • createReader

      public static io.cloudevents.core.message.MessageReader createReader(Consumer<BiConsumer<String,String>> forEachHeader, byte[] body)
      Creates a new MessageReader that can read both structured and binary messages from a HTTP response (client) or request (server).
       Example of usage with HttpServletRequest:
       
       Consumer<BiConsumer<String,String>> forEachHeader = processHeader -> {
           Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
           while (headerNames.hasMoreElements()) {
               String name = headerNames.nextElement();
               processHeader.accept(name, httpServletRequest.getHeader(name));
      
           }
       };
       byte[] body = httpServletRequest.getInputStream().readAllBytes();
       HttpMessageFactory.createReader(forEachHeader, body);
       
       
      Parameters:
      forEachHeader - http headers visitor function
      body - nullable buffer of the body
      Returns:
      a message reader implementation with potentially an unknown encoding
      Throws:
      IllegalArgumentException - If, in case of binary mode, the spec version is invalid
    • createReader

      public static io.cloudevents.core.message.MessageReader createReader(Map<String,String> headers, byte[] body)
      Creates a new MessageReader that can read both structured and binary messages from a HTTP response (client) or request (server).
       This overload is equivalent to calling:
       HttpMessageFactory.createReader(headers::forEach, body);
       
      Parameters:
      headers - http headers as map
      body - nullable buffer of the body
      Returns:
      a message reader implementation with potentially an unknown encoding
      Throws:
      IllegalArgumentException - If, in case of binary mode, the spec version is invalid
    • createReaderFromMultimap

      public static io.cloudevents.core.message.MessageReader createReaderFromMultimap(Map<String,List<String>> headers, byte[] body)
      Creates a new MessageReader that can read both structured and binary messages from a HTTP response (client) or request (server).
      Parameters:
      headers - http headers as multimap
      body - nullable buffer of the body
      Returns:
      a message reader implementation with potentially an unknown encoding
      Throws:
      IllegalArgumentException - If, in case of binary mode, the spec version is invalid
    • createWriter

      public static HttpMessageWriter createWriter(BiConsumer<String,String> putHeader, Consumer<byte[]> sendBody)
      Creates a new MessageWriter that can write both structured and binary messages to a HTTP response (server) or request (client).
       Example of usage with HttpServletResponse:
       
       HttpMessageFactory.createWriter(
           httpServletResponse::addHeader,
           body -> {
               try {
                   if (body != null) {
                       httpServletResponse.setContentLength(body.length);
                       httpServletResponse.setStatus(HttpStatus.OK_200);
                       try (ServletOutputStream outputStream = httpServletResponse.getOutputStream()) {
                           outputStream.write(body);
                       }
                   } else {
                       httpServletResponse.setStatus(HttpStatus.NO_CONTENT_204);
                   }
               } catch (IOException e) {
                   throw new UncheckedIOException(e);
               }
       });
       
       
      Parameters:
      putHeader - a function that puts header into HTTP request or response.
      sendBody - a function that sends body (e.g. sets HTTP status code, content-length and writes the bytes into output stream).
      Returns:
      a message writer