@Test public void givenNetworkConnectivityIsAvailableAndRedirectIsAllowed_whenDownloadRemoteResourceWithTestRemoteCertificateURLNeedingRedirect_thenDownloadSucceedsAndRetrievesAFile() throws Exception { final String localFileURIInTemporaryTestDirectory = getLocalFileURIInTemporaryTestDirectory(LOCAL_FILE_NAME); final boolean redirectIsAllowed = true; final boolean wasDownloadSuccessful = testFileDownloader.downloadRemoteResource( new URL(TEST_REMOTE_CERTIFICATE_URL_NEEDING_REDIRECT_AS_STRING), localFileURIInTemporaryTestDirectory, redirectIsAllowed); assertTrue(wasDownloadSuccessful); assertTrue(fileExists(localFileURIInTemporaryTestDirectory)); }
final boolean downloadRemoteResource( @NonNull final URL remoteURL, @NonNull final String localURI, final boolean isRedirectAllowed) { checkNotNull( remoteURL, "The URL at which the remote resource is located must not be null"); checkNotNull( localURI, "The URI at which storing the file pointed by the remote URL must not be null"); checkArgument( remoteURL.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL_AS_STRING) || remoteURL.getProtocol().equalsIgnoreCase(HTTP_PROTOCOL_AS_STRING), "The FileDownloader supports only HTTP, and HTTPS protocols"); NetworkInfo activeNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) { Log.w(TAG_FILE_DOWNLOADER, "No connectivity is available. Download failed!"); return false; } Thread dataThread = new Thread() { @Override public void run() { HttpURLConnection urlConnection = null; InputStream inputStream = null; OutputStream outputStream = null; try { urlConnection = openAndReturnConnectionAssociatedToURL(remoteURL); urlConnection.setConnectTimeout(1000); Log.d(TAG_FILE_DOWNLOADER, "A connection was set up: " + remoteURL.toString()); if (isRedirectAllowed) { int connection_status = urlConnection.getResponseCode(); if (connection_status != HttpURLConnection.HTTP_OK) { if (connection_status == HttpURLConnection.HTTP_MOVED_TEMP || connection_status == HttpURLConnection.HTTP_MOVED_PERM || connection_status == HttpURLConnection.HTTP_SEE_OTHER) { urlConnection = followURLRedirection(urlConnection); } } } if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { retrievedFileMimeType = Optional.fromNullable(urlConnection.getContentType()); inputStream = new BufferedInputStream(urlConnection.getInputStream()); outputStream = new FileOutputStream(localURI); copyReadContentFromInputStreamToOutputStream(inputStream, outputStream); } } catch (IOException e) { } finally { closeConnectionAndStreams(urlConnection, inputStream, outputStream); } } private HttpURLConnection followURLRedirection(HttpURLConnection urlConnection) throws IOException { URL redirectedURL = new URL(urlConnection.getHeaderField("Location")); String cookies = urlConnection.getHeaderField("Set-Cookie"); urlConnection = openAndReturnConnectionAssociatedToURL(redirectedURL); urlConnection.setConnectTimeout(1000); urlConnection.setRequestProperty("Cookie", cookies); Log.d(TAG_FILE_DOWNLOADER, "The connection was redirected to: " + redirectedURL.toString()); return urlConnection; } private HttpURLConnection openAndReturnConnectionAssociatedToURL(URL remoteURL) throws IOException { if (remoteURL.getProtocol().equals(HTTPS_PROTOCOL_AS_STRING)) { return (HttpsURLConnection) remoteURL.openConnection(); } else { return (HttpURLConnection) remoteURL.openConnection(); } } private void copyReadContentFromInputStreamToOutputStream( InputStream inputStream, OutputStream outputStream) throws IOException { int read; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) > 0) { outputStream.write(bytes, 0, read); } Log.i(TAG_FILE_DOWNLOADER, "Download complete. Container Path: " + localURI); } private void closeConnectionAndStreams( HttpURLConnection urlConnection, InputStream inputStream, OutputStream outputStream) { Log.d(TAG_FILE_DOWNLOADER, "Clean up all pending streams.."); if (urlConnection != null) urlConnection.disconnect(); if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }; dataThread.start(); try { dataThread.join(); } catch (InterruptedException e) { e.printStackTrace(); return false; } File fileAtLocalURI = new File(localURI); return fileAtLocalURI.exists() && fileAtLocalURI.length() > 0; }
FileDownloader { final boolean downloadRemoteResource( @NonNull final URL remoteURL, @NonNull final String localURI, final boolean isRedirectAllowed) { checkNotNull( remoteURL, "The URL at which the remote resource is located must not be null"); checkNotNull( localURI, "The URI at which storing the file pointed by the remote URL must not be null"); checkArgument( remoteURL.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL_AS_STRING) || remoteURL.getProtocol().equalsIgnoreCase(HTTP_PROTOCOL_AS_STRING), "The FileDownloader supports only HTTP, and HTTPS protocols"); NetworkInfo activeNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) { Log.w(TAG_FILE_DOWNLOADER, "No connectivity is available. Download failed!"); return false; } Thread dataThread = new Thread() { @Override public void run() { HttpURLConnection urlConnection = null; InputStream inputStream = null; OutputStream outputStream = null; try { urlConnection = openAndReturnConnectionAssociatedToURL(remoteURL); urlConnection.setConnectTimeout(1000); Log.d(TAG_FILE_DOWNLOADER, "A connection was set up: " + remoteURL.toString()); if (isRedirectAllowed) { int connection_status = urlConnection.getResponseCode(); if (connection_status != HttpURLConnection.HTTP_OK) { if (connection_status == HttpURLConnection.HTTP_MOVED_TEMP || connection_status == HttpURLConnection.HTTP_MOVED_PERM || connection_status == HttpURLConnection.HTTP_SEE_OTHER) { urlConnection = followURLRedirection(urlConnection); } } } if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { retrievedFileMimeType = Optional.fromNullable(urlConnection.getContentType()); inputStream = new BufferedInputStream(urlConnection.getInputStream()); outputStream = new FileOutputStream(localURI); copyReadContentFromInputStreamToOutputStream(inputStream, outputStream); } } catch (IOException e) { } finally { closeConnectionAndStreams(urlConnection, inputStream, outputStream); } } private HttpURLConnection followURLRedirection(HttpURLConnection urlConnection) throws IOException { URL redirectedURL = new URL(urlConnection.getHeaderField("Location")); String cookies = urlConnection.getHeaderField("Set-Cookie"); urlConnection = openAndReturnConnectionAssociatedToURL(redirectedURL); urlConnection.setConnectTimeout(1000); urlConnection.setRequestProperty("Cookie", cookies); Log.d(TAG_FILE_DOWNLOADER, "The connection was redirected to: " + redirectedURL.toString()); return urlConnection; } private HttpURLConnection openAndReturnConnectionAssociatedToURL(URL remoteURL) throws IOException { if (remoteURL.getProtocol().equals(HTTPS_PROTOCOL_AS_STRING)) { return (HttpsURLConnection) remoteURL.openConnection(); } else { return (HttpURLConnection) remoteURL.openConnection(); } } private void copyReadContentFromInputStreamToOutputStream( InputStream inputStream, OutputStream outputStream) throws IOException { int read; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) > 0) { outputStream.write(bytes, 0, read); } Log.i(TAG_FILE_DOWNLOADER, "Download complete. Container Path: " + localURI); } private void closeConnectionAndStreams( HttpURLConnection urlConnection, InputStream inputStream, OutputStream outputStream) { Log.d(TAG_FILE_DOWNLOADER, "Clean up all pending streams.."); if (urlConnection != null) urlConnection.disconnect(); if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }; dataThread.start(); try { dataThread.join(); } catch (InterruptedException e) { e.printStackTrace(); return false; } File fileAtLocalURI = new File(localURI); return fileAtLocalURI.exists() && fileAtLocalURI.length() > 0; } }
FileDownloader { final boolean downloadRemoteResource( @NonNull final URL remoteURL, @NonNull final String localURI, final boolean isRedirectAllowed) { checkNotNull( remoteURL, "The URL at which the remote resource is located must not be null"); checkNotNull( localURI, "The URI at which storing the file pointed by the remote URL must not be null"); checkArgument( remoteURL.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL_AS_STRING) || remoteURL.getProtocol().equalsIgnoreCase(HTTP_PROTOCOL_AS_STRING), "The FileDownloader supports only HTTP, and HTTPS protocols"); NetworkInfo activeNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) { Log.w(TAG_FILE_DOWNLOADER, "No connectivity is available. Download failed!"); return false; } Thread dataThread = new Thread() { @Override public void run() { HttpURLConnection urlConnection = null; InputStream inputStream = null; OutputStream outputStream = null; try { urlConnection = openAndReturnConnectionAssociatedToURL(remoteURL); urlConnection.setConnectTimeout(1000); Log.d(TAG_FILE_DOWNLOADER, "A connection was set up: " + remoteURL.toString()); if (isRedirectAllowed) { int connection_status = urlConnection.getResponseCode(); if (connection_status != HttpURLConnection.HTTP_OK) { if (connection_status == HttpURLConnection.HTTP_MOVED_TEMP || connection_status == HttpURLConnection.HTTP_MOVED_PERM || connection_status == HttpURLConnection.HTTP_SEE_OTHER) { urlConnection = followURLRedirection(urlConnection); } } } if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { retrievedFileMimeType = Optional.fromNullable(urlConnection.getContentType()); inputStream = new BufferedInputStream(urlConnection.getInputStream()); outputStream = new FileOutputStream(localURI); copyReadContentFromInputStreamToOutputStream(inputStream, outputStream); } } catch (IOException e) { } finally { closeConnectionAndStreams(urlConnection, inputStream, outputStream); } } private HttpURLConnection followURLRedirection(HttpURLConnection urlConnection) throws IOException { URL redirectedURL = new URL(urlConnection.getHeaderField("Location")); String cookies = urlConnection.getHeaderField("Set-Cookie"); urlConnection = openAndReturnConnectionAssociatedToURL(redirectedURL); urlConnection.setConnectTimeout(1000); urlConnection.setRequestProperty("Cookie", cookies); Log.d(TAG_FILE_DOWNLOADER, "The connection was redirected to: " + redirectedURL.toString()); return urlConnection; } private HttpURLConnection openAndReturnConnectionAssociatedToURL(URL remoteURL) throws IOException { if (remoteURL.getProtocol().equals(HTTPS_PROTOCOL_AS_STRING)) { return (HttpsURLConnection) remoteURL.openConnection(); } else { return (HttpURLConnection) remoteURL.openConnection(); } } private void copyReadContentFromInputStreamToOutputStream( InputStream inputStream, OutputStream outputStream) throws IOException { int read; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) > 0) { outputStream.write(bytes, 0, read); } Log.i(TAG_FILE_DOWNLOADER, "Download complete. Container Path: " + localURI); } private void closeConnectionAndStreams( HttpURLConnection urlConnection, InputStream inputStream, OutputStream outputStream) { Log.d(TAG_FILE_DOWNLOADER, "Clean up all pending streams.."); if (urlConnection != null) urlConnection.disconnect(); if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }; dataThread.start(); try { dataThread.join(); } catch (InterruptedException e) { e.printStackTrace(); return false; } File fileAtLocalURI = new File(localURI); return fileAtLocalURI.exists() && fileAtLocalURI.length() > 0; } FileDownloader(@NonNull Context parentContext); }
FileDownloader { final boolean downloadRemoteResource( @NonNull final URL remoteURL, @NonNull final String localURI, final boolean isRedirectAllowed) { checkNotNull( remoteURL, "The URL at which the remote resource is located must not be null"); checkNotNull( localURI, "The URI at which storing the file pointed by the remote URL must not be null"); checkArgument( remoteURL.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL_AS_STRING) || remoteURL.getProtocol().equalsIgnoreCase(HTTP_PROTOCOL_AS_STRING), "The FileDownloader supports only HTTP, and HTTPS protocols"); NetworkInfo activeNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) { Log.w(TAG_FILE_DOWNLOADER, "No connectivity is available. Download failed!"); return false; } Thread dataThread = new Thread() { @Override public void run() { HttpURLConnection urlConnection = null; InputStream inputStream = null; OutputStream outputStream = null; try { urlConnection = openAndReturnConnectionAssociatedToURL(remoteURL); urlConnection.setConnectTimeout(1000); Log.d(TAG_FILE_DOWNLOADER, "A connection was set up: " + remoteURL.toString()); if (isRedirectAllowed) { int connection_status = urlConnection.getResponseCode(); if (connection_status != HttpURLConnection.HTTP_OK) { if (connection_status == HttpURLConnection.HTTP_MOVED_TEMP || connection_status == HttpURLConnection.HTTP_MOVED_PERM || connection_status == HttpURLConnection.HTTP_SEE_OTHER) { urlConnection = followURLRedirection(urlConnection); } } } if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { retrievedFileMimeType = Optional.fromNullable(urlConnection.getContentType()); inputStream = new BufferedInputStream(urlConnection.getInputStream()); outputStream = new FileOutputStream(localURI); copyReadContentFromInputStreamToOutputStream(inputStream, outputStream); } } catch (IOException e) { } finally { closeConnectionAndStreams(urlConnection, inputStream, outputStream); } } private HttpURLConnection followURLRedirection(HttpURLConnection urlConnection) throws IOException { URL redirectedURL = new URL(urlConnection.getHeaderField("Location")); String cookies = urlConnection.getHeaderField("Set-Cookie"); urlConnection = openAndReturnConnectionAssociatedToURL(redirectedURL); urlConnection.setConnectTimeout(1000); urlConnection.setRequestProperty("Cookie", cookies); Log.d(TAG_FILE_DOWNLOADER, "The connection was redirected to: " + redirectedURL.toString()); return urlConnection; } private HttpURLConnection openAndReturnConnectionAssociatedToURL(URL remoteURL) throws IOException { if (remoteURL.getProtocol().equals(HTTPS_PROTOCOL_AS_STRING)) { return (HttpsURLConnection) remoteURL.openConnection(); } else { return (HttpURLConnection) remoteURL.openConnection(); } } private void copyReadContentFromInputStreamToOutputStream( InputStream inputStream, OutputStream outputStream) throws IOException { int read; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) > 0) { outputStream.write(bytes, 0, read); } Log.i(TAG_FILE_DOWNLOADER, "Download complete. Container Path: " + localURI); } private void closeConnectionAndStreams( HttpURLConnection urlConnection, InputStream inputStream, OutputStream outputStream) { Log.d(TAG_FILE_DOWNLOADER, "Clean up all pending streams.."); if (urlConnection != null) urlConnection.disconnect(); if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }; dataThread.start(); try { dataThread.join(); } catch (InterruptedException e) { e.printStackTrace(); return false; } File fileAtLocalURI = new File(localURI); return fileAtLocalURI.exists() && fileAtLocalURI.length() > 0; } FileDownloader(@NonNull Context parentContext);  }
FileDownloader { final boolean downloadRemoteResource( @NonNull final URL remoteURL, @NonNull final String localURI, final boolean isRedirectAllowed) { checkNotNull( remoteURL, "The URL at which the remote resource is located must not be null"); checkNotNull( localURI, "The URI at which storing the file pointed by the remote URL must not be null"); checkArgument( remoteURL.getProtocol().equalsIgnoreCase(HTTPS_PROTOCOL_AS_STRING) || remoteURL.getProtocol().equalsIgnoreCase(HTTP_PROTOCOL_AS_STRING), "The FileDownloader supports only HTTP, and HTTPS protocols"); NetworkInfo activeNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) { Log.w(TAG_FILE_DOWNLOADER, "No connectivity is available. Download failed!"); return false; } Thread dataThread = new Thread() { @Override public void run() { HttpURLConnection urlConnection = null; InputStream inputStream = null; OutputStream outputStream = null; try { urlConnection = openAndReturnConnectionAssociatedToURL(remoteURL); urlConnection.setConnectTimeout(1000); Log.d(TAG_FILE_DOWNLOADER, "A connection was set up: " + remoteURL.toString()); if (isRedirectAllowed) { int connection_status = urlConnection.getResponseCode(); if (connection_status != HttpURLConnection.HTTP_OK) { if (connection_status == HttpURLConnection.HTTP_MOVED_TEMP || connection_status == HttpURLConnection.HTTP_MOVED_PERM || connection_status == HttpURLConnection.HTTP_SEE_OTHER) { urlConnection = followURLRedirection(urlConnection); } } } if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { retrievedFileMimeType = Optional.fromNullable(urlConnection.getContentType()); inputStream = new BufferedInputStream(urlConnection.getInputStream()); outputStream = new FileOutputStream(localURI); copyReadContentFromInputStreamToOutputStream(inputStream, outputStream); } } catch (IOException e) { } finally { closeConnectionAndStreams(urlConnection, inputStream, outputStream); } } private HttpURLConnection followURLRedirection(HttpURLConnection urlConnection) throws IOException { URL redirectedURL = new URL(urlConnection.getHeaderField("Location")); String cookies = urlConnection.getHeaderField("Set-Cookie"); urlConnection = openAndReturnConnectionAssociatedToURL(redirectedURL); urlConnection.setConnectTimeout(1000); urlConnection.setRequestProperty("Cookie", cookies); Log.d(TAG_FILE_DOWNLOADER, "The connection was redirected to: " + redirectedURL.toString()); return urlConnection; } private HttpURLConnection openAndReturnConnectionAssociatedToURL(URL remoteURL) throws IOException { if (remoteURL.getProtocol().equals(HTTPS_PROTOCOL_AS_STRING)) { return (HttpsURLConnection) remoteURL.openConnection(); } else { return (HttpURLConnection) remoteURL.openConnection(); } } private void copyReadContentFromInputStreamToOutputStream( InputStream inputStream, OutputStream outputStream) throws IOException { int read; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) > 0) { outputStream.write(bytes, 0, read); } Log.i(TAG_FILE_DOWNLOADER, "Download complete. Container Path: " + localURI); } private void closeConnectionAndStreams( HttpURLConnection urlConnection, InputStream inputStream, OutputStream outputStream) { Log.d(TAG_FILE_DOWNLOADER, "Clean up all pending streams.."); if (urlConnection != null) urlConnection.disconnect(); if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }; dataThread.start(); try { dataThread.join(); } catch (InterruptedException e) { e.printStackTrace(); return false; } File fileAtLocalURI = new File(localURI); return fileAtLocalURI.exists() && fileAtLocalURI.length() > 0; } FileDownloader(@NonNull Context parentContext);  static final ImmutableMap<String, String> SUPPORTED_MIME_TYPE_TO_FILE_EXTENSION_MAP; }