-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEmailService.java
194 lines (165 loc) · 8.42 KB
/
EmailService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.salaboy.conferences.email;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.salaboy.cloudevents.helper.CloudEventsHelper;
import com.salaboy.conferences.email.model.Proposal;
import io.cloudevents.CloudEvent;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.format.EventFormat;
import io.cloudevents.core.provider.EventFormatProvider;
import io.cloudevents.jackson.JsonFormat;
import io.zeebe.client.api.response.ActivatedJob;
import io.zeebe.client.api.worker.JobClient;
import io.zeebe.cloudevents.ZeebeCloudEventsHelper;
import io.zeebe.spring.client.EnableZeebeClient;
import io.zeebe.spring.client.annotation.ZeebeWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.time.OffsetDateTime;
import java.util.*;
@SpringBootApplication
@RestController
@EnableZeebeClient
@Slf4j
public class EmailService {
public static void main(String[] args) {
SpringApplication.run(EmailService.class, args);
}
private ObjectMapper objectMapper = new ObjectMapper();
@Value("${version:0.0.0}")
private String version;
@Value("${EVENTS_ENABLED:true}")
private Boolean eventsEnabled;
@Value("${K_SINK:http://broker-ingress.knative-eventing.svc.cluster.local/default/default}")
private String K_SINK;
@PostMapping("/")
public void sendEmail(@RequestBody Map<String, String> email) {
String toEmail = email.get("toEmail");
String emailTitle = email.get("title");
String emailContent = email.get("content");
log.info("+-------------------------------------------------------------------+");
printEmail(toEmail, emailTitle, emailContent);
log.info("+-------------------------------------------------------------------+\n\n");
}
@PostMapping("/notification")
public void sendEmailNotification(@RequestBody Proposal proposal) {
sendEmailNotificationWithLink(proposal, false);
emitEmailWithForProposalEvent(proposal);
}
private void sendEmailNotificationWithLink(Proposal proposal, boolean withLink) {
String emailBody = "Dear " + proposal.getAuthor() + ", \n";
String emailTitle = "Conference Committee Communication";
emailBody += "\t\t We are";
if (proposal.isApproved()) {
emailBody += " happy ";
} else {
emailBody += " sorry ";
}
emailBody += "to inform you that: \n";
emailBody += "\t\t\t `" + proposal.getTitle() + "` -> `" + proposal.getDescription() + "`, \n";
emailBody += "\t\t was";
if (proposal.isApproved()) {
emailBody += " approved ";
} else {
emailBody += " rejected ";
}
emailBody += "for this conference.";
printProposalEmail(proposal, emailTitle, emailBody, withLink);
}
private void sendEmailToCommittee(Proposal proposal) {
String emailTitle = "Conference Committee Please Review Proposal";
String emailBody = "Dear Committee Member, \n" +
"\t\t please review and accept or reject the following proposal \n";
emailBody += "\t From Author: " + proposal.getAuthor() + "\n";
emailBody += "\t With Id: " + proposal.getId() + "\n";
emailBody += "\t Notification Sent at: " + new Date() + "\n";
log.info("+-------------------------------------------------------------------+");
printEmail("committee@conference.org", emailTitle, emailBody);
log.info("+-------------------------------------------------------------------+");
}
private void printEmail(String toEmail, String title, String body) {
log.info("\t Email Sent to: " + toEmail);
log.info("\t Email Title: " + title);
log.info("\t Email Body: " + body);
}
private void printProposalEmail(Proposal proposal, String title, String body, boolean withLink) {
log.info("+-------------------------------------------------------------------+");
printEmail(proposal.getEmail(), title, body);
if (withLink) {
log.info("\t Please CURL the following link to confirm \n" +
"\t\t that you are committing to speak in our conference: \n" +
"\t\t curl -X POST <REPLACE API GATEWAY URL>/speakers/" + proposal.getId());
}
log.info("+-------------------------------------------------------------------+\n\n");
}
@ZeebeWorker(name = "email-worker", type = "email")
public void sendEmailNotification(final JobClient client, final ActivatedJob job) {
Proposal proposal = objectMapper.convertValue(job.getVariablesAsMap().get("proposal"), Proposal.class);
sendEmailNotification(proposal);
emitEmailWithForProposalEvent(proposal);
client.newCompleteCommand(job.getKey()).send();
}
@ZeebeWorker(name = "email-worker", type = "email-with-link")
public void sendEmailNotificationWithLink(final JobClient client, final ActivatedJob job) {
Proposal proposal = objectMapper.convertValue(job.getVariablesAsMap().get("proposal"), Proposal.class);
sendEmailNotificationWithLink(proposal, true);
emitEmailWithForProposalEvent(proposal);
client.newCompleteCommand(job.getKey()).send();
}
@ZeebeWorker(name = "email-worker", type = "email-to-committee")
public void sendEmailCommittee(final JobClient client, final ActivatedJob job) {
Proposal proposal = objectMapper.convertValue(job.getVariablesAsMap().get("proposal"), Proposal.class);
sendEmailToCommittee(proposal);
emitEmailWithForProposalEvent(proposal);
client.newCompleteCommand(job.getKey()).send();
}
public void emitEmailWithForProposalEvent(Proposal proposal){
if(eventsEnabled) {
String proposalString = null;
try {
proposalString = objectMapper.writeValueAsString(proposal);
proposalString = objectMapper.writeValueAsString(proposalString); //needs double quoted ??
} catch (JsonProcessingException e) {
e.printStackTrace();
}
CloudEventBuilder cloudEventBuilder = CloudEventBuilder.v1()
.withId(UUID.randomUUID().toString())
.withTime(OffsetDateTime.now().toZonedDateTime()) // bug-> https://github.com/cloudevents/sdk-java/issues/200
.withType("Email.Sent")
.withSource(URI.create("email-service.default.svc.cluster.local"))
.withData(proposalString.getBytes())
.withDataContentType("application/json")
.withSubject(proposal.getTitle());
CloudEvent zeebeCloudEvent = ZeebeCloudEventsHelper
.buildZeebeCloudEvent(cloudEventBuilder)
.withCorrelationKey(proposal.getId()).build();
logCloudEvent(zeebeCloudEvent);
WebClient webClient = WebClient.builder().baseUrl(K_SINK).filter(logRequest()).build();
WebClient.ResponseSpec postCloudEvent = CloudEventsHelper.createPostCloudEvent(webClient, zeebeCloudEvent);
postCloudEvent.bodyToMono(String.class)
.doOnError(t -> t.printStackTrace())
.doOnSuccess(s -> log.info("Cloud Event Posted to K_SINK -> " + K_SINK + ": Result: " + s))
.subscribe();
}
}
private void logCloudEvent(CloudEvent cloudEvent) {
EventFormat format = EventFormatProvider
.getInstance()
.resolveFormat(JsonFormat.CONTENT_TYPE);
log.info("Cloud Event: " + new String(format.serialize(cloudEvent)));
}
private static ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
log.info("Request: " + clientRequest.method() + " - " + clientRequest.url());
clientRequest.headers().forEach((name, values) -> values.forEach(value -> log.info(name + "=" + value)));
return Mono.just(clientRequest);
});
}
}