<commit_before>/*
 * Copyright (c) 2013 mgm technology partners GmbH
 *
 * 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 com.mgmtp.jfunk.core.reporting;

import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.newArrayListWithCapacity;
import static org.apache.commons.io.IOUtils.LINE_SEPARATOR;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.trimToNull;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;

import javax.activation.DataHandler;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.log4j.Logger;

import com.google.common.base.Strings;
import com.google.common.io.Resources;
import com.mgmtp.jfunk.core.exception.MailException;
import com.mgmtp.jfunk.core.mail.SmtpClient;
import com.mgmtp.jfunk.core.mail.TransportSession;

/**
 * <p>
 * Generates a report email containing in HTML format.
 * </p>
 * <p>
 * 
 */
@ThreadSafe
public final class EmailReporter implements Reporter {
	private static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
	private static final FastDateFormat TIME_FORMAT = FastDateFormat.getInstance("HH:mm:ss.SSS");
	private static final FastDateFormat TIMESTAMP_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

	private final Logger log = Logger.getLogger(getClass());

	private final List<ReportData> reportDataList = newArrayList();
	private final String recipients;
	private final SmtpClient smtpClient;
	private final Session session;

	@Inject
	public EmailReporter(final SmtpClient smtpClient, @Nullable @ReportMailRecipients final String recipients,
			@TransportSession final Session session) {
		this.smtpClient = smtpClient;
		this.recipients = recipients;
		this.session = session;
	}

	@Override
	public String getName() {
		return "E-mail Report";
	}

	@Override
	public synchronized void addResult(final ReportData reportData) {
		reportDataList.add(reportData);
	}

	@Override
	public synchronized void createReport() throws IOException {
		if (trimToNull(recipients) == null) {
			log.warn("No recipients for global e-mail report set. Cannot send report.");
			return;
		}

		if (!reportDataList.isEmpty()) {
			String content = createEmailContent();
			sendMessage(content);
			reportDataList.clear();
		}
	}

	private void sendMessage(final String content) {
		try {
			MimeMessage msg = new MimeMessage(session);
			msg.setSubject("jFunk E-mail Report");
			msg.addRecipients(Message.RecipientType.TO, recipients);

			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setContent(content, "text/html; charset=UTF-8");

			MimeMultipart multipart = new MimeMultipart("related");
			multipart.addBodyPart(messageBodyPart);

			messageBodyPart = new MimeBodyPart();
			messageBodyPart.setDataHandler(new DataHandler(getClass().getResource("check.gif")));
			messageBodyPart.setHeader("Content-ID", "<check>");
			multipart.addBodyPart(messageBodyPart);

			messageBodyPart = new MimeBodyPart();
			messageBodyPart.setDataHandler(new DataHandler(getClass().getResource("error.gif")));
			messageBodyPart.setHeader("Content-ID", "<error>");
			multipart.addBodyPart(messageBodyPart);

			msg.setContent(multipart);

			smtpClient.send(msg);

			int anzahlRecipients = msg.getAllRecipients().length;
			log.info("Report e-mail was sent to " + anzahlRecipients + " recipient(s): " + recipients);
		} catch (MessagingException e) {
			log.error("Error while creating report e-mail", e);
		} catch (MailException e) {
			log.error("Error while sending report e-mail", e);
		}
	}

	private String createEmailContent() throws IOException {
		int size = reportDataList.size();
		List<String> rowData = newArrayListWithCapacity(size);

		String reportRowTemplate = Resources.toString(getClass().getResource("email-report-row-template.html"),
				Charset.forName("UTF-8"));
		for (int i = 0; i < size; ++i) {
			ReportData data = reportDataList.get(i);

			String rowContent = replacePlaceholderToken(reportRowTemplate, "counter", String.valueOf(i));
			rowContent = replacePlaceholderToken(rowContent, "date", DATE_FORMAT.format(data.getStartMillis()));
			rowContent = replacePlaceholderToken(rowContent, "start", TIME_FORMAT.format(data.getStartMillis()));
			rowContent = replacePlaceholderToken(rowContent, "finish", TIME_FORMAT.format(data.getStopMillis()));
			rowContent = replacePlaceholderToken(rowContent, "duration",
					DurationFormatUtils.formatDurationHMS(data.getStopMillis() - data.getStartMillis()));
			rowContent = replacePlaceholderToken(rowContent, "testobject", data.getTestObject().getName());

			Throwable th = data.getThrowable();
			if (th == null) {
				rowContent = replacePlaceholderToken(rowContent, "image", "check");
				rowContent = replacePlaceholderToken(rowContent, "errormsg", "");
				rowContent = replacePlaceholderToken(rowContent, "style", "success");
			} else {
				rowContent = replacePlaceholderToken(rowContent, "image", "error");<commit_msg>Fix scoping issues.<commit_after>/*
 * Copyright (c) 2013 mgm technology partners GmbH
 *
 * 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 com.mgmtp.jfunk.core.reporting;

import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.newArrayListWithCapacity;
import static org.apache.commons.io.IOUtils.LINE_SEPARATOR;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.trimToNull;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;

import javax.activation.DataHandler;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.log4j.Logger;

import com.google.common.base.Strings;
import com.google.common.io.Resources;
import com.mgmtp.jfunk.core.exception.MailException;
import com.mgmtp.jfunk.core.mail.SmtpClient;
import com.mgmtp.jfunk.core.mail.TransportSession;

/**
 * <p>
 * Generates a report email containing in HTML format.
 * </p>
 * <p>
 * 
 */
@ThreadSafe
public final class EmailReporter implements Reporter {
	private static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
	private static final FastDateFormat TIME_FORMAT = FastDateFormat.getInstance("HH:mm:ss.SSS");
	private static final FastDateFormat TIMESTAMP_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

	private final Logger log = Logger.getLogger(getClass());

	private final List<ReportData> reportDataList = newArrayList();
	private final Provider<String> recipientsProvider;
	private final Provider<SmtpClient> smtpClientProvider;
	private final Provider<Session> sessionProvider;

	@Inject
	public EmailReporter(final Provider<SmtpClient> smtpClientProvider,
			@Nullable @ReportMailRecipients final Provider<String> recipientsProvider,
			@TransportSession final Provider<Session> sessionProvider) {
		this.smtpClientProvider = smtpClientProvider;
		this.recipientsProvider = recipientsProvider;
		this.sessionProvider = sessionProvider;
	}

	@Override
	public String getName() {
		return "E-mail Report";
	}

	@Override
	public synchronized void addResult(final ReportData reportData) {
		reportDataList.add(reportData);
	}

	@Override
	public synchronized void createReport() throws IOException {
		if (trimToNull(recipientsProvider.get()) == null) {
			log.warn("No recipients for global e-mail report set. Cannot send report.");
			return;
		}

		if (!reportDataList.isEmpty()) {
			String content = createEmailContent();
			sendMessage(content);
			reportDataList.clear();
		}
	}

	private void sendMessage(final String content) {
		try {
			MimeMessage msg = new MimeMessage(sessionProvider.get());
			msg.setSubject("jFunk E-mail Report");
			msg.addRecipients(Message.RecipientType.TO, recipientsProvider.get());

			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setContent(content, "text/html; charset=UTF-8");

			MimeMultipart multipart = new MimeMultipart("related");
			multipart.addBodyPart(messageBodyPart);

			messageBodyPart = new MimeBodyPart();
			messageBodyPart.setDataHandler(new DataHandler(getClass().getResource("check.gif")));
			messageBodyPart.setHeader("Content-ID", "<check>");
			multipart.addBodyPart(messageBodyPart);

			messageBodyPart = new MimeBodyPart();
			messageBodyPart.setDataHandler(new DataHandler(getClass().getResource("error.gif")));
			messageBodyPart.setHeader("Content-ID", "<error>");
			multipart.addBodyPart(messageBodyPart);

			msg.setContent(multipart);

			smtpClientProvider.get().send(msg);

			int anzahlRecipients = msg.getAllRecipients().length;
			log.info("Report e-mail was sent to " + anzahlRecipients + " recipient(s): " + recipientsProvider.get());
		} catch (MessagingException e) {
			log.error("Error while creating report e-mail", e);
		} catch (MailException e) {
			log.error("Error while sending report e-mail", e);
		}
	}

	private String createEmailContent() throws IOException {
		int size = reportDataList.size();
		List<String> rowData = newArrayListWithCapacity(size);

		String reportRowTemplate = Resources.toString(getClass().getResource("email-report-row-template.html"),
				Charset.forName("UTF-8"));
		for (int i = 0; i < size; ++i) {
			ReportData data = reportDataList.get(i);

			String rowContent = replacePlaceholderToken(reportRowTemplate, "counter", String.valueOf(i));
			rowContent = replacePlaceholderToken(rowContent, "date", DATE_FORMAT.format(data.getStartMillis()));
			rowContent = replacePlaceholderToken(rowContent, "start", TIME_FORMAT.format(data.getStartMillis()));
			rowContent = replacePlaceholderToken(rowContent, "finish", TIME_FORMAT.format(data.getStopMillis()));
			rowContent = replacePlaceholderToken(rowContent, "duration",
					DurationFormatUtils.formatDurationHMS(data.getStopMillis() - data.getStartMillis()));
			rowContent = replacePlaceholderToken(rowContent, "testobject", data.getTestObject().getName());

			Throwable th = data.getThrowable();
			if (th == null) {
				rowContent = replacePlaceholderToken(rowContent, "image", "check");
				rowContent = replacePlaceholderToken(rowContent, "errormsg", "");
				rowContent = replacePlaceholderToken(rowContent, "style", "success");
			} else {
				rowContent = replacePlaceholderToken(rowContent, "image", "error");
{'fraction_non_alphanumeric': 0.11285542651181418, 'fraction_numerical': 0.002723267921505807, 'mean_word_length': 5.476141078838174, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7489698', 'n_tokens_mistral': 3915, 'n_tokens_neox': 3602, 'n_words': 851}