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>
AbstractHttpAsyncResponseConsumer
implementation 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 T
buildResult(HttpContext context)
Invoked to generate a result object from the received HTTP response message.boolean
cancel()
Cancels the ongoing operation or process.void
close()
void
consumeContent(ContentDecoder decoder, IOControl ioControl)
UseonContentReceived(ContentDecoder, IOControl)
instead.void
failed(java.lang.Exception ex)
Invoked to signal that the response processing terminated abnormally.protected ContentType
getContentType(HttpEntity entity)
java.lang.Exception
getException()
Returns an exception in case of an abnormal termination.T
getResult()
Returns a result of the response processing, when available.boolean
isDone()
Determines whether or not the response processing completed.protected void
onClose()
Invoked when the consumer is being closed.protected abstract void
onContentReceived(ContentDecoder decoder, IOControl ioControl)
Invoked to process a chunk of content from theContentDecoder
.protected abstract void
onEntityEnclosed(HttpEntity entity, ContentType contentType)
Invoked if the response message encloses a content entity.protected abstract void
onResponseReceived(HttpResponse response)
Invoked when a HTTP response message is received.protected abstract void
releaseResources()
Invoked to release all system resources currently allocated.void
responseCompleted(HttpContext context)
UsebuildResult(HttpContext)
instead.void
responseReceived(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
. TheIOControl
interface 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.IOException
Invoked 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:
responseReceived
in 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:
consumeContent
in 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:
responseCompleted
in interfaceHttpAsyncResponseConsumer<T>
- Parameters:
context
- HTTP context
-
cancel
public final boolean cancel()
Description copied from interface:Cancellable
Cancels the ongoing operation or process.- Specified by:
cancel
in interfaceCancellable
- Returns:
true
if the operation or process has been cancelled as a result of this method call orfalse
if it has already been cancelled or not started.
-
failed
public final void failed(java.lang.Exception ex)
Description copied from interface:HttpAsyncResponseConsumer
Invoked to signal that the response processing terminated abnormally.- Specified by:
failed
in interfaceHttpAsyncResponseConsumer<T>
- Parameters:
ex
- exception
-
close
public final void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
getException
public java.lang.Exception getException()
Description copied from interface:HttpAsyncResponseConsumer
Returns an exception in case of an abnormal termination. This method returnsnull
if the response processing is still ongoing or if it completed successfully.- Specified by:
getException
in interfaceHttpAsyncResponseConsumer<T>
- See Also:
HttpAsyncResponseConsumer.isDone()
-
getResult
public T getResult()
Description copied from interface:HttpAsyncResponseConsumer
Returns a result of the response processing, when available. This method returnsnull
if the response processing is still ongoing.- Specified by:
getResult
in interfaceHttpAsyncResponseConsumer<T>
- See Also:
HttpAsyncResponseConsumer.isDone()
-
isDone
public boolean isDone()
Description copied from interface:HttpAsyncResponseConsumer
Determines 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:
isDone
in interfaceHttpAsyncResponseConsumer<T>
-
-