HipchatNotifier.java 4.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2013-2014 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.
 */
16 17
package de.codecentric.boot.admin.notify;

18 19 20 21
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

22 23 24
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
25 26 27
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
28 29
import org.springframework.web.client.RestTemplate;

30
import de.codecentric.boot.admin.event.ClientApplicationEvent;
31
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
32 33

/**
34 35 36
 * Notifier submitting events to HipChat.
 *
 * @author Jamie Brown
37
 */
38
public class HipchatNotifier extends AbstractStatusChangeNotifier {
Johannes Edmeier committed
39
	private static final String DEFAULT_DESCRIPTION = "<strong>#{application.name}</strong>/#{application.id} is <strong>#{to.status}</strong>";
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

	private final SpelExpressionParser parser = new SpelExpressionParser();
	private RestTemplate restTemplate = new RestTemplate();

	/**
	 * Base URL for HipChat API (i.e. https://ACCOUNT_NAME.hipchat.com/v2
	 */
	private URI url;

	/**
	 * API token that has access to notify in the room
	 */
	private String authToken;

	/**
	 * Id of the room to notify
	 */
	private String roomId;

	/**
	 * TRUE will cause OS notification, FALSE will only notify to room
	 */
62
	private boolean notify = false;
63 64 65 66 67 68 69 70 71 72 73 74

	/**
	 * Trigger description. SpEL template using event as root;
	 */
	private Expression description;

	public HipchatNotifier() {
		this.description = parser.parseExpression(DEFAULT_DESCRIPTION,
				ParserContext.TEMPLATE_EXPRESSION);
	}

	@Override
75
	protected void doNotify(ClientApplicationEvent event) {
76 77 78 79 80 81 82 83
		restTemplate.postForEntity(buildUrl(), createHipChatNotification(event), Void.class);
	}

	protected String buildUrl() {
		return String.format("%s/room/%s/notification?auth_token=%s", url.toString(), roomId,
				authToken);
	}

84 85 86 87 88 89 90 91 92 93 94
	protected HttpEntity<Map<String, Object>> createHipChatNotification(
			ClientApplicationEvent event) {
		Map<String, Object> body = new HashMap<>();
		body.put("color", getColor(event));
		body.put("message", getMessage(event));
		body.put("notify", getNotify());
		body.put("message_format", "html");

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		return new HttpEntity<>(body, headers);
95 96
	}

97 98 99 100 101 102 103 104 105 106
	protected boolean getNotify() {
		return notify;
	}

	protected String getMessage(ClientApplicationEvent event) {
		return description.getValue(event, String.class);
	}

	protected String getColor(ClientApplicationEvent event) {
		if (event instanceof ClientApplicationStatusChangedEvent) {
107 108
			return "UP".equals(((ClientApplicationStatusChangedEvent) event).getTo().getStatus())
					? "green" : "red";
109 110 111 112 113
		} else {
			return "gray";
		}
	}

114 115 116 117
	public void setUrl(URI url) {
		this.url = url;
	}

118 119 120 121
	public URI getUrl() {
		return url;
	}

122 123 124 125
	public void setAuthToken(String authToken) {
		this.authToken = authToken;
	}

126 127 128 129
	public String getAuthToken() {
		return authToken;
	}

130 131 132 133
	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}

134 135 136 137
	public String getRoomId() {
		return roomId;
	}

138
	public void setNotify(boolean notify) {
139 140 141
		this.notify = notify;
	}

142 143 144 145
	public boolean isNotify() {
		return notify;
	}

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

150 151 152 153
	public String getDescription() {
		return description.getExpressionString();
	}

154 155 156
	public void setRestTemplate(RestTemplate restTemplate) {
		this.restTemplate = restTemplate;
	}
157
}