Refactor complex logic out of formatters into getters.

Create 2 new getters via refactoring: getSshHost() and
getEmailRegistrationToken() and use them in formatters.

Change-Id: I7f9bc24521b38222c80ed3f57fcbf8de032a6203
This commit is contained in:
Martin Fick
2010-07-20 18:47:14 -06:00
parent df89f998d5
commit dc8df3553d
3 changed files with 44 additions and 26 deletions

View File

@@ -112,24 +112,31 @@ public abstract class NewChangeSender extends ChangeEmail {
}
private String getPullUrl() {
final List<HostKey> hostKeys = sshInfo.getHostKeys();
if (hostKeys.isEmpty()) {
final String host = getSshHost();
if (host == null) {
return "";
}
final String host = hostKeys.get(0).getHost();
final StringBuilder r = new StringBuilder();
r.append("git pull ssh://");
if (host.startsWith("*:")) {
r.append(getGerritHost());
r.append(host.substring(1));
} else {
r.append(host);
}
r.append("/");
r.append(projectName);
r.append(" ");
r.append(patchSet.getRefName());
return r.toString();
}
public String getSshHost() {
final List<HostKey> hostKeys = sshInfo.getHostKeys();
if (hostKeys.isEmpty()) {
return null;
}
final String host = hostKeys.get(0).getHost();
if (host.startsWith("*:")) {
return getGerritHost() + host.substring(1);
}
return host;
}
}

View File

@@ -56,14 +56,7 @@ public class RegisterNewEmailSender extends OutgoingEmail {
final StringBuilder url = new StringBuilder();
url.append(getGerritUrl());
url.append("#VE,");
try {
url.append(authConfig.getEmailRegistrationToken().newToken(
Base64.encodeBytes(addr.getBytes("UTF-8"))));
} catch (XsrfException e) {
throw new IllegalArgumentException(e);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
url.append(getEmailRegistrationToken());
appendText("Welcome to Gerrit Code Review at ");
appendText(getGerritHost());
@@ -93,4 +86,15 @@ public class RegisterNewEmailSender extends OutgoingEmail {
+ " Replies to this message will not\n");
appendText("be read or answered.\n");
}
public String getEmailRegistrationToken() {
try {
return authConfig.getEmailRegistrationToken().newToken(
Base64.encodeBytes(addr.getBytes("UTF-8")));
} catch (XsrfException e) {
throw new IllegalArgumentException(e);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
}
}

View File

@@ -125,24 +125,31 @@ public class ReplacePatchSetSender extends ReplyToChangeSender {
}
private String getPullUrl() {
final List<HostKey> hostKeys = sshInfo.getHostKeys();
if (hostKeys.isEmpty()) {
final String host = getSshHost();
if (host == null) {
return "";
}
final String host = hostKeys.get(0).getHost();
final StringBuilder r = new StringBuilder();
r.append("git pull ssh://");
if (host.startsWith("*:")) {
r.append(getGerritHost());
r.append(host.substring(1));
} else {
r.append(host);
}
r.append("/");
r.append(projectName);
r.append(" ");
r.append(patchSet.getRefName());
return r.toString();
}
public String getSshHost() {
final List<HostKey> hostKeys = sshInfo.getHostKeys();
if (hostKeys.isEmpty()) {
return null;
}
final String host = hostKeys.get(0).getHost();
if (host.startsWith("*:")) {
return getGerritHost() + host.substring(1);
}
return host;
}
}