Interface HttpDecoder<T>
- Type Parameters:
T
- the result type of being decoded
- All Known Implementing Classes:
ArmeriaMessageDeframer
@UnstableApi public interface HttpDecoder<T>
Decodes a stream of
HttpObject
s to N objects.
Follow the below steps to decode HTTP payload using HttpDecoder
.
- Implement your decoding logic in
HttpDecoder
.> class FixedLengthDecoder implements HttpDecoder<String> { > private final int length; > > FixedLengthDecoder(int length) { > this.length = length; > } > > @Override > public void process(HttpDecoderInput in, HttpDecoderOutput<String> out) { > int remaining = in.readableBytes(); > if (remaining < length) { > // The input is not enough to process. Waiting for more data. > return; > } > > do { > // Read data from 'HttpDecoderInput' and > // write the processed result to 'HttpDecoderOutput'. > ByteBuf buf = in.readBytes(length); > out.add(buf.toString(StandardCharsets.UTF_8)); > // Should release the returned 'ByteBuf' > buf.release(); > remaining -= length; > } while (remaining >= length); > } > }
- Create a decoded
StreamMessage
usingHttpMessage.decode(HttpDecoder)
with theHttpDecoder
instance.FixedLengthDecoder decoder = new FixedLengthDecoder(11); HttpRequest req = ...; StreamMessage<String> decoded = req.decode(decoder);
- Subscribe to the
Publisher
of the decoded data and connect to your business logic.import reactor.core.publisher.Flux; Flux.from(decoded).map(...); // Consume and manipulate the decoded data.
-
Method Summary
Modifier and Type Method Description void
process(HttpDecoderInput in, HttpDecoderOutput<T> out)
Decodes a stream ofHttpData
s to N objects.default void
processHeaders(HttpHeaders in, HttpDecoderOutput<T> out)
Decodes a non-informationalHttpHeaders
to N objects.default void
processInformationalHeaders(ResponseHeaders in, HttpDecoderOutput<T> out)
Decodes an informationalResponseHeaders
to N objects.default void
processOnError(Throwable cause)
Invoked when aThrowable
is raised while deframing.default void
processTrailers(HttpHeaders in, HttpDecoderOutput<T> out)
Decodes atrailers
to N objects.
-
Method Details
-
process
Decodes a stream ofHttpData
s to N objects. This method will be called whenever anHttpData
is signaled fromPublisher
.- Throws:
Exception
-
processInformationalHeaders
default void processInformationalHeaders(ResponseHeaders in, HttpDecoderOutput<T> out) throws ExceptionDecodes an informationalResponseHeaders
to N objects.- Throws:
Exception
-
processHeaders
Decodes a non-informationalHttpHeaders
to N objects.- Throws:
Exception
-
processTrailers
Decodes atrailers
to N objects.- Throws:
Exception
-
processOnError
Invoked when aThrowable
is raised while deframing.
-