diff --git a/Documentation/dev-plugins.txt b/Documentation/dev-plugins.txt index 74ed72548b..7975575147 100644 --- a/Documentation/dev-plugins.txt +++ b/Documentation/dev-plugins.txt @@ -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 diff --git a/java/com/google/gerrit/server/config/GerritGlobalModule.java b/java/com/google/gerrit/server/config/GerritGlobalModule.java index 3794f04d79..3b9c40e67f 100644 --- a/java/com/google/gerrit/server/config/GerritGlobalModule.java +++ b/java/com/google/gerrit/server/config/GerritGlobalModule.java @@ -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); diff --git a/java/com/google/gerrit/server/mail/send/MailSoySauceProvider.java b/java/com/google/gerrit/server/mail/send/MailSoySauceProvider.java index 151567efa2..92220eb890 100644 --- a/java/com/google/gerrit/server/mail/send/MailSoySauceProvider.java +++ b/java/com/google/gerrit/server/mail/send/MailSoySauceProvider.java @@ -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 { private final SitePaths site; private final SoyAstCache cache; + private final PluginSetContext templateProviders; @Inject - MailSoySauceProvider(SitePaths site, SoyAstCache cache) { + MailSoySauceProvider( + SitePaths site, + SoyAstCache cache, + PluginSetContext templateProviders) { this.site = site; this.cache = cache; + this.templateProviders = templateProviders; } @Override @@ -92,12 +98,15 @@ public class MailSoySauceProvider implements Provider { 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 { } // 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)); } } diff --git a/java/com/google/gerrit/server/mail/send/MailSoyTemplateProvider.java b/java/com/google/gerrit/server/mail/send/MailSoyTemplateProvider.java new file mode 100644 index 0000000000..3a6ff64c37 --- /dev/null +++ b/java/com/google/gerrit/server/mail/send/MailSoyTemplateProvider.java @@ -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. + * + *

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 getFileNames(); +}