Package org.apache.ivy.util.url
Class HttpClientHandler
- java.lang.Object
-
- org.apache.ivy.util.url.AbstractURLHandler
-
- org.apache.ivy.util.url.HttpClientHandler
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,TimeoutConstrainedURLHandler
,URLHandler
public class HttpClientHandler extends AbstractURLHandler implements TimeoutConstrainedURLHandler, java.lang.AutoCloseable
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
HttpClientHandler.IvyCredentialsProvider
-
Nested classes/interfaces inherited from interface org.apache.ivy.util.url.URLHandler
URLHandler.URLInfo
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static HttpClientHandler
DELETE_ON_EXIT_INSTANCE
private org.apache.http.impl.client.CloseableHttpClient
httpClient
private static java.text.SimpleDateFormat
LAST_MODIFIED_FORMAT
-
Fields inherited from interface org.apache.ivy.util.url.URLHandler
REQUEST_METHOD_GET, REQUEST_METHOD_HEAD, UNAVAILABLE
-
-
Constructor Summary
Constructors Constructor Description HttpClientHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private org.apache.http.impl.client.CloseableHttpClient
buildUnderlyingClient()
private boolean
checkStatusCode(java.lang.String httpMethod, java.net.URL sourceURL, org.apache.http.HttpResponse response)
void
close()
private static org.apache.http.config.Lookup<org.apache.http.auth.AuthSchemeProvider>
createAuthSchemeRegistry()
private static org.apache.http.conn.HttpClientConnectionManager
createConnectionManager()
private static org.apache.http.conn.routing.HttpRoutePlanner
createProxyRoutePlanner()
private org.apache.http.client.methods.CloseableHttpResponse
doGet(java.net.URL url, int connectionTimeout, int readTimeout)
private org.apache.http.client.methods.CloseableHttpResponse
doHead(java.net.URL url, int connectionTimeout, int readTimeout)
void
download(java.net.URL src, java.io.File dest, CopyProgressListener l)
void
download(java.net.URL src, java.io.File dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint)
Downloads the resource available atsrc
to the targetdest
private static java.util.List<java.lang.String>
getAuthSchemePreferredOrder()
private org.apache.http.Header
getContentEncoding(org.apache.http.HttpResponse response)
long
getContentLength(java.net.URL url, TimeoutConstraint timeoutConstraint)
Returns the number of bytes of data that's available for the resource at the passedurl
.long
getLastModified(java.net.URL url, TimeoutConstraint timeoutConstraint)
Returns the last modified timestamp of the resource accessible at the passedurl
.private long
getLastModified(org.apache.http.HttpResponse response)
URLHandler.URLInfo
getURLInfo(java.net.URL url)
URLHandler.URLInfo
getURLInfo(java.net.URL url, int timeout)
URLHandler.URLInfo
getURLInfo(java.net.URL url, TimeoutConstraint timeoutConstraint)
Returns theURLHandler.URLInfo
extracted from the given url, orURLHandler.UNAVAILABLE
when the url is not reachable.private boolean
hasCredentialsConfigured(java.net.URL url)
boolean
isReachable(java.net.URL url, TimeoutConstraint timeoutConstraint)
Returns true if the passedURL
is reachable.java.io.InputStream
openStream(java.net.URL url)
java.io.InputStream
openStream(java.net.URL url, TimeoutConstraint timeoutConstraint)
Opens and returns anInputStream
to the passedurl
.private void
requireSuccessStatus(java.lang.String httpMethod, java.net.URL sourceURL, org.apache.http.client.methods.CloseableHttpResponse response)
Checks the status code of the response and if it's considered as successful response, then this method just returns back.void
upload(java.io.File src, java.net.URL dest, CopyProgressListener l)
void
upload(java.io.File src, java.net.URL dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint)
Uploads thesrc
File
to the targetdest
URL
-
Methods inherited from class org.apache.ivy.util.url.AbstractURLHandler
createTimeoutConstraints, getContentLength, getContentLength, getDecodingInputStream, getLastModified, getLastModified, getRequestMethod, getUserAgent, isReachable, isReachable, normalizeToString, normalizeToURL, setRequestMethod, validatePutStatusCode
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.ivy.util.url.URLHandler
getContentLength, getContentLength, getLastModified, getLastModified, isReachable, isReachable, setRequestMethod
-
-
-
-
Field Detail
-
LAST_MODIFIED_FORMAT
private static final java.text.SimpleDateFormat LAST_MODIFIED_FORMAT
-
DELETE_ON_EXIT_INSTANCE
static final HttpClientHandler DELETE_ON_EXIT_INSTANCE
-
httpClient
private final org.apache.http.impl.client.CloseableHttpClient httpClient
-
-
Method Detail
-
buildUnderlyingClient
private org.apache.http.impl.client.CloseableHttpClient buildUnderlyingClient()
-
createProxyRoutePlanner
private static org.apache.http.conn.routing.HttpRoutePlanner createProxyRoutePlanner()
-
createAuthSchemeRegistry
private static org.apache.http.config.Lookup<org.apache.http.auth.AuthSchemeProvider> createAuthSchemeRegistry()
-
createConnectionManager
private static org.apache.http.conn.HttpClientConnectionManager createConnectionManager()
-
getAuthSchemePreferredOrder
private static java.util.List<java.lang.String> getAuthSchemePreferredOrder()
-
openStream
public java.io.InputStream openStream(java.net.URL url) throws java.io.IOException
- Specified by:
openStream
in interfaceURLHandler
- Parameters:
url
- ditto- Returns:
- InputStream
- Throws:
java.io.IOException
- if something goes wrong
-
openStream
public java.io.InputStream openStream(java.net.URL url, TimeoutConstraint timeoutConstraint) throws java.io.IOException
Description copied from interface:TimeoutConstrainedURLHandler
Opens and returns anInputStream
to the passedurl
.- Specified by:
openStream
in interfaceTimeoutConstrainedURLHandler
- Parameters:
url
- The URL to which anInputStream
has to be openedtimeoutConstraint
- The connectivity timeout constraints. Can be null, in which case the timeouts are implementation specific- Returns:
- InputStream
- Throws:
java.io.IOException
- if something goes wrong
-
download
public void download(java.net.URL src, java.io.File dest, CopyProgressListener l) throws java.io.IOException
- Specified by:
download
in interfaceURLHandler
- Parameters:
src
- URLdest
- Filel
- CopyProgressListener- Throws:
java.io.IOException
- if something goes wrong
-
download
public void download(java.net.URL src, java.io.File dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint) throws java.io.IOException
Description copied from interface:TimeoutConstrainedURLHandler
Downloads the resource available atsrc
to the targetdest
- Specified by:
download
in interfaceTimeoutConstrainedURLHandler
- Parameters:
src
- The source URL to download the resource fromdest
- The destinationFile
to download the resource tolistener
- The listener that will be notified of the download progresstimeoutConstraint
- The connectivity timeout constraints. Can be null, in which case the timeouts are implementation specific- Throws:
java.io.IOException
- if something goes wrong
-
upload
public void upload(java.io.File src, java.net.URL dest, CopyProgressListener l) throws java.io.IOException
- Specified by:
upload
in interfaceURLHandler
- Parameters:
src
- Filedest
- URLl
- CopyProgressListener- Throws:
java.io.IOException
- if something goes wrong
-
upload
public void upload(java.io.File src, java.net.URL dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint) throws java.io.IOException
Description copied from interface:TimeoutConstrainedURLHandler
Uploads thesrc
File
to the targetdest
URL
- Specified by:
upload
in interfaceTimeoutConstrainedURLHandler
- Parameters:
src
- The sourceFile
to uploaddest
- The target URL where theFile
has to be uploadedlistener
- The listener that will be notified of the upload progresstimeoutConstraint
- The connectivity timeout constraints. Can be null, in which case the timeouts are implementation specific- Throws:
java.io.IOException
- if something goes wrong
-
getURLInfo
public URLHandler.URLInfo getURLInfo(java.net.URL url)
- Specified by:
getURLInfo
in interfaceURLHandler
- Parameters:
url
- The url from which information is retrieved.- Returns:
- The URLInfo extracted from the given url, or
URLHandler.UNAVAILABLE
instance when the url is not reachable.
-
getURLInfo
public URLHandler.URLInfo getURLInfo(java.net.URL url, int timeout)
- Specified by:
getURLInfo
in interfaceURLHandler
- Parameters:
url
- The url from which information is retrieved.timeout
- The timeout in milliseconds.- Returns:
- The URLInfo extracted from the given url, or
URLHandler.UNAVAILABLE
when the url is not reachable, never null.
-
isReachable
public boolean isReachable(java.net.URL url, TimeoutConstraint timeoutConstraint)
Description copied from interface:TimeoutConstrainedURLHandler
Returns true if the passedURL
is reachable. Else returns false. Uses the passedtimeoutConstraint
for determining the connectivity to the URL.Please use
TimeoutConstrainedURLHandler.getURLInfo(URL, TimeoutConstraint)
if more one information about theurl
is needed- Specified by:
isReachable
in interfaceTimeoutConstrainedURLHandler
- Parameters:
url
- The URL to accesstimeoutConstraint
- The connectivity timeout constraints. Can be null, in which case the timeouts are implementation specific- Returns:
- boolean
-
getContentLength
public long getContentLength(java.net.URL url, TimeoutConstraint timeoutConstraint)
Description copied from interface:TimeoutConstrainedURLHandler
Returns the number of bytes of data that's available for the resource at the passedurl
. Returns 0 if the passedurl
isn't reachable- Specified by:
getContentLength
in interfaceTimeoutConstrainedURLHandler
- Parameters:
url
- The URL to accesstimeoutConstraint
- The connectivity timeout constraints. Can be null, in which case the timeouts are implementation specific- Returns:
- long
-
getLastModified
public long getLastModified(java.net.URL url, TimeoutConstraint timeoutConstraint)
Description copied from interface:TimeoutConstrainedURLHandler
Returns the last modified timestamp of the resource accessible at the passedurl
.Please use
TimeoutConstrainedURLHandler.getURLInfo(URL, TimeoutConstraint)
if more one information about theurl
is needed- Specified by:
getLastModified
in interfaceTimeoutConstrainedURLHandler
- Parameters:
url
- The URL to accesstimeoutConstraint
- The connectivity timeout constraints. Can be null, in which case the timeouts are implementation specific- Returns:
- long
-
getURLInfo
public URLHandler.URLInfo getURLInfo(java.net.URL url, TimeoutConstraint timeoutConstraint)
Description copied from interface:TimeoutConstrainedURLHandler
Returns theURLHandler.URLInfo
extracted from the given url, orURLHandler.UNAVAILABLE
when the url is not reachable. Never returns null.- Specified by:
getURLInfo
in interfaceTimeoutConstrainedURLHandler
- Parameters:
url
- The URL for which the information is to be retrievedtimeoutConstraint
- The connectivity timeout constraints. Can be null, in which case the timeouts are implementation specific- Returns:
- URLInfo
-
checkStatusCode
private boolean checkStatusCode(java.lang.String httpMethod, java.net.URL sourceURL, org.apache.http.HttpResponse response)
-
requireSuccessStatus
private void requireSuccessStatus(java.lang.String httpMethod, java.net.URL sourceURL, org.apache.http.client.methods.CloseableHttpResponse response) throws java.io.IOException
Checks the status code of the response and if it's considered as successful response, then this method just returns back. Else itcloses the response
and throws anIOException
for the unsuccessful response.- Parameters:
httpMethod
- The HTTP method that was used for the source requestsourceURL
- The URL of the source requestresponse
- The response to the source request- Throws:
java.io.IOException
- Thrown if the response was considered unsuccessful
-
getContentEncoding
private org.apache.http.Header getContentEncoding(org.apache.http.HttpResponse response)
-
getLastModified
private long getLastModified(org.apache.http.HttpResponse response)
-
doGet
private org.apache.http.client.methods.CloseableHttpResponse doGet(java.net.URL url, int connectionTimeout, int readTimeout) throws java.io.IOException
- Throws:
java.io.IOException
-
doHead
private org.apache.http.client.methods.CloseableHttpResponse doHead(java.net.URL url, int connectionTimeout, int readTimeout) throws java.io.IOException
- Throws:
java.io.IOException
-
hasCredentialsConfigured
private boolean hasCredentialsConfigured(java.net.URL url)
-
close
public void close() throws java.lang.Exception
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Throws:
java.lang.Exception
-
-