java.lang.Object
io.cloudevents.http.HttpMessageFactory
This class provides a collection of methods to create
MessageReader
and MessageWriter for various HTTP APIs.-
Method Summary
Modifier and TypeMethodDescriptionstatic io.cloudevents.core.message.MessageReadercreateReader(Consumer<BiConsumer<String, String>> forEachHeader, byte[] body) Creates a newMessageReaderthat can read both structured and binary messages from a HTTP response (client) or request (server).static io.cloudevents.core.message.MessageReadercreateReader(Map<String, String> headers, byte[] body) Creates a newMessageReaderthat can read both structured and binary messages from a HTTP response (client) or request (server).static io.cloudevents.core.message.MessageReadercreateReaderFromMultimap(Map<String, List<String>> headers, byte[] body) Creates a newMessageReaderthat can read both structured and binary messages from a HTTP response (client) or request (server).static HttpMessageWritercreateWriter(BiConsumer<String, String> putHeader, Consumer<byte[]> sendBody) Creates a newMessageWriterthat can write both structured and binary messages to a HTTP response (server) or request (client).
-
Method Details
-
createReader
public static io.cloudevents.core.message.MessageReader createReader(Consumer<BiConsumer<String, String>> forEachHeader, byte[] body) Creates a newMessageReaderthat 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 functionbody- 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 newMessageReaderthat 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 mapbody- 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 newMessageReaderthat can read both structured and binary messages from a HTTP response (client) or request (server).- Parameters:
headers- http headers as multimapbody- 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 newMessageWriterthat 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
-