Skip to content

Commit d89281f

Browse files
authored
merge (#18)
* ADD: subscription template (#66) * ADD: subscription template * Add RAML implementation * Subscription multikey post form and raw json body support added. Unit… (#67) * Subscription multikey post form and raw json body support added. Unit & Flowtests updated/added * Subscription multikey post form and raw json body support added. Unit & Flowtests updated/added * Subscription validation updated for MAIL (#69)
1 parent 81a0d31 commit d89281f

File tree

10 files changed

+505
-129
lines changed

10 files changed

+505
-129
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
package com.ericsson.ei.controller.model;
3+
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
7+
import com.fasterxml.jackson.annotation.JsonAnySetter;
8+
import com.fasterxml.jackson.annotation.JsonIgnore;
9+
import com.fasterxml.jackson.annotation.JsonInclude;
10+
import com.fasterxml.jackson.annotation.JsonProperty;
11+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
12+
import org.apache.commons.lang3.builder.EqualsBuilder;
13+
import org.apache.commons.lang3.builder.HashCodeBuilder;
14+
import org.apache.commons.lang3.builder.ToStringBuilder;
15+
16+
@JsonInclude(JsonInclude.Include.NON_NULL)
17+
@JsonPropertyOrder({
18+
"formkey",
19+
"formvalue"
20+
})
21+
public class NotificationMessageKeyValue {
22+
23+
@JsonProperty("formkey")
24+
private String formkey;
25+
@JsonProperty("formvalue")
26+
private String formvalue;
27+
@JsonIgnore
28+
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
29+
30+
@JsonProperty("formkey")
31+
public String getFormkey() {
32+
return formkey;
33+
}
34+
35+
@JsonProperty("formkey")
36+
public void setFormkey(String formkey) {
37+
this.formkey = formkey;
38+
}
39+
40+
@JsonProperty("formvalue")
41+
public String getFormvalue() {
42+
return formvalue;
43+
}
44+
45+
@JsonProperty("formvalue")
46+
public void setFormvalue(String formvalue) {
47+
this.formvalue = formvalue;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return ToStringBuilder.reflectionToString(this);
53+
}
54+
55+
@JsonAnyGetter
56+
public Map<String, Object> getAdditionalProperties() {
57+
return this.additionalProperties;
58+
}
59+
60+
@JsonAnySetter
61+
public void setAdditionalProperty(String name, Object value) {
62+
this.additionalProperties.put(name, value);
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
return new HashCodeBuilder().append(formkey).append(formvalue).append(additionalProperties).toHashCode();
68+
}
69+
70+
@Override
71+
public boolean equals(Object other) {
72+
if (other == this) {
73+
return true;
74+
}
75+
if ((other instanceof NotificationMessageKeyValue) == false) {
76+
return false;
77+
}
78+
NotificationMessageKeyValue rhs = ((NotificationMessageKeyValue) other);
79+
return new EqualsBuilder().append(formkey, rhs.formkey).append(formvalue, rhs.formvalue).append(additionalProperties, rhs.additionalProperties).isEquals();
80+
}
81+
82+
}

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
@JsonPropertyOrder({
2020
"aggregationtype",
2121
"created",
22-
"notificationMessage",
2322
"notificationMeta",
2423
"notificationType",
24+
"restPostBodyMediaType",
25+
"notificationMessageKeyValues",
2526
"repeat",
2627
"requirements",
2728
"subscriptionName"
@@ -32,12 +33,14 @@ public class Subscription {
3233
private String aggregationtype;
3334
@JsonProperty("created")
3435
private Object created;
35-
@JsonProperty("notificationMessage")
36-
private String notificationMessage;
3736
@JsonProperty("notificationMeta")
3837
private String notificationMeta;
3938
@JsonProperty("notificationType")
4039
private String notificationType;
40+
@JsonProperty("restPostBodyMediaType")
41+
private String restPostBodyMediaType;
42+
@JsonProperty("notificationMessageKeyValues")
43+
private List<NotificationMessageKeyValue> notificationMessageKeyValues = new ArrayList<NotificationMessageKeyValue>();
4144
@JsonProperty("repeat")
4245
private Boolean repeat;
4346
@JsonProperty("requirements")
@@ -67,16 +70,6 @@ public void setCreated(Object created) {
6770
this.created = created;
6871
}
6972

70-
@JsonProperty("notificationMessage")
71-
public String getNotificationMessage() {
72-
return notificationMessage;
73-
}
74-
75-
@JsonProperty("notificationMessage")
76-
public void setNotificationMessage(String notificationMessage) {
77-
this.notificationMessage = notificationMessage;
78-
}
79-
8073
@JsonProperty("notificationMeta")
8174
public String getNotificationMeta() {
8275
return notificationMeta;
@@ -97,6 +90,26 @@ public void setNotificationType(String notificationType) {
9790
this.notificationType = notificationType;
9891
}
9992

93+
@JsonProperty("restPostBodyMediaType")
94+
public String getRestPostBodyMediaType() {
95+
return restPostBodyMediaType;
96+
}
97+
98+
@JsonProperty("restPostBodyMediaType")
99+
public void setRestPostBodyMediaType(String restPostBodyMediaType) {
100+
this.restPostBodyMediaType = restPostBodyMediaType;
101+
}
102+
103+
@JsonProperty("notificationMessageKeyValues")
104+
public List<NotificationMessageKeyValue> getNotificationMessageKeyValues() {
105+
return notificationMessageKeyValues;
106+
}
107+
108+
@JsonProperty("notificationMessageKeyValues")
109+
public void setNotificationMessageKeyValues(List<NotificationMessageKeyValue> notificationMessageKeyValues) {
110+
this.notificationMessageKeyValues = notificationMessageKeyValues;
111+
}
112+
100113
@JsonProperty("repeat")
101114
public Boolean getRepeat() {
102115
return repeat;
@@ -144,7 +157,7 @@ public void setAdditionalProperty(String name, Object value) {
144157

145158
@Override
146159
public int hashCode() {
147-
return new HashCodeBuilder().append(aggregationtype).append(created).append(notificationMessage).append(notificationMeta).append(notificationType).append(repeat).append(requirements).append(subscriptionName).append(additionalProperties).toHashCode();
160+
return new HashCodeBuilder().append(aggregationtype).append(created).append(notificationMeta).append(notificationType).append(restPostBodyMediaType).append(notificationMessageKeyValues).append(repeat).append(requirements).append(subscriptionName).append(additionalProperties).toHashCode();
148161
}
149162

150163
@Override
@@ -156,7 +169,7 @@ public boolean equals(Object other) {
156169
return false;
157170
}
158171
Subscription rhs = ((Subscription) other);
159-
return new EqualsBuilder().append(aggregationtype, rhs.aggregationtype).append(created, rhs.created).append(notificationMessage, rhs.notificationMessage).append(notificationMeta, rhs.notificationMeta).append(notificationType, rhs.notificationType).append(repeat, rhs.repeat).append(requirements, rhs.requirements).append(subscriptionName, rhs.subscriptionName).append(additionalProperties, rhs.additionalProperties).isEquals();
172+
return new EqualsBuilder().append(aggregationtype, rhs.aggregationtype).append(created, rhs.created).append(notificationMeta, rhs.notificationMeta).append(notificationType, rhs.notificationType).append(restPostBodyMediaType, rhs.restPostBodyMediaType).append(notificationMessageKeyValues, rhs.notificationMessageKeyValues).append(repeat, rhs.repeat).append(requirements, rhs.requirements).append(subscriptionName, rhs.subscriptionName).append(additionalProperties, rhs.additionalProperties).isEquals();
160173
}
161174

162175
}

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import java.text.DateFormat;
2020
import java.text.SimpleDateFormat;
2121
import java.util.Date;
22+
import java.util.List;
2223

2324
import javax.annotation.PostConstruct;
2425

2526
import com.ericsson.ei.jmespath.JmesPathInterface;
27+
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import com.fasterxml.jackson.databind.node.ArrayNode;
2629
import lombok.Getter;
2730
import org.slf4j.Logger;
2831
import org.slf4j.LoggerFactory;
@@ -95,34 +98,30 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
9598
log.info("NotificationType : " + notificationType);
9699
String notificationMeta = subscriptionJson.get("notificationMeta").toString().replaceAll("^\"|\"$", "");
97100
log.info("NotificationMeta : " + notificationMeta);
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")) {
101+
MultiValueMap<String, String> mapNotificationMessage= new LinkedMultiValueMap<String, String>();
102+
ArrayNode arrNode = (ArrayNode) subscriptionJson.get("notificationMessageKeyValues");
103+
if (arrNode.isArray()) {
104+
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+
}
107+
}
108+
if(notificationType.trim().equals("REST_POST")){
101109
log.info("Notification through REST_POST");
102110
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-
}
111+
String headerContentMediaType = subscriptionJson.get("restPostBodyMediaType").toString().replaceAll("^\"|\"$", "");
112+
log.info("headerContentMediaType : " + headerContentMediaType);
113+
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage, headerContentMediaType);
109114
if (result == HttpStatus.OK.value() || result == HttpStatus.CREATED.value() || result == HttpStatus.NO_CONTENT.value()) {
110115
log.info("The result is : " + result);
111116
} else {
112117
for (int i = 0; i < failAttempt; i++) {
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);
118+
result = restTemplate.postDataMultiValue(notificationMeta, mapNotificationMessage, headerContentMediaType);
119+
log.info("After trying for " + (i + 1) + " times, the result is : " + result);
120120
if (result == HttpStatus.OK.value())
121121
break;
122122
}
123-
124123
if (result != HttpStatus.OK.value() && result != HttpStatus.CREATED.value() && result != HttpStatus.NO_CONTENT.value()) {
125-
String input = prepareMissedNotification(filteredAggregatedObject, subscriptionName, notificationMeta);
124+
String input = prepareMissedNotification(aggregatedObject, subscriptionName, notificationMeta);
126125
log.info("Input missed Notification document : " + input);
127126
mongoDBHandler.createTTLIndex(missedNotificationDataBaseName, missedNotificationCollectionName,
128127
"Time", ttlValue);
@@ -134,13 +133,11 @@ public void informSubscriber(String aggregatedObject, JsonNode subscriptionJson)
134133
} else
135134
log.info("Notification saved in the database");
136135
}
137-
138136
}
139137
}
140138
else if (notificationType.trim().equals("MAIL")) {
141139
log.info("Notification through EMAIL");
142-
sendMail.sendMail(notificationMeta, filteredAggregatedObject);
143-
140+
sendMail.sendMail(notificationMeta, String.valueOf(((List<String>) mapNotificationMessage.get("")).get(0)));
144141
}
145142
}
146143

@@ -150,7 +147,7 @@ else if (notificationType.trim().equals("MAIL")) {
150147
*
151148
* @param aggregatedObject
152149
* @param subscriptionName
153-
* @param notificationType
150+
* @param notificationMeta
154151
*
155152
* @return String
156153
*/

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

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

19+
import com.ericsson.ei.jmespath.JmesPathInterface;
1920
import org.slf4j.Logger;
2021
import org.slf4j.LoggerFactory;
22+
import org.springframework.beans.factory.annotation.Autowired;
2123
import org.springframework.boot.web.client.RestTemplateBuilder;
2224
import org.springframework.http.*;
2325
import org.springframework.stereotype.Component;
24-
import org.springframework.util.LinkedMultiValueMap;
2526
import org.springframework.util.MultiValueMap;
2627
import org.springframework.web.client.RestOperations;
2728

2829
import com.fasterxml.jackson.databind.JsonNode;
2930
import com.fasterxml.jackson.databind.ObjectMapper;
3031

32+
import java.util.List;
33+
3134
/**
3235
* This class is responsible to send notification through REST POST to the
3336
* recipient of the Subscription Object.
34-
*
35-
* @author xjibbal
3637
*
3738
*/
3839

@@ -48,48 +49,26 @@ public SpringRestTemplate(RestTemplateBuilder builder) {
4849
}
4950

5051
/**
51-
* This method is responsible to notify the subscriber through REST POST.
52-
*
53-
* @param aggregatedObject
54-
* @param notificationMeta
55-
* @return integer
56-
*/
57-
public int postData(String aggregatedObject, String notificationMeta) {
58-
JsonNode aggregatedJson = null;
59-
ResponseEntity<JsonNode> response = null;
60-
try {
61-
aggregatedJson = new ObjectMapper().readTree(aggregatedObject);
62-
response = rest.postForEntity(notificationMeta, aggregatedJson, JsonNode.class);
63-
} catch (Exception e) {
64-
log.error(e.getMessage(), e);
65-
return HttpStatus.NOT_FOUND.value();
66-
}
67-
HttpStatus status = response.getStatusCode();
68-
log.info("The response code after POST is : " + status);
69-
if (status == HttpStatus.OK) {
70-
JsonNode restCall = response.getBody();
71-
log.info("The response Body is : " + restCall);
72-
}
73-
return response.getStatusCode().value();
74-
75-
}
76-
77-
/**
78-
* This method is responsible to notify the subscriber through REST POST With multivalues.
79-
* MediaType.APPLICATION_FORM_URLENCODED
52+
* This method is responsible to notify the subscriber through REST POST With raw body and form parameters.
8053
*
8154
* @param notificationMeta
82-
* @param map
55+
* @param mapNotificationMessage
56+
* @param headerContentMediaType
8357
* @return integer
8458
*/
85-
public int postDataMultiValue(String notificationMeta, MultiValueMap<String, String> map) {
86-
JsonNode aggregatedJson = null;
59+
public int postDataMultiValue(String notificationMeta, MultiValueMap<String, String> mapNotificationMessage, String headerContentMediaType) {
8760
ResponseEntity<JsonNode> response = null;
8861
try {
8962
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 );
63+
headers.setContentType(MediaType.valueOf(headerContentMediaType));
64+
if(headerContentMediaType.equals(MediaType.APPLICATION_FORM_URLENCODED.toString())){ //"application/x-www-form-urlencoded"
65+
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(mapNotificationMessage, headers);
66+
response = rest.postForEntity(notificationMeta, request , JsonNode.class );
67+
}
68+
else{
69+
HttpEntity<String> request = new HttpEntity<String>(String.valueOf(((List<String>) mapNotificationMessage.get("")).get(0)), headers);
70+
response = rest.postForEntity(notificationMeta, request, JsonNode.class);
71+
}
9372
} catch (Exception e) {
9473
log.error(e.getMessage(), e);
9574
return HttpStatus.NOT_FOUND.value();
@@ -101,7 +80,5 @@ public int postDataMultiValue(String notificationMeta, MultiValueMap<String, Str
10180
log.info("The response Body is : " + restCall);
10281
}
10382
return response.getStatusCode().value();
104-
10583
}
106-
10784
}

0 commit comments

Comments
 (0)