16
16
*/
17
17
package com .ericsson .ei .subscriptionhandler ;
18
18
19
- import java .text .DateFormat ;
20
- import java .text .SimpleDateFormat ;
21
- import java .util .Date ;
22
- import java .util .List ;
23
-
24
- import javax .annotation .PostConstruct ;
25
- import javax .mail .MessagingException ;
26
-
27
- import com .ericsson .ei .exception .SubscriptionValidationException ;
28
19
import com .ericsson .ei .jmespath .JmesPathInterface ;
20
+ import com .ericsson .ei .mongodbhandler .MongoDBHandler ;
21
+ import com .fasterxml .jackson .databind .JsonNode ;
29
22
import com .fasterxml .jackson .databind .node .ArrayNode ;
23
+ import com .mongodb .BasicDBObject ;
24
+ import com .mongodb .util .JSON ;
30
25
import lombok .Getter ;
31
26
import org .slf4j .Logger ;
32
27
import org .slf4j .LoggerFactory ;
33
28
import org .springframework .beans .factory .annotation .Autowired ;
34
29
import org .springframework .beans .factory .annotation .Value ;
35
30
import org .springframework .http .HttpStatus ;
36
31
import org .springframework .stereotype .Component ;
37
-
38
- import com .ericsson .ei .mongodbhandler .MongoDBHandler ;
39
- import com .fasterxml .jackson .databind .JsonNode ;
40
- import com .mongodb .BasicDBObject ;
41
- import com .mongodb .util .JSON ;
42
32
import org .springframework .util .LinkedMultiValueMap ;
43
33
import org .springframework .util .MultiValueMap ;
44
34
35
+ import javax .annotation .PostConstruct ;
36
+ import javax .mail .MessagingException ;
37
+ import java .text .DateFormat ;
38
+ import java .text .SimpleDateFormat ;
39
+ import java .util .Date ;
40
+
45
41
/**
46
42
* This class represents the REST POST notification mechanism and the alternate
47
43
* way to save the aggregatedObject details in the database when the
48
44
* notification fails.
49
- *
45
+ *
50
46
* @author xjibbal
51
- *
52
47
*/
53
48
54
49
@ Component
55
50
public class InformSubscription {
56
51
52
+ private static final Logger LOGGER = (Logger ) LoggerFactory .getLogger (InformSubscription .class );
53
+ //Regular expression for replacement unexpected character like \"|
54
+ private static final String REGEX = "^\" |\" $" ;
55
+
57
56
@ Getter
58
57
@ Value ("${notification.failAttempt}" )
59
58
private int failAttempt ;
@@ -74,108 +73,100 @@ public class InformSubscription {
74
73
private JmesPathInterface jmespath ;
75
74
76
75
@ Autowired
77
- SpringRestTemplate restTemplate ;
76
+ private SpringRestTemplate restTemplate ;
78
77
79
78
@ Autowired
80
- MongoDBHandler mongoDBHandler ;
79
+ private MongoDBHandler mongoDBHandler ;
81
80
82
81
@ Autowired
83
- SendMail sendMail ;
84
-
85
- static Logger log = (Logger ) LoggerFactory .getLogger (InformSubscription .class );
82
+ private SendMail sendMail ;
86
83
87
84
/**
88
85
* This method extracts the mode of notification through which the subscriber
89
86
* should be notified, from the subscription Object. And if the notification
90
87
* fails, then it saved in the database.
91
- *
88
+ *
92
89
* @param aggregatedObject
93
90
* @param subscriptionJson
94
91
*/
95
92
public void informSubscriber (String aggregatedObject , JsonNode subscriptionJson ) {
96
- String subscriptionName = subscriptionJson .get ("subscriptionName" ).toString ().replaceAll ("^ \" | \" $" , "" );
97
- log . info ("SubscriptionName : " + subscriptionName );
98
- String notificationType = subscriptionJson .get ("notificationType" ).toString ().replaceAll ("^ \" | \" $" , "" );
99
- log . info ("NotificationType : " + notificationType );
100
- String notificationMeta = subscriptionJson .get ("notificationMeta" ).toString ().replaceAll ("^ \" | \" $" , "" );
101
- log . info ("NotificationMeta : " + notificationMeta );
102
- MultiValueMap <String , String > mapNotificationMessage = new LinkedMultiValueMap <String , String >();
93
+ String subscriptionName = subscriptionJson .get ("subscriptionName" ).toString ().replaceAll (REGEX , "" );
94
+ LOGGER . debug ("SubscriptionName : " + subscriptionName );
95
+ String notificationType = subscriptionJson .get ("notificationType" ).toString ().replaceAll (REGEX , "" );
96
+ LOGGER . debug ("NotificationType : " + notificationType );
97
+ String notificationMeta = subscriptionJson .get ("notificationMeta" ).toString ().replaceAll (REGEX , "" );
98
+ LOGGER . debug ("NotificationMeta : " + notificationMeta );
99
+ MultiValueMap <String , String > mapNotificationMessage = new LinkedMultiValueMap <>();
103
100
ArrayNode arrNode = (ArrayNode ) subscriptionJson .get ("notificationMessageKeyValues" );
104
101
if (arrNode .isArray ()) {
105
102
for (final JsonNode objNode : arrNode ) {
106
- mapNotificationMessage .add (objNode .get ("formkey" ).toString ().replaceAll ("^ \" | \" $" , "" ), jmespath
107
- .runRuleOnEvent (objNode .get ("formvalue" ).toString ().replaceAll ("^ \" | \" $" , "" ), aggregatedObject )
108
- .toString ().toString (). replaceAll ("^ \" | \" $" , "" ));
103
+ mapNotificationMessage .add (objNode .get ("formkey" ).toString ().replaceAll (REGEX , "" ), jmespath
104
+ .runRuleOnEvent (objNode .get ("formvalue" ).toString ().replaceAll (REGEX , "" ), aggregatedObject )
105
+ .toString ().replaceAll (REGEX , "" ));
109
106
}
110
107
}
111
108
if (notificationType .trim ().equals ("REST_POST" )) {
112
- log . info ("Notification through REST_POST" );
113
- int result = - 1 ;
109
+ LOGGER . debug ("Notification through REST_POST" );
110
+ int result ;
114
111
String headerContentMediaType = subscriptionJson .get ("restPostBodyMediaType" ).toString ()
115
- .replaceAll ("^ \" | \" $" , "" );
116
- log . info ("headerContentMediaType : " + headerContentMediaType );
112
+ .replaceAll (REGEX , "" );
113
+ LOGGER . debug ("headerContentMediaType : " + headerContentMediaType );
117
114
result = restTemplate .postDataMultiValue (notificationMeta , mapNotificationMessage , headerContentMediaType );
118
115
if (result == HttpStatus .OK .value () || result == HttpStatus .CREATED .value ()
119
116
|| result == HttpStatus .NO_CONTENT .value ()) {
120
- log . info ("The result is : " + result );
117
+ LOGGER . debug ("The result is : " + result );
121
118
} else {
122
119
for (int i = 0 ; i < failAttempt ; i ++) {
123
120
result = restTemplate .postDataMultiValue (notificationMeta , mapNotificationMessage ,
124
121
headerContentMediaType );
125
- log . info ("After trying for " + (i + 1 ) + " times, the result is : " + result );
122
+ LOGGER . debug ("After trying for " + (i + 1 ) + " times, the result is : " + result );
126
123
if (result == HttpStatus .OK .value ())
127
124
break ;
128
125
}
129
126
if (result != HttpStatus .OK .value () && result != HttpStatus .CREATED .value ()
130
127
&& result != HttpStatus .NO_CONTENT .value ()) {
131
128
String input = prepareMissedNotification (aggregatedObject , subscriptionName , notificationMeta );
132
- log . info ("Input missed Notification document : " + input );
129
+ LOGGER . debug ("Input missed Notification document : " + input );
133
130
mongoDBHandler .createTTLIndex (missedNotificationDataBaseName , missedNotificationCollectionName ,
134
131
"Time" , ttlValue );
135
132
boolean output = mongoDBHandler .insertDocument (missedNotificationDataBaseName ,
136
133
missedNotificationCollectionName , input );
137
- log . info ("The output of insertion of missed Notification : " + output );
138
- if (output == false ) {
139
- log . info ("failed to insert the notification into database" );
134
+ LOGGER . debug ("The output of insertion of missed Notification : " + output );
135
+ if (! output ) {
136
+ LOGGER . debug ("failed to insert the notification into database" );
140
137
} else
141
- log . info ("Notification saved in the database" );
138
+ LOGGER . debug ("Notification saved in the database" );
142
139
}
143
140
}
144
141
} else if (notificationType .trim ().equals ("MAIL" )) {
145
- log . info ("Notification through EMAIL" );
142
+ LOGGER . debug ("Notification through EMAIL" );
146
143
try {
147
144
sendMail .sendMail (notificationMeta ,
148
- String .valueOf ((( List < String >) mapNotificationMessage .get ("" )).get (0 )));
145
+ String .valueOf ((mapNotificationMessage .get ("" )).get (0 )));
149
146
} catch (MessagingException e ) {
150
147
e .printStackTrace ();
151
- log .error (e .getMessage ());
152
- } catch (SubscriptionValidationException e ) {
153
- e .printStackTrace ();
154
- log .error (e .getMessage ());
148
+ LOGGER .error (e .getMessage ());
155
149
}
156
150
}
157
151
}
158
152
159
153
/**
160
154
* This method saves the missed Notification into a single document along with
161
155
* Subscription name, notification meta and time period.
162
- *
156
+ *
163
157
* @param aggregatedObject
164
158
* @param subscriptionName
165
159
* @param notificationMeta
166
- *
167
160
* @return String
168
161
*/
169
- public String prepareMissedNotification (String aggregatedObject , String subscriptionName , String notificationMeta ) {
170
- String time = null ;
171
- Date date = null ;
162
+ private String prepareMissedNotification (String aggregatedObject , String subscriptionName , String notificationMeta ) {
163
+ Date date = new Date ();
172
164
DateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss" );
173
- date = new Date ();
174
- time = dateFormat .format (date );
165
+ String time = dateFormat .format (date );
175
166
try {
176
167
date = dateFormat .parse (time );
177
168
} catch (Exception e ) {
178
- log . info (e .getMessage (), e );
169
+ LOGGER . error (e .getMessage (), e );
179
170
}
180
171
BasicDBObject document = new BasicDBObject ();
181
172
document .put ("subscriptionName" , subscriptionName );
@@ -191,10 +182,9 @@ public String prepareMissedNotification(String aggregatedObject, String subscrip
191
182
*/
192
183
@ PostConstruct
193
184
public void init () {
194
- log .debug ("missedNotificationCollectionName : " + missedNotificationCollectionName );
195
- log .debug ("missedNotificationDataBaseName : " + missedNotificationDataBaseName );
196
- log .debug ("notification.failAttempt : " + failAttempt );
197
- log .debug ("Missed Notification TTL value : " + ttlValue );
185
+ LOGGER .debug ("missedNotificationCollectionName : " + missedNotificationCollectionName );
186
+ LOGGER .debug ("missedNotificationDataBaseName : " + missedNotificationDataBaseName );
187
+ LOGGER .debug ("notification.failAttempt : " + failAttempt );
188
+ LOGGER .debug ("Missed Notification TTL value : " + ttlValue );
198
189
}
199
-
200
- }
190
+ }
0 commit comments