22
22
import java .util .List ;
23
23
24
24
import javax .annotation .PostConstruct ;
25
+ import javax .mail .MessagingException ;
25
26
27
+ import com .ericsson .ei .exception .SubscriptionValidationException ;
26
28
import com .ericsson .ei .jmespath .JmesPathInterface ;
27
- import com .fasterxml .jackson .databind .ObjectMapper ;
28
29
import com .fasterxml .jackson .databind .node .ArrayNode ;
29
30
import lombok .Getter ;
30
31
import org .slf4j .Logger ;
@@ -84,9 +85,9 @@ public class InformSubscription {
84
85
static Logger log = (Logger ) LoggerFactory .getLogger (InformSubscription .class );
85
86
86
87
/**
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.
90
91
*
91
92
* @param aggregatedObject
92
93
* @param subscriptionJson
@@ -98,29 +99,35 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
98
99
log .info ("NotificationType : " + notificationType );
99
100
String notificationMeta = subscriptionJson .get ("notificationMeta" ).toString ().replaceAll ("^\" |\" $" , "" );
100
101
log .info ("NotificationMeta : " + notificationMeta );
101
- MultiValueMap <String , String > mapNotificationMessage = new LinkedMultiValueMap <String , String >();
102
+ MultiValueMap <String , String > mapNotificationMessage = new LinkedMultiValueMap <String , String >();
102
103
ArrayNode arrNode = (ArrayNode ) subscriptionJson .get ("notificationMessageKeyValues" );
103
104
if (arrNode .isArray ()) {
104
105
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 ("^\" |\" $" , "" ));
106
109
}
107
110
}
108
- if (notificationType .trim ().equals ("REST_POST" )){
111
+ if (notificationType .trim ().equals ("REST_POST" )) {
109
112
log .info ("Notification through REST_POST" );
110
113
int result = -1 ;
111
- String headerContentMediaType = subscriptionJson .get ("restPostBodyMediaType" ).toString ().replaceAll ("^\" |\" $" , "" );
114
+ String headerContentMediaType = subscriptionJson .get ("restPostBodyMediaType" ).toString ()
115
+ .replaceAll ("^\" |\" $" , "" );
112
116
log .info ("headerContentMediaType : " + headerContentMediaType );
113
117
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 ()) {
115
120
log .info ("The result is : " + result );
116
121
} else {
117
122
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 );
120
126
if (result == HttpStatus .OK .value ())
121
127
break ;
122
128
}
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 ()) {
124
131
String input = prepareMissedNotification (aggregatedObject , subscriptionName , notificationMeta );
125
132
log .info ("Input missed Notification document : " + input );
126
133
mongoDBHandler .createTTLIndex (missedNotificationDataBaseName , missedNotificationCollectionName ,
@@ -134,16 +141,24 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
134
141
log .info ("Notification saved in the database" );
135
142
}
136
143
}
137
- }
138
- else if (notificationType .trim ().equals ("MAIL" )) {
144
+ } else if (notificationType .trim ().equals ("MAIL" )) {
139
145
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
+ }
141
156
}
142
157
}
143
158
144
159
/**
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.
147
162
*
148
163
* @param aggregatedObject
149
164
* @param subscriptionName
@@ -171,8 +186,8 @@ public String prepareMissedNotification(String aggregatedObject, String subscrip
171
186
}
172
187
173
188
/**
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.
176
191
*/
177
192
@ PostConstruct
178
193
public void init () {
0 commit comments