public interface IHttpConversation
HttpConversationManager.create(java.net.URL)
and as such it belongs to it's creating manager throughout
it's lifecycle. Make sure to become familiar with the concepts layed down by the manager before using this interface.
After having instantiated a new object from this type it's in the preparation state. At this time headers and parameters can be added, setters can be invoked. After having called the
start()
method the conversation begins and no more modifications are allowed anymore.
The response listener is mandatory. The request listener is optional in case the user does not want to inject additional headers or a body.
If not set explicitly then a conversation by default is configured with the HTTP GET method.
For efficiency reasons, as recommended by HttpURLConnection
, by default all conversations are configured to use chunked transfer mode using the default chunk size.
The default Content-Type header for methods allowing a request body is 'text/plain'.
Modifier and Type | Method and Description |
---|---|
IHttpConversation |
addHeader(java.lang.String name,
java.lang.String value)
Adds a new request header.
|
IHttpConversation |
addParameter(java.lang.String name,
java.lang.String value)
Adds a new request parameter.
|
void |
cancel()
Cancels the conversation after it has been started.
|
java.lang.String |
getName()
Returns the name of this conversation.
|
ITextBasedResponseDetector |
getTextBasedResponseDetector()
Returns the text-based response detector of this conversation.
|
boolean |
isCancelled()
Returns whether this converation has been cancelled, either explicitly using
cancel() or by one of the request/response filters. |
IHttpConversation |
setChunkSize(int chunkSize)
Sets the chunk size for the chunked transfer encoding.
|
IHttpConversation |
setContentLength(int length)
Sets the content length which is the total amount of bytes to be sent in the request body.
|
IHttpConversation |
setFlowListener(IConversationFlowListener listener)
Sets the flow listener.
|
IHttpConversation |
setMaximumRestarts(int maximumRestarts)
Sets the maximum number of flow restarts allowed.
|
IHttpConversation |
setMethod(HttpMethod method)
Sets the HTTP method to use.
|
IHttpConversation |
setName(java.lang.String name)
Sets the name of this conversation.
|
IHttpConversation |
setRequestListener(IRequestBodyListener listener)
Convenience method setting a request listener with
setRequestListener(IRequestListener) which sends only the body and does not make
use of the IRequestHeaderListener.onRequestHeaderSending(ISendEvent) method. |
IHttpConversation |
setRequestListener(IRequestListener listener)
Sets the request listener which is going to be invoked when the request is about to be sent.
|
IHttpConversation |
setResponseListener(IResponseListener listener)
Sets the response listener which is going to be invoked when the response has been received.
|
IHttpConversation |
setTextBasedResponseDetector(ITextBasedResponseDetector detector)
Sets the text-based response detector for this conversation.
|
IHttpConversation |
setTimeout(int timeoutInMillis)
Sets a connection timeout to be used for the underlying
HttpURLConnection s throughout this conversation. |
void |
start()
Starts the conversation.
|
IHttpConversation setName(java.lang.String name)
IContext
objects accessible from IBaseEvent
s.name
- the name, can be nulljava.lang.IllegalStateException
- in case the conversation has already startedjava.lang.String getName()
IHttpConversation addHeader(java.lang.String name, java.lang.String value)
Note that the final set of headers sent might be overwritten by the request filters and listeners during the conversation.
name
- the header name, must be non-nullvalue
- the value, can be nulljava.lang.IllegalStateException
- in case the conversation has already startedIHttpConversation addParameter(java.lang.String name, java.lang.String value)
Note that the final set of parameters sent might be overwritten by the request filters and listeners during the conversation.
name
- the parameter name, must be non-nullvalue
- the value, can be nulljava.lang.IllegalStateException
- in case the conversation has already startedIHttpConversation setMethod(HttpMethod method)
If the method set is allowed to have a request body and the 'Content-Type' header is not set then it's set to the default.
method
- the method, can be nulljava.lang.IllegalStateException
- in case the conversation has already startedIHttpConversation setContentLength(int length)
As content length and chunk size are mutually exclusive concepts, this method clears the chunk size.
length
- the number of bytes that are planned to be sent, must be positivejava.lang.IllegalStateException
- in case the conversation has already startedjava.lang.IllegalArgumentException
- in case the argument is zero or negativeIHttpConversation setChunkSize(int chunkSize)
As content length and chunk size are mutually exclusive concepts, this method clears the content length.
chunkSize
- the chunk size in bytes, must be positive or zero (for the default)java.lang.IllegalStateException
- in case the conversation has already startedjava.lang.IllegalArgumentException
- in case the argument is negativeIHttpConversation setMaximumRestarts(int maximumRestarts)
HttpConversationManager
. If the response filter chain singals to restart the conversation flow
meanwhile the maximum has already been reached then the conversation gets cancelled and the IConversationFlowListener
gets notified.
If the set value is less than or equals to zero then the response filter chain is not allowed to restart the flow at all.
maximumRestarts
- the maximum number of restartsjava.lang.IllegalStateException
- in case the conversation has already startedIHttpConversation setTimeout(int timeoutInMillis)
HttpURLConnection
s throughout this conversation. Setting a negative value disables the timeout as if
URLConnection.setConnectTimeout(int)
were not called. This results in a default timeout being used.timeoutInMillis
- the timeout in milliseconds or a negative value to revert to default behaviourIHttpConversation setTextBasedResponseDetector(ITextBasedResponseDetector detector)
HttpConversationManager
.detector
- the detector, can be nulljava.lang.IllegalStateException
- in case the conversation has already startedITextBasedResponseDetector getTextBasedResponseDetector()
IHttpConversation setFlowListener(IConversationFlowListener listener)
HttpConversationManager
.listener
- the listener, can be nulljava.lang.IllegalStateException
- in case the conversation has already startedIHttpConversation setRequestListener(IRequestListener listener)
listener
- the listener, can be nulljava.lang.IllegalStateException
- in case the conversation has already startedIHttpConversation setRequestListener(IRequestBodyListener listener)
setRequestListener(IRequestListener)
which sends only the body and does not make
use of the IRequestHeaderListener.onRequestHeaderSending(ISendEvent)
method.
In 99% of the cases, this method is going to suffice. Its main advantage is that it can be used with Java 8 lambdas.
listener
- the listener, can be nullIHttpConversation setResponseListener(IResponseListener listener)
listener
- the listener, must be non-nulljava.lang.IllegalStateException
- in case the conversation has already startedvoid start()
HttpConversationManager
which this instance has been created
with. Calling this method on an already started conversation has no effect.java.lang.IllegalStateException
- in case mandatory parameters are missing, such as request/response listeners, etc..void cancel()
java.lang.IllegalStateException
- in case the conversation was not yet startedboolean isCancelled()
cancel()
or by one of the request/response filters. Returns false in case the flow hasn't started yet.