public abstract class HttpReceiver
extends java.lang.Object
HttpReceiver
provides the abstract code to implement the various steps of the receive of HTTP responses.
HttpReceiver
maintains a state machine that is updated when the steps of receiving a response are executed.
Subclasses must handle the transport-specific details, for example how to read from the raw socket and how to parse the bytes read from the socket. Then they have to call the methods defined in this class in the following order:
responseBegin(HttpExchange)
, when the HTTP response data containing the HTTP status code
is availableresponseHeader(HttpExchange, HttpField)
, when an HTTP field is availableresponseHeaders(HttpExchange)
, when all HTTP headers are availableresponseContent(HttpExchange, ByteBuffer, Callback)
, when HTTP content is availableresponseSuccess(HttpExchange)
, when the response is successfulresponseFailure(Throwable)
to indicate that the response has failed
(for example, because of I/O exceptions).
At any time, user threads may abort the response which will cause responseFailure(Throwable)
to be
invoked.
The state machine maintained by this class ensures that the response steps are not executed by an I/O thread if the response has already been failed.
HttpSender
Modifier and Type | Class and Description |
---|---|
private class |
HttpReceiver.ContentListeners
Wraps a list of content listeners, notifies them about content events and
tracks individual listener demand to produce a global demand for content.
|
private class |
HttpReceiver.Decoder
Implements the decoding of content, producing decoded buffers only if there is demand for content.
|
private static class |
HttpReceiver.ResponseState
The request states
HttpReceiver goes through when receiving a response. |
Modifier and Type | Field and Description |
---|---|
private HttpChannel |
channel |
private HttpReceiver.ContentListeners |
contentListeners |
private HttpReceiver.Decoder |
decoder |
private long |
demand |
private java.lang.Throwable |
failure |
protected static Logger |
LOG |
private java.util.concurrent.atomic.AtomicReference<HttpReceiver.ResponseState> |
responseState |
private boolean |
stalled |
Modifier | Constructor and Description |
---|---|
protected |
HttpReceiver(HttpChannel channel) |
Modifier and Type | Method and Description |
---|---|
boolean |
abort(HttpExchange exchange,
java.lang.Throwable failure) |
private void |
cleanup() |
protected long |
demand() |
(package private) void |
demand(long n) |
private long |
demand(java.util.function.LongUnaryOperator operator) |
protected void |
dispose()
Disposes the state of this HttpReceiver.
|
protected HttpChannel |
getHttpChannel() |
protected HttpDestination |
getHttpDestination() |
protected HttpExchange |
getHttpExchange() |
protected boolean |
hasDemandOrStall() |
boolean |
isFailed() |
protected void |
receive() |
protected void |
reset()
Resets the state of this HttpReceiver.
|
protected boolean |
responseBegin(HttpExchange exchange)
Method to be invoked when the response status code is available.
|
protected boolean |
responseContent(HttpExchange exchange,
java.nio.ByteBuffer buffer,
Callback callback)
Method to be invoked when response HTTP content is available.
|
protected boolean |
responseFailure(java.lang.Throwable failure)
Method to be invoked when the response is failed.
|
protected boolean |
responseHeader(HttpExchange exchange,
HttpField field)
Method to be invoked when a response HTTP header is available.
|
protected boolean |
responseHeaders(HttpExchange exchange)
Method to be invoked after all response HTTP headers are available.
|
protected boolean |
responseSuccess(HttpExchange exchange)
Method to be invoked when the response is successful.
|
protected void |
storeCookie(java.net.URI uri,
HttpField field) |
private void |
terminateResponse(HttpExchange exchange) |
private void |
terminateResponse(HttpExchange exchange,
Result result) |
java.lang.String |
toString() |
private boolean |
updateResponseState(HttpReceiver.ResponseState from,
HttpReceiver.ResponseState to) |
protected static final Logger LOG
private final java.util.concurrent.atomic.AtomicReference<HttpReceiver.ResponseState> responseState
private final HttpChannel channel
private HttpReceiver.ContentListeners contentListeners
private HttpReceiver.Decoder decoder
private java.lang.Throwable failure
private long demand
private boolean stalled
protected HttpReceiver(HttpChannel channel)
protected HttpChannel getHttpChannel()
void demand(long n)
protected long demand()
private long demand(java.util.function.LongUnaryOperator operator)
protected boolean hasDemandOrStall()
protected HttpExchange getHttpExchange()
protected HttpDestination getHttpDestination()
public boolean isFailed()
protected void receive()
protected boolean responseBegin(HttpExchange exchange)
Subclasses must have set the response status code on the Response
object of the HttpExchange
prior invoking this method.
This method takes case of notifying Response.BeginListener
s.
exchange
- the HTTP exchangeprotected boolean responseHeader(HttpExchange exchange, HttpField field)
Subclasses must not have added the header to the Response
object of the HttpExchange
prior invoking this method.
This method takes case of notifying Response.HeaderListener
s and storing cookies.
exchange
- the HTTP exchangefield
- the response HTTP fieldprotected void storeCookie(java.net.URI uri, HttpField field)
protected boolean responseHeaders(HttpExchange exchange)
This method takes case of notifying Response.HeadersListener
s.
exchange
- the HTTP exchangeprotected boolean responseContent(HttpExchange exchange, java.nio.ByteBuffer buffer, Callback callback)
This method takes case of decoding the content, if necessary, and notifying Response.ContentListener
s.
exchange
- the HTTP exchangebuffer
- the response HTTP content buffercallback
- the callbackprotected boolean responseSuccess(HttpExchange exchange)
This method takes case of notifying Response.SuccessListener
s and possibly
Response.CompleteListener
s (if the exchange is completed).
exchange
- the HTTP exchangeprotected boolean responseFailure(java.lang.Throwable failure)
This method takes care of notifying Response.FailureListener
s.
failure
- the response failureprivate void terminateResponse(HttpExchange exchange)
private void terminateResponse(HttpExchange exchange, Result result)
protected void reset()
Subclasses should override (but remember to call super
) to reset their own state.
Either this method or dispose()
is called.
protected void dispose()
Subclasses should override (but remember to call super
) to dispose their own state.
Either this method or reset()
is called.
private void cleanup()
public boolean abort(HttpExchange exchange, java.lang.Throwable failure)
private boolean updateResponseState(HttpReceiver.ResponseState from, HttpReceiver.ResponseState to)
public java.lang.String toString()
toString
in class java.lang.Object