Add extension point that allows plugins to register soy templates for sending emails
Plugins want to use the existing mail infrastructure for sending custom emails. If the plugin can register soy templates, the plugin can easily extend one of the existing sender class (e.g. ReplyToChangeSender) to send custom emails with these custom soy templates. Signed-off-by: Edwin Kempin <ekempin@google.com> Change-Id: Ic3adcbb73ed3f82395da4f273bc9fc856a50a257
This commit is contained in:
@@ -2752,6 +2752,12 @@ update could not be achieved (e.g. due to slow responding server nodes)
|
||||
this interface can be used to retry the request instead of failing it
|
||||
immediately.
|
||||
|
||||
[[mail-soy-template-provider]]
|
||||
== MailSoyTemplateProvider
|
||||
|
||||
This extension point allows to provide soy templates for registration
|
||||
so that they can be used for sending emails from a plugin.
|
||||
|
||||
[[quota-enforcer]]
|
||||
== Quota Enforcer
|
||||
|
||||
|
@@ -150,6 +150,7 @@ import com.google.gerrit.server.mail.send.FromAddressGenerator;
|
||||
import com.google.gerrit.server.mail.send.FromAddressGeneratorProvider;
|
||||
import com.google.gerrit.server.mail.send.InboundEmailRejectionSender;
|
||||
import com.google.gerrit.server.mail.send.MailSoySauceProvider;
|
||||
import com.google.gerrit.server.mail.send.MailSoyTemplateProvider;
|
||||
import com.google.gerrit.server.mail.send.MailTemplates;
|
||||
import com.google.gerrit.server.mime.FileTypeRegistry;
|
||||
import com.google.gerrit.server.mime.MimeUtilFileTypeRegistry;
|
||||
@@ -392,6 +393,7 @@ public class GerritGlobalModule extends FactoryModule {
|
||||
DynamicSet.bind(binder(), RequestListener.class).to(TraceRequestListener.class);
|
||||
DynamicSet.setOf(binder(), ChangeETagComputation.class);
|
||||
DynamicSet.setOf(binder(), ExceptionHook.class);
|
||||
DynamicSet.setOf(binder(), MailSoyTemplateProvider.class);
|
||||
|
||||
DynamicMap.mapOf(binder(), MailFilter.class);
|
||||
bind(MailFilter.class).annotatedWith(Exports.named("ListMailFilter")).to(ListMailFilter.class);
|
||||
|
@@ -17,6 +17,7 @@ package com.google.gerrit.server.mail.send;
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.google.common.io.Resources;
|
||||
import com.google.gerrit.server.config.SitePaths;
|
||||
import com.google.gerrit.server.plugincontext.PluginSetContext;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.ProvisionException;
|
||||
@@ -80,11 +81,16 @@ public class MailSoySauceProvider implements Provider<SoySauce> {
|
||||
|
||||
private final SitePaths site;
|
||||
private final SoyAstCache cache;
|
||||
private final PluginSetContext<MailSoyTemplateProvider> templateProviders;
|
||||
|
||||
@Inject
|
||||
MailSoySauceProvider(SitePaths site, SoyAstCache cache) {
|
||||
MailSoySauceProvider(
|
||||
SitePaths site,
|
||||
SoyAstCache cache,
|
||||
PluginSetContext<MailSoyTemplateProvider> templateProviders) {
|
||||
this.site = site;
|
||||
this.cache = cache;
|
||||
this.templateProviders = templateProviders;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -92,12 +98,15 @@ public class MailSoySauceProvider implements Provider<SoySauce> {
|
||||
SoyFileSet.Builder builder = SoyFileSet.builder();
|
||||
builder.setSoyAstCache(cache);
|
||||
for (String name : TEMPLATES) {
|
||||
addTemplate(builder, name);
|
||||
addTemplate(builder, "com/google/gerrit/server/mail/", name);
|
||||
}
|
||||
templateProviders.runEach(
|
||||
e -> e.getFileNames().forEach(p -> addTemplate(builder, e.getPath(), p)));
|
||||
return builder.build().compileTemplates();
|
||||
}
|
||||
|
||||
private void addTemplate(SoyFileSet.Builder builder, String name) throws ProvisionException {
|
||||
private void addTemplate(SoyFileSet.Builder builder, String resourcePath, String name)
|
||||
throws ProvisionException {
|
||||
// Load as a file in the mail templates directory if present.
|
||||
Path tmpl = site.mail_dir.resolve(name);
|
||||
if (Files.isRegularFile(tmpl)) {
|
||||
@@ -115,7 +124,9 @@ public class MailSoySauceProvider implements Provider<SoySauce> {
|
||||
}
|
||||
|
||||
// Otherwise load the template as a resource.
|
||||
String resourcePath = "com/google/gerrit/server/mail/" + name;
|
||||
builder.add(Resources.getResource(resourcePath));
|
||||
if (!resourcePath.endsWith("/")) {
|
||||
resourcePath += "/";
|
||||
}
|
||||
builder.add(Resources.getResource(resourcePath + name));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,43 @@
|
||||
// Copyright (C) 2019 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.mail.send;
|
||||
|
||||
import com.google.gerrit.extensions.annotations.ExtensionPoint;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Extension point to provide soy templates that should be registered so that they can be used for
|
||||
* sending emails from a plugin.
|
||||
*/
|
||||
@ExtensionPoint
|
||||
public interface MailSoyTemplateProvider {
|
||||
/**
|
||||
* Return the name of the resource path that contains the soy template files that are returned by
|
||||
* {@link #getFileNames()}.
|
||||
*
|
||||
* @return resource path of the templates
|
||||
*/
|
||||
String getPath();
|
||||
|
||||
/**
|
||||
* Return the names of the soy template files.
|
||||
*
|
||||
* <p>These files are expected to exist in the resource path that is returned by {@link
|
||||
* #getPath()}.
|
||||
*
|
||||
* @return names of the template files, including the {@code .soy} file extension
|
||||
*/
|
||||
Set<String> getFileNames();
|
||||
}
|
Reference in New Issue
Block a user