|
| 1 | +/* |
| 2 | + * Copyright 2014-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package de.codecentric.boot.admin.server.notify; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.springframework.context.expression.MapAccessor; |
| 25 | +import org.springframework.expression.Expression; |
| 26 | +import org.springframework.expression.ParserContext; |
| 27 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 28 | +import org.springframework.expression.spel.support.DataBindingPropertyAccessor; |
| 29 | +import org.springframework.expression.spel.support.SimpleEvaluationContext; |
| 30 | +import org.springframework.http.HttpEntity; |
| 31 | +import org.springframework.http.HttpHeaders; |
| 32 | +import org.springframework.http.MediaType; |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | +import org.springframework.web.client.RestTemplate; |
| 35 | +import reactor.core.publisher.Mono; |
| 36 | + |
| 37 | +import de.codecentric.boot.admin.server.domain.entities.Instance; |
| 38 | +import de.codecentric.boot.admin.server.domain.entities.InstanceRepository; |
| 39 | +import de.codecentric.boot.admin.server.domain.events.InstanceEvent; |
| 40 | +import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent; |
| 41 | +import de.codecentric.boot.admin.server.domain.values.StatusInfo; |
| 42 | + |
| 43 | +/** |
| 44 | + * Notifier submitting events to Mattermost. |
| 45 | + * |
| 46 | + * @author Emir Boyaci |
| 47 | + */ |
| 48 | +public class MattermostNotifier extends AbstractStatusChangeNotifier { |
| 49 | + |
| 50 | +private static final String DEFAULT_MESSAGE = "**#{instance.registration.name}** (#{instance.id}) is **#{event.statusInfo.status}**"; |
| 51 | + |
| 52 | +private final SpelExpressionParser parser = new SpelExpressionParser(); |
| 53 | + |
| 54 | +private RestTemplate restTemplate; |
| 55 | + |
| 56 | +/** |
| 57 | + * API url for Mattermost (i.e. https://example.mattermost.com/api/v4/posts) |
| 58 | + */ |
| 59 | +@Nullable |
| 60 | +private URI apiUrl; |
| 61 | + |
| 62 | +/** |
| 63 | + * Bot access token (i.e. dufc8q78hjgeccwsfhe37pcq1w) |
| 64 | + */ |
| 65 | +@Nullable |
| 66 | +private String botAccessToken; |
| 67 | + |
| 68 | +/** |
| 69 | + * Optional channel name without # sign (i.e. h616jh436pysjpopp3259mhwxc) |
| 70 | + */ |
| 71 | +@Nullable |
| 72 | +private String channelId; |
| 73 | + |
| 74 | +/** |
| 75 | + * Message formatted using Mattermost markups. SpEL template using event as root |
| 76 | + */ |
| 77 | +private Expression message; |
| 78 | + |
| 79 | +public MattermostNotifier(InstanceRepository repository, RestTemplate restTemplate) { |
| 80 | +super(repository); |
| 81 | +this.restTemplate = restTemplate; |
| 82 | +this.message = parser.parseExpression(DEFAULT_MESSAGE, ParserContext.TEMPLATE_EXPRESSION); |
| 83 | +} |
| 84 | + |
| 85 | +@Override |
| 86 | +protected Mono<Void> doNotify(InstanceEvent event, Instance instance) { |
| 87 | +if (apiUrl == null) { |
| 88 | +return Mono.error(new IllegalStateException("'url' must not be null.")); |
| 89 | +} |
| 90 | +return Mono.fromRunnable(() -> restTemplate.postForEntity(apiUrl, createMessage(event, instance), Void.class)); |
| 91 | +} |
| 92 | + |
| 93 | +public void setRestTemplate(RestTemplate restTemplate) { |
| 94 | +this.restTemplate = restTemplate; |
| 95 | +} |
| 96 | + |
| 97 | +protected Object createMessage(InstanceEvent event, Instance instance) { |
| 98 | +Map<String, Object> messageJson = new HashMap<>(); |
| 99 | +if (channelId != null) { |
| 100 | +messageJson.put("channel_id", channelId); |
| 101 | +} |
| 102 | + |
| 103 | +Map<String, Object> attachments = new HashMap<>(); |
| 104 | +attachments.put("text", getText(event, instance)); |
| 105 | +attachments.put("fallback", getText(event, instance)); |
| 106 | +attachments.put("color", getColor(event)); |
| 107 | + |
| 108 | +Map<String, Object> props = new HashMap<>(); |
| 109 | +props.put("attachments", Collections.singletonList(attachments)); |
| 110 | + |
| 111 | +messageJson.put("props", props); |
| 112 | + |
| 113 | +HttpHeaders headers = new HttpHeaders(); |
| 114 | +headers.setContentType(MediaType.APPLICATION_JSON); |
| 115 | +headers.setBearerAuth(botAccessToken); |
| 116 | +return new HttpEntity<>(messageJson, headers); |
| 117 | +} |
| 118 | + |
| 119 | +@Nullable |
| 120 | +protected String getText(InstanceEvent event, Instance instance) { |
| 121 | +Map<String, Object> root = new HashMap<>(); |
| 122 | +root.put("event", event); |
| 123 | +root.put("instance", instance); |
| 124 | +root.put("lastStatus", getLastStatus(event.getInstance())); |
| 125 | +SimpleEvaluationContext context = SimpleEvaluationContext |
| 126 | +.forPropertyAccessors(DataBindingPropertyAccessor.forReadOnlyAccess(), new MapAccessor()) |
| 127 | +.withRootObject(root) |
| 128 | +.build(); |
| 129 | + |
| 130 | +return message.getValue(context, String.class); |
| 131 | +} |
| 132 | + |
| 133 | +protected String getColor(InstanceEvent event) { |
| 134 | +if (event instanceof InstanceStatusChangedEvent statusChangedEvent) { |
| 135 | +return StatusInfo.STATUS_UP.equals(statusChangedEvent.getStatusInfo().getStatus()) ? "#2eb885" : "#a30100"; |
| 136 | +} |
| 137 | +else { |
| 138 | +return "#439FE0"; |
| 139 | +} |
| 140 | +} |
| 141 | + |
| 142 | +@Nullable |
| 143 | +public URI getApiUrl() { |
| 144 | +return apiUrl; |
| 145 | +} |
| 146 | + |
| 147 | +public void setApiUrl(@Nullable URI apiUrl) { |
| 148 | +this.apiUrl = apiUrl; |
| 149 | +} |
| 150 | + |
| 151 | +@Nullable |
| 152 | +public String getChannelId() { |
| 153 | +return channelId; |
| 154 | +} |
| 155 | + |
| 156 | +public void setChannelId(@Nullable String channelId) { |
| 157 | +this.channelId = channelId; |
| 158 | +} |
| 159 | + |
| 160 | +@Nullable |
| 161 | +public String getBotAccessToken() { |
| 162 | +return botAccessToken; |
| 163 | +} |
| 164 | + |
| 165 | +public void setBotAccessToken(@Nullable String botAccessToken) { |
| 166 | +this.botAccessToken = botAccessToken; |
| 167 | +} |
| 168 | + |
| 169 | +public String getMessage() { |
| 170 | +return message.getExpressionString(); |
| 171 | +} |
| 172 | + |
| 173 | +public void setMessage(String message) { |
| 174 | +this.message = parser.parseExpression(message, ParserContext.TEMPLATE_EXPRESSION); |
| 175 | +} |
| 176 | + |
| 177 | +} |
0 commit comments