Package org.openstreetmap.josm.tools
Class HttpClient
java.lang.Object
org.openstreetmap.josm.tools.HttpClient
- Direct Known Subclasses:
Http1Client
Provides uniform access for a HTTP/HTTPS server. This class should be used in favour of
HttpURLConnection.- Since:
- 9168
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA wrapper for the HTTP connection response.static interfaceHTTP client factory.static classA wrapper for the HTTP response. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate booleanprivate static HttpClient.HttpClientFactoryprivate booleanprivate longprivate intprivate Stringprivate intprivate Stringprivate byte[]private final Stringprivate HttpClient.Responseprivate static final Patternprivate URLprivate boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedHttpClient(URL url, String requestMethod) Constructs a newHttpClient. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract HttpClient.ResponsebuildResponse(ProgressMonitor progressMonitor) final HttpClient.Responseconnect()Opens the HTTP connection.final HttpClient.Responseconnect(ProgressMonitor progressMonitor) Opens the HTTP connection.final HttpClient.Responseconnect(ProgressMonitor progressMonitor, String authRedirectLocation, String authRequestProperty) Opens the HTTP connection.static HttpClientCreates a new instance for the given URL and aGETrequeststatic HttpClientCreates a new instance for the given URL and aGETrequestabstract voidDisconnect client.final intReturns the connect timeout.Returns the headers.final longReturns theIf-Modified-Sinceheader value.protected final StringReturns the output message.final intReturns the read timeout.final StringReturns the reason for request.final byte[]Returns the request body set for this connection.final StringgetRequestHeader(String header) Returns the set value for the givenheader.final StringReturns the request method set for this connection.final HttpClient.ResponseReturns the HTTP response which is set only after callingconnect().static MatchergetTomcatErrorMatcher(String data) Returns aMatcheragainst predefined Tomcat error messages.final URLgetURL()Returns the URL set for this connection.final booleanDetermines if a non-empty request body has been set for this connection.protected final booleanDetermines whether the progress monitor task will be finished when the output stream is closed.static booleanisRedirect(int statusCode) Determines if the given status code is an HTTP redirection.final booleanDetermines whether not to set headerCache-Control=no-cache.final HttpClientkeepAlive(boolean keepAlive) Sets whether not to set headerConnection=closeprotected final voidlogRequest(String pattern, Object... args) protected final voidprotected final voidnotifyConnect(ProgressMonitor progressMonitor) protected abstract HttpClient.ConnectionResponseprotected abstract voidfinal booleanDetermines if the underlying HTTP method requires a body.final HttpClientSets theAcceptheader.final HttpClientsetConnectTimeout(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 voidsetFactory(HttpClient.HttpClientFactory newFactory) Registers a new HTTP client factory.final HttpClientsetFinishOnCloseOutput(boolean finishOnCloseOutput) Sets whether the progress monitor task will be finished when the output stream is closed.final HttpClientSets an arbitrary HTTP header.final HttpClientsetHeaders(Map<String, String> headers) Sets arbitrary HTTP headers.final HttpClientsetIfModifiedSince(long ifModifiedSince) Sets theIf-Modified-Sinceheader.final HttpClientsetLogAtDebug(boolean debug) Sets the connect log at DEBUG level instead of the default INFO level.final HttpClientsetMaxRedirects(int maxRedirects) Sets the maximum number of redirections to follow.final HttpClientsetOutputMessage(String outputMessage) Sets the output message to be displayed in progress monitor forPUT,POSTandDELETEmethods.final HttpClientsetReadTimeout(int readTimeout) Sets the read timeout to a specified timeout, in milliseconds.final HttpClientsetReasonForRequest(String reasonForRequest) Sets a reason to show on console.final HttpClientsetRequestBody(byte[] requestBody) Sets the request body forPUT/POSTrequests.protected abstract voidsetupConnection(ProgressMonitor progressMonitor) final HttpClientuseCache(boolean useCache) Sets whether not to set headerCache-Control=no-cache.
-
Field Details
-
url
-
requestMethod
-
connectTimeout
-
readTimeout
-
requestBody
-
ifModifiedSince
-
headers
-
maxRedirects
-
useCache
-
reasonForRequest
-
outputMessage
-
response
-
finishOnCloseOutput
-
debug
-
TOMCAT_ERR_MESSAGE
-
factory
-
-
Constructor Details
-
HttpClient
Constructs a newHttpClient.- Parameters:
url- URL to accessrequestMethod- HTTP request method (GET, POST, PUT, DELETE...)
-
-
Method Details
-
setFactory
Registers a new HTTP client factory.- Parameters:
newFactory- new HTTP client factory- Since:
- 15229
-
connect
Opens the HTTP connection.- Returns:
- HTTP response
- Throws:
IOException- if any I/O error occurs
-
connect
Opens the HTTP connection.- Parameters:
progressMonitor- progress monitor- Returns:
- HTTP response
- Throws:
IOException- if any I/O error occurs- Since:
- 9179
-
connect
public final HttpClient.Response connect(ProgressMonitor progressMonitor, String authRedirectLocation, String authRequestProperty) throws 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:
IOException- if any I/O error occurs- Since:
- 18913
-
setupConnection
- Throws:
IOException
-
performConnection
- Throws:
IOException
-
performDisconnection
- Throws:
IOException
-
buildResponse
protected abstract HttpClient.Response buildResponse(ProgressMonitor progressMonitor) throws IOException - Throws:
IOException
-
notifyConnect
-
logRequest
-
logRequestBody
-
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
Creates a new instance for the given URL and aGETrequest- Parameters:
url- the URL- Returns:
- a new instance
-
create
Creates a new instance for the given URL and aGETrequest- Parameters:
url- the URLrequestMethod- the HTTP request method to perform when calling- Returns:
- a new instance
-
getURL
Returns the URL set for this connection.- Returns:
- the URL
- Since:
- 9172
- See Also:
-
getRequestBody
Returns the request body set for this connection.- Returns:
- the HTTP request body, or null
- Since:
- 15229
-
hasRequestBody
Determines if a non-empty request body has been set for this connection.- Returns:
trueif the request body is set and non-empty- Since:
- 15229
-
requiresBody
Determines if the underlying HTTP method requires a body.- Returns:
trueif the underlying HTTP method requires a body- Since:
- 15229
-
getRequestMethod
Returns the request method set for this connection.- Returns:
- the HTTP request method
- Since:
- 9172
- See Also:
-
getRequestHeader
Returns the set value for the givenheader.- Parameters:
header- HTTP header name- Returns:
- HTTP header value
- Since:
- 9172
-
getConnectTimeout
Returns the connect timeout.- Returns:
- the connect timeout, in milliseconds
- Since:
- 15229
-
getReadTimeout
Returns the read timeout.- Returns:
- the read timeout, in milliseconds
- Since:
- 15229
-
getIfModifiedSince
Returns theIf-Modified-Sinceheader value.- Returns:
- the
If-Modified-Sinceheader value - Since:
- 15229
-
isUseCache
Determines whether not to set headerCache-Control=no-cache. By default,useCacheis true, i.e., the headerCache-Control=no-cacheis not sent.- Returns:
- whether not to set header
Cache-Control=no-cache - Since:
- 15229
-
getHeaders
Returns the headers.- Returns:
- the headers
- Since:
- 15229
-
getReasonForRequest
Returns the reason for request.- Returns:
- the reason for request
- Since:
- 15229
-
getOutputMessage
Returns the output message.- Returns:
- the output message
-
isFinishOnCloseOutput
Determines whether the progress monitor task will be finished when the output stream is closed.trueby default.- Returns:
- the finishOnCloseOutput
-
useCache
Sets whether not to set headerCache-Control=no-cache. By default,useCacheis true, i.e., the headerCache-Control=no-cacheis not sent.- Parameters:
useCache- whether not to set headerCache-Control=no-cache- Returns:
this- See Also:
-
keepAlive
Sets whether not to set headerConnection=closeThis might fix #7640, see here.
- Parameters:
keepAlive- whether not to set headerConnection=close- Returns:
this
-
setConnectTimeout
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, aSocketTimeoutExceptionis raised. A timeout of zero is interpreted as an infinite timeout.- Parameters:
connectTimeout- anintthat specifies the connect timeout value in milliseconds- Returns:
this- See Also:
-
setReadTimeout
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, aSocketTimeoutExceptionis raised. A timeout of zero is interpreted as an infinite timeout.- Parameters:
readTimeout- anintthat specifies the read timeout value in milliseconds- Returns:
this- See Also:
-
setAccept
Sets theAcceptheader.- Parameters:
accept- header value- Returns:
this
-
setRequestBody
Sets the request body forPUT/POSTrequests.- Parameters:
requestBody- request body- Returns:
this
-
setIfModifiedSince
Sets theIf-Modified-Sinceheader.- Parameters:
ifModifiedSince- header value- Returns:
this
-
setMaxRedirects
Sets the maximum number of redirections to follow. SetmaxRedirectsto-1in order to ignore redirects, i.e., to not throw anIOExceptioninconnect().- Parameters:
maxRedirects- header value- Returns:
this
-
setHeader
Sets an arbitrary HTTP header.- Parameters:
key- header namevalue- header value- Returns:
this
-
setHeaders
Sets arbitrary HTTP headers.- Parameters:
headers- HTTP headers- Returns:
this
-
setReasonForRequest
Sets a reason to show on console. Can benullif no reason is given.- Parameters:
reasonForRequest- Reason to show- Returns:
this- Since:
- 9172
-
setOutputMessage
Sets the output message to be displayed in progress monitor forPUT,POSTandDELETEmethods. Defaults to "Uploading data ..." (translated). Has no effect forGETor any other method.- Parameters:
outputMessage- message to be displayed in progress monitor- Returns:
this- Since:
- 12711
-
setFinishOnCloseOutput
Sets whether the progress monitor task will be finished when the output stream is closed. This istrueby default.- Parameters:
finishOnCloseOutput- whether the progress monitor task will be finished when the output stream is closed- Returns:
this- Since:
- 10302
-
setLogAtDebug
Sets the connect log at DEBUG level instead of the default INFO level.- Parameters:
debug-trueto set the connect log at DEBUG level- Returns:
this- Since:
- 15389
-
isRedirect
Determines if the given status code is an HTTP redirection.- Parameters:
statusCode- HTTP status code- Returns:
trueif the given status code is an HTTP redirection- Since:
- 15423
-
disconnect
Disconnect client.- Since:
- 9309
- See Also:
-
getTomcatErrorMatcher
Returns aMatcheragainst predefined Tomcat error messages. If it matches, error message can be extracted fromgroup(1).- Parameters:
data- HTML contents to check- Returns:
- a
Matcheragainst predefined Tomcat error messages - Since:
- 13358
-