Skip to content

Commit 764bf72

Browse files
pahlijoshiste
authored andcommitted
Add Let´s Chat notifications
Adds an new Notifier implementation for Let's Chat.
1 parent 4f80a48 commit 764bf72

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed

spring-boot-admin-docs/src/main/asciidoc/server-notifications.adoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,41 @@ account and configure it appropriately.
241241
| `+++"*#{application.name}* (#{application.id}) is *#{to.status}*"+++`
242242
|
243243
|===
244+
245+
[letschat-notifications]
246+
==== Let´s Chat notifications ====
247+
To enable Let´s Chat notifications you need to add the host url and add the API token and username from Let´s Chat
248+
249+
.Let´s Chat notifications configuration options
250+
|===
251+
| Property name |Description |Default value
252+
253+
| spring.boot.admin.notify.letschat.enabled
254+
| Enable let´s Chat notifications
255+
| `true`
256+
257+
| spring.boot.admin.notify.letschat.ignore-changes
258+
| Comma-delimited list of status changes to be ignored. Format: "<from-status>:<to-status>". Wildcards allowed.
259+
| `"UNKNOWN:UP"`
260+
261+
| spring.boot.admin.notify.letschat.url
262+
| The let´s Chat Host URL to send notifications
263+
|
264+
265+
| spring.boot.admin.notify.letschat.room
266+
| the room where to send the messages
267+
|
268+
269+
| spring.boot.admin.notify.letschat.token
270+
| the token to access the let´s Chat API
271+
|
272+
273+
| spring.boot.admin.notify.letschat.username
274+
| The username for which the token was created
275+
| `Spring Boot Admin`
276+
277+
| spring.boot.admin.notify.letschat.message
278+
| Message to use in the event. SpEL-expressions are supported
279+
| `+++"*#{application.name}* (#{application.id}) is *#{to.status}*"+++`
280+
|
281+
|===

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/config/NotifierConfiguration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import de.codecentric.boot.admin.notify.NotifierListener;
4141
import de.codecentric.boot.admin.notify.PagerdutyNotifier;
4242
import de.codecentric.boot.admin.notify.SlackNotifier;
43+
import de.codecentric.boot.admin.notify.LetsChatNotifier;
4344
import de.codecentric.boot.admin.notify.filter.FilteringNotifier;
4445
import de.codecentric.boot.admin.notify.filter.web.NotificationFilterController;
4546
import de.codecentric.boot.admin.web.PrefixHandlerMapping;
@@ -161,4 +162,16 @@ public SlackNotifier slackNotifier() {
161162
}
162163
}
163164

165+
@Configuration
166+
@ConditionalOnProperty(prefix = "spring.boot.admin.notify.letschat", name = "url")
167+
@AutoConfigureBefore({ NotifierListenerConfiguration.class,
168+
CompositeNotifierConfiguration.class })
169+
public static class LetsChatNotifierConfiguration {
170+
@Bean
171+
@ConditionalOnMissingBean
172+
@ConfigurationProperties("spring.boot.admin.notify.letschat")
173+
public LetsChatNotifier letsChatNotifier() {
174+
return new LetsChatNotifier();
175+
}
176+
}
164177
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2014-2017 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+
* http://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+
package de.codecentric.boot.admin.notify;
17+
18+
import java.net.URI;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.expression.Expression;
23+
import org.springframework.expression.ParserContext;
24+
import org.springframework.expression.spel.standard.SpelExpressionParser;
25+
import org.springframework.http.HttpEntity;
26+
import org.springframework.http.HttpHeaders;
27+
import org.springframework.http.HttpMethod;
28+
import org.springframework.http.MediaType;
29+
import org.springframework.util.Base64Utils;
30+
import org.springframework.web.client.RestTemplate;
31+
32+
import de.codecentric.boot.admin.event.ClientApplicationEvent;
33+
34+
35+
/**
36+
* Notifier submitting events to let´s Chat.
37+
*
38+
* @author Rico Pahlisch
39+
*/
40+
public class LetsChatNotifier extends AbstractStatusChangeNotifier {
41+
private static final String DEFAULT_MESSAGE = "*#{application.name}* (#{application.id}) is *#{to.status}*";
42+
43+
private final SpelExpressionParser parser = new SpelExpressionParser();
44+
private RestTemplate restTemplate = new RestTemplate();
45+
46+
/**
47+
* Host URL for Let´s Chat
48+
*/
49+
private URI url;
50+
51+
/**
52+
* Name of the room
53+
*/
54+
private String room;
55+
56+
/**
57+
* Token for the Let´s chat API
58+
*/
59+
private String token;
60+
61+
/**
62+
* username which sends notification
63+
*/
64+
private String username = "Spring Boot Admin";
65+
66+
/**
67+
* Message template. SpEL template using event as root
68+
*/
69+
private Expression message;
70+
71+
public LetsChatNotifier() {
72+
this.message = parser.parseExpression(DEFAULT_MESSAGE, ParserContext.TEMPLATE_EXPRESSION);
73+
}
74+
75+
@Override
76+
protected void doNotify(ClientApplicationEvent event) throws Exception {
77+
HttpHeaders headers = new HttpHeaders();
78+
headers.setContentType(MediaType.APPLICATION_JSON);
79+
// Let's Chat requiers the token as basic username, the password can be an arbitrary string.
80+
String auth = Base64Utils.encodeToString(String.format("%s:%s", token, username).getBytes());
81+
headers.add(HttpHeaders.AUTHORIZATION, String.format("Basic %s", auth));
82+
restTemplate.exchange(createUrl(), HttpMethod.POST, new HttpEntity<>(createMessage(event), headers), Void.class);
83+
}
84+
85+
private URI createUrl() {
86+
return URI.create(String.format("%s/rooms/%s/messages", url, room));
87+
}
88+
89+
public void setRestTemplate(RestTemplate restTemplate) {
90+
this.restTemplate = restTemplate;
91+
}
92+
93+
public void setUrl(URI url) {
94+
this.url = url;
95+
}
96+
97+
public void setUsername(String username) {
98+
this.username = username;
99+
}
100+
101+
public void setRoom(String room) {
102+
this.room = room;
103+
}
104+
105+
public void setToken(String token) {
106+
this.token = token;
107+
}
108+
109+
public void setMessage(String message) {
110+
this.message = parser.parseExpression(message, ParserContext.TEMPLATE_EXPRESSION);
111+
}
112+
113+
protected Object createMessage(ClientApplicationEvent event) {
114+
Map<String, String> messageJson = new HashMap<>();
115+
messageJson.put("text", getText(event));
116+
return messageJson;
117+
}
118+
119+
protected String getText(ClientApplicationEvent event) {
120+
return message.getValue(event, String.class);
121+
}
122+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package de.codecentric.boot.admin.notify;
2+
3+
import static org.mockito.Matchers.eq;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.verify;
6+
7+
import java.net.URI;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.springframework.http.HttpEntity;
14+
import org.springframework.http.HttpHeaders;
15+
import org.springframework.http.HttpMethod;
16+
import org.springframework.http.MediaType;
17+
import org.springframework.util.Base64Utils;
18+
import org.springframework.web.client.RestTemplate;
19+
20+
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
21+
import de.codecentric.boot.admin.model.Application;
22+
import de.codecentric.boot.admin.model.StatusInfo;
23+
24+
public class LetsChatNotifierTest {
25+
private static final String room = "text_room";
26+
private static final String token = "text_token";
27+
private static final String user = "api_user";
28+
private static final String appName = "App";
29+
private static final String id = "-id-";
30+
private static final String host = "http://localhost";
31+
32+
private LetsChatNotifier notifier;
33+
private RestTemplate restTemplate;
34+
35+
@Before
36+
public void setUp() {
37+
restTemplate = mock(RestTemplate.class);
38+
notifier = new LetsChatNotifier();
39+
notifier.setUsername(user);
40+
notifier.setUrl(URI.create(host));
41+
notifier.setRoom(room);
42+
notifier.setToken(token);
43+
notifier.setRestTemplate(restTemplate);
44+
}
45+
46+
@Test
47+
public void test_onApplicationEvent_resolve() {
48+
StatusInfo infoDown = StatusInfo.ofDown();
49+
StatusInfo infoUp = StatusInfo.ofUp();
50+
51+
notifier.notify(getEvent(infoDown, infoUp));
52+
53+
HttpEntity<?> expected = expectedMessage(standardMessage(infoUp.getStatus(), appName, id));
54+
verify(restTemplate).exchange(eq(URI.create(String.format("%s/rooms/%s/messages", host, room))), eq(HttpMethod.POST), eq(expected), eq(Void.class));
55+
}
56+
57+
@Test
58+
public void test_onApplicationEvent_resolve_with_custom_message() {
59+
StatusInfo infoDown = StatusInfo.ofDown();
60+
StatusInfo infoUp = StatusInfo.ofUp();
61+
62+
notifier.setMessage("TEST");
63+
notifier.notify(getEvent(infoDown, infoUp));
64+
65+
HttpEntity<?> expected = expectedMessage("TEST");
66+
verify(restTemplate).exchange(eq(URI.create(String.format("%s/rooms/%s/messages", host, room))), eq(HttpMethod.POST), eq(expected), eq(Void.class));
67+
}
68+
69+
private ClientApplicationStatusChangedEvent getEvent(StatusInfo infoDown, StatusInfo infoUp) {
70+
return new ClientApplicationStatusChangedEvent(
71+
Application.create(appName).withId(id).withHealthUrl("http://health").build(),
72+
infoDown, infoUp);
73+
}
74+
75+
private HttpEntity<?> expectedMessage(String message) {
76+
HttpHeaders httpHeaders = new HttpHeaders();
77+
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
78+
String auth = Base64Utils.encodeToString(String.format("%s:%s", token, user).getBytes());
79+
httpHeaders.add(HttpHeaders.AUTHORIZATION, String.format("Basic %s", auth));
80+
Map<String, Object> messageJson = new HashMap<>();
81+
messageJson.put("text", message);
82+
return new HttpEntity<>(messageJson, httpHeaders);
83+
}
84+
85+
private String standardMessage(String status, String appName, String id) {
86+
return "*" + appName + "* (" + id + ") is *" + status + "*";
87+
}
88+
89+
}

0 commit comments

Comments
 (0)