Skip to content

Commit

Permalink
Fix parsing of protocol/username/hostname from host string (issue ima…
Browse files Browse the repository at this point in the history
…gej#53)

FilesUploader: remove logic of parsing the host string which is kinda
duplicated and have it as part of the UpdateSite class.
UpdateSite: set the upload protocol, username, and hostname as part of
setting the host string.
  • Loading branch information
carandraug committed Jun 7, 2018
1 parent ab983db commit a4a37e9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
22 changes: 7 additions & 15 deletions src/main/java/net/imagej/updater/FilesUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,16 @@ public String getSiteName() {
}

public String getDefaultUsername() {
String host = site.getHost();
if (host.startsWith("sftp:")) host = host.substring(5);
final int at = host.indexOf('@');
if (at > 0) return host.substring(0, at);
final String name = UpdaterUserInterface.get().getPref(UpdaterUtil.PREFS_USER);
if (name == null) return "";
return name;
String username = site.getUploadUsername();
if (username == null)
username = UpdaterUserInterface.get().getPref(UpdaterUtil.PREFS_USER);
if (username == null)
username = "";
return username;
}

public String getUploadHost() {
String host = site.getHost();
if (uploader != null) {
final String protocol = uploader.getProtocol();
if (protocol != null && host.startsWith(protocol + ":")) {
host = host.substring(protocol.length() + 1);
}
}
return host.substring(host.indexOf('@') + 1);
return site.getUploadHost();
}

public String getUploadDirectory() {
Expand Down
37 changes: 33 additions & 4 deletions src/main/java/net/imagej/updater/UpdateSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class UpdateSite implements Cloneable, Comparable<UpdateSite> {
private String name;
private String url;
private String host;
private String upload_protocol;
private String upload_username;
private String upload_host;
private String uploadDirectory;
private String description;
private String maintainer;
Expand Down Expand Up @@ -119,6 +122,25 @@ public String getHost() {

public void setHost(final String host) {
this.host = host;
if (host == null) {
this.upload_protocol = null;
this.upload_username = null;
this.upload_host = null;
} else {
final int colon = host.indexOf(':');
if (colon < 0)
this.upload_protocol = null;
else
this.upload_protocol = host.substring(0, colon);

final int at = host.indexOf('@');
if (at < 0)
this.upload_username = null;
else
this.upload_username = host.substring(colon+1, at);

this.upload_host = host.substring(at+1);
}
}

public String getUploadDirectory() {
Expand Down Expand Up @@ -205,10 +227,17 @@ public int hashCode() {
public String getUploadProtocol() {
if (host == null)
throw new RuntimeException("Missing upload information for site " + url);
final int at = host.indexOf('@');
final int colon = host.indexOf(':');
if (colon > 0 && (at < 0 || colon < at)) return host.substring(0, colon);
return "ssh";
return this.upload_protocol;
}
public String getUploadUsername() {
if (host == null)
throw new RuntimeException("Missing upload information for site " + url);
return this.upload_username;
}
public String getUploadHost() {
if (host == null)
throw new RuntimeException("Missing upload information for site " + url);
return this.upload_host;
}

// -- Helper methods --
Expand Down

0 comments on commit a4a37e9

Please sign in to comment.