View Javadoc
1   package org.kuali.common.devops.jenkins.monitor.ses;
2   
3   import org.kuali.common.aws.model.ImmutableAWSCredentials;
4   import org.kuali.common.core.build.ValidatingBuilder;
5   import org.kuali.common.core.validate.annotation.IdiotProofImmutable;
6   
7   import com.amazonaws.auth.AWSCredentials;
8   import com.amazonaws.regions.Region;
9   import com.amazonaws.regions.Regions;
10  import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
11  import com.amazonaws.services.simpleemail.model.Body;
12  import com.amazonaws.services.simpleemail.model.Content;
13  import com.amazonaws.services.simpleemail.model.Destination;
14  import com.amazonaws.services.simpleemail.model.Message;
15  import com.amazonaws.services.simpleemail.model.SendEmailRequest;
16  
17  @IdiotProofImmutable
18  public final class DefaultEmailService implements EmailService {
19  
20  	private static final String[] EMPTY_STRING_ARRAY = {};
21  
22  	private final ImmutableAWSCredentials credentials;
23  	private final Region region;
24  	private final AmazonSimpleEmailServiceClient client;
25  
26  	@Override
27  	public void send(Email email) {
28  		// Construct an object to contain the recipient address.
29  		Destination destination = new Destination().withToAddresses(email.getTo().toArray(EMPTY_STRING_ARRAY));
30  
31  		// Create the subject and body of the message.
32  		Content subject = new Content().withData(email.getSubject());
33  		Content contentBody = new Content().withData(email.getBody());
34  		Body body = new Body();
35  		if (email.isHtml()) {
36  			body.setHtml(contentBody);
37  		} else {
38  			body.setText(contentBody);
39  		}
40  
41  		// Create a message with the specified subject and body.
42  		Message message = new Message().withSubject(subject).withBody(body);
43  
44  		// Assemble the email.
45  		SendEmailRequest request = new SendEmailRequest().withSource(email.getFrom()).withDestination(destination).withMessage(message);
46  
47  		// Send the email.
48  		client.sendEmail(request);
49  	}
50  
51  	private DefaultEmailService(Builder builder) {
52  		this.credentials = ImmutableAWSCredentials.copyOf(builder.credentials);
53  		this.region = builder.region;
54  		this.client = new AmazonSimpleEmailServiceClient(credentials);
55  		this.client.setRegion(region);
56  	}
57  
58  	public static Builder builder() {
59  		return new Builder();
60  	}
61  
62  	public static class Builder extends ValidatingBuilder<DefaultEmailService> {
63  
64  		private AWSCredentials credentials;
65  		private Region region = Region.getRegion(Regions.US_EAST_1);
66  
67  		public Builder withCredentials(AWSCredentials credentials) {
68  			this.credentials = credentials;
69  			return this;
70  		}
71  
72  		public Builder withRegion(Region region) {
73  			this.region = region;
74  			return this;
75  		}
76  
77  		@Override
78  		public DefaultEmailService build() {
79  			return validate(new DefaultEmailService(this));
80  		}
81  	}
82  
83  	public AWSCredentials getCredentials() {
84  		return credentials;
85  	}
86  
87  	public Region getRegion() {
88  		return region;
89  	}
90  
91  }