Skip to content

Commit d74e63f

Browse files
authored
Subscription NotificationMessage functionality added (JMESPATH), Favicon added & REST_POST_JENKINS multivalue functionality added (#62)
* Eiffel favicon added * Favicon added & REST_POST_JENKINS multivalue functionality added
1 parent 1cc4d1b commit d74e63f

File tree

5 files changed

+62
-11
lines changed

5 files changed

+62
-11
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import javax.annotation.PostConstruct;
2424

25+
import com.ericsson.ei.jmespath.JmesPathInterface;
2526
import lombok.Getter;
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
@@ -34,6 +35,8 @@
3435
import com.fasterxml.jackson.databind.JsonNode;
3536
import com.mongodb.BasicDBObject;
3637
import com.mongodb.util.JSON;
38+
import org.springframework.util.LinkedMultiValueMap;
39+
import org.springframework.util.MultiValueMap;
3740

3841
/**
3942
* This class represents the REST POST notification mechanism and the alternate
@@ -63,6 +66,9 @@ public class InformSubscription {
6366
@Value("${notification.ttl.value}")
6467
private int ttlValue;
6568

69+
@Autowired
70+
private JmesPathInterface jmespath;
71+
6672
@Autowired
6773
SpringRestTemplate restTemplate;
6874

@@ -89,21 +95,34 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
8995
log.info("NotificationType : " + notificationType);
9096
String notificationMeta = subscriptionJson.get("notificationMeta").toString().replaceAll("^\"|\"$", "");
9197
log.info("NotificationMeta : " + notificationMeta);
92-
if (notificationType.trim().equals("REST_POST")) {
98+
String notificationMessage = subscriptionJson.get("notificationMessage").toString().replaceAll("^\"|\"$", "");
99+
String filteredAggregatedObject = jmespath.runRuleOnEvent(notificationMessage, aggregatedObject).toString();
100+
if (notificationType.trim().equals("REST_POST") || notificationType.trim().equals("REST_POST_JENKINS")) {
93101
log.info("Notification through REST_POST");
94-
int result = restTemplate.postData(aggregatedObject, notificationMeta);
102+
int result = -1;
103+
if(notificationType.trim().equals("REST_POST")){result = restTemplate.postData(filteredAggregatedObject, notificationMeta);}
104+
if(notificationType.trim().equals("REST_POST_JENKINS")){
105+
MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
106+
map.add("json", filteredAggregatedObject);
107+
result = restTemplate.postDataMultiValue(notificationMeta, map);
108+
}
95109
if (result == HttpStatus.OK.value() || result == HttpStatus.CREATED.value() || result == HttpStatus.NO_CONTENT.value()) {
96110
log.info("The result is : " + result);
97111
} else {
98112
for (int i = 0; i < failAttempt; i++) {
99-
result = restTemplate.postData(aggregatedObject, notificationMeta);
100-
log.info("After trying for " + (i + 1) + " times, the result is : " + result);
113+
if(notificationType.trim().equals("REST_POST")){result = restTemplate.postData(filteredAggregatedObject, notificationMeta);}
114+
if(notificationType.trim().equals("REST_POST_JENKINS")){
115+
MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
116+
map.add("json", filteredAggregatedObject);
117+
result = restTemplate.postDataMultiValue(notificationMeta, map);
118+
}
119+
log.info("After trying for " + (i + 1) + " times, the result is : " + result);
101120
if (result == HttpStatus.OK.value())
102121
break;
103122
}
104123

105124
if (result != HttpStatus.OK.value() && result != HttpStatus.CREATED.value() && result != HttpStatus.NO_CONTENT.value()) {
106-
String input = prepareMissedNotification(aggregatedObject, subscriptionName, notificationMeta);
125+
String input = prepareMissedNotification(filteredAggregatedObject, subscriptionName, notificationMeta);
107126
log.info("Input missed Notification document : " + input);
108127
mongoDBHandler.createTTLIndex(missedNotificationDataBaseName, missedNotificationCollectionName,
109128
"Time", ttlValue);
@@ -120,7 +139,7 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
120139
}
121140
else if (notificationType.trim().equals("MAIL")) {
122141
log.info("Notification through EMAIL");
123-
sendMail.sendMail(notificationMeta, aggregatedObject);
142+
sendMail.sendMail(notificationMeta, filteredAggregatedObject);
124143

125144
}
126145
}

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
import org.slf4j.Logger;
2020
import org.slf4j.LoggerFactory;
2121
import org.springframework.boot.web.client.RestTemplateBuilder;
22-
import org.springframework.http.HttpStatus;
23-
import org.springframework.http.ResponseEntity;
22+
import org.springframework.http.*;
2423
import org.springframework.stereotype.Component;
24+
import org.springframework.util.LinkedMultiValueMap;
25+
import org.springframework.util.MultiValueMap;
2526
import org.springframework.web.client.RestOperations;
2627

2728
import com.fasterxml.jackson.databind.JsonNode;
@@ -73,4 +74,34 @@ public int postData(String aggregatedObject, String notificationMeta) {
7374

7475
}
7576

77+
/**
78+
* This method is responsible to notify the subscriber through REST POST With multivalues.
79+
* MediaType.APPLICATION_FORM_URLENCODED
80+
*
81+
* @param notificationMeta
82+
* @param map
83+
* @return integer
84+
*/
85+
public int postDataMultiValue(String notificationMeta, MultiValueMap<String, String> map) {
86+
JsonNode aggregatedJson = null;
87+
ResponseEntity<JsonNode> response = null;
88+
try {
89+
HttpHeaders headers = new HttpHeaders();
90+
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
91+
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
92+
response = rest.postForEntity( notificationMeta, request , JsonNode.class );
93+
} catch (Exception e) {
94+
log.error(e.getMessage(), e);
95+
return HttpStatus.NOT_FOUND.value();
96+
}
97+
HttpStatus status = response.getStatusCode();
98+
log.info("The response code after POST is : " + status);
99+
if (status == HttpStatus.OK) {
100+
JsonNode restCall = response.getBody();
101+
log.info("The response Body is : " + restCall);
102+
}
103+
return response.getStatusCode().value();
104+
105+
}
106+
76107
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ public void validateSubscriptionName(String subscriptionName) throws Subscriptio
7575
*/
7676
public void validateNotificationMessage(String notificationMessage) throws SubscriptionValidationException {
7777

78-
String regex = "^[A-Za-z0-9@.]+$";
78+
String regex = "^[A-Za-z0-9@\\s,:_\\[\\](){}'\".]+$";
7979

8080
if (!Pattern.matches(regex, notificationMessage)) {
81-
throw new SubscriptionValidationException("Wrong format of NotificationMessage: " + notificationMessage);
81+
throw new SubscriptionValidationException("Wrong format of NotificationMessage: " + notificationMessage);
8282
}
8383
}
8484

@@ -107,8 +107,9 @@ public void validateNotificationMeta(String notificationMeta) throws Subscriptio
107107
public void validateNotificationType(String notificationType) throws SubscriptionValidationException {
108108
String regexMail = "[\\s]*MAIL[\\\\s]*";
109109
String regexRestPost = "[\\s]*REST_POST[\\\\s]*";
110+
String regexRestPostJenkins = "[\\s]*REST_POST_JENKINS[\\\\s]*";
110111

111-
if (!(Pattern.matches(regexMail, notificationType) || Pattern.matches(regexRestPost, notificationType))) {
112+
if (!(Pattern.matches(regexMail, notificationType) || Pattern.matches(regexRestPost, notificationType) || Pattern.matches(regexRestPostJenkins, notificationType))) {
112113
throw new SubscriptionValidationException("Wrong format of NotificationType: " + notificationType);
113114
}
114115
}

src/main/resources/application.properties

100755100644
File mode changed.

src/main/resources/favicon.ico

771 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)