Package org.openstreetmap.josm.tools
Class HttpClient
- java.lang.Object
-
- org.openstreetmap.josm.tools.HttpClient
-
- Direct Known Subclasses:
Http1Client
public abstract class HttpClient extends java.lang.Object
Provides uniform access for a HTTP/HTTPS server. This class should be used in favour ofHttpURLConnection
.- Since:
- 9168
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 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.
-
Field Summary
Fields Modifier and Type Field 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
-
Constructor Summary
Constructors Modifier Constructor Description protected
HttpClient(java.net.URL url, java.lang.String requestMethod)
Constructs a newHttpClient
.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract HttpClient.Response
buildResponse(ProgressMonitor progressMonitor)
HttpClient.Response
connect()
Opens the HTTP connection.HttpClient.Response
connect(ProgressMonitor progressMonitor)
Opens the HTTP connection.HttpClient.Response
connect(ProgressMonitor progressMonitor, java.lang.String authRedirectLocation, java.lang.String authRequestProperty)
Opens the HTTP connection.static HttpClient
create(java.net.URL url)
Creates a new instance for the given URL and aGET
requeststatic HttpClient
create(java.net.URL url, java.lang.String requestMethod)
Creates a new instance for the given URL and aGET
requestabstract 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 theIf-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 givenheader
.java.lang.String
getRequestMethod()
Returns the request method set for this connection.HttpClient.Response
getResponse()
Returns the HTTP response which is set only after callingconnect()
.static java.util.regex.Matcher
getTomcatErrorMatcher(java.lang.String data)
Returns aMatcher
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 headerCache-Control=no-cache
.HttpClient
keepAlive(boolean keepAlive)
Sets whether not to set headerConnection=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 theAccept
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 theIf-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 forPUT
,POST
andDELETE
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 forPUT
/POST
requests.protected abstract void
setupConnection(ProgressMonitor progressMonitor)
HttpClient
useCache(boolean useCache)
Sets whether not to set headerCache-Control=no-cache
.
-
-
-
Field Detail
-
url
private java.net.URL url
-
requestMethod
private final java.lang.String requestMethod
-
connectTimeout
private int connectTimeout
-
readTimeout
private int readTimeout
-
requestBody
private byte[] requestBody
-
ifModifiedSince
private long ifModifiedSince
-
headers
private final java.util.Map<java.lang.String,java.lang.String> headers
-
maxRedirects
private int maxRedirects
-
useCache
private boolean useCache
-
reasonForRequest
private java.lang.String reasonForRequest
-
outputMessage
private java.lang.String outputMessage
-
response
private HttpClient.Response response
-
finishOnCloseOutput
private boolean finishOnCloseOutput
-
debug
private boolean debug
-
TOMCAT_ERR_MESSAGE
private static final java.util.regex.Pattern TOMCAT_ERR_MESSAGE
-
factory
private static HttpClient.HttpClientFactory factory
-
-
Constructor Detail
-
HttpClient
protected HttpClient(java.net.URL url, java.lang.String requestMethod)
Constructs a newHttpClient
.- Parameters:
url
- URL to accessrequestMethod
- HTTP request method (GET, POST, PUT, DELETE...)
-
-
Method Detail
-
setFactory
public static void setFactory(HttpClient.HttpClientFactory newFactory)
Registers a new HTTP client factory.- Parameters:
newFactory
- new HTTP client factory- Since:
- 15229
-
connect
public final HttpClient.Response connect() throws java.io.IOException
Opens the HTTP connection.- Returns:
- HTTP response
- Throws:
java.io.IOException
- if any I/O error occurs
-
connect
public final HttpClient.Response connect(ProgressMonitor progressMonitor) throws java.io.IOException
Opens the HTTP connection.- Parameters:
progressMonitor
- progress monitor- Returns:
- HTTP response
- Throws:
java.io.IOException
- if any I/O error occurs- Since:
- 9179
-
connect
public final HttpClient.Response connect(ProgressMonitor progressMonitor, java.lang.String authRedirectLocation, java.lang.String authRequestProperty) throws java.io.IOException
Opens the HTTP connection.- Parameters:
progressMonitor
- progress monitorauthRedirectLocation
- The location where we will be redirected for authenticationauthRequestProperty
- The authorization header to set when being redirected to the auth location- Returns:
- HTTP response
- Throws:
java.io.IOException
- if any I/O error occurs- Since:
- 18913
-
setupConnection
protected abstract void setupConnection(ProgressMonitor progressMonitor) throws java.io.IOException
- Throws:
java.io.IOException
-
performConnection
protected abstract HttpClient.ConnectionResponse performConnection() throws java.io.IOException
- Throws:
java.io.IOException
-
performDisconnection
protected abstract void performDisconnection() throws java.io.IOException
- Throws:
java.io.IOException
-
buildResponse
protected abstract HttpClient.Response buildResponse(ProgressMonitor progressMonitor) throws java.io.IOException
- Throws:
java.io.IOException
-
notifyConnect
protected final void notifyConnect(ProgressMonitor progressMonitor)
-
logRequest
protected final void logRequest(java.lang.String pattern, java.lang.Object... args)
-
logRequestBody
protected final void logRequestBody()
-
getResponse
public final HttpClient.Response getResponse()
Returns the HTTP response which is set only after callingconnect()
. Calling this method again, returns the identical object (unless anotherconnect()
is performed).- Returns:
- the HTTP response
- Since:
- 9309
-
create
public static HttpClient create(java.net.URL url)
Creates a new instance for the given URL and aGET
request- Parameters:
url
- the URL- Returns:
- a new instance
-
create
public static HttpClient create(java.net.URL url, java.lang.String requestMethod)
Creates a new instance for the given URL and aGET
request- Parameters:
url
- the URLrequestMethod
- the HTTP request method to perform when calling- Returns:
- a new instance
-
getURL
public final java.net.URL getURL()
Returns the URL set for this connection.- Returns:
- the URL
- Since:
- 9172
- See Also:
create(URL)
,create(URL, String)
-
getRequestBody
public final byte[] getRequestBody()
Returns the request body set for this connection.- Returns:
- the HTTP request body, or null
- Since:
- 15229
-
hasRequestBody
public final boolean hasRequestBody()
Determines if a non-empty request body has been set for this connection.- Returns:
true
if the request body is set and non-empty- Since:
- 15229
-
requiresBody
public final boolean requiresBody()
Determines if the underlying HTTP method requires a body.- Returns:
true
if the underlying HTTP method requires a body- Since:
- 15229
-
getRequestMethod
public final java.lang.String getRequestMethod()
Returns the request method set for this connection.- Returns:
- the HTTP request method
- Since:
- 9172
- See Also:
create(URL, String)
-
getRequestHeader
public final java.lang.String getRequestHeader(java.lang.String header)
Returns the set value for the givenheader
.- Parameters:
header
- HTTP header name- Returns:
- HTTP header value
- Since:
- 9172
-
getConnectTimeout
public final int getConnectTimeout()
Returns the connect timeout.- Returns:
- the connect timeout, in milliseconds
- Since:
- 15229
-
getReadTimeout
public final int getReadTimeout()
Returns the read timeout.- Returns:
- the read timeout, in milliseconds
- Since:
- 15229
-
getIfModifiedSince
public final long getIfModifiedSince()
Returns theIf-Modified-Since
header value.- Returns:
- the
If-Modified-Since
header value - Since:
- 15229
-
isUseCache
public final boolean isUseCache()
Determines whether not to set headerCache-Control=no-cache
. By default,useCache
is true, i.e., the headerCache-Control=no-cache
is not sent.- Returns:
- whether not to set header
Cache-Control=no-cache
- Since:
- 15229
-
getHeaders
public final java.util.Map<java.lang.String,java.lang.String> getHeaders()
Returns the headers.- Returns:
- the headers
- Since:
- 15229
-
getReasonForRequest
public final java.lang.String getReasonForRequest()
Returns the reason for request.- Returns:
- the reason for request
- Since:
- 15229
-
getOutputMessage
protected final java.lang.String getOutputMessage()
Returns the output message.- Returns:
- the output message
-
isFinishOnCloseOutput
protected final boolean isFinishOnCloseOutput()
Determines whether the progress monitor task will be finished when the output stream is closed.true
by default.- Returns:
- the finishOnCloseOutput
-
useCache
public final HttpClient useCache(boolean useCache)
Sets whether not to set headerCache-Control=no-cache
. By default,useCache
is true, i.e., the headerCache-Control=no-cache
is not sent.- Parameters:
useCache
- whether not to set headerCache-Control=no-cache
- Returns:
this
- See Also:
URLConnection.setUseCaches(boolean)
-
keepAlive
public final HttpClient keepAlive(boolean keepAlive)
Sets whether not to set headerConnection=close
This might fix #7640, see here.
- Parameters:
keepAlive
- whether not to set headerConnection=close
- Returns:
this
-
setConnectTimeout
public final 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. If the timeout expires before the connection can be established, aSocketTimeoutException
is raised. A timeout of zero is interpreted as an infinite timeout.- Parameters:
connectTimeout
- anint
that specifies the connect timeout value in milliseconds- Returns:
this
- See Also:
URLConnection.setConnectTimeout(int)
-
setReadTimeout
public final HttpClient setReadTimeout(int readTimeout)
Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from input stream when a connection is established to a resource. If the timeout expires before there is data available for read, aSocketTimeoutException
is raised. A timeout of zero is interpreted as an infinite timeout.- Parameters:
readTimeout
- anint
that specifies the read timeout value in milliseconds- Returns:
this
- See Also:
URLConnection.setReadTimeout(int)
-
setAccept
public final HttpClient setAccept(java.lang.String accept)
Sets theAccept
header.- Parameters:
accept
- header value- Returns:
this
-
setRequestBody
public final HttpClient setRequestBody(byte[] requestBody)
Sets the request body forPUT
/POST
requests.- Parameters:
requestBody
- request body- Returns:
this
-
setIfModifiedSince
public final HttpClient setIfModifiedSince(long ifModifiedSince)
Sets theIf-Modified-Since
header.- Parameters:
ifModifiedSince
- header value- Returns:
this
-
setMaxRedirects
public final HttpClient setMaxRedirects(int maxRedirects)
Sets the maximum number of redirections to follow. SetmaxRedirects
to-1
in order to ignore redirects, i.e., to not throw anIOException
inconnect()
.- Parameters:
maxRedirects
- header value- Returns:
this
-
setHeader
public final HttpClient setHeader(java.lang.String key, java.lang.String value)
Sets an arbitrary HTTP header.- Parameters:
key
- header namevalue
- header value- Returns:
this
-
setHeaders
public final HttpClient setHeaders(java.util.Map<java.lang.String,java.lang.String> headers)
Sets arbitrary HTTP headers.- Parameters:
headers
- HTTP headers- Returns:
this
-
setReasonForRequest
public final HttpClient setReasonForRequest(java.lang.String reasonForRequest)
Sets a reason to show on console. Can benull
if no reason is given.- Parameters:
reasonForRequest
- Reason to show- Returns:
this
- Since:
- 9172
-
setOutputMessage
public final HttpClient setOutputMessage(java.lang.String outputMessage)
Sets the output message to be displayed in progress monitor forPUT
,POST
andDELETE
methods. Defaults to "Uploading data ..." (translated). Has no effect forGET
or any other method.- Parameters:
outputMessage
- message to be displayed in progress monitor- Returns:
this
- Since:
- 12711
-
setFinishOnCloseOutput
public final HttpClient setFinishOnCloseOutput(boolean finishOnCloseOutput)
Sets whether the progress monitor task will be finished when the output stream is closed. This istrue
by default.- Parameters:
finishOnCloseOutput
- whether the progress monitor task will be finished when the output stream is closed- Returns:
this
- Since:
- 10302
-
setLogAtDebug
public final HttpClient setLogAtDebug(boolean debug)
Sets the connect log at DEBUG level instead of the default INFO level.- Parameters:
debug
-true
to set the connect log at DEBUG level- Returns:
this
- Since:
- 15389
-
isRedirect
public static boolean isRedirect(int statusCode)
Determines if the given status code is an HTTP redirection.- Parameters:
statusCode
- HTTP status code- Returns:
true
if the given status code is an HTTP redirection- Since:
- 15423
-
disconnect
public abstract void disconnect()
Disconnect client.- Since:
- 9309
- See Also:
HttpURLConnection.disconnect()
-
getTomcatErrorMatcher
public static java.util.regex.Matcher getTomcatErrorMatcher(java.lang.String data)
Returns aMatcher
against predefined Tomcat error messages. If it matches, error message can be extracted fromgroup(1)
.- Parameters:
data
- HTML contents to check- Returns:
- a
Matcher
against predefined Tomcat error messages - Since:
- 13358
-
-