AttachmentResourceTranslatorSender.java

1
package fr.sii.ogham.email.sender;
2
3
import static fr.sii.ogham.core.util.LogUtils.logString;
4
5
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
7
8
import fr.sii.ogham.core.exception.MessageException;
9
import fr.sii.ogham.core.exception.MessageNotSentException;
10
import fr.sii.ogham.core.message.Message;
11
import fr.sii.ogham.core.resource.NamedResource;
12
import fr.sii.ogham.core.resource.resolver.ResourceResolver;
13
import fr.sii.ogham.core.sender.ConditionalSender;
14
import fr.sii.ogham.core.sender.MessageSender;
15
import fr.sii.ogham.core.translator.resource.AttachmentResourceTranslator;
16
import fr.sii.ogham.email.attachment.Attachment;
17
import fr.sii.ogham.email.exception.attachment.translator.ResourceTranslatorException;
18
import fr.sii.ogham.email.message.Email;
19
20
/**
21
 * Decorator sender that transforms the attachments of the message before really
22
 * sending it. This sender relies on {@link AttachmentResourceTranslator} to
23
 * transform attachments. Once the attachments are transformed, this sender
24
 * delegates to a real implementation the sending of the message.
25
 * 
26
 * @author Aurélien Baudet
27
 * @see ResourceResolver
28
 * @see NamedResource
29
 */
30
public class AttachmentResourceTranslatorSender implements ConditionalSender {
31
	private static final Logger LOG = LoggerFactory.getLogger(AttachmentResourceTranslatorSender.class);
32
33
	/**
34
	 * The translator used to transform attachments
35
	 */
36
	private AttachmentResourceTranslator translator;
37
38
	/**
39
	 * The decorated sender that will really send the message
40
	 */
41
	private MessageSender delegate;
42
43
	/**
44
	 * Initialize the sender with the provided translator and decorated sender.
45
	 * The translator implementation will transform attachments of the message.
46
	 * The decorated sender will really send the message.
47
	 * 
48
	 * @param translator
49
	 *            the translator implementation that will transform the
50
	 *            attachments of the message
51
	 * @param delegate
52
	 *            The decorated sender will really send the message
53
	 */
54
	public AttachmentResourceTranslatorSender(AttachmentResourceTranslator translator, MessageSender delegate) {
55
		super();
56
		this.translator = translator;
57
		this.delegate = delegate;
58
	}
59
60
	@Override
61
	public boolean supports(Message message) {
62 1 1. supports : negated conditional → RUN_ERROR
		if (delegate instanceof ConditionalSender) {
63 2 1. supports : replaced boolean return with false for fr/sii/ogham/email/sender/AttachmentResourceTranslatorSender::supports → RUN_ERROR
2. supports : replaced boolean return with true for fr/sii/ogham/email/sender/AttachmentResourceTranslatorSender::supports → RUN_ERROR
			return ((ConditionalSender) delegate).supports(message);
64
		}
65 1 1. supports : replaced boolean return with false for fr/sii/ogham/email/sender/AttachmentResourceTranslatorSender::supports → NO_COVERAGE
		return true;
66
	}
67
68
	@Override
69
	public void send(Message message) throws MessageException {
70
		try {
71
			for (Attachment attachment : ((Email) message).getAttachments()) {
72
				LOG.debug("Translate attachment {} for the message {} using {}", attachment, logString(message), translator);
73 1 1. send : removed call to fr/sii/ogham/email/attachment/Attachment::setResource → NO_COVERAGE
				attachment.setResource((NamedResource) translator.translate(attachment.getResource()));
74
			}
75
			LOG.debug("Sending message {} using {}", logString(message), delegate);
76 1 1. send : removed call to fr/sii/ogham/core/sender/MessageSender::send → RUN_ERROR
			delegate.send(message);
77
		} catch (ResourceTranslatorException e) {
78
			throw new MessageNotSentException("Failed to send message due to attachment translation", message, e);
79
		}
80
	}
81
82
	@Override
83
	public String toString() {
84
		StringBuilder builder = new StringBuilder();
85
		builder.append("AttachmentResourceResolverSender [translator=").append(translator).append(", delegate=").append(delegate).append("]");
86 1 1. toString : replaced return value with "" for fr/sii/ogham/email/sender/AttachmentResourceTranslatorSender::toString → RUN_ERROR
		return builder.toString();
87
	}
88
}

Mutations

62

1.1
Location : supports
Killed by :
negated conditional → RUN_ERROR

63

1.1
Location : supports
Killed by :
replaced boolean return with false for fr/sii/ogham/email/sender/AttachmentResourceTranslatorSender::supports → RUN_ERROR

2.2
Location : supports
Killed by :
replaced boolean return with true for fr/sii/ogham/email/sender/AttachmentResourceTranslatorSender::supports → RUN_ERROR

65

1.1
Location : supports
Killed by :
replaced boolean return with false for fr/sii/ogham/email/sender/AttachmentResourceTranslatorSender::supports → NO_COVERAGE

73

1.1
Location : send
Killed by :
removed call to fr/sii/ogham/email/attachment/Attachment::setResource → NO_COVERAGE

76

1.1
Location : send
Killed by :
removed call to fr/sii/ogham/core/sender/MessageSender::send → RUN_ERROR

86

1.1
Location : toString
Killed by :
replaced return value with "" for fr/sii/ogham/email/sender/AttachmentResourceTranslatorSender::toString → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1