Package org.apache.http.nio.protocol
Class AbstractAsyncResponseConsumer<T>
- java.lang.Object
-
- org.apache.http.nio.protocol.AbstractAsyncResponseConsumer<T>
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,Cancellable,HttpAsyncResponseConsumer<T>
- Direct Known Subclasses:
BasicAsyncResponseConsumer
public abstract class AbstractAsyncResponseConsumer<T> extends java.lang.Object implements HttpAsyncResponseConsumer<T>
AbstractHttpAsyncResponseConsumerimplementation that relieves its subclasses from having to manage internal state and provides a number of protected event methods that they need to implement.- Since:
- 4.2
-
-
Constructor Summary
Constructors Constructor Description AbstractAsyncResponseConsumer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract TbuildResult(HttpContext context)Invoked to generate a result object from the received HTTP response message.booleancancel()Cancels the ongoing operation or process.voidclose()voidconsumeContent(ContentDecoder decoder, IOControl ioControl)UseonContentReceived(ContentDecoder, IOControl)instead.voidfailed(java.lang.Exception ex)Invoked to signal that the response processing terminated abnormally.protected ContentTypegetContentType(HttpEntity entity)java.lang.ExceptiongetException()Returns an exception in case of an abnormal termination.TgetResult()Returns a result of the response processing, when available.booleanisDone()Determines whether or not the response processing completed.protected voidonClose()Invoked when the consumer is being closed.protected abstract voidonContentReceived(ContentDecoder decoder, IOControl ioControl)Invoked to process a chunk of content from theContentDecoder.protected abstract voidonEntityEnclosed(HttpEntity entity, ContentType contentType)Invoked if the response message encloses a content entity.protected abstract voidonResponseReceived(HttpResponse response)Invoked when a HTTP response message is received.protected abstract voidreleaseResources()Invoked to release all system resources currently allocated.voidresponseCompleted(HttpContext context)UsebuildResult(HttpContext)instead.voidresponseReceived(HttpResponse response)UseonResponseReceived(HttpResponse)instead.
-
-
-
Field Detail
-
completed
private final java.util.concurrent.atomic.AtomicBoolean completed
-
result
private volatile T result
-
ex
private volatile java.lang.Exception ex
-
-
Method Detail
-
onResponseReceived
protected abstract void onResponseReceived(HttpResponse response) throws HttpException, java.io.IOException
Invoked when a HTTP response message is received. Please note that theonContentReceived(ContentDecoder, IOControl)method will be invoked only if the response messages has a content entity enclosed.- Parameters:
response- HTTP response message.- Throws:
HttpException- in case of HTTP protocol violationjava.io.IOException- in case of an I/O error
-
onContentReceived
protected abstract void onContentReceived(ContentDecoder decoder, IOControl ioControl) throws java.io.IOException
Invoked to process a chunk of content from theContentDecoder. TheIOControlinterface can be used to suspend input events if the consumer is temporarily unable to consume more content.The consumer can use the
ContentDecoder.isCompleted()method to find out whether or not the message content has been fully consumed.- Parameters:
decoder- content decoder.ioControl- I/O control of the underlying connection.- Throws:
java.io.IOException- in case of an I/O error
-
onEntityEnclosed
protected abstract void onEntityEnclosed(HttpEntity entity, ContentType contentType) throws java.io.IOException
Invoked if the response message encloses a content entity.- Parameters:
entity- HTTP entitycontentType- expected content type.- Throws:
java.io.IOException- in case of an I/O error
-
buildResult
protected abstract T buildResult(HttpContext context) throws java.lang.Exception
Invoked to generate a result object from the received HTTP response message.- Parameters:
context- HTTP context.- Returns:
- result of the response processing.
- Throws:
java.lang.Exception- in case of an abnormal termination.
-
releaseResources
protected abstract void releaseResources()
Invoked to release all system resources currently allocated.
-
onClose
protected void onClose() throws java.io.IOExceptionInvoked when the consumer is being closed.- Throws:
java.io.IOException- may be thrown by subclassses- Since:
- 4.3
-
getContentType
protected ContentType getContentType(HttpEntity entity)
- Since:
- 4.4
-
responseReceived
public final void responseReceived(HttpResponse response) throws java.io.IOException, HttpException
UseonResponseReceived(HttpResponse)instead.- Specified by:
responseReceivedin interfaceHttpAsyncResponseConsumer<T>- Parameters:
response- HTTP response message.- Throws:
java.io.IOException- in case of an I/O errorHttpException- in case of HTTP protocol violation
-
consumeContent
public final void consumeContent(ContentDecoder decoder, IOControl ioControl) throws java.io.IOException
UseonContentReceived(ContentDecoder, IOControl)instead.- Specified by:
consumeContentin interfaceHttpAsyncResponseConsumer<T>- Parameters:
decoder- content decoder.ioControl- I/O control of the underlying connection.- Throws:
java.io.IOException- in case of an I/O error
-
responseCompleted
public final void responseCompleted(HttpContext context)
UsebuildResult(HttpContext)instead.- Specified by:
responseCompletedin interfaceHttpAsyncResponseConsumer<T>- Parameters:
context- HTTP context
-
cancel
public final boolean cancel()
Description copied from interface:CancellableCancels the ongoing operation or process.- Specified by:
cancelin interfaceCancellable- Returns:
trueif the operation or process has been cancelled as a result of this method call orfalseif it has already been cancelled or not started.
-
failed
public final void failed(java.lang.Exception ex)
Description copied from interface:HttpAsyncResponseConsumerInvoked to signal that the response processing terminated abnormally.- Specified by:
failedin interfaceHttpAsyncResponseConsumer<T>- Parameters:
ex- exception
-
close
public final void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
getException
public java.lang.Exception getException()
Description copied from interface:HttpAsyncResponseConsumerReturns an exception in case of an abnormal termination. This method returnsnullif the response processing is still ongoing or if it completed successfully.- Specified by:
getExceptionin interfaceHttpAsyncResponseConsumer<T>- See Also:
HttpAsyncResponseConsumer.isDone()
-
getResult
public T getResult()
Description copied from interface:HttpAsyncResponseConsumerReturns a result of the response processing, when available. This method returnsnullif the response processing is still ongoing.- Specified by:
getResultin interfaceHttpAsyncResponseConsumer<T>- See Also:
HttpAsyncResponseConsumer.isDone()
-
isDone
public boolean isDone()
Description copied from interface:HttpAsyncResponseConsumerDetermines whether or not the response processing completed. If the response processing terminated normallyHttpAsyncResponseConsumer.getResult()can be used to obtain the result. If the response processing terminated abnormallyHttpAsyncResponseConsumer.getException()can be used to obtain the cause.- Specified by:
isDonein interfaceHttpAsyncResponseConsumer<T>
-
-