Skip to content

Commit f74355f

Browse files
committed
move download method to httputils
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 969660a commit f74355f

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

Diff for: engine/schema/src/main/java/com/cloud/upgrade/SystemVmTemplateRegistration.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import com.cloud.upgrade.dao.BasicTemplateDataStoreDaoImpl;
7878
import com.cloud.user.Account;
7979
import com.cloud.utils.DateUtil;
80+
import com.cloud.utils.HttpUtils;
8081
import com.cloud.utils.Pair;
8182
import com.cloud.utils.UriUtils;
8283
import com.cloud.utils.db.GlobalLock;
@@ -779,7 +780,7 @@ protected File getTemplateFile(MetadataTemplateDetails templateDetails) {
779780
StringUtils.isNotBlank(templateDetails.getUrl())) {
780781
LOGGER.debug("Downloading the template file {} for hypervisor {} and arch {} as it is not present",
781782
templateDetails.getUrl(), templateDetails.getHypervisorType().name(), templateDetails.getArch());
782-
if (!NetUtils.downloadFileWithProgress(templateDetails.getUrl(), filePath, LOGGER)) {
783+
if (!HttpUtils.downloadFileWithProgress(templateDetails.getUrl(), filePath, LOGGER)) {
783784
return null;
784785
}
785786
return new File(filePath);

Diff for: utils/src/main/java/com/cloud/utils/HttpUtils.java

+51
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
import javax.servlet.http.Cookie;
2626
import javax.servlet.http.HttpServletResponse;
2727
import javax.servlet.http.HttpSession;
28+
29+
import java.io.FileOutputStream;
2830
import java.io.IOException;
31+
import java.io.InputStream;
32+
import java.net.HttpURLConnection;
33+
import java.net.URL;
2934
import java.util.Map;
3035

3136
public class HttpUtils {
@@ -151,4 +156,50 @@ public static boolean validateSessionKey(final HttpSession session, final Map<St
151156
}
152157
}
153158

159+
public static boolean downloadFileWithProgress(final String fileURL, final String savePath, final Logger logger) {
160+
HttpURLConnection httpConn = null;
161+
try {
162+
URL url = new URL(fileURL);
163+
httpConn = (HttpURLConnection) url.openConnection();
164+
int responseCode = httpConn.getResponseCode();
165+
if (responseCode == HttpURLConnection.HTTP_OK) {
166+
int contentLength = httpConn.getContentLength();
167+
if (contentLength < 0) {
168+
logger.warn("Content length not provided for {}, progress updates may not be accurate",
169+
fileURL);
170+
}
171+
try (InputStream inputStream = httpConn.getInputStream();
172+
FileOutputStream outputStream = new FileOutputStream(savePath)) {
173+
byte[] buffer = new byte[4096];
174+
int bytesRead;
175+
int downloaded = 0;
176+
int lastReportedPercent = 0;
177+
while ((bytesRead = inputStream.read(buffer)) != -1) {
178+
outputStream.write(buffer, 0, bytesRead);
179+
downloaded += bytesRead;
180+
if (contentLength > 0) {
181+
int percentDownloaded = (int) ((downloaded / (double) contentLength) * 100);
182+
// Update every 5 percent or on completion
183+
if (percentDownloaded - lastReportedPercent >= 5 || percentDownloaded == 100) {
184+
logger.debug("Downloaded {}% from {}", downloaded, fileURL);
185+
lastReportedPercent = percentDownloaded;
186+
}
187+
}
188+
}
189+
}
190+
logger.info("File {} downloaded successfully using {}.", fileURL, savePath);
191+
} else {
192+
logger.error("No file to download {}. Server replied with code: {}", fileURL, responseCode);
193+
return false;
194+
}
195+
} catch (IOException ex) {
196+
logger.error("Failed to download {} due to: {}", fileURL, ex.getMessage(), ex);
197+
return false;
198+
} finally {
199+
if (httpConn != null) {
200+
httpConn.disconnect();
201+
}
202+
}
203+
return true;
204+
}
154205
}

Diff for: utils/src/main/java/com/cloud/utils/net/NetUtils.java

+1-52
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
package com.cloud.utils.net;
2121

2222
import java.io.BufferedReader;
23-
import java.io.FileOutputStream;
2423
import java.io.IOException;
25-
import java.io.InputStream;
2624
import java.io.InputStreamReader;
2725
import java.math.BigInteger;
28-
import java.net.HttpURLConnection;
2926
import java.net.Inet4Address;
3027
import java.net.Inet6Address;
3128
import java.net.InetAddress;
@@ -34,7 +31,6 @@
3431
import java.net.SocketException;
3532
import java.net.URI;
3633
import java.net.URISyntaxException;
37-
import java.net.URL;
3834
import java.net.UnknownHostException;
3935
import java.util.ArrayList;
4036
import java.util.Arrays;
@@ -55,8 +51,8 @@
5551
import org.apache.commons.net.util.SubnetUtils;
5652
import org.apache.commons.validator.routines.InetAddressValidator;
5753
import org.apache.commons.validator.routines.RegexValidator;
58-
import org.apache.logging.log4j.LogManager;
5954
import org.apache.logging.log4j.Logger;
55+
import org.apache.logging.log4j.LogManager;
6056

6157
import com.cloud.utils.IteratorUtil;
6258
import com.cloud.utils.Pair;
@@ -1878,51 +1874,4 @@ public static String transformCidr(final String cidr) {
18781874
final long start = (ip & startNetMask);
18791875
return String.format("%s/%s", long2Ip(start), size);
18801876
}
1881-
1882-
public static boolean downloadFileWithProgress(final String fileURL, final String savePath, final Logger logger) {
1883-
HttpURLConnection httpConn = null;
1884-
try {
1885-
URL url = new URL(fileURL);
1886-
httpConn = (HttpURLConnection) url.openConnection();
1887-
int responseCode = httpConn.getResponseCode();
1888-
if (responseCode == HttpURLConnection.HTTP_OK) {
1889-
int contentLength = httpConn.getContentLength();
1890-
if (contentLength < 0) {
1891-
logger.warn("Content length not provided for {}, progress updates may not be accurate",
1892-
fileURL);
1893-
}
1894-
try (InputStream inputStream = httpConn.getInputStream();
1895-
FileOutputStream outputStream = new FileOutputStream(savePath)) {
1896-
byte[] buffer = new byte[4096];
1897-
int bytesRead;
1898-
int downloaded = 0;
1899-
int lastReportedPercent = 0;
1900-
while ((bytesRead = inputStream.read(buffer)) != -1) {
1901-
outputStream.write(buffer, 0, bytesRead);
1902-
downloaded += bytesRead;
1903-
if (contentLength > 0) {
1904-
int percentDownloaded = (int) ((downloaded / (double) contentLength) * 100);
1905-
// Update every 5 percent or on completion
1906-
if (percentDownloaded - lastReportedPercent >= 5 || percentDownloaded == 100) {
1907-
logger.debug("Downloaded {}% from {}", downloaded, fileURL);
1908-
lastReportedPercent = percentDownloaded;
1909-
}
1910-
}
1911-
}
1912-
}
1913-
logger.info("File {} downloaded successfully using {}.", fileURL, savePath);
1914-
} else {
1915-
logger.error("No file to download {}. Server replied with code: {}", fileURL, responseCode);
1916-
return false;
1917-
}
1918-
} catch (IOException ex) {
1919-
logger.error("Failed to download {} due to: {}", fileURL, ex.getMessage(), ex);
1920-
return false;
1921-
} finally {
1922-
if (httpConn != null) {
1923-
httpConn.disconnect();
1924-
}
1925-
}
1926-
return true;
1927-
}
19281877
}

0 commit comments

Comments
 (0)