Skip to content

Commit e632d92

Browse files
Refactor subscription and notification handling (#284)
Improved javadoc in InformSubscriber and JmesPathInterface. Renamed to HttpRequestSender, and SendMail to EmailSender. Extracted prepareMail method, fix logging.
1 parent f6654d6 commit e632d92

File tree

9 files changed

+208
-158
lines changed

9 files changed

+208
-158
lines changed

src/main/java/com/ericsson/ei/controller/model/ParseInstanceInfoEI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.ericsson.ei.handlers.ObjectHandler;
2121
import com.ericsson.ei.handlers.RmqHandler;
2222
import com.ericsson.ei.subscription.InformSubscriber;
23-
import com.ericsson.ei.subscription.SendMail;
23+
import com.ericsson.ei.subscription.EmailSender;
2424
import com.ericsson.ei.subscription.SubscriptionHandler;
2525
import com.ericsson.ei.waitlist.WaitListStorageHandler;
2626
import lombok.Getter;
@@ -73,7 +73,7 @@ public class ParseInstanceInfoEI {
7373

7474
@Getter
7575
@Autowired
76-
private List<SendMail> email;
76+
private List<EmailSender> email;
7777

7878
@Getter
7979
@Autowired

src/main/java/com/ericsson/ei/jmespath/JmesPathInterface.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
@Component
3737
public class JmesPathInterface {
3838

39-
static Logger log = LoggerFactory.getLogger(JmesPathInterface.class);
39+
static Logger LOGGER = LoggerFactory.getLogger(JmesPathInterface.class);
4040

4141
private JmesPath<JsonNode> jmespath;
4242

@@ -50,6 +50,15 @@ public JmesPathInterface() {
5050
jmespath = new JacksonRuntime(customFunctions);
5151
}
5252

53+
/**
54+
* This method makes use of the JMESPath to compile the expression and then
55+
* searches for this expression in the given JSON structure.
56+
*
57+
* @param rule
58+
* @param event
59+
* @return result
60+
* JSONNode of the result from the JMESPath expression search
61+
* */
5362
public JsonNode runRuleOnEvent(String rule, String event) {
5463
JsonNode result = JsonNodeFactory.instance.nullNode();
5564
String inputs[] = { rule, event };
@@ -66,10 +75,8 @@ public JsonNode runRuleOnEvent(String rule, String event) {
6675
JsonNode eventJson = objectMapper.readValue(event, JsonNode.class);
6776
result = expression.search(eventJson);
6877
} catch (Exception e) {
69-
String msg = "runRuleOnEvent failed for given arguments:\n";
70-
msg += "rule was: " + rule + "\n";
71-
msg += "event was: " + event + "\n";
72-
log.error(msg, e);
78+
String message = String.format("runRuleOnEvent failed for given arguments: \nRule was:\n %s \nEvent was:\n %s", rule, event);
79+
LOGGER.error(message, e);
7380
}
7481

7582
return result;

src/main/java/com/ericsson/ei/subscription/SendMail.java renamed to src/main/java/com/ericsson/ei/subscription/EmailSender.java

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.HashSet;
2020
import java.util.Set;
2121

22-
import javax.annotation.PostConstruct;
2322
import javax.mail.MessagingException;
2423
import javax.mail.internet.MimeMessage;
2524

@@ -43,9 +42,9 @@
4342
*/
4443

4544
@Component
46-
public class SendMail {
45+
public class EmailSender {
4746

48-
private static final Logger LOGGER = LoggerFactory.getLogger(SendMail.class);
47+
private static final Logger LOGGER = LoggerFactory.getLogger(EmailSender.class);
4948

5049
@Getter
5150
@Value("${email.sender}")
@@ -58,74 +57,82 @@ public class SendMail {
5857
@Autowired
5958
private JavaMailSender emailSender;
6059

61-
public void setMailSender(JavaMailSender emailSender) {
60+
public void setEmailSender(JavaMailSender emailSender) {
6261
this.emailSender = emailSender;
6362
}
6463

6564
/**
66-
* This method takes two arguments i.e receiver mail-id and aggregatedObject and send mail to the
67-
* receiver with aggregatedObject as the body.
65+
* This method sends mail to the given receivers mail address(es) with the
66+
* given email subject and body from the mapNotificationMessage.
6867
*
69-
* @param receiver
68+
* @param receivers
69+
* Who to send the mail to
7070
* @param mapNotificationMessage
71+
* A String to be used as the body of the email
72+
* @param emailSubject
73+
* The subject of the email to send
7174
*/
72-
public void sendMail(String receiver, String mapNotificationMessage, String emailSubject)
73-
throws MessagingException {
74-
Set<String> extEmails = new HashSet<>();
75-
try {
76-
extEmails = extractEmails(receiver);
77-
} catch (SubscriptionValidationException e) {
78-
LOGGER.error(e.getMessage());
79-
e.printStackTrace();
80-
}
75+
public void sendEmail(String receivers, String mapNotificationMessage, String emailSubject) {
76+
Set<String> emails = new HashSet<>();
77+
emails = extractEmails(receivers);
78+
String[] to = emails.toArray(new String[0]);
79+
MimeMessage message = prepareEmail(mapNotificationMessage, emailSubject, to);
80+
emailSender.send(message);
81+
}
8182

83+
/**
84+
* This method creates a MimeMessageHelper and prepares the email to send
85+
*
86+
* @param mapNotificationMessage
87+
* A String to be used by the body of the email
88+
* @param emailSubject
89+
* The subject of the email to send
90+
* @param receivers
91+
* Who to send the email to
92+
* */
93+
private MimeMessage prepareEmail(String mapNotificationMessage, String emailSubject, String[] receivers) {
8294
MimeMessage message = emailSender.createMimeMessage();
83-
MimeMessageHelper helper = new MimeMessageHelper(message, true);
84-
String[] to = extEmails.toArray(new String[0]);
8595
try {
96+
MimeMessageHelper helper = new MimeMessageHelper(message, true);
8697
helper.setFrom(sender);
8798
helper.setSubject(getSubject(emailSubject));
8899
helper.setText(mapNotificationMessage);
89-
helper.setTo(to);
100+
helper.setTo(receivers);
90101
} catch (MessagingException e) {
91102
LOGGER.error(e.getMessage());
92103
e.printStackTrace();
93104
}
94-
emailSender.send(message);
105+
return message;
95106
}
96107

97108
/**
98-
* This method takes string of comma separated email addresses and return the Set of validated email
99-
* addresses
109+
* This method takes a string of comma separated email addresses and
110+
* puts them in a Set of email addresses to return.
100111
*
101-
* @param contents
112+
* @param receivers
113+
* A string containing one or more comma separated email addresses
114+
* @return emailAdd
102115
*/
103-
public Set<String> extractEmails(String contents) throws SubscriptionValidationException {
116+
public Set<String> extractEmails(String receivers) {
104117
Set<String> emailAdd = new HashSet<>();
105-
String[] addresses = contents.split(",");
118+
String[] addresses = receivers.split(",");
106119
for (String add : addresses) {
107-
SubscriptionValidator.validateEmail(add.trim());
108120
emailAdd.add(add);
109121
}
110122
return emailAdd;
111123
}
112124

113125
/**
114-
* This method takes the user provided email subject and if it is not empty, return it. Otherwise,
115-
* it return the default subject
126+
* This method takes the user provided email subject and if it is not empty,
127+
* returns it. Otherwise, it returns the default subject.
116128
*
117129
* @param emailSubject
130+
* @return emailSubject
118131
*/
119-
public String getSubject(String emailSubject) {
132+
private String getSubject(String emailSubject) {
120133
if (emailSubject.isEmpty()) {
121134
return subject;
122135
}
123136
return emailSubject;
124137
}
125-
126-
@PostConstruct
127-
public void display() {
128-
LOGGER.debug("Email Sender : " + sender);
129-
LOGGER.debug("Email Subject : " + subject);
130-
}
131138
}

src/main/java/com/ericsson/ei/subscription/SendHttpRequest.java renamed to src/main/java/com/ericsson/ei/subscription/HttpRequestSender.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,26 @@
3232
* This class is responsible to send HTTP requests when handling subscriptions.
3333
*/
3434
@Component
35-
public class SendHttpRequest {
35+
public class HttpRequestSender {
3636

37-
private static final Logger LOGGER = LoggerFactory.getLogger(SendHttpRequest.class);
37+
private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequestSender.class);
3838

3939
private RestOperations rest;
4040

41-
public SendHttpRequest(RestTemplateBuilder builder) {
41+
public HttpRequestSender(RestTemplateBuilder builder) {
4242
rest = builder.build();
4343
}
4444

4545
/**
46-
* This method is responsible to notify the subscriber through REST POST with raw body and form
47-
* parameters.
46+
* This method is responsible to notify the subscriber through REST POST
47+
* with raw body and form parameters.
4848
*
4949
* @param notificationMeta
50+
* A String containing the URL to send request to
5051
* @param mapNotificationMessage
52+
* Contains the body of the HTTP request
5153
* @param headers
52-
* @return integer
54+
* @return boolean success of the request
5355
*/
5456
public boolean postDataMultiValue(String notificationMeta, MultiValueMap<String, String> mapNotificationMessage,
5557
HttpHeaders headers) {
@@ -94,12 +96,12 @@ public boolean postDataMultiValue(String notificationMeta, MultiValueMap<String,
9496
}
9597

9698
/**
97-
* This method performs a get request to given url and with given headers, then returns the result
98-
* as a ResponseEntity<JsonNode>.
99+
* This method performs a get request to given url and with given headers,
100+
* then returns the result as a ResponseEntity<JsonNode>.
99101
*
100102
* @param url
101103
* @param headers
102-
* @return
104+
* @return ResponseEntity
103105
*/
104106
public ResponseEntity<JsonNode> makeGetRequest(String url, HttpHeaders headers) {
105107
HttpEntity<String> request = new HttpEntity<>(headers);

0 commit comments

Comments
 (0)