summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/mail/OutgoingEmail.java
diff options
context:
space:
mode:
Diffstat (limited to 'gerrit-server/src/main/java/com/google/gerrit/server/mail/OutgoingEmail.java')
-rw-r--r--gerrit-server/src/main/java/com/google/gerrit/server/mail/OutgoingEmail.java97
1 files changed, 75 insertions, 22 deletions
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/mail/OutgoingEmail.java b/gerrit-server/src/main/java/com/google/gerrit/server/mail/OutgoingEmail.java
index e6a1ebcf75..de8628f990 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/mail/OutgoingEmail.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/mail/OutgoingEmail.java
@@ -14,18 +14,23 @@
package com.google.gerrit.server.mail;
-import com.google.gerrit.reviewdb.Account;
-import com.google.gerrit.reviewdb.UserIdentity;
+import com.google.gerrit.reviewdb.client.Account;
+import com.google.gerrit.reviewdb.client.UserIdentity;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.mail.EmailHeader.AddressList;
+import com.google.gwtorm.server.OrmException;
import org.apache.commons.lang.StringUtils;
+import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.Velocity;
+import org.apache.velocity.context.InternalContextAdapterImpl;
+import org.apache.velocity.runtime.RuntimeInstance;
+import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.eclipse.jgit.util.SystemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
@@ -54,10 +59,14 @@ public abstract class OutgoingEmail {
protected VelocityContext velocityContext;
protected final EmailArguments args;
+ private final String anonymousCowardName;
protected Account.Id fromId;
- protected OutgoingEmail(EmailArguments ea, final String mc) {
+
+ protected OutgoingEmail(EmailArguments ea, final String anonymousCowardName,
+ final String mc) {
args = ea;
+ this.anonymousCowardName = anonymousCowardName;
messageClass = mc;
headers = new LinkedHashMap<String, EmailHeader>();
}
@@ -226,7 +235,7 @@ public abstract class OutgoingEmail {
/** Lookup a human readable name for an account, usually the "full name". */
protected String getNameFor(final Account.Id accountId) {
if (accountId == null) {
- return "Anonymous Coward";
+ return anonymousCowardName;
}
final Account userAccount = args.accountCache.get(accountId).getAccount();
@@ -235,7 +244,7 @@ public abstract class OutgoingEmail {
name = userAccount.getPreferredEmail();
}
if (name == null) {
- name = "Anonymous Coward #" + accountId;
+ name = anonymousCowardName + " #" + accountId;
}
return name;
}
@@ -254,7 +263,7 @@ public abstract class OutgoingEmail {
return email;
} else /* (name == null && email == null) */{
- return "Anonymous Coward #" + accountId;
+ return anonymousCowardName + " #" + accountId;
}
}
@@ -297,13 +306,17 @@ public abstract class OutgoingEmail {
/** Schedule delivery of this message to the given account. */
protected void add(final RecipientType rt, final Account.Id to) {
- if (!rcptTo.contains(to) && isVisibleTo(to)) {
- rcptTo.add(to);
- add(rt, toAddress(to));
+ try {
+ if (!rcptTo.contains(to) && isVisibleTo(to)) {
+ rcptTo.add(to);
+ add(rt, toAddress(to));
+ }
+ } catch (OrmException e) {
+ log.error("Error reading database for account: " + to, e);
}
}
- protected boolean isVisibleTo(final Account.Id to) {
+ protected boolean isVisibleTo(final Account.Id to) throws OrmException {
return true;
}
@@ -345,24 +358,64 @@ public abstract class OutgoingEmail {
protected String velocify(String template) throws EmailException {
try {
- StringWriter w = new StringWriter();
- Velocity.evaluate(velocityContext, w, "OutgoingEmail", template);
- return w.toString();
- } catch(Exception e) {
- throw new EmailException("Velocity template " + template, e);
+ RuntimeInstance runtime = args.velocityRuntime;
+ String templateName = "OutgoingEmail";
+ SimpleNode tree = runtime.parse(new StringReader(template), templateName);
+ InternalContextAdapterImpl ica = new InternalContextAdapterImpl(velocityContext);
+ ica.pushCurrentTemplateName(templateName);
+ try {
+ tree.init(ica, runtime);
+ StringWriter w = new StringWriter();
+ tree.render(ica, w);
+ return w.toString();
+ } finally {
+ ica.popCurrentTemplateName();
+ }
+ } catch (Exception e) {
+ throw new EmailException("Cannot format velocity template: " + template, e);
}
}
protected String velocifyFile(String name) throws EmailException {
- if (!Velocity.resourceExists(name)) {
- name = "com/google/gerrit/server/mail/" + name;
- }
try {
+ RuntimeInstance runtime = args.velocityRuntime;
+ if (runtime.getLoaderNameForResource(name) == null) {
+ name = "com/google/gerrit/server/mail/" + name;
+ }
+ Template template = runtime.getTemplate(name, "UTF-8");
StringWriter w = new StringWriter();
- Velocity.mergeTemplate(name, "UTF-8", velocityContext, w);
+ template.merge(velocityContext, w);
return w.toString();
- } catch(Exception e) {
- throw new EmailException("Velocity template " + name + ".\n", e);
+ } catch (EmailException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new EmailException("Cannot format velocity template " + name, e);
+ }
+ }
+
+ public String joinStrings(Iterable<Object> in, String joiner) {
+ return joinStrings(in.iterator(), joiner);
+ }
+
+ public String joinStrings(Iterator<Object> in, String joiner) {
+ if (!in.hasNext()) {
+ return "";
+ }
+
+ Object first = in.next();
+ if (!in.hasNext()) {
+ return safeToString(first);
+ }
+
+ StringBuilder r = new StringBuilder();
+ r.append(safeToString(first));
+ while (in.hasNext()) {
+ r.append(joiner).append(safeToString(in.next()));
}
+ return r.toString();
+ }
+
+ private static String safeToString(Object obj) {
+ return obj != null ? obj.toString() : "";
}
}