From 210eb6f0e26f0edf66f09a6883fcd45d85450233 Mon Sep 17 00:00:00 2001 From: Nikolay Demyankov Date: Fri, 3 Jun 2016 11:07:43 +0200 Subject: [PATCH] Renamed UpdateRequest to UpdateDownloadRequest. Added helper class for URLConnection. Adding headers to all requests, if they are set. https://github.com/nordnet/cordova-hot-code-push/issues/153 --- .../chcp/main/network/FileDownloader.java | 41 +++++++--------- .../chcp/main/network/JsonDownloader.java | 24 +++------- ...equest.java => UpdateDownloadRequest.java} | 16 +++---- .../chcp/main/utils/URLConnectionHelper.java | 48 +++++++++++++++++++ 4 files changed, 79 insertions(+), 50 deletions(-) rename src/android/src/com/nordnetab/chcp/main/updater/{UpdateRequest.java => UpdateDownloadRequest.java} (88%) create mode 100644 src/android/src/com/nordnetab/chcp/main/utils/URLConnectionHelper.java diff --git a/src/android/src/com/nordnetab/chcp/main/network/FileDownloader.java b/src/android/src/com/nordnetab/chcp/main/network/FileDownloader.java index ec58c277..f8e66ed8 100644 --- a/src/android/src/com/nordnetab/chcp/main/network/FileDownloader.java +++ b/src/android/src/com/nordnetab/chcp/main/network/FileDownloader.java @@ -6,6 +6,7 @@ import com.nordnetab.chcp.main.utils.FilesUtility; import com.nordnetab.chcp.main.utils.MD5; import com.nordnetab.chcp.main.utils.Paths; +import com.nordnetab.chcp.main.utils.URLConnectionHelper; import com.nordnetab.chcp.main.utils.URLUtility; import java.io.BufferedInputStream; @@ -18,6 +19,7 @@ import java.net.URL; import java.net.URLConnection; import java.util.List; +import java.util.Map; /** * Created by Nikolay Demyankov on 22.07.15. @@ -26,12 +28,6 @@ */ public class FileDownloader { - // connection timeout in milliseconds - private static final int CONNECTION_TIMEOUT = 30000; - - // data read timeout in milliseconds - private static final int READ_TIMEOUT = 30000; - /** * Download list of files. * Full url to the file is constructed from the contentFolderUrl and ManifestFile#hash (relative path). @@ -42,14 +38,17 @@ public class FileDownloader { * @param downloadFolder absolute path to the folder, where downloaded files should be placed * @param contentFolderUrl root url on the server, where all files are located * @param files list of files to download - * @throws IOException + * @throws Exception * @see ManifestFile */ - public static void downloadFiles(final String downloadFolder, final String contentFolderUrl, List files) throws IOException { + public static void downloadFiles(final String downloadFolder, + final String contentFolderUrl, + final List files, + final Map requestHeaders) throws Exception { for (ManifestFile file : files) { String fileUrl = URLUtility.construct(contentFolderUrl, file.name); String filePath = Paths.get(downloadFolder, file.name); - download(fileUrl, filePath, file.hash); + download(fileUrl, filePath, file.hash, requestHeaders); } } @@ -61,28 +60,20 @@ public static void downloadFiles(final String downloadFolder, final String conte * @param checkSum checksum of the file * @throws IOException */ - public static void download(final String urlFrom, final String filePath, final String checkSum) throws IOException { + public static void download(final String urlFrom, + final String filePath, + final String checkSum, + final Map requestHeaders) throws Exception { Log.d("CHCP", "Loading file: " + urlFrom); + final MD5 md5 = new MD5(); final File downloadFile = new File(filePath); FilesUtility.delete(downloadFile); FilesUtility.ensureDirectoryExists(downloadFile.getParentFile()); - final MD5 md5 = new MD5(); - - final URL downloadUrl = URLUtility.stringToUrl(urlFrom); - if (downloadUrl == null) { - throw new IOException("Invalid url format"); - } - - // create connection - final URLConnection connection = downloadUrl.openConnection(); - connection.setConnectTimeout(CONNECTION_TIMEOUT); - connection.setReadTimeout(READ_TIMEOUT); - connection.connect(); - - // create streams - final InputStream input = new BufferedInputStream(downloadUrl.openStream()); + // download file + final URLConnection connection = URLConnectionHelper.createConnectionToURL(urlFrom, requestHeaders); + final InputStream input = new BufferedInputStream(connection.getInputStream()); final OutputStream output = new BufferedOutputStream(new FileOutputStream(filePath, false)); final byte data[] = new byte[1024]; diff --git a/src/android/src/com/nordnetab/chcp/main/network/JsonDownloader.java b/src/android/src/com/nordnetab/chcp/main/network/JsonDownloader.java index 0dc0bf7c..e8a529c6 100644 --- a/src/android/src/com/nordnetab/chcp/main/network/JsonDownloader.java +++ b/src/android/src/com/nordnetab/chcp/main/network/JsonDownloader.java @@ -1,11 +1,13 @@ package com.nordnetab.chcp.main.network; +import com.nordnetab.chcp.main.utils.URLConnectionHelper; import com.nordnetab.chcp.main.utils.URLUtility; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; +import java.util.Map; /** * Created by Nikolay Demyankov on 22.07.15. @@ -17,13 +19,8 @@ */ abstract class JsonDownloader { - private String downloadUrl; - - // connection timeout in milliseconds - private static final int CONNECTION_TIMEOUT = 30000; - - // data read timeout in milliseconds - private static final int READ_TIMEOUT = 30000; + private final String downloadUrl; + private final Map requestHeaders; /** * Create instance of the object from json string. @@ -38,8 +35,9 @@ abstract class JsonDownloader { * * @param url url from which JSON should be loaded */ - public JsonDownloader(String url) { + public JsonDownloader(final String url, final Map requestHeaders) { this.downloadUrl = url; + this.requestHeaders = requestHeaders; } /** @@ -68,15 +66,7 @@ public DownloadResult download() { private String downloadJson() throws Exception { final StringBuilder jsonContent = new StringBuilder(); - final URL url = URLUtility.stringToUrl(downloadUrl); - if (url == null) { - throw new Exception("Invalid url format:" + downloadUrl); - } - - final URLConnection urlConnection = url.openConnection(); - urlConnection.setConnectTimeout(CONNECTION_TIMEOUT); - urlConnection.setReadTimeout(READ_TIMEOUT); - + final URLConnection urlConnection = URLConnectionHelper.createConnectionToURL(downloadUrl, requestHeaders); final InputStreamReader streamReader = new InputStreamReader(urlConnection.getInputStream()); final BufferedReader bufferedReader = new BufferedReader(streamReader); diff --git a/src/android/src/com/nordnetab/chcp/main/updater/UpdateRequest.java b/src/android/src/com/nordnetab/chcp/main/updater/UpdateDownloadRequest.java similarity index 88% rename from src/android/src/com/nordnetab/chcp/main/updater/UpdateRequest.java rename to src/android/src/com/nordnetab/chcp/main/updater/UpdateDownloadRequest.java index f57a1a71..a4b01dd7 100644 --- a/src/android/src/com/nordnetab/chcp/main/updater/UpdateRequest.java +++ b/src/android/src/com/nordnetab/chcp/main/updater/UpdateDownloadRequest.java @@ -11,7 +11,7 @@ *

* Model for update request parameters. */ -public class UpdateRequest { +public class UpdateDownloadRequest { private String configURL; private PluginFilesStructure currentReleaseFS; @@ -27,11 +27,11 @@ public class UpdateRequest { * @param currentNativeVersion current native interface version * @param requestHeaders additional request headers, which will be added to all requests */ - public UpdateRequest(final Context context, - final String configURL, - final String currentReleaseVersion, - final int currentNativeVersion, - final Map requestHeaders) { + public UpdateDownloadRequest(final Context context, + final String configURL, + final String currentReleaseVersion, + final int currentNativeVersion, + final Map requestHeaders) { this.configURL = configURL; this.currentNativeVersion = currentNativeVersion; this.requestHeaders = requestHeaders; @@ -152,8 +152,8 @@ public Builder setCurrentNativeVersion(final int currentNativeVersion) { * * @return update request instance */ - public UpdateRequest build() { - return new UpdateRequest(mContext, configURL, currentReleaseVersion, currentNativeVersion, requestHeaders); + public UpdateDownloadRequest build() { + return new UpdateDownloadRequest(mContext, configURL, currentReleaseVersion, currentNativeVersion, requestHeaders); } } diff --git a/src/android/src/com/nordnetab/chcp/main/utils/URLConnectionHelper.java b/src/android/src/com/nordnetab/chcp/main/utils/URLConnectionHelper.java new file mode 100644 index 00000000..d4fe03a9 --- /dev/null +++ b/src/android/src/com/nordnetab/chcp/main/utils/URLConnectionHelper.java @@ -0,0 +1,48 @@ +package com.nordnetab.chcp.main.utils; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Map; + +/** + * Created by Nikolay Demyankov on 03.06.16. + *

+ * Helper class to work with URLConnection + */ +public class URLConnectionHelper { + + // connection timeout in milliseconds + private static final int CONNECTION_TIMEOUT = 30000; + + // data read timeout in milliseconds + private static final int READ_TIMEOUT = 30000; + + /** + * Create URLConnection instance. + * + * @param url to what url + * @param requestHeaders additional request headers + * @return connection instance + * @throws IOException when url is invalid or failed to establish connection + */ + public static URLConnection createConnectionToURL(final String url, final Map requestHeaders) throws IOException { + final URL connectionURL = URLUtility.stringToUrl(url); + if (connectionURL == null) { + throw new IOException("Invalid url format: " + url); + } + + final URLConnection urlConnection = connectionURL.openConnection(); + urlConnection.setConnectTimeout(CONNECTION_TIMEOUT); + urlConnection.setReadTimeout(READ_TIMEOUT); + + if (requestHeaders != null) { + for (final Map.Entry entry : requestHeaders.entrySet()) { + urlConnection.setRequestProperty(entry.getKey(), entry.getValue()); + } + } + + return urlConnection; + } + +}