Skip to content

Commit b8e5b88

Browse files
saif-ericssonemichaf
authored andcommitted
Subscriptions need to handle multiple Mail receivers (#82)
* JMESPath REST API * JMESPath API with fixed reviews * partial work jmespath rest * JMESPath API with RAML 0.8 specification * remove componet comment * replace tab with spaces * fix usage examples * prepare for new asserts in flow test * fix unit tests * Subscription for individual events * pom file * subscription individual objects * pom * error removal * Subscription Individual Object * deleting unused import * jmespath comment correction * space removal and new clean build * review changes * review change file deleted * some files * sendmail * dded some test code * deleting from other story * removig from other stories * raml * added missing property * added missing property * removed unrelated code * removing subscription rules * review * handler
1 parent 68ff28d commit b8e5b88

File tree

9 files changed

+306
-180
lines changed

9 files changed

+306
-180
lines changed

src/main/java/com/ericsson/ei/controller/SubscriptionControllerImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ public ResponseEntity<SubscriptionResponse> updateSubscriptions(@RequestBody Lis
144144
}
145145

146146
}
147-
148147
@Override
149148
@CrossOrigin
150149
@ApiOperation(value = "Removes the subscription from the database")

src/main/java/com/ericsson/ei/handlers/ObjectHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public boolean insertObject(String aggregatedObject, RulesObject rulesObject, St
7575
}
7676
JsonNode document = prepareDocumentForInsertion(id, aggregatedObject);
7777
log.debug("ObjectHandler: Aggregated Object document to be inserted: " + document.toString());
78+
7879
boolean result = mongoDbHandler.insertDocument(databaseName, collectionName, document.toString());
7980
if (result)
8081
eventToObjectMap.updateEventToObjectMapInMemoryDB(rulesObject, event, id);

src/main/java/com/ericsson/ei/subscriptionhandler/InformSubscription.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
import java.util.List;
2323

2424
import javax.annotation.PostConstruct;
25+
import javax.mail.MessagingException;
2526

27+
import com.ericsson.ei.exception.SubscriptionValidationException;
2628
import com.ericsson.ei.jmespath.JmesPathInterface;
27-
import com.fasterxml.jackson.databind.ObjectMapper;
2829
import com.fasterxml.jackson.databind.node.ArrayNode;
2930
import lombok.Getter;
3031
import org.slf4j.Logger;
@@ -84,9 +85,9 @@ public class InformSubscription {
8485
static Logger log = (Logger) LoggerFactory.getLogger(InformSubscription.class);
8586

8687
/**
87-
* This method extracts the mode of notification through which the
88-
* subscriber should be notified, from the subscription Object. And if the
89-
* notification fails, then it saved in the database.
88+
* This method extracts the mode of notification through which the subscriber
89+
* should be notified, from the subscription Object. And if the notification
90+
* fails, then it saved in the database.
9091
*
9192
* @param aggregatedObject
9293
* @param subscriptionJson
@@ -98,29 +99,35 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
9899
log.info("NotificationType : " + notificationType);
99100
String notificationMeta = subscriptionJson.get("notificationMeta").toString().replaceAll("^\"|\"$", "");
100101
log.info("NotificationMeta : " + notificationMeta);
101-
MultiValueMap<String, String> mapNotificationMessage= new LinkedMultiValueMap<String, String>();
102+
MultiValueMap<String, String> mapNotificationMessage = new LinkedMultiValueMap<String, String>();
102103
ArrayNode arrNode = (ArrayNode) subscriptionJson.get("notificationMessageKeyValues");
103104
if (arrNode.isArray()) {
104105
for (final JsonNode objNode : arrNode) {
105-
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll("^\"|\"$", ""), jmespath.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll("^\"|\"$", ""), aggregatedObject).toString().toString().replaceAll("^\"|\"$", ""));
106+
mapNotificationMessage.add(objNode.get("formkey").toString().replaceAll("^\"|\"$", ""), jmespath
107+
.runRuleOnEvent(objNode.get("formvalue").toString().replaceAll("^\"|\"$", ""), aggregatedObject)
108+
.toString().toString().replaceAll("^\"|\"$", ""));
106109
}
107110
}
108-
if(notificationType.trim().equals("REST_POST")){
111+
if (notificationType.trim().equals("REST_POST")) {
109112
log.info("Notification through REST_POST");
110113
int result = -1;
111-
String headerContentMediaType = subscriptionJson.get("restPostBodyMediaType").toString().replaceAll("^\"|\"$", "");
114+
String headerContentMediaType = subscriptionJson.get("restPostBodyMediaType").toString()
115+
.replaceAll("^\"|\"$", "");
112116
log.info("headerContentMediaType : " + headerContentMediaType);
113117
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage, headerContentMediaType);
114-
if (result == HttpStatus.OK.value() || result == HttpStatus.CREATED.value() || result == HttpStatus.NO_CONTENT.value()) {
118+
if (result == HttpStatus.OK.value() || result == HttpStatus.CREATED.value()
119+
|| result == HttpStatus.NO_CONTENT.value()) {
115120
log.info("The result is : " + result);
116121
} else {
117122
for (int i = 0; i < failAttempt; i++) {
118-
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage, headerContentMediaType);
119-
log.info("After trying for " + (i + 1) + " times, the result is : " + result);
123+
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage,
124+
headerContentMediaType);
125+
log.info("After trying for " + (i + 1) + " times, the result is : " + result);
120126
if (result == HttpStatus.OK.value())
121127
break;
122128
}
123-
if (result != HttpStatus.OK.value() && result != HttpStatus.CREATED.value() && result != HttpStatus.NO_CONTENT.value()) {
129+
if (result != HttpStatus.OK.value() && result != HttpStatus.CREATED.value()
130+
&& result != HttpStatus.NO_CONTENT.value()) {
124131
String input = prepareMissedNotification(aggregatedObject, subscriptionName, notificationMeta);
125132
log.info("Input missed Notification document : " + input);
126133
mongoDBHandler.createTTLIndex(missedNotificationDataBaseName, missedNotificationCollectionName,
@@ -134,16 +141,24 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
134141
log.info("Notification saved in the database");
135142
}
136143
}
137-
}
138-
else if (notificationType.trim().equals("MAIL")) {
144+
} else if (notificationType.trim().equals("MAIL")) {
139145
log.info("Notification through EMAIL");
140-
sendMail.sendMail(notificationMeta, String.valueOf(((List<String>) mapNotificationMessage.get("")).get(0)));
146+
try {
147+
sendMail.sendMail(notificationMeta,
148+
String.valueOf(((List<String>) mapNotificationMessage.get("")).get(0)));
149+
} catch (MessagingException e) {
150+
e.printStackTrace();
151+
log.error(e.getMessage());
152+
} catch (SubscriptionValidationException e) {
153+
e.printStackTrace();
154+
log.error(e.getMessage());
155+
}
141156
}
142157
}
143158

144159
/**
145-
* This method saves the missed Notification into a single document along
146-
* with Subscription name, notification meta and time period.
160+
* This method saves the missed Notification into a single document along with
161+
* Subscription name, notification meta and time period.
147162
*
148163
* @param aggregatedObject
149164
* @param subscriptionName
@@ -171,8 +186,8 @@ public String prepareMissedNotification(String aggregatedObject, String subscrip
171186
}
172187

173188
/**
174-
* This method is responsible to display the configurable application
175-
* properties and to create TTL index on the missed Notification collection.
189+
* This method is responsible to display the configurable application properties
190+
* and to create TTL index on the missed Notification collection.
176191
*/
177192
@PostConstruct
178193
public void init() {

src/main/java/com/ericsson/ei/subscriptionhandler/SendMail.java

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@
1616
*/
1717
package com.ericsson.ei.subscriptionhandler;
1818

19+
import java.util.HashSet;
20+
import java.util.Set;
1921
import javax.annotation.PostConstruct;
20-
22+
import javax.mail.MessagingException;
23+
import javax.mail.internet.MimeMessage;
2124
import lombok.Getter;
2225
import org.slf4j.Logger;
2326
import org.slf4j.LoggerFactory;
2427
import org.springframework.beans.factory.annotation.Autowired;
2528
import org.springframework.beans.factory.annotation.Value;
26-
import org.springframework.mail.MailSender;
27-
import org.springframework.mail.SimpleMailMessage;
29+
import org.springframework.mail.javamail.JavaMailSender;
30+
import org.springframework.mail.javamail.MimeMessageHelper;
2831
import org.springframework.stereotype.Component;
32+
import com.ericsson.ei.exception.SubscriptionValidationException;
33+
import com.ericsson.ei.subscriptionhandler.SubscriptionValidator;
2934

3035
/**
3136
* This class represents the mechanism to send e-mail notification to the
@@ -47,35 +52,71 @@ public class SendMail {
4752
@Getter
4853
@Value("${email.subject}")
4954
private String subject;
50-
55+
5156
@Autowired
52-
private MailSender mailSender;
57+
private JavaMailSender emailSender;
58+
59+
private SubscriptionValidator subscriptionValidator = new SubscriptionValidator();
5360

54-
public void setMailSender(MailSender mailSender) {
55-
this.mailSender = mailSender;
61+
public void setMailSender(JavaMailSender emailSender) {
62+
this.emailSender = emailSender;
5663
}
5764

5865
/**
59-
* This method takes two arguments i.e receiver mail-id and aggregatedObject
60-
* and send mail to the receiver with aggregatedObject as the body.
66+
* This method takes two arguments i.e receiver mail-id and aggregatedObject and
67+
* send mail to the receiver with aggregatedObject as the body.
6168
*
6269
* @param receiver
6370
* @param aggregatedObject
6471
*/
65-
public void sendMail(String receiver, String aggregatedObject) {
72+
public void sendMail(String receiver, String mapNotificationMessage)
73+
throws MessagingException, SubscriptionValidationException {
74+
Set<String> extEmails = new HashSet<>();
75+
try {
76+
extEmails = extractEmails(receiver);
77+
} catch (SubscriptionValidationException e) {
78+
e.printStackTrace();
79+
log.error(e.getMessage());
80+
}
6681

67-
SimpleMailMessage message = new SimpleMailMessage();
82+
MimeMessage message = emailSender.createMimeMessage();
83+
MimeMessageHelper helper = new MimeMessageHelper(message, true);
84+
String[] to = extEmails.toArray(new String[0]);
6885

69-
message.setFrom(sender);
70-
message.setTo(receiver);
71-
message.setSubject(subject);
72-
message.setText(aggregatedObject);
73-
mailSender.send(message);
86+
try {
87+
helper.setFrom(sender);
88+
helper.setSubject(subject);
89+
helper.setText(mapNotificationMessage);
90+
helper.setTo(to);
91+
} catch (MessagingException e) {
92+
e.printStackTrace();
93+
log.error(e.getMessage());
94+
}
95+
96+
emailSender.send(message);
97+
}
98+
99+
/**
100+
* This method takes string of comma separated email addresses and return the
101+
* Set of validated email addresses
102+
*
103+
* @param contetns
104+
*/
105+
public Set<String> extractEmails(String contents) throws SubscriptionValidationException {
106+
Set<String> emailAdd = new HashSet<>();
107+
String[] addresses = contents.split(",");
108+
109+
for (String add : addresses) {
110+
subscriptionValidator.validateEmail(add.trim());
111+
emailAdd.add(add);
112+
}
113+
return emailAdd;
74114
}
75115

76116
@PostConstruct
77117
public void display() {
78118
log.info("Email Sender : " + sender);
79119
log.info("Email Subject : " + subject);
80120
}
81-
}
121+
122+
}

0 commit comments

Comments
 (0)