Interface HttpDecoder<T>
- Type Parameters:
T
- the result type of being decoded
- All Superinterfaces:
StreamDecoder<HttpData,
T>
- All Known Implementing Classes:
ArmeriaMessageDeframer
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 TypeMethodDescriptiondefault void
processHeaders
(HttpHeaders in, StreamDecoderOutput<T> out) Decodes a non-informationalHttpHeaders
to N objects.default void
Decodes an informationalResponseHeaders
to N objects.default void
processTrailers
(HttpHeaders in, StreamDecoderOutput<T> out) Decodes atrailers
to N objects.default ByteBuf
Converts the specifiedStreamDecoder
type object into aByteBuf
that is added toStreamDecoderInput
.Methods inherited from interface com.linecorp.armeria.common.stream.StreamDecoder
process, processOnComplete, processOnError
-
Method Details
-
toByteBuf
Description copied from interface:StreamDecoder
Converts the specifiedStreamDecoder
type object into aByteBuf
that is added toStreamDecoderInput
.- Specified by:
toByteBuf
in interfaceStreamDecoder<HttpData,
T>
-
processInformationalHeaders
default void processInformationalHeaders(ResponseHeaders in, StreamDecoderOutput<T> out) throws Exception Decodes 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
-