Improvements for notification template processing

This commit is contained in:
ViacheslavKlimov 2023-06-28 15:43:26 +03:00
parent 7528fceeb2
commit 996f11da9e
11 changed files with 175 additions and 74 deletions

View File

@ -18,7 +18,7 @@ package org.thingsboard.server.service.notification;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
import org.thingsboard.server.common.data.notification.NotificationRequest; import org.thingsboard.server.common.data.notification.NotificationRequest;
@ -27,10 +27,8 @@ import org.thingsboard.server.common.data.notification.settings.NotificationDeli
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; import org.thingsboard.server.common.data.notification.settings.NotificationSettings;
import org.thingsboard.server.common.data.notification.targets.NotificationRecipient; import org.thingsboard.server.common.data.notification.targets.NotificationRecipient;
import org.thingsboard.server.common.data.notification.template.DeliveryMethodNotificationTemplate; import org.thingsboard.server.common.data.notification.template.DeliveryMethodNotificationTemplate;
import org.thingsboard.server.common.data.notification.template.HasSubject;
import org.thingsboard.server.common.data.notification.template.NotificationTemplate; import org.thingsboard.server.common.data.notification.template.NotificationTemplate;
import org.thingsboard.server.common.data.notification.template.NotificationTemplateConfig; import org.thingsboard.server.common.data.notification.template.NotificationTemplateConfig;
import org.thingsboard.server.common.data.notification.template.WebDeliveryMethodNotificationTemplate;
import org.thingsboard.server.common.data.util.TemplateUtils; import org.thingsboard.server.common.data.util.TemplateUtils;
import java.util.EnumMap; import java.util.EnumMap;
@ -38,8 +36,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class NotificationProcessingContext { public class NotificationProcessingContext {
@ -86,13 +82,12 @@ public class NotificationProcessingContext {
public <T extends DeliveryMethodNotificationTemplate> T getProcessedTemplate(NotificationDeliveryMethod deliveryMethod, NotificationRecipient recipient) { public <T extends DeliveryMethodNotificationTemplate> T getProcessedTemplate(NotificationDeliveryMethod deliveryMethod, NotificationRecipient recipient) {
T template = (T) templates.get(deliveryMethod); T template = (T) templates.get(deliveryMethod);
Map<String, String> additionalTemplateContext = null;
if (recipient != null) { if (recipient != null) {
additionalTemplateContext = createTemplateContextForRecipient(recipient); Map<String, String> additionalTemplateContext = createTemplateContextForRecipient(recipient);
} if (template.getTemplatableValues().stream().anyMatch(value -> value.containsParams(additionalTemplateContext.keySet()))) {
if (MapUtils.isNotEmpty(additionalTemplateContext) && template.containsAny(additionalTemplateContext.keySet().toArray(String[]::new))) {
template = processTemplate(template, additionalTemplateContext); template = processTemplate(template, additionalTemplateContext);
} }
}
return template; return template;
} }
@ -107,22 +102,13 @@ public class NotificationProcessingContext {
if (templateContext.isEmpty()) return template; if (templateContext.isEmpty()) return template;
template = (T) template.copy(); template = (T) template.copy();
template.setBody(TemplateUtils.processTemplate(template.getBody(), templateContext)); template.getTemplatableValues().forEach(templatableValue -> {
if (template instanceof HasSubject) { String value = templatableValue.get();
String subject = ((HasSubject) template).getSubject(); if (StringUtils.isNotEmpty(value)) {
((HasSubject) template).setSubject(TemplateUtils.processTemplate(subject, templateContext)); value = TemplateUtils.processTemplate(value, templateContext);
} templatableValue.set(value);
if (template instanceof WebDeliveryMethodNotificationTemplate) {
WebDeliveryMethodNotificationTemplate webNotificationTemplate = (WebDeliveryMethodNotificationTemplate) template;
String buttonText = webNotificationTemplate.getButtonText();
if (isNotEmpty(buttonText)) {
webNotificationTemplate.setButtonText(TemplateUtils.processTemplate(buttonText, templateContext));
}
String buttonLink = webNotificationTemplate.getButtonLink();
if (isNotEmpty(buttonLink)) {
webNotificationTemplate.setButtonLink(TemplateUtils.processTemplate(buttonLink, templateContext));
}
} }
});
return template; return template;
} }

View File

@ -1,3 +1,18 @@
/**
* Copyright © 2016-2023 The Thingsboard Authors
*
* 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 org.thingsboard.server.service.notification.channels; package org.thingsboard.server.service.notification.channels;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;

View File

@ -23,9 +23,10 @@ import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.NotificationCenter; import org.thingsboard.rule.engine.api.NotificationCenter;
import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.audit.ActionType;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.NotificationTargetId; import org.thingsboard.server.common.data.id.NotificationTargetId;
import org.thingsboard.server.common.data.notification.Notification; import org.thingsboard.server.common.data.notification.Notification;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
@ -36,7 +37,7 @@ import org.thingsboard.server.common.data.notification.NotificationRequestPrevie
import org.thingsboard.server.common.data.notification.NotificationRequestStats; import org.thingsboard.server.common.data.notification.NotificationRequestStats;
import org.thingsboard.server.common.data.notification.NotificationRequestStatus; import org.thingsboard.server.common.data.notification.NotificationRequestStatus;
import org.thingsboard.server.common.data.notification.NotificationType; import org.thingsboard.server.common.data.notification.NotificationType;
import org.thingsboard.server.common.data.notification.info.AlarmNotificationInfo; import org.thingsboard.server.common.data.notification.info.EntityActionNotificationInfo;
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; import org.thingsboard.server.common.data.notification.settings.NotificationSettings;
import org.thingsboard.server.common.data.notification.settings.SlackNotificationDeliveryMethodConfig; import org.thingsboard.server.common.data.notification.settings.SlackNotificationDeliveryMethodConfig;
import org.thingsboard.server.common.data.notification.targets.MicrosoftTeamsNotificationTargetConfig; import org.thingsboard.server.common.data.notification.targets.MicrosoftTeamsNotificationTargetConfig;
@ -66,20 +67,18 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.in;
import static org.assertj.core.api.InstanceOfAssertFactories.type; import static org.assertj.core.api.InstanceOfAssertFactories.type;
import static org.awaitility.Awaitility.await; import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@DaoSqlTest @DaoSqlTest
@Slf4j @Slf4j
@ -554,12 +553,13 @@ public class NotificationApiTest extends AbstractNotificationApiTest {
var template = new MicrosoftTeamsDeliveryMethodNotificationTemplate(); var template = new MicrosoftTeamsDeliveryMethodNotificationTemplate();
template.setEnabled(true); template.setEnabled(true);
template.setSubject("This is subject"); String templateParams = "${recipientTitle} - ${entityType}";
template.setBody("This is text"); template.setSubject("Subject: " + templateParams);
template.setBody("Body: " + templateParams);
template.setThemeColor("ff0000"); template.setThemeColor("ff0000");
var button = new MicrosoftTeamsDeliveryMethodNotificationTemplate.Button(); var button = new MicrosoftTeamsDeliveryMethodNotificationTemplate.Button();
button.setName("Go to ThingsBoard Cloud"); button.setName("Button: " + templateParams);
button.setUri("https://thingsboard.cloud"); button.setUri("https://" + templateParams);
template.setButton(button); template.setButton(button);
NotificationTemplate notificationTemplate = new NotificationTemplate(); NotificationTemplate notificationTemplate = new NotificationTemplate();
notificationTemplate.setName("Notification to Teams"); notificationTemplate.setName("Notification to Teams");
@ -576,23 +576,28 @@ public class NotificationApiTest extends AbstractNotificationApiTest {
.originatorEntityId(tenantAdminUserId) .originatorEntityId(tenantAdminUserId)
.templateId(notificationTemplate.getId()) .templateId(notificationTemplate.getId())
.targets(List.of(target.getUuidId())) .targets(List.of(target.getUuidId()))
.info(EntityActionNotificationInfo.builder()
.entityId(new DeviceId(UUID.randomUUID())) // to test templatization
.actionType(ActionType.ADDED)
.userId(tenantAdminUserId.getId())
.build())
.build(); .build();
NotificationRequestPreview preview = doPost("/api/notification/request/preview", notificationRequest, NotificationRequestPreview.class); NotificationRequestPreview preview = doPost("/api/notification/request/preview", notificationRequest, NotificationRequestPreview.class);
System.err.println(preview);
assertThat(preview.getRecipientsCountByTarget().get(target.getName())).isEqualTo(1); assertThat(preview.getRecipientsCountByTarget().get(target.getName())).isEqualTo(1);
assertThat(preview.getRecipientsPreview()).containsOnly(targetConfig.getChannelName()); assertThat(preview.getRecipientsPreview()).containsOnly(targetConfig.getChannelName());
var messageCaptor = ArgumentCaptor.forClass(MicrosoftTeamsNotificationChannel.Message.class); var messageCaptor = ArgumentCaptor.forClass(MicrosoftTeamsNotificationChannel.Message.class);
doPost("/api/notification/request", notificationRequest, NotificationRequest.class); notificationCenter.processNotificationRequest(tenantId, notificationRequest, null);
verify(restTemplate, timeout(20000)).postForEntity(eq(webhookUrl), messageCaptor.capture(), any()); verify(restTemplate, timeout(20000)).postForEntity(eq(webhookUrl), messageCaptor.capture(), any());
var message = messageCaptor.getValue(); var message = messageCaptor.getValue();
String expectedParams = "My channel - Device";
assertThat(message.getThemeColor()).isEqualTo(template.getThemeColor()); assertThat(message.getThemeColor()).isEqualTo(template.getThemeColor());
assertThat(message.getSections().get(0).getActivityTitle()).isEqualTo(template.getSubject()); assertThat(message.getSections().get(0).getActivityTitle()).isEqualTo("Subject: " + expectedParams);
assertThat(message.getSections().get(0).getActivitySubtitle()).isEqualTo(template.getBody()); assertThat(message.getSections().get(0).getActivitySubtitle()).isEqualTo("Body: " + expectedParams);
assertThat(message.getPotentialAction().get(0).getName()).isEqualTo(button.getName()); assertThat(message.getPotentialAction().get(0).getName()).isEqualTo("Button: " + expectedParams);
assertThat(message.getPotentialAction().get(0).getTargets().get(0).getUri()).isEqualTo(button.getUri()); assertThat(message.getPotentialAction().get(0).getTargets().get(0).getUri()).isEqualTo("https://" + expectedParams);
} }
private void checkFullNotificationsUpdate(UnreadNotificationsUpdate notificationsUpdate, String... expectedNotifications) { private void checkFullNotificationsUpdate(UnreadNotificationsUpdate notificationsUpdate, String... expectedNotifications) {

View File

@ -1,3 +1,18 @@
/**
* Copyright © 2016-2023 The Thingsboard Authors
*
* 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 org.thingsboard.server.common.data.notification.targets; package org.thingsboard.server.common.data.notification.targets;
import lombok.Data; import lombok.Data;

View File

@ -22,10 +22,10 @@ import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "method") @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "method")
@ -33,7 +33,8 @@ import javax.validation.constraints.NotEmpty;
@Type(name = "WEB", value = WebDeliveryMethodNotificationTemplate.class), @Type(name = "WEB", value = WebDeliveryMethodNotificationTemplate.class),
@Type(name = "EMAIL", value = EmailDeliveryMethodNotificationTemplate.class), @Type(name = "EMAIL", value = EmailDeliveryMethodNotificationTemplate.class),
@Type(name = "SMS", value = SmsDeliveryMethodNotificationTemplate.class), @Type(name = "SMS", value = SmsDeliveryMethodNotificationTemplate.class),
@Type(name = "SLACK", value = SlackDeliveryMethodNotificationTemplate.class) @Type(name = "SLACK", value = SlackDeliveryMethodNotificationTemplate.class),
@Type(name = "MICROSOFT_TEAMS", value = MicrosoftTeamsDeliveryMethodNotificationTemplate.class)
}) })
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ -41,7 +42,7 @@ public abstract class DeliveryMethodNotificationTemplate {
private boolean enabled; private boolean enabled;
@NotEmpty @NotEmpty
private String body; protected String body;
public DeliveryMethodNotificationTemplate(DeliveryMethodNotificationTemplate other) { public DeliveryMethodNotificationTemplate(DeliveryMethodNotificationTemplate other) {
this.enabled = other.enabled; this.enabled = other.enabled;
@ -54,8 +55,7 @@ public abstract class DeliveryMethodNotificationTemplate {
@JsonIgnore @JsonIgnore
public abstract DeliveryMethodNotificationTemplate copy(); public abstract DeliveryMethodNotificationTemplate copy();
public boolean containsAny(String... params) { @JsonIgnore
return StringUtils.containsAny(body, params); public abstract List<TemplatableValue> getTemplatableValues();
}
} }

View File

@ -19,12 +19,12 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString; import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
import org.thingsboard.server.common.data.validation.Length; import org.thingsboard.server.common.data.validation.Length;
import org.thingsboard.server.common.data.validation.NoXss; import org.thingsboard.server.common.data.validation.NoXss;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import java.util.List;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ -37,6 +37,11 @@ public class EmailDeliveryMethodNotificationTemplate extends DeliveryMethodNotif
@NotEmpty @NotEmpty
private String subject; private String subject;
private final List<TemplatableValue> templatableValues = List.of(
TemplatableValue.of(this::getBody, this::setBody),
TemplatableValue.of(this::getSubject, this::setSubject)
);
public EmailDeliveryMethodNotificationTemplate(EmailDeliveryMethodNotificationTemplate other) { public EmailDeliveryMethodNotificationTemplate(EmailDeliveryMethodNotificationTemplate other) {
super(other); super(other);
this.subject = other.subject; this.subject = other.subject;
@ -52,9 +57,4 @@ public class EmailDeliveryMethodNotificationTemplate extends DeliveryMethodNotif
return new EmailDeliveryMethodNotificationTemplate(this); return new EmailDeliveryMethodNotificationTemplate(this);
} }
@Override
public boolean containsAny(String... params) {
return super.containsAny(params) || StringUtils.containsAny(subject, params);
}
} }

View File

@ -1,15 +1,27 @@
/**
* Copyright © 2016-2023 The Thingsboard Authors
*
* 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 org.thingsboard.server.common.data.notification.template; package org.thingsboard.server.common.data.notification.template;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString; import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ -23,8 +35,22 @@ public class MicrosoftTeamsDeliveryMethodNotificationTemplate extends DeliveryMe
private String customMessageCardJson; private String customMessageCardJson;
public MicrosoftTeamsDeliveryMethodNotificationTemplate(DeliveryMethodNotificationTemplate other) { private final List<TemplatableValue> templatableValues = List.of(
TemplatableValue.of(this::getBody, this::setBody),
TemplatableValue.of(this::getSubject, this::setSubject),
TemplatableValue.of(() -> button != null ? button.getName() : null,
processed -> {if (button != null) button.setName(processed);}),
TemplatableValue.of(() -> button != null ? button.getUri() : null,
processed -> {if (button != null) button.setUri(processed);}),
TemplatableValue.of(this::getCustomMessageCardJson, this::setCustomMessageCardJson)
);
public MicrosoftTeamsDeliveryMethodNotificationTemplate(MicrosoftTeamsDeliveryMethodNotificationTemplate other) {
super(other); super(other);
this.subject = other.subject;
this.themeColor = other.themeColor;
this.button = other.button != null ? new Button(other.button) : null;
this.customMessageCardJson = other.customMessageCardJson;
} }
@Override @Override
@ -37,21 +63,16 @@ public class MicrosoftTeamsDeliveryMethodNotificationTemplate extends DeliveryMe
return new MicrosoftTeamsDeliveryMethodNotificationTemplate(this); return new MicrosoftTeamsDeliveryMethodNotificationTemplate(this);
} }
@Override
public List<TemplatableValue> getTemplatableValues() {
return List.of(
TemplatableValue.of(body, this::setBody),
TemplatableValue.of(subject, this::setSubject),
TemplatableValue.of(button, Button::getName, Button::setName),
TemplatableValue.of(button, Button::getUri, Button::setUri),
TemplatableValue.of(customMessageCardJson, this::setCustomMessageCardJson)
);
}
@Data @Data
@NoArgsConstructor
public static class Button { public static class Button {
private String name; private String name;
private String uri; private String uri;
public Button(Button other) {
this.name = other.name;
this.uri = other.uri;
}
} }
} }

View File

@ -22,12 +22,18 @@ import lombok.ToString;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
import org.thingsboard.server.common.data.validation.NoXss; import org.thingsboard.server.common.data.validation.NoXss;
import java.util.List;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)
public class SlackDeliveryMethodNotificationTemplate extends DeliveryMethodNotificationTemplate { public class SlackDeliveryMethodNotificationTemplate extends DeliveryMethodNotificationTemplate {
private final List<TemplatableValue> templatableValues = List.of(
TemplatableValue.of(this::getBody, this::setBody)
);
public SlackDeliveryMethodNotificationTemplate(DeliveryMethodNotificationTemplate other) { public SlackDeliveryMethodNotificationTemplate(DeliveryMethodNotificationTemplate other) {
super(other); super(other);
} }

View File

@ -23,12 +23,18 @@ import org.thingsboard.server.common.data.notification.NotificationDeliveryMetho
import org.thingsboard.server.common.data.validation.Length; import org.thingsboard.server.common.data.validation.Length;
import org.thingsboard.server.common.data.validation.NoXss; import org.thingsboard.server.common.data.validation.NoXss;
import java.util.List;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)
public class SmsDeliveryMethodNotificationTemplate extends DeliveryMethodNotificationTemplate { public class SmsDeliveryMethodNotificationTemplate extends DeliveryMethodNotificationTemplate {
private final List<TemplatableValue> templatableValues = List.of(
TemplatableValue.of(this::getBody, this::setBody)
);
public SmsDeliveryMethodNotificationTemplate(SmsDeliveryMethodNotificationTemplate other) { public SmsDeliveryMethodNotificationTemplate(SmsDeliveryMethodNotificationTemplate other) {
super(other); super(other);
} }

View File

@ -0,0 +1,46 @@
/**
* Copyright © 2016-2023 The Thingsboard Authors
*
* 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 org.thingsboard.server.common.data.notification.template;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import java.util.Collection;
import java.util.function.Consumer;
import java.util.function.Supplier;
@RequiredArgsConstructor
public class TemplatableValue {
private final Supplier<String> getter;
private final Consumer<String> setter;
public static TemplatableValue of(Supplier<String> getter, Consumer<String> setter) {
return new TemplatableValue(getter, setter);
}
public String get() {
return getter.get();
}
public void set(String processed) {
setter.accept(processed);
}
public boolean containsParams(Collection<String> params) {
return StringUtils.containsAny(get(), params.toArray(String[]::new));
}
}

View File

@ -23,12 +23,12 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString; import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
import org.thingsboard.server.common.data.validation.Length; import org.thingsboard.server.common.data.validation.Length;
import org.thingsboard.server.common.data.validation.NoXss; import org.thingsboard.server.common.data.validation.NoXss;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Data @Data
@ -43,6 +43,13 @@ public class WebDeliveryMethodNotificationTemplate extends DeliveryMethodNotific
private String subject; private String subject;
private JsonNode additionalConfig; private JsonNode additionalConfig;
private final List<TemplatableValue> templatableValues = List.of(
TemplatableValue.of(this::getBody, this::setBody),
TemplatableValue.of(this::getSubject, this::setSubject),
TemplatableValue.of(this::getButtonText, this::setButtonText),
TemplatableValue.of(this::getButtonLink, this::setButtonLink)
);
public WebDeliveryMethodNotificationTemplate(WebDeliveryMethodNotificationTemplate other) { public WebDeliveryMethodNotificationTemplate(WebDeliveryMethodNotificationTemplate other) {
super(other); super(other);
this.subject = other.subject; this.subject = other.subject;
@ -107,10 +114,4 @@ public class WebDeliveryMethodNotificationTemplate extends DeliveryMethodNotific
return new WebDeliveryMethodNotificationTemplate(this); return new WebDeliveryMethodNotificationTemplate(this);
} }
@Override
public boolean containsAny(String... params) {
return super.containsAny(params) || StringUtils.containsAny(subject, params)
|| StringUtils.containsAny(getButtonText(), params) || StringUtils.containsAny(getButtonLink(), params);
}
} }