|
35 | 35 | import java.util.ListIterator; |
36 | 36 | import java.util.StringTokenizer; |
37 | 37 |
|
38 | | -import javax.net.ssl.HttpsURLConnection; |
39 | | - |
40 | 38 | import org.apache.commons.httpclient.Credentials; |
41 | 39 | import org.apache.commons.httpclient.HttpClient; |
42 | 40 | import org.apache.commons.httpclient.HttpException; |
@@ -201,40 +199,53 @@ private static List<NameValuePair> getUserDetails(String query) { |
201 | 199 | return details; |
202 | 200 | } |
203 | 201 |
|
| 202 | + public static void main(String[] args) { |
| 203 | + long size = getRemoteSize("http://localhost:8000/systemvm/target/systemvm.zip"); |
| 204 | + System.out.println(size); |
| 205 | + } |
| 206 | + |
204 | 207 | // Get the size of a file from URL response header. |
205 | | - public static Long getRemoteSize(String url) { |
206 | | - Long remoteSize = (long)0; |
207 | | - HttpURLConnection httpConn = null; |
208 | | - HttpsURLConnection httpsConn = null; |
209 | | - try { |
210 | | - URI uri = new URI(url); |
211 | | - if (uri.getScheme().equalsIgnoreCase("http")) { |
| 208 | + public static long getRemoteSize(String url) { |
| 209 | + long remoteSize = 0L; |
| 210 | + final String[] methods = new String[]{"HEAD", "GET"}; |
| 211 | + IllegalArgumentException exception = null; |
| 212 | + // Attempting first a HEAD request to avoid downloading the whole file. If |
| 213 | + // it fails (for example with S3 presigned URL), fallback on a standard GET |
| 214 | + // request. |
| 215 | + for (String method : methods) { |
| 216 | + HttpURLConnection httpConn = null; |
| 217 | + try { |
| 218 | + URI uri = new URI(url); |
212 | 219 | httpConn = (HttpURLConnection)uri.toURL().openConnection(); |
213 | | - if (httpConn != null) { |
214 | | - httpConn.setConnectTimeout(2000); |
215 | | - httpConn.setReadTimeout(5000); |
216 | | - String contentLength = httpConn.getHeaderField("content-length"); |
217 | | - if (contentLength != null) { |
218 | | - remoteSize = Long.parseLong(contentLength); |
| 220 | + httpConn.setRequestMethod(method); |
| 221 | + httpConn.setConnectTimeout(2000); |
| 222 | + httpConn.setReadTimeout(5000); |
| 223 | + String contentLength = httpConn.getHeaderField("Content-Length"); |
| 224 | + if (contentLength != null) { |
| 225 | + remoteSize = Long.parseLong(contentLength); |
| 226 | + } else if (method.equals("GET") && httpConn.getResponseCode() < 300) { |
| 227 | + // Calculate the content size based on the input stream content |
| 228 | + byte[] buf = new byte[1024]; |
| 229 | + int length; |
| 230 | + while ((length = httpConn.getInputStream().read(buf, 0, buf.length)) != -1) { |
| 231 | + remoteSize += length; |
219 | 232 | } |
220 | | - httpConn.disconnect(); |
221 | 233 | } |
222 | | - } else if (uri.getScheme().equalsIgnoreCase("https")) { |
223 | | - httpsConn = (HttpsURLConnection)uri.toURL().openConnection(); |
224 | | - if (httpsConn != null) { |
225 | | - String contentLength = httpsConn.getHeaderField("content-length"); |
226 | | - if (contentLength != null) { |
227 | | - remoteSize = Long.parseLong(contentLength); |
228 | | - } |
229 | | - httpsConn.disconnect(); |
| 234 | + return remoteSize; |
| 235 | + } catch (URISyntaxException e) { |
| 236 | + throw new IllegalArgumentException("Invalid URL " + url); |
| 237 | + } catch (IOException e) { |
| 238 | + exception = new IllegalArgumentException("Unable to establish connection with URL " + url); |
| 239 | + } finally { |
| 240 | + if (httpConn != null) { |
| 241 | + httpConn.disconnect(); |
230 | 242 | } |
231 | 243 | } |
232 | | - } catch (URISyntaxException e) { |
233 | | - throw new IllegalArgumentException("Invalid URL " + url); |
234 | | - } catch (IOException e) { |
235 | | - throw new IllegalArgumentException("Unable to establish connection with URL " + url); |
236 | 244 | } |
237 | | - return remoteSize; |
| 245 | + if (exception != null) { |
| 246 | + throw exception; |
| 247 | + } |
| 248 | + return 0L; |
238 | 249 | } |
239 | 250 |
|
240 | 251 | public static Pair<String, Integer> validateUrl(String url) throws IllegalArgumentException { |
|
0 commit comments