Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Renamed UpdateRequest to UpdateDownloadRequest. Added helper class fo…
Browse files Browse the repository at this point in the history
…r URLConnection. Adding headers to all requests, if they are set.

#153
  • Loading branch information
nikDemyankov committed Jun 3, 2016
1 parent 5001511 commit 210eb6f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 50 deletions.
41 changes: 16 additions & 25 deletions src/android/src/com/nordnetab/chcp/main/network/FileDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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).
Expand All @@ -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<ManifestFile> files) throws IOException {
public static void downloadFiles(final String downloadFolder,
final String contentFolderUrl,
final List<ManifestFile> files,
final Map<String, String> 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);
}
}

Expand All @@ -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<String, String> 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];
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,13 +19,8 @@
*/
abstract class JsonDownloader<T> {

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<String, String> requestHeaders;

/**
* Create instance of the object from json string.
Expand All @@ -38,8 +35,9 @@ abstract class JsonDownloader<T> {
*
* @param url url from which JSON should be loaded
*/
public JsonDownloader(String url) {
public JsonDownloader(final String url, final Map<String, String> requestHeaders) {
this.downloadUrl = url;
this.requestHeaders = requestHeaders;
}

/**
Expand Down Expand Up @@ -68,15 +66,7 @@ public DownloadResult<T> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* <p/>
* Model for update request parameters.
*/
public class UpdateRequest {
public class UpdateDownloadRequest {

private String configURL;
private PluginFilesStructure currentReleaseFS;
Expand All @@ -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<String, String> requestHeaders) {
public UpdateDownloadRequest(final Context context,
final String configURL,
final String currentReleaseVersion,
final int currentNativeVersion,
final Map<String, String> requestHeaders) {
this.configURL = configURL;
this.currentNativeVersion = currentNativeVersion;
this.requestHeaders = requestHeaders;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p/>
* 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<String, String> 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<String, String> entry : requestHeaders.entrySet()) {
urlConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
}

return urlConnection;
}

}

0 comments on commit 210eb6f

Please sign in to comment.