public abstract class HttpClient extends java.lang.Object
HttpURLConnection
.Modifier and Type | Class and Description |
---|---|
static interface |
HttpClient.ConnectionResponse
A wrapper for the HTTP connection response.
|
static interface |
HttpClient.HttpClientFactory
HTTP client factory.
|
static class |
HttpClient.Response
A wrapper for the HTTP response.
|
Modifier and Type | Field and Description |
---|---|
private int |
connectTimeout |
private boolean |
debug |
private static HttpClient.HttpClientFactory |
factory |
private boolean |
finishOnCloseOutput |
private java.util.Map<java.lang.String,java.lang.String> |
headers |
private long |
ifModifiedSince |
private int |
maxRedirects |
private java.lang.String |
outputMessage |
private int |
readTimeout |
private java.lang.String |
reasonForRequest |
private byte[] |
requestBody |
private java.lang.String |
requestMethod |
private HttpClient.Response |
response |
private static java.util.regex.Pattern |
TOMCAT_ERR_MESSAGE |
private java.net.URL |
url |
private boolean |
useCache |
Modifier | Constructor and Description |
---|---|
protected |
HttpClient(java.net.URL url,
java.lang.String requestMethod)
Constructs a new
HttpClient . |
Modifier and Type | Method and Description |
---|---|
protected abstract HttpClient.Response |
buildResponse(ProgressMonitor progressMonitor) |
HttpClient.Response |
connect()
Opens the HTTP connection.
|
HttpClient.Response |
connect(ProgressMonitor progressMonitor)
Opens the HTTP connection.
|
static HttpClient |
create(java.net.URL url)
Creates a new instance for the given URL and a
GET request |
static HttpClient |
create(java.net.URL url,
java.lang.String requestMethod)
Creates a new instance for the given URL and a
GET request |
abstract void |
disconnect()
Disconnect client.
|
int |
getConnectTimeout()
Returns the connect timeout.
|
java.util.Map<java.lang.String,java.lang.String> |
getHeaders()
Returns the headers.
|
long |
getIfModifiedSince()
Returns the
If-Modified-Since header value. |
protected java.lang.String |
getOutputMessage()
Returns the output message.
|
int |
getReadTimeout()
Returns the read timeout.
|
java.lang.String |
getReasonForRequest()
Returns the reason for request.
|
byte[] |
getRequestBody()
Returns the request body set for this connection.
|
java.lang.String |
getRequestHeader(java.lang.String header)
Returns the set value for the given
header . |
java.lang.String |
getRequestMethod()
Returns the request method set for this connection.
|
HttpClient.Response |
getResponse()
Returns the HTTP response which is set only after calling
connect() . |
static java.util.regex.Matcher |
getTomcatErrorMatcher(java.lang.String data)
Returns a
Matcher against predefined Tomcat error messages. |
java.net.URL |
getURL()
Returns the URL set for this connection.
|
boolean |
hasRequestBody()
Determines if a non-empty request body has been set for this connection.
|
protected boolean |
isFinishOnCloseOutput()
Determines whether the progress monitor task will be finished when the output stream is closed.
|
static boolean |
isRedirect(int statusCode)
Determines if the given status code is an HTTP redirection.
|
boolean |
isUseCache()
Determines whether not to set header
Cache-Control=no-cache . |
HttpClient |
keepAlive(boolean keepAlive)
Sets whether not to set header
Connection=close |
protected void |
logRequest(java.lang.String pattern,
java.lang.Object... args) |
protected void |
logRequestBody() |
protected void |
notifyConnect(ProgressMonitor progressMonitor) |
protected abstract HttpClient.ConnectionResponse |
performConnection() |
protected abstract void |
performDisconnection() |
boolean |
requiresBody()
Determines if the underlying HTTP method requires a body.
|
HttpClient |
setAccept(java.lang.String accept)
Sets the
Accept header. |
HttpClient |
setConnectTimeout(int connectTimeout)
Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced
by this URLConnection.
|
static void |
setFactory(HttpClient.HttpClientFactory newFactory)
Registers a new HTTP client factory.
|
HttpClient |
setFinishOnCloseOutput(boolean finishOnCloseOutput)
Sets whether the progress monitor task will be finished when the output stream is closed.
|
HttpClient |
setHeader(java.lang.String key,
java.lang.String value)
Sets an arbitrary HTTP header.
|
HttpClient |
setHeaders(java.util.Map<java.lang.String,java.lang.String> headers)
Sets arbitrary HTTP headers.
|
HttpClient |
setIfModifiedSince(long ifModifiedSince)
Sets the
If-Modified-Since header. |
HttpClient |
setLogAtDebug(boolean debug)
Sets the connect log at DEBUG level instead of the default INFO level.
|
HttpClient |
setMaxRedirects(int maxRedirects)
Sets the maximum number of redirections to follow.
|
HttpClient |
setOutputMessage(java.lang.String outputMessage)
Sets the output message to be displayed in progress monitor for
PUT , POST and DELETE methods. |
HttpClient |
setReadTimeout(int readTimeout)
Sets the read timeout to a specified timeout, in milliseconds.
|
HttpClient |
setReasonForRequest(java.lang.String reasonForRequest)
Sets a reason to show on console.
|
HttpClient |
setRequestBody(byte[] requestBody)
Sets the request body for
PUT /POST requests. |
protected abstract void |
setupConnection(ProgressMonitor progressMonitor) |
HttpClient |
useCache(boolean useCache)
Sets whether not to set header
Cache-Control=no-cache . |
private java.net.URL url
private final java.lang.String requestMethod
private int connectTimeout
private int readTimeout
private byte[] requestBody
private long ifModifiedSince
private final java.util.Map<java.lang.String,java.lang.String> headers
private int maxRedirects
private boolean useCache
private java.lang.String reasonForRequest
private java.lang.String outputMessage
private HttpClient.Response response
private boolean finishOnCloseOutput
private boolean debug
private static final java.util.regex.Pattern TOMCAT_ERR_MESSAGE
private static HttpClient.HttpClientFactory factory
protected HttpClient(java.net.URL url, java.lang.String requestMethod)
HttpClient
.url
- URL to accessrequestMethod
- HTTP request method (GET, POST, PUT, DELETE...)public static void setFactory(HttpClient.HttpClientFactory newFactory)
newFactory
- new HTTP client factorypublic final HttpClient.Response connect() throws java.io.IOException
java.io.IOException
- if any I/O error occurspublic final HttpClient.Response connect(ProgressMonitor progressMonitor) throws java.io.IOException
progressMonitor
- progress monitorjava.io.IOException
- if any I/O error occursprotected abstract void setupConnection(ProgressMonitor progressMonitor) throws java.io.IOException
java.io.IOException
protected abstract HttpClient.ConnectionResponse performConnection() throws java.io.IOException
java.io.IOException
protected abstract void performDisconnection() throws java.io.IOException
java.io.IOException
protected abstract HttpClient.Response buildResponse(ProgressMonitor progressMonitor) throws java.io.IOException
java.io.IOException
protected final void notifyConnect(ProgressMonitor progressMonitor)
protected final void logRequest(java.lang.String pattern, java.lang.Object... args)
protected final void logRequestBody()
public final HttpClient.Response getResponse()
connect()
.
Calling this method again, returns the identical object (unless another connect()
is performed).public static HttpClient create(java.net.URL url)
GET
requesturl
- the URLpublic static HttpClient create(java.net.URL url, java.lang.String requestMethod)
GET
requesturl
- the URLrequestMethod
- the HTTP request method to perform when callingpublic final java.net.URL getURL()
create(URL)
,
create(URL, String)
public final byte[] getRequestBody()
public final boolean hasRequestBody()
true
if the request body is set and non-emptypublic final boolean requiresBody()
true
if the underlying HTTP method requires a bodypublic final java.lang.String getRequestMethod()
create(URL, String)
public final java.lang.String getRequestHeader(java.lang.String header)
header
.header
- HTTP header namepublic final int getConnectTimeout()
public final int getReadTimeout()
public final long getIfModifiedSince()
If-Modified-Since
header value.If-Modified-Since
header valuepublic final boolean isUseCache()
Cache-Control=no-cache
.
By default, useCache
is true, i.e., the header Cache-Control=no-cache
is not sent.Cache-Control=no-cache
public final java.util.Map<java.lang.String,java.lang.String> getHeaders()
public final java.lang.String getReasonForRequest()
protected final java.lang.String getOutputMessage()
protected final boolean isFinishOnCloseOutput()
true
by default.public final HttpClient useCache(boolean useCache)
Cache-Control=no-cache
.
By default, useCache
is true, i.e., the header Cache-Control=no-cache
is not sent.useCache
- whether not to set header Cache-Control=no-cache
this
URLConnection.setUseCaches(boolean)
public final HttpClient keepAlive(boolean keepAlive)
Connection=close
This might fix #7640, see here.
keepAlive
- whether not to set header Connection=close
this
public final HttpClient setConnectTimeout(int connectTimeout)
SocketTimeoutException
is raised. A timeout of zero is interpreted as an infinite timeout.connectTimeout
- an int
that specifies the connect timeout value in millisecondsthis
URLConnection.setConnectTimeout(int)
public final HttpClient setReadTimeout(int readTimeout)
SocketTimeoutException
is raised. A timeout of zero is interpreted as an infinite timeout.readTimeout
- an int
that specifies the read timeout value in millisecondsthis
URLConnection.setReadTimeout(int)
public final HttpClient setAccept(java.lang.String accept)
Accept
header.accept
- header valuethis
public final HttpClient setRequestBody(byte[] requestBody)
PUT
/POST
requests.requestBody
- request bodythis
public final HttpClient setIfModifiedSince(long ifModifiedSince)
If-Modified-Since
header.ifModifiedSince
- header valuethis
public final HttpClient setMaxRedirects(int maxRedirects)
maxRedirects
to -1
in order to ignore redirects, i.e.,
to not throw an IOException
in connect()
.maxRedirects
- header valuethis
public final HttpClient setHeader(java.lang.String key, java.lang.String value)
key
- header namevalue
- header valuethis
public final HttpClient setHeaders(java.util.Map<java.lang.String,java.lang.String> headers)
headers
- HTTP headersthis
public final HttpClient setReasonForRequest(java.lang.String reasonForRequest)
null
if no reason is given.reasonForRequest
- Reason to showthis
public final HttpClient setOutputMessage(java.lang.String outputMessage)
PUT
, POST
and DELETE
methods.
Defaults to "Uploading data ..." (translated). Has no effect for GET
or any other method.outputMessage
- message to be displayed in progress monitorthis
public final HttpClient setFinishOnCloseOutput(boolean finishOnCloseOutput)
true
by default.finishOnCloseOutput
- whether the progress monitor task will be finished when the output stream is closedthis
public final HttpClient setLogAtDebug(boolean debug)
debug
- true
to set the connect log at DEBUG levelthis
public static boolean isRedirect(int statusCode)
statusCode
- HTTP status codetrue
if the given status code is an HTTP redirectionpublic abstract void disconnect()
HttpURLConnection.disconnect()
public static java.util.regex.Matcher getTomcatErrorMatcher(java.lang.String data)
Matcher
against predefined Tomcat error messages.
If it matches, error message can be extracted from group(1)
.data
- HTML contents to checkMatcher
against predefined Tomcat error messages