SendGridV2Builder.java

1
package fr.sii.ogham.email.sendgrid.v2.builder.sendgrid;
2
3
import java.net.URL;
4
5
import org.slf4j.Logger;
6
import org.slf4j.LoggerFactory;
7
8
import com.sendgrid.SendGrid;
9
10
import fr.sii.ogham.core.builder.MessagingBuilder;
11
import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilder;
12
import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilderHelper;
13
import fr.sii.ogham.core.builder.context.BuildContext;
14
import fr.sii.ogham.core.builder.context.DefaultBuildContext;
15
import fr.sii.ogham.core.message.content.MayHaveStringContent;
16
import fr.sii.ogham.core.message.content.MultiContent;
17
import fr.sii.ogham.core.mimetype.MimeTypeProvider;
18
import fr.sii.ogham.email.builder.EmailBuilder;
19
import fr.sii.ogham.email.message.Email;
20
import fr.sii.ogham.email.message.content.ContentWithAttachments;
21
import fr.sii.ogham.email.sendgrid.builder.AbstractSendGridBuilder;
22
import fr.sii.ogham.email.sendgrid.v2.sender.impl.SendGridV2Sender;
23
import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.client.DelegateSendGridClient;
24
import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.client.SendGridClient;
25
import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.client.SendGridInterceptor;
26
import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.handler.ContentWithAttachmentsHandler;
27
import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.handler.MultiContentHandler;
28
import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.handler.PriorizedContentHandler;
29
import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.handler.StringContentHandler;
30
31
/**
32
 * Configures how SendGrid implementation will send {@link Email}s.
33
 * 
34
 * This implementation uses SendGrid HTTP API.
35
 * 
36
 * <p>
37
 * To send {@link Email} using SendGrid, you need to register this builder into
38
 * a {@link MessagingBuilder} like this:
39
 * 
40
 * <pre>
41
 * <code>
42
 * MessagingBuilder msgBuilder = ...
43
 * msgBuilder.email()
44
 *    .sender(SendGridV2Builder.class)    // registers the builder and accesses to that builder for configuring it
45
 * </code>
46
 * </pre>
47
 * 
48
 * Once the builder is registered, sending email through SendGrid requires
49
 * either an API key or a username/password pair. You can define it using:
50
 * 
51
 * <pre>
52
 * <code>
53
 * msgBuilder.email()
54
 *    .sender(SendGridV2Builder.class)    // registers the builder and accesses to that builder for configuring it
55
 *       .apiKey("foo")
56
 * </code>
57
 * </pre>
58
 * 
59
 * Or you can also use property keys (using interpolation):
60
 * 
61
 * <pre>
62
 * <code>
63
 * msgBuilder
64
 * .environment()
65
 *    .properties()
66
 *       .set("custom.property.for.api-key", "foo")
67
 *       .and()
68
 *    .and()
69
 * .email()
70
 *    .sender(SendGridV2Builder.class)    // registers the builder and accesses to that builder for configuring it
71
 *       .apiKey("${custom.property.for.api-key}")
72
 * </code>
73
 * </pre>
74
 * 
75
 * <p>
76
 * Finally, Ogham will transform general {@link Email} object into
77
 * {@link SendGrid}.Email object. This transformation will fit almost all use
78
 * cases but you may need to customize a part of the SendGrid message. Instead
79
 * of doing again the same work Ogham does, this builder allows you to intercept
80
 * the message to modify it just before sending it:
81
 * 
82
 * <pre>
83
 * <code>
84
 * .sender(SendGridV2Builder.class)
85
 *    .intercept(new MyCustomInterceptor())
86
 * </code>
87
 * </pre>
88
 * 
89
 * See {@link SendGridInterceptor} for more information.
90
 * 
91
 * @author Aurélien Baudet
92
 *
93
 */
94
public class SendGridV2Builder extends AbstractSendGridBuilder<SendGridV2Builder, EmailBuilder> {
95
	private static final Logger LOG = LoggerFactory.getLogger(SendGridV2Builder.class);
96
97
	private final ConfigurationValueBuilderHelper<SendGridV2Builder, String> usernameValueBuilder;
98
	private final ConfigurationValueBuilderHelper<SendGridV2Builder, String> passwordValueBuilder;
99
	private SendGridClient client;
100
	private SendGridInterceptor interceptor;
101
102
	/**
103
	 * Default constructor when using SendGrid sender without all Ogham work.
104
	 * 
105
	 * <strong>WARNING: use is only if you know what you are doing !</strong>
106
	 */
107
	public SendGridV2Builder() {
108
		this(null, new DefaultBuildContext());
109
	}
110
111
	/**
112
	 * Constructor that is called when using Ogham builder:
113
	 * 
114
	 * <pre>
115
	 * MessagingBuilder msgBuilder = ...
116
	 * msgBuilder
117
	 * .email()
118
	 *    .sender(SendGridV2Builder.class)
119
	 * </pre>
120
	 * 
121
	 * @param parent
122
	 *            the parent builder instance for fluent chaining
123
	 * @param buildContext
124
	 *            for registering instances and property evaluation
125
	 */
126
	public SendGridV2Builder(EmailBuilder parent, BuildContext buildContext) {
127
		super(SendGridV2Builder.class, parent, buildContext);
128
		usernameValueBuilder = buildContext.newConfigurationValueBuilder(this, String.class);
129
		passwordValueBuilder = buildContext.newConfigurationValueBuilder(this, String.class);
130
	}
131
132
	@Override
133
	public SendGridV2Builder username(String username) {
134 1 1. username : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → RUN_ERROR
		usernameValueBuilder.setValue(username);
135 1 1. username : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → RUN_ERROR
		return this;
136
	}
137
138
	@Override
139
	public ConfigurationValueBuilder<SendGridV2Builder, String> username() {
140 1 1. username : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → RUN_ERROR
		return usernameValueBuilder;
141
	}
142
143
	@Override
144
	public SendGridV2Builder password(String password) {
145 1 1. password : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → RUN_ERROR
		passwordValueBuilder.setValue(password);
146 1 1. password : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::password → RUN_ERROR
		return this;
147
	}
148
149
	@Override
150
	public ConfigurationValueBuilder<SendGridV2Builder, String> password() {
151 1 1. password : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::password → RUN_ERROR
		return passwordValueBuilder;
152
	}
153
154
	/**
155
	 * By default, calling SendGrid HTTP API is done through the default
156
	 * {@link SendGrid} implementation. If you want to use another client
157
	 * implementation (creating your custom HTTP API caller for example), you
158
	 * can implement the {@link SendGridClient} interface and provide it:
159
	 * 
160
	 * <pre>
161
	 * .client(new MyCustomHttpApiCaller())
162
	 * </pre>
163
	 * 
164
	 * NOTE: if you provide your custom implementation, any defined properties
165
	 * and values using {@link #apiKey(String)}, {@link #username(String)} or
166
	 * {@link #password(String)} won't be used at all. You then have to handle
167
	 * it by yourself.
168
	 * 
169
	 * @param client
170
	 *            the custom client used to call SendGrid HTTP API
171
	 * @return this instance for fluent chaining
172
	 */
173
	public SendGridV2Builder client(SendGridClient client) {
174
		this.client = client;
175 1 1. client : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::client → RUN_ERROR
		return this;
176
	}
177
178
	/**
179
	 * Ogham will transform general {@link Email} object into
180
	 * {@link SendGrid}.Email objects. This transformation will fit almost all
181
	 * use cases but you may need to customize a part of the SendGrid message.
182
	 * Instead of doing again the same work Ogham does, this builder allows you
183
	 * to intercept the message to modify it just before sending it:
184
	 * 
185
	 * <pre>
186
	 * .sender(SendGridV2Builder.class)
187
	 *    .intercept(new MyCustomInterceptor())
188
	 * </pre>
189
	 * 
190
	 * See {@link SendGridInterceptor} for more information.
191
	 * 
192
	 * @param interceptor
193
	 *            the custom interceptor used to modify {@link SendGrid}.Email
194
	 * @return this instance for fluent chaining
195
	 */
196
	public SendGridV2Builder intercept(SendGridInterceptor interceptor) {
197
		this.interceptor = interceptor;
198 1 1. intercept : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::intercept → NO_COVERAGE
		return this;
199
	}
200
201
	@Override
202
	public SendGridV2Sender build() {
203
		String apiKey = apiKeyValueBuilder.getValue();
204
		String username = usernameValueBuilder.getValue();
205
		String password = passwordValueBuilder.getValue();
206
		URL url = urlValueBuilder.getValue();
207
		SendGridClient builtClient = buildClient(apiKey, username, password, url);
208 1 1. build : negated conditional → RUN_ERROR
		if (builtClient == null) {
209
			return null;
210
		}
211
		LOG.info("Sending email using SendGrid API is registered");
212
		LOG.debug("SendGrid account: apiKey={}, username={}", apiKey, username);
213 1 1. build : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::build → RUN_ERROR
		return buildContext.register(new SendGridV2Sender(builtClient, buildContentHandler(), interceptor));
214
	}
215
216
	private SendGridClient buildClient(String apiKey, String username, String password, URL url) {
217 1 1. buildClient : negated conditional → RUN_ERROR
		if (client != null) {
218 1 1. buildClient : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → RUN_ERROR
			return client;
219
		}
220 3 1. buildClient : negated conditional → RUN_ERROR
2. buildClient : negated conditional → RUN_ERROR
3. buildClient : negated conditional → RUN_ERROR
		if (apiKey != null || (username != null && password != null)) {
221 1 1. buildClient : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → RUN_ERROR
			return buildContext.register(new DelegateSendGridClient(buildSendGrid(apiKey, username, password, url)));
222
		}
223
		return null;
224
	}
225
226
	private SendGrid buildSendGrid(String apiKey, String username, String password, URL url) {
227
		SendGrid sendGrid = newSendGrid(apiKey, username, password);
228 1 1. buildSendGrid : negated conditional → RUN_ERROR
		if (url != null) {
229
			sendGrid.setUrl(url.toString());
230
		}
231 1 1. buildSendGrid : negated conditional → RUN_ERROR
		if (httpClient != null) {
232
			sendGrid.setClient(httpClient);
233
		}
234 1 1. buildSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildSendGrid → RUN_ERROR
		return sendGrid;
235
	}
236
237
	private SendGrid newSendGrid(String apiKey, String username, String password) {
238 1 1. newSendGrid : negated conditional → RUN_ERROR
		if (apiKey != null) {
239 1 1. newSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::newSendGrid → RUN_ERROR
			return buildContext.register(new SendGrid(apiKey));
240
		}
241 1 1. newSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::newSendGrid → RUN_ERROR
		return buildContext.register(new SendGrid(username, password));
242
	}
243
244
	private PriorizedContentHandler buildContentHandler() {
245
		MimeTypeProvider mimetypeProvider = mimetypeBuilder.build();
246
		PriorizedContentHandler contentHandler = buildContext.register(new PriorizedContentHandler());
247 1 1. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → RUN_ERROR
		contentHandler.register(MultiContent.class, buildContext.register(new MultiContentHandler(contentHandler)));
248 1 1. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → RUN_ERROR
		contentHandler.register(ContentWithAttachments.class, buildContext.register(new ContentWithAttachmentsHandler(contentHandler)));
249 1 1. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → RUN_ERROR
		contentHandler.register(MayHaveStringContent.class, buildContext.register(new StringContentHandler(mimetypeProvider)));
250 1 1. buildContentHandler : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildContentHandler → RUN_ERROR
		return contentHandler;
251
	}
252
}

Mutations

134

1.1
Location : username
Killed by :
removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → RUN_ERROR

135

1.1
Location : username
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → RUN_ERROR

140

1.1
Location : username
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → RUN_ERROR

145

1.1
Location : password
Killed by :
removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → RUN_ERROR

146

1.1
Location : password
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::password → RUN_ERROR

151

1.1
Location : password
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::password → RUN_ERROR

175

1.1
Location : client
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::client → RUN_ERROR

198

1.1
Location : intercept
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::intercept → NO_COVERAGE

208

1.1
Location : build
Killed by :
negated conditional → RUN_ERROR

213

1.1
Location : build
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::build → RUN_ERROR

217

1.1
Location : buildClient
Killed by :
negated conditional → RUN_ERROR

218

1.1
Location : buildClient
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → RUN_ERROR

220

1.1
Location : buildClient
Killed by :
negated conditional → RUN_ERROR

2.2
Location : buildClient
Killed by :
negated conditional → RUN_ERROR

3.3
Location : buildClient
Killed by :
negated conditional → RUN_ERROR

221

1.1
Location : buildClient
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → RUN_ERROR

228

1.1
Location : buildSendGrid
Killed by :
negated conditional → RUN_ERROR

231

1.1
Location : buildSendGrid
Killed by :
negated conditional → RUN_ERROR

234

1.1
Location : buildSendGrid
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildSendGrid → RUN_ERROR

238

1.1
Location : newSendGrid
Killed by :
negated conditional → RUN_ERROR

239

1.1
Location : newSendGrid
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::newSendGrid → RUN_ERROR

241

1.1
Location : newSendGrid
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::newSendGrid → RUN_ERROR

247

1.1
Location : buildContentHandler
Killed by :
removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → RUN_ERROR

248

1.1
Location : buildContentHandler
Killed by :
removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → RUN_ERROR

249

1.1
Location : buildContentHandler
Killed by :
removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → RUN_ERROR

250

1.1
Location : buildContentHandler
Killed by :
replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildContentHandler → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1