Skip to content

Commit

Permalink
feat: improve notifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
SteKoe committed Oct 26, 2023
1 parent 21357ee commit 224d148
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package de.codecentric.boot.admin.server.notify;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -29,7 +30,8 @@
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.DataBindingPropertyAccessor;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -101,17 +103,18 @@ private Object getText(InstanceEvent event, Instance instance) {
root.put("event", event);
root.put("instance", instance);
root.put("lastStatus", getLastStatus(event.getInstance()));
StandardEvaluationContext context = new StandardEvaluationContext(root);
context.addPropertyAccessor(new MapAccessor());
SimpleEvaluationContext context = SimpleEvaluationContext
.forPropertyAccessors(DataBindingPropertyAccessor.forReadOnlyAccess(), new MapAccessor())
.withRootObject(root).build();
return message.getValue(context, String.class);
}

private String getSign(Long timestamp) {
try {
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
return URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
}
catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.DataBindingPropertyAccessor;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -117,8 +118,9 @@ protected String createContent(InstanceEvent event, Instance instance) {
root.put("event", event);
root.put("instance", instance);
root.put("lastStatus", getLastStatus(event.getInstance()));
StandardEvaluationContext context = new StandardEvaluationContext(root);
context.addPropertyAccessor(new MapAccessor());
SimpleEvaluationContext context = SimpleEvaluationContext
.forPropertyAccessors(DataBindingPropertyAccessor.forReadOnlyAccess(), new MapAccessor())
.withRootObject(root).build();
return message.getValue(context, String.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.DataBindingPropertyAccessor;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -96,7 +97,7 @@ protected String buildUrl() {
if (url == null) {
throw new IllegalStateException("'url' must not be null.");
}
return String.format("%s/room/%s/notification?auth_token=%s", url.toString(), roomId, authToken);
return String.format("%s/room/%s/notification?auth_token=%s", url, roomId, authToken);
}

protected HttpEntity<Map<String, Object>> createHipChatNotification(InstanceEvent event, Instance instance) {
Expand All @@ -121,8 +122,9 @@ protected String getMessage(InstanceEvent event, Instance instance) {
root.put("event", event);
root.put("instance", instance);
root.put("lastStatus", getLastStatus(event.getInstance()));
StandardEvaluationContext context = new StandardEvaluationContext(root);
context.addPropertyAccessor(new MapAccessor());
SimpleEvaluationContext context = SimpleEvaluationContext
.forPropertyAccessors(DataBindingPropertyAccessor.forReadOnlyAccess(), new MapAccessor())
.withRootObject(root).build();
return description.getValue(context, String.class);
}

Expand All @@ -136,49 +138,49 @@ protected String getColor(InstanceEvent event) {
}
}

public void setUrl(@Nullable URI url) {
this.url = url;
}

@Nullable
public URI getUrl() {
return url;
}

public void setAuthToken(@Nullable String authToken) {
this.authToken = authToken;
public void setUrl(@Nullable URI url) {
this.url = url;
}

@Nullable
public String getAuthToken() {
return authToken;
}

public void setRoomId(@Nullable String roomId) {
this.roomId = roomId;
public void setAuthToken(@Nullable String authToken) {
this.authToken = authToken;
}

@Nullable
public String getRoomId() {
return roomId;
}

public void setNotify(boolean notify) {
this.notify = notify;
public void setRoomId(@Nullable String roomId) {
this.roomId = roomId;
}

public boolean isNotify() {
return notify;
}

public void setDescription(String description) {
this.description = parser.parseExpression(description, ParserContext.TEMPLATE_EXPRESSION);
public void setNotify(boolean notify) {
this.notify = notify;
}

public String getDescription() {
return description.getExpressionString();
}

public void setDescription(String description) {
this.description = parser.parseExpression(description, ParserContext.TEMPLATE_EXPRESSION);
}

public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.DataBindingPropertyAccessor;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -119,56 +120,57 @@ protected String getText(InstanceEvent event, Instance instance) {
root.put("event", event);
root.put("instance", instance);
root.put("lastStatus", getLastStatus(event.getInstance()));
StandardEvaluationContext context = new StandardEvaluationContext(root);
context.addPropertyAccessor(new MapAccessor());
SimpleEvaluationContext context = SimpleEvaluationContext
.forPropertyAccessors(DataBindingPropertyAccessor.forReadOnlyAccess(), new MapAccessor())
.withRootObject(root).build();
return message.getValue(context, String.class);
}

public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

public void setUrl(@Nullable URI url) {
this.url = url;
}

@Nullable
public URI getUrl() {
return url;
}

public void setUsername(String username) {
this.username = username;
public void setUrl(@Nullable URI url) {
this.url = url;
}

public String getUsername() {
return username;
}

public void setRoom(@Nullable String room) {
this.room = room;
public void setUsername(String username) {
this.username = username;
}

@Nullable
public String getRoom() {
return room;
}

public void setToken(@Nullable String token) {
this.token = token;
public void setRoom(@Nullable String room) {
this.room = room;
}

@Nullable
public String getToken() {
return token;
}

public void setMessage(String message) {
this.message = parser.parseExpression(message, ParserContext.TEMPLATE_EXPRESSION);
public void setToken(@Nullable String token) {
this.token = token;
}

public String getMessage() {
return message.getExpressionString();
}

public void setMessage(String message) {
this.message = parser.parseExpression(message, ParserContext.TEMPLATE_EXPRESSION);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.DataBindingPropertyAccessor;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -141,7 +143,7 @@ public MicrosoftTeamsNotifier(InstanceRepository repository, RestTemplate restTe
@Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
Message message;
StandardEvaluationContext context = createEvaluationContext(event, instance);
EvaluationContext context = createEvaluationContext(event, instance);
if (event instanceof InstanceRegisteredEvent) {
message = getRegisteredMessage(instance, context);
}
Expand Down Expand Up @@ -172,23 +174,23 @@ protected boolean shouldNotify(InstanceEvent event, Instance instance) {
|| super.shouldNotify(event, instance);
}

protected Message getDeregisteredMessage(Instance instance, StandardEvaluationContext context) {
protected Message getDeregisteredMessage(Instance instance, EvaluationContext context) {
String activitySubtitle = evaluateExpression(context, deregisterActivitySubtitle);
return createMessage(instance, deRegisteredTitle, activitySubtitle, context);
}

protected Message getRegisteredMessage(Instance instance, StandardEvaluationContext context) {
protected Message getRegisteredMessage(Instance instance, EvaluationContext context) {
String activitySubtitle = evaluateExpression(context, registerActivitySubtitle);
return createMessage(instance, registeredTitle, activitySubtitle, context);
}

protected Message getStatusChangedMessage(Instance instance, StandardEvaluationContext context) {
protected Message getStatusChangedMessage(Instance instance, EvaluationContext context) {
String activitySubtitle = evaluateExpression(context, statusActivitySubtitle);
return createMessage(instance, statusChangedTitle, activitySubtitle, context);
}

protected Message createMessage(Instance instance, String registeredTitle, String activitySubtitle,
StandardEvaluationContext context) {
EvaluationContext context) {
List<Fact> facts = new ArrayList<>();
facts.add(new Fact(STATUS_KEY, instance.getStatusInfo().getStatus()));
facts.add(new Fact(SERVICE_URL_KEY, instance.getRegistration().getServiceUrl()));
Expand All @@ -203,18 +205,18 @@ protected Message createMessage(Instance instance, String registeredTitle, Strin
.themeColor(evaluateExpression(context, themeColor)).sections(singletonList(section)).build();
}

protected String evaluateExpression(StandardEvaluationContext context, Expression expression) {
protected String evaluateExpression(EvaluationContext context, Expression expression) {
return Objects.requireNonNull(expression.getValue(context, String.class));
}

protected StandardEvaluationContext createEvaluationContext(InstanceEvent event, Instance instance) {
protected EvaluationContext createEvaluationContext(InstanceEvent event, Instance instance) {
Map<String, Object> root = new HashMap<>();
root.put("event", event);
root.put("instance", instance);
root.put("lastStatus", getLastStatus(event.getInstance()));
StandardEvaluationContext context = new StandardEvaluationContext(root);
context.addPropertyAccessor(new MapAccessor());
return context;
return SimpleEvaluationContext
.forPropertyAccessors(DataBindingPropertyAccessor.forReadOnlyAccess(), new MapAccessor())
.withRootObject(root).build();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.DataBindingPropertyAccessor;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -118,7 +119,7 @@ protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
protected String buildUrl(InstanceEvent event, Instance instance) {
if ((event instanceof InstanceStatusChangedEvent)
&& (StatusInfo.STATUS_UP.equals(((InstanceStatusChangedEvent) event).getStatusInfo().getStatus()))) {
return String.format("%s/%s/close", url.toString(), generateAlias(instance));
return String.format("%s/%s/close", url, generateAlias(instance));
}
return url.toString();
}
Expand Down Expand Up @@ -172,8 +173,9 @@ protected String getMessage(InstanceEvent event, Instance instance) {
root.put("event", event);
root.put("instance", instance);
root.put("lastStatus", getLastStatus(event.getInstance()));
StandardEvaluationContext context = new StandardEvaluationContext(root);
context.addPropertyAccessor(new MapAccessor());
SimpleEvaluationContext context = SimpleEvaluationContext
.forPropertyAccessors(DataBindingPropertyAccessor.forReadOnlyAccess(), new MapAccessor())
.withRootObject(root).build();
return description.getValue(context, String.class);
}

Expand All @@ -183,15 +185,15 @@ protected String getDescription(InstanceEvent event, Instance instance) {
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
}

public void setApiKey(@Nullable String apiKey) {
this.apiKey = apiKey;
}

@Nullable
public String getApiKey() {
return apiKey;
}

public void setApiKey(@Nullable String apiKey) {
this.apiKey = apiKey;
}

public void setDescription(String description) {
this.description = parser.parseExpression(description, ParserContext.TEMPLATE_EXPRESSION);
}
Expand Down
Loading

0 comments on commit 224d148

Please sign in to comment.