Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/template resolution by external service #180

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .mvn/wrapper/maven-wrapper.jar
Binary file not shown.
18 changes: 18 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,5 @@ kafka.sasl.mechanism=PLAIN
kafka.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="<API_KEY>" password="<API_SECRET>";
kafka.security.protocol=SASL_SSL
```

For detailed configuration of templating and i18n please see [Template resolution documentation](/docs/template-resolution.md)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public Optional<TemplatedEmailSender> lookupTemplatedEmailSenderFor(final String
}

private static void assertValidProviderTypeArgument(final String providerType) {
Assert.hasText(providerType, "Null or empty text was passed as ana rgument for parameter 'providerType'.");
Assert.hasText(providerType, "Null or empty text was passed as an argument for parameter 'providerType'.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ public class TemplatingConfigurationImpl implements TemplatingConfiguration {

private String templatesPath = "/";
private String templateExtension = ".ftl";
private Boolean externalUrl = false;

private final ResourceLoader resourceLoader = new DefaultResourceLoader();

TemplatingConfigurationImpl(
@Value("${notifier.templates.path}") final String templatesPath,
@Value("${notifier.templates.extension:.ftl}") final String templateExtension) {
@Value("${notifier.templates.extension:.ftl}") final String templateExtension,
@Value("${notifier.templates.externalUrl:false}") final Boolean externalUrl) {
this.templatesPath = templatesPath;
this.templateExtension = templateExtension;
this.externalUrl = externalUrl;
}


Expand All @@ -38,9 +41,14 @@ public Configuration getFreemarkerConfiguration(final boolean localizedLookupEna
@Override
protected URL getURL(final String resourceName) {
try {
final Resource resource = getResource(resourceName);
return resource.exists() ? resource.getURL() : null;
} catch (final IOException ex) {
final String fullPath = getFullPath(resourceName);
if (externalUrl) {
return new URL(fullPath);
} else {
final Resource resource = getResource(fullPath);
return resource.exists() ? resource.getURL() : null;
}
} catch (IOException ex) {
throw new TemplateLookupFailedException(resourceName, templatesPath, ex);
}
}
Expand All @@ -53,15 +61,18 @@ protected URL getURL(final String resourceName) {
return cfg;
}

private Resource getResource(final String resourceName) throws IOException {
private Resource getResource(final String fullPath) throws IOException {
return resourceLoader.getResource(fullPath);
}

private String getFullPath(final String resourceName) {
if (!resourceName.endsWith(templateExtension)) {
return resourceLoader.getResource(templatesPath + "/" + resourceName + templateExtension);
return templatesPath + "/" + resourceName + templateExtension;
} else {
return resourceLoader.getResource(templatesPath + "/" + resourceName);
return templatesPath + "/" + resourceName;
}
}


private static final class TemplateLookupFailedException extends RuntimeException {

private static String message(final String resourceName, final String templatesPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ notification.push.application.arn.operator.android=customer_ios_arn

# Freemarker
notifier.templates.path=
notifier.templates.externalUrl=

flyway.locations=classpath:db/migration/{vendor}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public void testGetFreemarkerConfiguration() throws IOException {
final String prefix = "dummy";
final String extension = ".ftl";
tempFile = File.createTempFile(prefix, extension);
localizedTempFile = new File(tempFile.getAbsolutePath().replace(extension, "_" + locale.toString() + extension));
localizedTempFile = new File(tempFile.getAbsolutePath().replace(extension, "_" + locale + extension));
localizedTempFile.createNewFile();
final TemplatingConfiguration configuration = new TemplatingConfigurationImpl(
"file:///" + tempFile.getParent(),
extension
extension,
false
);
assertThat(configuration.getFreemarkerConfiguration(true).getTemplate(
tempFile.getName(),
Expand Down
51 changes: 51 additions & 0 deletions docs/template-resolution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Template resolution strategies
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please go over this file and update it in accordance with the implementation. Then link this file from the main README.md as the place where one can read more about the templating config.


General intro

## Local Templates Resolution Strategy

Description of this approach

Configuration example
```yaml
notifier:
templates:
externalUrl: false
path: file:/templates
extension: .ftl
```

### File naming

Template naming patters

### Configuring local templates' resolution strategy for dockerized environments

Paths to override and docker command examples

## External Service Templates Resolution Strategy

Description of this approach

Configuration example
```yaml
notifier:
templates:
externalUrl: true
path: https://example.com
extension: .ftl
```

### External Service API Specification

Implement the following API endpoints, assuming .ftl template extension:

GET /<template_name>.ftl
Returns the template content.

Endpoints should return the following responses:

200 OK
The FTL template content with Content-type: text.
404 Not Found
The template does not exist.
Loading