public final class HttpMessageFactory extends Object
MessageReader
and MessageWriter for various HTTP APIs.| Modifier and Type | Method and Description |
|---|---|
static 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). |
static 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). |
static 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). |
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). |
public static MessageReader createReader(Consumer<BiConsumer<String,String>> forEachHeader, byte[] body)
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);
forEachHeader - http headers visitor functionbody - nullable buffer of the bodyIllegalArgumentException - If, in case of binary mode, the spec version is invalidpublic static MessageReader createReader(Map<String,String> headers, byte[] body)
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);
headers - http headers as mapbody - nullable buffer of the bodyIllegalArgumentException - If, in case of binary mode, the spec version is invalidpublic static MessageReader createReaderFromMultimap(Map<String,List<String>> headers, byte[] body)
MessageReader that can read both structured and binary messages from a HTTP response (client) or request (server).headers - http headers as multimapbody - nullable buffer of the bodyIllegalArgumentException - If, in case of binary mode, the spec version is invalidpublic static HttpMessageWriter createWriter(BiConsumer<String,String> putHeader, Consumer<byte[]> sendBody)
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);
}
});
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).Copyright © 2024. All rights reserved.