MailNotifier.java 3.07 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 28 29
/*
 * 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;

import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;

30 31 32
public class MailNotifier extends AbstractNotifier {
	private final static String DEFAULT_SUBJECT = "#{application.name} (#{application.id}) is #{to.status}";
	private final static String DEFAULT_TEXT = "#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}";
33 34

	private final SpelExpressionParser parser = new SpelExpressionParser();
35
	private final MailSender sender;
36 37 38 39 40 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

	/**
	 * 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);
	}

68 69
	@Override
	protected void notify(ClientApplicationStatusChangedEvent event) {
70 71 72 73 74 75 76 77 78 79 80 81 82
		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) {
83
		this.to = Arrays.copyOf(to, to.length);
84 85 86
	}

	public void setCc(String[] cc) {
87
		this.cc = Arrays.copyOf(cc, cc.length);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	}

	public void setFrom(String from) {
		this.from = from;
	}

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

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

}