
de.codecentric.boot.admin.server.notify.HipchatNotifier Maven / Gradle / Ivy
/*
* Copyright 2014-2018 the original author or 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 de.codecentric.boot.admin.server.notify;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import org.springframework.context.expression.MapAccessor;
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.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;
import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.domain.values.StatusInfo;
/**
* Notifier submitting events to HipChat.
*
* @author Jamie Brown
*/
public class HipchatNotifier extends AbstractStatusChangeNotifier {
private static final String DEFAULT_DESCRIPTION = "#{instance.registration.name}/#{instance.id} is #{event.statusInfo.status}";
private final SpelExpressionParser parser = new SpelExpressionParser();
private RestTemplate restTemplate;
/**
* Base URL for HipChat API (i.e. https://ACCOUNT_NAME.hipchat.com/v2
*/
@Nullable
private URI url;
/**
* API token that has access to notify in the room
*/
@Nullable
private String authToken;
/**
* Id of the room to notify
*/
@Nullable
private String roomId;
/**
* TRUE will cause OS notification, FALSE will only notify to room
*/
private boolean notify = false;
/**
* Trigger description. SpEL template using event as root;
*/
private Expression description;
public HipchatNotifier(InstanceRepository repository, RestTemplate restTemplate) {
super(repository);
this.restTemplate = restTemplate;
this.description = parser.parseExpression(DEFAULT_DESCRIPTION, ParserContext.TEMPLATE_EXPRESSION);
}
@Override
protected Mono doNotify(InstanceEvent event, Instance instance) {
return Mono.fromRunnable(
() -> restTemplate.postForEntity(buildUrl(), createHipChatNotification(event, instance), Void.class));
}
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);
}
protected HttpEntity
© 2015 - 2025 Weber Informatics LLC | Privacy Policy