ImageHandlingBuilder.java

1
package fr.sii.ogham.email.builder;
2
3
import fr.sii.ogham.core.builder.Builder;
4
import fr.sii.ogham.core.builder.context.BuildContext;
5
import fr.sii.ogham.core.builder.env.EnvironmentBuilder;
6
import fr.sii.ogham.core.fluent.AbstractParent;
7
import fr.sii.ogham.core.translator.content.ContentTranslator;
8
9
/**
10
 * Image handling consists of defining how images are inlined in the email:
11
 * <ul>
12
 * <li>Either inlining directly in the HTML content by encoding image into
13
 * base64 string</li>
14
 * <li>Or attaching the image to the email and referencing it using a
15
 * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID
16
 * (CID)</a></li>
17
 * <li>Or no inlining</li>
18
 * </ul>
19
 * 
20
 * @author Aurélien Baudet
21
 *
22
 */
23
public class ImageHandlingBuilder extends AbstractParent<EmailBuilder> implements Builder<ContentTranslator> {
24
	private final BuildContext buildContext;
25
	private ImageInliningBuilder imageInliningBuilder;
26
27
	/**
28
	 * Initializes the builder with a parent builder. The parent builder is used
29
	 * when calling {@link #and()} method. The {@link EnvironmentBuilder} is
30
	 * used to evaluate properties when {@link #build()} method is called.
31
	 * 
32
	 * @param parent
33
	 *            the parent builder
34
	 * @param buildContext
35
	 *            for registering instances and property evaluation
36
	 */
37
	public ImageHandlingBuilder(EmailBuilder parent, BuildContext buildContext) {
38
		super(parent);
39
		this.buildContext = buildContext;
40
	}
41
42
	/**
43
	 * Configures how images are handled. Image handling consists of defining
44
	 * how images are inlined in the email:
45
	 * <ul>
46
	 * <li>Either inlining directly in the HTML content by encoding image into
47
	 * base64 string</li>
48
	 * <li>Or attaching the image to the email and referencing it using a
49
	 * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID
50
	 * (CID)</a></li>
51
	 * <li>Or no inlining</li>
52
	 * </ul>
53
	 * 
54
	 * 
55
	 * This builder is used to enable the inlining modes (and to configure
56
	 * them). Several modes can be enabled.
57
	 * 
58
	 * <p>
59
	 * If {@link ImageInliningBuilder#attach()} is called, it enables image
60
	 * attachment.
61
	 * 
62
	 * Image defined in a html must be referenced by a
63
	 * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID
64
	 * (or CID)</a> if the image is attached to the email.
65
	 * 
66
	 * For example, if your template contains the following HTML code:
67
	 * 
68
	 * <pre>
69
	 * {@code
70
	 *    <img src="classpath:/foo.png" data-inline-image="attach" />
71
	 * }
72
	 * </pre>
73
	 * 
74
	 * Then the image will be loaded from the classpath and attached to the
75
	 * email. The src attribute will be replaced by the Content-ID.
76
	 * 
77
	 * It also works for images included from CSS:
78
	 * 
79
	 * <pre>
80
	 * <code>
81
	 *  &lt;style&gt;
82
	 *     .some-class {
83
	 *       background: url('classpath:/foo.png');
84
	 *       --inline-image: attach;
85
	 *     }
86
	 *  &lt;/style&gt;
87
	 * </code>
88
	 * </pre>
89
	 * 
90
	 * Or directly on {@code style} attribute:
91
	 * 
92
	 * <pre>
93
	 * {@code
94
	 * 	<div style="background: url('classpath:/foo.png'); --inline-image: attach;"></div>
95
	 * }
96
	 * </pre>
97
	 * 
98
	 * Then the image will be loaded from the classpath and attached to the
99
	 * email. The url will be replaced by the Content-ID.
100
	 * 
101
	 * <p>
102
	 * If {@link ImageInliningBuilder#base64()} is called, it enables inlining
103
	 * by converting image content into base64 string and using the base64
104
	 * string as image source.
105
	 * 
106
	 * For example, if your template contains the following HTML code:
107
	 * 
108
	 * <pre>
109
	 * {@code
110
	 *    <img src="classpath:/foo.png" data-inline-image="base64" />
111
	 * }
112
	 * </pre>
113
	 * 
114
	 * Then the image will be loaded from the classpath and encoded into a
115
	 * base64 string. This base64 string is used in the src attribute of the
116
	 * {@code <img>}.
117
	 * 
118
	 * It also works for images included from CSS:
119
	 * 
120
	 * <pre>
121
	 * <code>
122
	 *  &lt;style&gt;
123
	 *     .some-class {
124
	 *       background: url('classpath:/foo.png');
125
	 *       --inline-image: base64;
126
	 *     }
127
	 *  &lt;/style&gt;
128
	 * </code>
129
	 * </pre>
130
	 * 
131
	 * Or directly on {@code style} attribute:
132
	 * 
133
	 * <pre>
134
	 * {@code
135
	 * 	<div style="background: url('classpath:/foo.png'); --inline-image: base64;"></div>
136
	 * }
137
	 * </pre>
138
	 * 
139
	 * Then the image will be loaded from the classpath and encoded into a
140
	 * base64 string. The url is updated with the base64 string.
141
	 * 
142
	 * <p>
143
	 * If you don't want to inline a particular image, you can set the
144
	 * "data-inline-image" attribute to "skip":
145
	 * 
146
	 * <pre>
147
	 * {@code
148
	 *    <img src="classpath:/foo.png" data-inline-image="skip" />
149
	 * }
150
	 * </pre>
151
	 * 
152
	 * Then the image won't be inlined at all.
153
	 * 
154
	 * <p>
155
	 * If no inline mode is explicitly defined on the {@code <img>}:
156
	 * 
157
	 * <pre>
158
	 * {@code
159
	 *    <img src="classpath:/foo.png" />
160
	 * }
161
	 * </pre>
162
	 * 
163
	 * The behavior depends on what you have configured:
164
	 * <ul>
165
	 * <li>If {@link ImageInliningBuilder#attach()} is enabled (has been
166
	 * called), then image will be loaded from the classpath and attached to the
167
	 * email. The src attribute will be replaced by the Content-ID.</li>
168
	 * <li>If {@link ImageInliningBuilder#attach()} is not enabled (never
169
	 * called) and {@link ImageInliningBuilder#base64()} is enabled (has been
170
	 * called), then the image will be loaded from the classpath and encoded
171
	 * into a base64 string. This base64 string is used in the src attribute of
172
	 * the image.</li>
173
	 * <li>If neither {@link ImageInliningBuilder#attach()} nor
174
	 * {@link ImageInliningBuilder#base64()} are enabled (never called), then
175
	 * images won't be inlined at all</li>
176
	 * </ul>
177
	 * 
178
	 * @return the builder to configure image inlining
179
	 */
180
	public ImageInliningBuilder inline() {
181 8 1. inline : negated conditional → NO_COVERAGE
2. inline : negated conditional → KILLED
3. inline : negated conditional → KILLED
4. inline : negated conditional → KILLED
5. inline : negated conditional → KILLED
6. inline : negated conditional → KILLED
7. inline : negated conditional → KILLED
8. inline : negated conditional → KILLED
		if (imageInliningBuilder == null) {
182
			imageInliningBuilder = new ImageInliningBuilder(this, buildContext);
183
		}
184 8 1. inline : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → NO_COVERAGE
2. inline : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED
3. inline : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED
4. inline : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED
5. inline : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED
6. inline : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED
7. inline : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED
8. inline : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED
		return imageInliningBuilder;
185
	}
186
187
	@Override
188
	public ContentTranslator build() {
189 3 1. build : negated conditional → NO_COVERAGE
2. build : negated conditional → SURVIVED
3. build : negated conditional → KILLED
		if (imageInliningBuilder == null) {
190
			return null;
191
		}
192 3 1. build : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::build → NO_COVERAGE
2. build : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::build → SURVIVED
3. build : replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::build → KILLED
		return imageInliningBuilder.build();
193
	}
194
}

Mutations

181

1.1
Location : inline
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : inline
Killed by : oghamcore.it.core.service.CleanupTest.manualCleanupShouldAutomaticallyCleanTheSenders(oghamcore.it.core.service.CleanupTest)
negated conditional → KILLED

3.3
Location : inline
Killed by : oghamjavamail.it.UnreadableAttachmentTest.attachmentUnreadable(oghamjavamail.it.UnreadableAttachmentTest)
negated conditional → KILLED

4.4
Location : inline
Killed by : oghamsmsglobal.it.SmsglobalServiceProviderConfigurerSpec
negated conditional → KILLED

5.5
Location : inline
Killed by : oghamall.it.html.translator.JsoupInlineCssTranslatorTest.notHtml(oghamall.it.html.translator.JsoupInlineCssTranslatorTest)
negated conditional → KILLED

6.6
Location : inline
Killed by : oghamspringbootv1autoconfigure.it.OghamSpringBoot1JavaMailAutoConfigurationTests.oghamAloneShouldUseOghamProperties(oghamspringbootv1autoconfigure.it.OghamSpringBoot1JavaMailAutoConfigurationTests)
negated conditional → KILLED

7.7
Location : inline
Killed by : oghamcloudhopper.it.AutoRetryExtensionTest.smsNotRetriedDueToCloudhopperError(oghamcloudhopper.it.AutoRetryExtensionTest)
negated conditional → KILLED

8.8
Location : inline
Killed by : oghamspringbootv2autoconfigure.it.OghamSpringBoot2JavaMailAutoConfigurationTests.oghamPropertiesWithSpringPropsShouldUseOghamPropertiesPrecedence(oghamspringbootv2autoconfigure.it.OghamSpringBoot2JavaMailAutoConfigurationTests)
negated conditional → KILLED

184

1.1
Location : inline
Killed by : none
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → NO_COVERAGE

2.2
Location : inline
Killed by : oghamcore.it.core.service.CleanupTest.manualCleanupShouldAutomaticallyCleanTheSenders(oghamcore.it.core.service.CleanupTest)
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED

3.3
Location : inline
Killed by : oghamspringbootv2autoconfigure.it.OghamSpringBoot2JavaMailAutoConfigurationTests.oghamPropertiesWithSpringPropsShouldUseOghamPropertiesPrecedence(oghamspringbootv2autoconfigure.it.OghamSpringBoot2JavaMailAutoConfigurationTests)
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED

4.4
Location : inline
Killed by : oghamcloudhopper.it.AutoRetryExtensionTest.smsNotRetriedDueToCloudhopperError(oghamcloudhopper.it.AutoRetryExtensionTest)
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED

5.5
Location : inline
Killed by : oghamspringbootv1autoconfigure.it.OghamSpringBoot1JavaMailAutoConfigurationTests.oghamAloneShouldUseOghamProperties(oghamspringbootv1autoconfigure.it.OghamSpringBoot1JavaMailAutoConfigurationTests)
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED

6.6
Location : inline
Killed by : oghamsmsglobal.it.SmsglobalServiceProviderConfigurerSpec
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED

7.7
Location : inline
Killed by : oghamall.it.html.translator.JsoupInlineCssTranslatorTest.notHtml(oghamall.it.html.translator.JsoupInlineCssTranslatorTest)
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED

8.8
Location : inline
Killed by : oghamjavamail.it.UnreadableAttachmentTest.attachmentUnreadable(oghamjavamail.it.UnreadableAttachmentTest)
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::inline → KILLED

189

1.1
Location : build
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : build
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndImageInliningEnabledButUnconfiguredResourceResolutionCantInlineImages(oghamall.it.configuration.EmptyBuilderTest)
negated conditional → KILLED

3.3
Location : build
Killed by : none
negated conditional → SURVIVED

192

1.1
Location : build
Killed by : none
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::build → NO_COVERAGE

2.2
Location : build
Killed by : oghamall.it.configuration.EmptyBuilderTest.emailSenderManuallyRegisteredAndImageInliningEnabledButUnconfiguredResourceResolutionCantInlineImages(oghamall.it.configuration.EmptyBuilderTest)
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::build → KILLED

3.3
Location : build
Killed by : none
replaced return value with null for fr/sii/ogham/email/builder/ImageHandlingBuilder::build → SURVIVED

Active mutators

Tests examined


Report generated by PIT OGHAM