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 interfaceHttpClient.ConnectionResponseA wrapper for the HTTP connection response.static interfaceHttpClient.HttpClientFactoryHTTP client factory.static classHttpClient.ResponseA wrapper for the HTTP response.
-
Field Summary
Fields Modifier and Type Field Description private intconnectTimeoutprivate booleandebugprivate static HttpClient.HttpClientFactoryfactoryprivate booleanfinishOnCloseOutputprivate java.util.Map<java.lang.String,java.lang.String>headersprivate longifModifiedSinceprivate intmaxRedirectsprivate java.lang.StringoutputMessageprivate intreadTimeoutprivate java.lang.StringreasonForRequestprivate byte[]requestBodyprivate java.lang.StringrequestMethodprivate HttpClient.Responseresponseprivate static java.util.regex.PatternTOMCAT_ERR_MESSAGEprivate java.net.URLurlprivate booleanuseCache
-
Constructor Summary
Constructors Modifier Constructor Description protectedHttpClient(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.ResponsebuildResponse(ProgressMonitor progressMonitor)HttpClient.Responseconnect()Opens the HTTP connection.HttpClient.Responseconnect(ProgressMonitor progressMonitor)Opens the HTTP connection.HttpClient.Responseconnect(ProgressMonitor progressMonitor, java.lang.String authRedirectLocation, java.lang.String authRequestProperty)Opens the HTTP connection.static HttpClientcreate(java.net.URL url)Creates a new instance for the given URL and aGETrequeststatic HttpClientcreate(java.net.URL url, java.lang.String requestMethod)Creates a new instance for the given URL and aGETrequestabstract voiddisconnect()Disconnect client.intgetConnectTimeout()Returns the connect timeout.java.util.Map<java.lang.String,java.lang.String>getHeaders()Returns the headers.longgetIfModifiedSince()Returns theIf-Modified-Sinceheader value.protected java.lang.StringgetOutputMessage()Returns the output message.intgetReadTimeout()Returns the read timeout.java.lang.StringgetReasonForRequest()Returns the reason for request.byte[]getRequestBody()Returns the request body set for this connection.java.lang.StringgetRequestHeader(java.lang.String header)Returns the set value for the givenheader.java.lang.StringgetRequestMethod()Returns the request method set for this connection.HttpClient.ResponsegetResponse()Returns the HTTP response which is set only after callingconnect().static java.util.regex.MatchergetTomcatErrorMatcher(java.lang.String data)Returns aMatcheragainst predefined Tomcat error messages.java.net.URLgetURL()Returns the URL set for this connection.booleanhasRequestBody()Determines if a non-empty request body has been set for this connection.protected booleanisFinishOnCloseOutput()Determines 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.booleanisUseCache()Determines whether not to set headerCache-Control=no-cache.HttpClientkeepAlive(boolean keepAlive)Sets whether not to set headerConnection=closeprotected voidlogRequest(java.lang.String pattern, java.lang.Object... args)protected voidlogRequestBody()protected voidnotifyConnect(ProgressMonitor progressMonitor)protected abstract HttpClient.ConnectionResponseperformConnection()protected abstract voidperformDisconnection()booleanrequiresBody()Determines if the underlying HTTP method requires a body.HttpClientsetAccept(java.lang.String accept)Sets theAcceptheader.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.HttpClientsetFinishOnCloseOutput(boolean finishOnCloseOutput)Sets whether the progress monitor task will be finished when the output stream is closed.HttpClientsetHeader(java.lang.String key, java.lang.String value)Sets an arbitrary HTTP header.HttpClientsetHeaders(java.util.Map<java.lang.String,java.lang.String> headers)Sets arbitrary HTTP headers.HttpClientsetIfModifiedSince(long ifModifiedSince)Sets theIf-Modified-Sinceheader.HttpClientsetLogAtDebug(boolean debug)Sets the connect log at DEBUG level instead of the default INFO level.HttpClientsetMaxRedirects(int maxRedirects)Sets the maximum number of redirections to follow.HttpClientsetOutputMessage(java.lang.String outputMessage)Sets the output message to be displayed in progress monitor forPUT,POSTandDELETEmethods.HttpClientsetReadTimeout(int readTimeout)Sets the read timeout to a specified timeout, in milliseconds.HttpClientsetReasonForRequest(java.lang.String reasonForRequest)Sets a reason to show on console.HttpClientsetRequestBody(byte[] requestBody)Sets the request body forPUT/POSTrequests.protected abstract voidsetupConnection(ProgressMonitor progressMonitor)HttpClientuseCache(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 aGETrequest- 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 aGETrequest- 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:
trueif 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:
trueif 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-Sinceheader value.- Returns:
- the
If-Modified-Sinceheader value - Since:
- 15229
-
isUseCache
public final boolean 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
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.trueby default.- Returns:
- the finishOnCloseOutput
-
useCache
public final HttpClient useCache(boolean 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:
URLConnection.setUseCaches(boolean)
-
keepAlive
public final HttpClient keepAlive(boolean keepAlive)
Sets whether not to set headerConnection=closeThis 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, 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:
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, 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:
URLConnection.setReadTimeout(int)
-
setAccept
public final HttpClient setAccept(java.lang.String accept)
Sets theAcceptheader.- Parameters:
accept- header value- Returns:
this
-
setRequestBody
public final HttpClient setRequestBody(byte[] requestBody)
Sets the request body forPUT/POSTrequests.- Parameters:
requestBody- request body- Returns:
this
-
setIfModifiedSince
public final HttpClient setIfModifiedSince(long ifModifiedSince)
Sets theIf-Modified-Sinceheader.- Parameters:
ifModifiedSince- header value- Returns:
this
-
setMaxRedirects
public final HttpClient setMaxRedirects(int maxRedirects)
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
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 benullif 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,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
public final HttpClient setFinishOnCloseOutput(boolean finishOnCloseOutput)
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
public final HttpClient setLogAtDebug(boolean debug)
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
public static boolean isRedirect(int statusCode)
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
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 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
-
-