MailNotifier.java 3.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * 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.
 */
package de.codecentric.boot.admin.notify;

import java.util.Arrays;

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.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

28
import de.codecentric.boot.admin.event.ClientApplicationEvent;
29

30 31 32 33 34 35
/**
 * Notifier sending emails.
 *
 * @author Johannes Edmeier
 */
public class MailNotifier extends AbstractStatusChangeNotifier {
Johannes Edmeier committed
36 37
	private static final String DEFAULT_SUBJECT = "#{application.name} (#{application.id}) is #{to.status}";
	private static final String DEFAULT_TEXT = "#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}";
38 39

	private final SpelExpressionParser parser = new SpelExpressionParser();
40
	private final MailSender sender;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

	/**
	 * recipients of the mail
	 */
	private String to[] = { "root@localhost" };

	/**
	 * cc-recipients of the mail
	 */
	private String cc[];

	/**
	 * sender of the change
	 */
	private String from = null;

	/**
	 * Mail Text. SpEL template using event as root;
	 */
	private Expression text;

	/**
	 * Mail Subject. SpEL template using event as root;
	 */
	private Expression subject;

	public MailNotifier(MailSender sender) {
		this.sender = sender;
		this.subject = parser.parseExpression(DEFAULT_SUBJECT, ParserContext.TEMPLATE_EXPRESSION);
		this.text = parser.parseExpression(DEFAULT_TEXT, ParserContext.TEMPLATE_EXPRESSION);
	}

73
	@Override
74
	protected void doNotify(ClientApplicationEvent event) {
75 76 77 78 79 80 81 82 83 84 85 86 87
		EvaluationContext context = new StandardEvaluationContext(event);

		SimpleMailMessage message = new SimpleMailMessage();
		message.setTo(to);
		message.setFrom(from);
		message.setSubject(subject.getValue(context, String.class));
		message.setText(text.getValue(context, String.class));
		message.setCc(cc);

		sender.send(message);
	}

	public void setTo(String[] to) {
88
		this.to = Arrays.copyOf(to, to.length);
89 90
	}

91 92 93 94
	public String[] getTo() {
		return Arrays.copyOf(to, to.length);
	}

95
	public void setCc(String[] cc) {
96
		this.cc = Arrays.copyOf(cc, cc.length);
97 98
	}

99 100 101 102
	public String[] getCc() {
		return Arrays.copyOf(cc, cc.length);
	}

103 104 105 106
	public void setFrom(String from) {
		this.from = from;
	}

107 108 109 110
	public String getFrom() {
		return from;
	}

111 112 113 114
	public void setSubject(String subject) {
		this.subject = parser.parseExpression(subject, ParserContext.TEMPLATE_EXPRESSION);
	}

115 116 117 118
	public String getSubject() {
		return subject.getExpressionString();
	}

119 120 121 122
	public void setText(String text) {
		this.text = parser.parseExpression(text, ParserContext.TEMPLATE_EXPRESSION);
	}

123 124 125 126
	public String getText() {
		return text.getExpressionString();
	}

127
}