diff --git a/java_console/autoupdate/src/com/rusefi/autoupdate/Autoupdate.java b/java_console/autoupdate/src/com/rusefi/autoupdate/Autoupdate.java index dd5427883b..a74ba262c8 100644 --- a/java_console/autoupdate/src/com/rusefi/autoupdate/Autoupdate.java +++ b/java_console/autoupdate/src/com/rusefi/autoupdate/Autoupdate.java @@ -1,5 +1,6 @@ package com.rusefi.autoupdate; +import com.rusefi.shared.ConnectionAndMeta; import com.rusefi.ui.util.FrameHelper; import javax.swing.*; @@ -8,7 +9,6 @@ import java.awt.event.ActionEvent; import java.io.*; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URLClassLoader; import java.util.Date; @@ -67,7 +67,7 @@ public class Autoupdate { private static void handleBundle(String bundleFullName, UpdateMode mode) { try { String zipFileName = bundleFullName + "_autoupdate" + ".zip"; - AutoupdateUtil.ConnectionAndMeta connectionAndMeta = new AutoupdateUtil.ConnectionAndMeta(zipFileName).invoke(); + ConnectionAndMeta connectionAndMeta = new ConnectionAndMeta(zipFileName).invoke(); System.out.println("Server has " + connectionAndMeta.getCompleteFileSize() + " from " + new Date(connectionAndMeta.getLastModified())); if (AutoupdateUtil.hasExistingFile(zipFileName, connectionAndMeta.getCompleteFileSize(), connectionAndMeta.getLastModified())) { diff --git a/java_console/autoupdate/src/com/rusefi/autoupdate/AutoupdateUtil.java b/java_console/autoupdate/src/com/rusefi/autoupdate/AutoupdateUtil.java index 9f198a81ce..8fbd017010 100644 --- a/java_console/autoupdate/src/com/rusefi/autoupdate/AutoupdateUtil.java +++ b/java_console/autoupdate/src/com/rusefi/autoupdate/AutoupdateUtil.java @@ -1,46 +1,25 @@ package com.rusefi.autoupdate; +import com.rusefi.shared.ConnectionAndMeta; import com.rusefi.ui.util.FrameHelper; import org.jetbrains.annotations.NotNull; -import javax.net.ssl.*; import javax.swing.*; import java.awt.*; import java.io.*; -import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; import java.util.Date; -import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; public class AutoupdateUtil { public static final boolean runHeadless = Boolean.getBoolean("run_headless") || GraphicsEnvironment.isHeadless(); - private static final int BUFFER_SIZE = 32 * 1024; - private static final int STEPS = 1000; // todo: figure out a better way to work with absolute path private static final String APPICON = "/appicon.png"; public static void downloadAutoupdateFile(String localZipFileName, ConnectionAndMeta connectionAndMeta, String title) throws IOException { - HttpURLConnection httpConnection = connectionAndMeta.httpConnection; - long completeFileSize = connectionAndMeta.completeFileSize; - Objects.requireNonNull(httpConnection, "httpConnection"); - BufferedInputStream in = new BufferedInputStream(httpConnection.getInputStream()); - FileOutputStream fos = new FileOutputStream(localZipFileName); - BufferedOutputStream bout = new BufferedOutputStream(fos, BUFFER_SIZE); - byte[] data = new byte[BUFFER_SIZE]; - long downloadedFileSize = 0; - int newDataSize; - - int printedPercentage = 0; - FrameHelper frameHelper = null; final AtomicReference jProgressBarAtomicReference = new AtomicReference<>(); if (!runHeadless) { @@ -48,32 +27,18 @@ public class AutoupdateUtil { JProgressBar jProgressBar = new JProgressBar(); frameHelper.getFrame().setTitle(title); - jProgressBar.setMaximum(STEPS); + jProgressBar.setMaximum(ConnectionAndMeta.STEPS); jProgressBarAtomicReference.set(jProgressBar); frameHelper.showFrame(jProgressBar, true); } - while ((newDataSize = in.read(data, 0, BUFFER_SIZE)) >= 0) { - downloadedFileSize += newDataSize; - - // calculate progress - final int currentProgress = (int) ((((double) downloadedFileSize) / ((double) completeFileSize)) * STEPS); - - int currentPercentage = (int) (100L * downloadedFileSize / completeFileSize); - if (currentPercentage > printedPercentage + 5) { - System.out.println("Downloaded " + currentPercentage + "%"); - printedPercentage = currentPercentage; - } - + ConnectionAndMeta.DownloadProgressListener listener = currentProgress -> { if (!runHeadless) { SwingUtilities.invokeLater(() -> jProgressBarAtomicReference.get().setValue(currentProgress)); } + }; - bout.write(data, 0, newDataSize); - } - bout.close(); - in.close(); - new File(localZipFileName).setLastModified(connectionAndMeta.lastModified); + ConnectionAndMeta.downloadFile(localZipFileName, connectionAndMeta, listener); if (!runHeadless) { frameHelper.getFrame().dispose(); @@ -123,53 +88,4 @@ public class AutoupdateUtil { trueLayout(window); } - public static class ConnectionAndMeta { - private String zipFileName; - private HttpsURLConnection httpConnection; - private long completeFileSize; - private long lastModified; - - public ConnectionAndMeta(String zipFileName) { - this.zipFileName = zipFileName; - } - - public HttpURLConnection getHttpConnection() { - return httpConnection; - } - - public long getCompleteFileSize() { - return completeFileSize; - } - - public long getLastModified() { - return lastModified; - } - - public ConnectionAndMeta invoke() throws IOException, NoSuchAlgorithmException, KeyManagementException { - // user can have java with expired certificates or funny proxy, we shall accept any certificate :( - SSLContext ctx = SSLContext.getInstance("TLS"); - ctx.init(new KeyManager[0], new TrustManager[] {new AcceptAnyCertificateTrustManager()}, new SecureRandom()); - - URL url = new URL("https://rusefi.com/build_server/autoupdate/" + zipFileName); - httpConnection = (HttpsURLConnection) url.openConnection(); - httpConnection.setSSLSocketFactory(ctx.getSocketFactory()); - completeFileSize = httpConnection.getContentLength(); - lastModified = httpConnection.getLastModified(); - return this; - } - } - - private static class AcceptAnyCertificateTrustManager implements X509TrustManager { - - @Override - public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {} - - @Override - public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {} - - @Override - public X509Certificate[] getAcceptedIssuers() { - return null; - } - } } diff --git a/java_console/autoupdate/src/com/rusefi/shared/ConnectionAndMeta.java b/java_console/autoupdate/src/com/rusefi/shared/ConnectionAndMeta.java new file mode 100644 index 0000000000..5a6fe6455a --- /dev/null +++ b/java_console/autoupdate/src/com/rusefi/shared/ConnectionAndMeta.java @@ -0,0 +1,102 @@ +package com.rusefi.shared; + +import javax.net.ssl.*; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.cert.X509Certificate; +import java.util.Objects; + +public class ConnectionAndMeta { + private static final int BUFFER_SIZE = 32 * 1024; + public static final int STEPS = 1000; + private String zipFileName; + private HttpsURLConnection httpConnection; + private long completeFileSize; + private long lastModified; + + public ConnectionAndMeta(String zipFileName) { + this.zipFileName = zipFileName; + } + + public static void downloadFile(String localTargetFileName, ConnectionAndMeta connectionAndMeta, DownloadProgressListener listener) throws IOException { + HttpURLConnection httpConnection = connectionAndMeta.getHttpConnection(); + long completeFileSize = connectionAndMeta.getCompleteFileSize(); + Objects.requireNonNull(httpConnection, "httpConnection"); + BufferedInputStream in = new BufferedInputStream(httpConnection.getInputStream()); + FileOutputStream fos = new FileOutputStream(localTargetFileName); + BufferedOutputStream bout = new BufferedOutputStream(fos, BUFFER_SIZE); + byte[] data = new byte[BUFFER_SIZE]; + long downloadedFileSize = 0; + int newDataSize; + + int printedPercentage = 0; + + while ((newDataSize = in.read(data, 0, BUFFER_SIZE)) >= 0) { + downloadedFileSize += newDataSize; + + // calculate progress + final int currentProgress = (int) ((((double) downloadedFileSize) / ((double) completeFileSize)) * STEPS); + + int currentPercentage = (int) (100L * downloadedFileSize / completeFileSize); + if (currentPercentage > printedPercentage + 5) { + System.out.println("Downloaded " + currentPercentage + "%"); + printedPercentage = currentPercentage; + } + + listener.onPercentage(currentProgress); + + bout.write(data, 0, newDataSize); + } + bout.close(); + in.close(); + new File(localTargetFileName).setLastModified(connectionAndMeta.getLastModified()); + } + + public HttpURLConnection getHttpConnection() { + return httpConnection; + } + + public long getCompleteFileSize() { + return completeFileSize; + } + + public long getLastModified() { + return lastModified; + } + + public ConnectionAndMeta invoke() throws IOException, NoSuchAlgorithmException, KeyManagementException { + // user can have java with expired certificates or funny proxy, we shall accept any certificate :( + SSLContext ctx = SSLContext.getInstance("TLS"); + ctx.init(new KeyManager[0], new TrustManager[]{new AcceptAnyCertificateTrustManager()}, new SecureRandom()); + + URL url = new URL("https://rusefi.com/build_server/autoupdate/" + zipFileName); + httpConnection = (HttpsURLConnection) url.openConnection(); + httpConnection.setSSLSocketFactory(ctx.getSocketFactory()); + completeFileSize = httpConnection.getContentLength(); + lastModified = httpConnection.getLastModified(); + return this; + } + + public interface DownloadProgressListener { + void onPercentage(int currentProgress); + } + + private static class AcceptAnyCertificateTrustManager implements X509TrustManager { + @Override + public void checkClientTrusted(X509Certificate[] arg0, String arg1) { + } + + @Override + public void checkServerTrusted(X509Certificate[] arg0, String arg1) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + } +}