Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions brainstorm/src/org/brainstorm/TestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ public static void TestDialog(){
// ===== TEST DOWNLOAD =====
public static void TestDownload(){
// Download file
// String srcUrl = "https://neuroimage.usc.edu/brainstorm3_register/getupdate.php?c=UbsM09";
String srcUrl = "https://neuroimage.usc.edu/brainstorm3_register/getupdate.php?t=Infant7w";
String srcUrl = "https://neuroimage.usc.edu/bst/getupdate.php?c=UbsM09";
//String srcUrl = "https://github.com/csn-le/wave_clus/archive/master.zip";
final BstDownload bstDownload = new BstDownload(srcUrl, "C:\\Users\\francois\\Downloads\\test.zip", "Download test");

bstDownload.download();
Expand All @@ -322,7 +322,7 @@ public static void TestDownload(){
if (bstDownload.getResult() != 1){
JOptionPane.showMessageDialog(null, "Error downloading file: \n" + bstDownload.getMessage(), "Download file", JOptionPane.WARNING_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, bstDownload.getMessage(), "Download file", JOptionPane.OK_OPTION);
JOptionPane.showMessageDialog(null, "File successfully downloaded!", "Download file", JOptionPane.OK_OPTION);
}
}

Expand Down Expand Up @@ -350,11 +350,11 @@ public void run() {
//TestTableWindow();
// TestFileSelector();
// TestDialog();
// TestDownload();
TestDownload();
//TestListWindow();
// TestCheckListWindow();
//TestTreeWindow();
TestHotkeyDialog();
//TestHotkeyDialog();
// TestTreeTableWindow();
//CloneControl.probe("C:\\Work\\Dev\\brainstorm_git\\brainstorm3");

Expand Down
68 changes: 52 additions & 16 deletions brainstorm/src/org/brainstorm/file/BstDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.security.SecureRandom;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import se.datadosen.component.RiverLayout;

/**
Expand All @@ -34,9 +36,20 @@ public BstDownload(String strUrl, String file, String title){
// ===== COPY ARGUMENTS =====
// Output filename
outputFile = file;
// Old versions of Matlab default to a weird ICE library URL Handler
// that causes HTTPS issues. Force usage of proper Handler if possible.
URLStreamHandler handler = null;
if (strUrl.toLowerCase().startsWith("https"))
{
try {
handler = new sun.net.www.protocol.https.Handler();
} catch (Exception e) {
handler = null;
}
}
// Input URL
try{
url = new java.net.URL(strUrl);
url = new java.net.URL(null, strUrl, handler);
}
catch (Exception e){
jLabel.setText("Error: Invalid URL.");
Expand Down Expand Up @@ -124,7 +137,7 @@ public void run(){

// Download thread
public void downloadThread(Proxy proxy){
RandomAccessFile file = null;
OutputStream file = null;
InputStream stream = null;
int downloaded = 0;

Expand Down Expand Up @@ -154,7 +167,7 @@ public void downloadThread(Proxy proxy){
jProgressBar.setMaximum(contentLength);

// Open output file
file = new RandomAccessFile(outputFile, "rws");
file = new FileOutputStream(outputFile);
stream = connection.getInputStream();
isDownloading = true;

Expand Down Expand Up @@ -210,16 +223,6 @@ private HttpURLConnection downloadAttempt(Proxy proxy) throws Exception {
// Open connection to URL
HttpURLConnection connection;
if (url.getProtocol().equals("https")) {
// If https is requested, ensure we use latest TLS version since
// some websites disable older versions for security issues.

// This is only supported in Java v1.7+
double version = Double.parseDouble(System.getProperty("java.specification.version"));
if (version < 1.7) {
message = "HTTPS connections require Java 1.7. Please update Java.";
throw new Exception("ConnectionError");
}

HttpsURLConnection tlsConnection;

if (proxy != null){
Expand All @@ -228,9 +231,42 @@ private HttpURLConnection downloadAttempt(Proxy proxy) throws Exception {
tlsConnection = (HttpsURLConnection) url.openConnection();
}

SSLContext ssl = SSLContext.getInstance("TLSv1.2");
ssl.init(null, null, new SecureRandom());
tlsConnection.setSSLSocketFactory(ssl.getSocketFactory());
// If https is requested, ensure we use latest TLS version since
// some websites disable older versions for security issues.

// This is only supported in Java v1.7+
double version = Double.parseDouble(System.getProperty("java.specification.version"));
if (version >= 1.7) {
SSLContext ssl = SSLContext.getInstance("TLSv1.2");
ssl.init(null, null, new SecureRandom());
tlsConnection.setSSLSocketFactory(ssl.getSocketFactory());
} else {
// GitHub requires the latest TLS version.
if (url.getHost().equals("github.com")) {
message = "HTTPS connections to GitHub require Java 1.7. Please update Java.";
throw new Exception("ConnectionError");
}

// Create a trust all certificate checker because early Java 1.6
// versions are too strict for our own neuroimage.usc.edu domain
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
}
};

SSLContext ssl = SSLContext.getInstance("SSL");
ssl.init(null, trustAllCerts, null);
tlsConnection.setSSLSocketFactory(ssl.getSocketFactory());
}

connection = tlsConnection;
} else {
if (proxy != null){
Expand Down