28
28
import com .ericsson .ei .controller .model .NotificationMessageKeyValue ;
29
29
import com .ericsson .ei .controller .model .Subscription ;
30
30
import com .ericsson .ei .exception .SubscriptionValidationException ;
31
+ import com .ericsson .eiffelcommons .utils .RegExProvider ;
31
32
import com .fasterxml .jackson .databind .JsonNode ;
32
33
import com .fasterxml .jackson .databind .ObjectMapper ;
33
34
import com .github .fge .jackson .JsonLoader ;
@@ -44,14 +45,16 @@ public class SubscriptionValidator {
44
45
private static final String SCHEMA_FILE_PATH = "/schemas/subscription_schema.json" ;
45
46
46
47
/**
47
- * The private constructor forces all implementations to use the functions as static methods.
48
+ * The private constructor forces all implementations to use the functions as
49
+ * static methods.
48
50
*/
49
51
private SubscriptionValidator () {
50
52
}
51
53
52
54
/**
53
- * Validation of parameters values in subscriptions objects. Throws SubscriptionValidationException
54
- * if validation of a parameter fails due to wrong format of parameter.
55
+ * Validation of parameters values in subscriptions objects. Throws
56
+ * SubscriptionValidationException if validation of a parameter fails due to
57
+ * wrong format of parameter.
55
58
*
56
59
* @param subscription
57
60
*/
@@ -70,29 +73,30 @@ public static void validateSubscription(Subscription subscription) throws Subscr
70
73
}
71
74
72
75
/**
73
- * Validation of subscriptionName parameter Throws SubscriptionValidationException if validation of
74
- * the parameter fails due to wrong format of parameter.
76
+ * Validation of subscriptionName parameter Throws
77
+ * SubscriptionValidationException if validation of the parameter fails due to
78
+ * wrong format of parameter.
75
79
*
76
80
* @param subscriptionName
77
81
*/
78
82
private static void validateSubscriptionName (String subscriptionName ) throws SubscriptionValidationException {
79
- // When this regExp need to be changed then remember to change the one in the
80
- // back-end (invalidSubscriptionNameRegex in subscription.js), which do the same
81
- // invalid subscription name check. The two
82
- // regEx always need to be the same for ensuring the same check.
83
- // /(\W)/ Is a regEx that matches anything that is not [A-Z,a-z,0-8] and _.
84
- String invalidSubscriptionNameRegex = "(\\ W)" ;
83
+ String invalidSubscriptionNameRegex = null ;
84
+ invalidSubscriptionNameRegex = RegExProvider .SUBSCRIPTION_NAME ;
85
+
85
86
if (subscriptionName == null ) {
86
87
throw new SubscriptionValidationException ("Required field SubscriptionName has not been set" );
88
+ } else if (invalidSubscriptionNameRegex == null || invalidSubscriptionNameRegex .isEmpty ()) {
89
+ throw new SubscriptionValidationException (
90
+ "A valid regular expression for validating subscription name is not provided." );
87
91
} else if (Pattern .matches (invalidSubscriptionNameRegex , subscriptionName )) {
88
92
throw new SubscriptionValidationException ("Wrong format of SubscriptionName: " + subscriptionName );
89
93
}
90
94
}
91
95
92
96
/**
93
97
* Validation of NotificationMessageKeyValues parameters (key/values) Throws
94
- * SubscriptionValidationException if validation of the parameter fails due to wrong format of
95
- * parameter.
98
+ * SubscriptionValidationException if validation of the parameter fails due to
99
+ * wrong format of parameter.
96
100
*
97
101
* @param notificationMessage
98
102
* @param restPostBodyMediaType
@@ -129,8 +133,9 @@ private static void validateNotificationMessageKeyValues(List<NotificationMessag
129
133
}
130
134
131
135
/**
132
- * Validation of notificationMeta parameter Throws SubscriptionValidationException if validation of
133
- * the parameter fails due to wrong format of parameter.
136
+ * Validation of notificationMeta parameter Throws
137
+ * SubscriptionValidationException if validation of the parameter fails due to
138
+ * wrong format of parameter.
134
139
*
135
140
* @param notificationMeta
136
141
* @param notificationType
@@ -151,8 +156,9 @@ private static void validateNotificationMeta(String notificationMeta, String not
151
156
}
152
157
153
158
/**
154
- * Validation of notificationType parameter Throws SubscriptionValidationException if validation of
155
- * the parameter fails due to wrong format of parameter.
159
+ * Validation of notificationType parameter Throws
160
+ * SubscriptionValidationException if validation of the parameter fails due to
161
+ * wrong format of parameter.
156
162
*
157
163
* @param notificationType
158
164
*/
@@ -179,20 +185,21 @@ private static void RestPostMediaType(String restPostMediaType) throws Subscript
179
185
}
180
186
181
187
/**
182
- * Validation of email address Throws SubscriptionValidationException if validation of the parameter
183
- * fails due to wrong format of parameter.
188
+ * Validation of email address Throws SubscriptionValidationException if
189
+ * validation of the parameter fails due to wrong format of parameter.
184
190
*
185
191
* @param email
186
192
*/
187
193
public static void validateEmail (String email ) throws SubscriptionValidationException {
188
- // When this regExp need to be changed then remember to change the one in the
189
- // back-end (validEmailRegExpression in subscription.js), which do the same
190
- // email validation check. The two
191
- // regEx always need to be the same for ensuring the same check.
192
- String validEmailRegExpression = "^(([^<>()\\ [\\ ]\\ \\ .,;:\\ s@\" ]+(\\ .[^<>()\\ [\\ ]\\ \\ .,;:\\ s@\" ]+)*)|(\" .+\" ))@((\\ [[0-9]{1,3}\\ .[0-9]{1,3}\\ .[0-9]{1,3}\\ .[0-9]{1,3}])|(([a-zA-Z\\ -0-9]+\\ .)+[a-zA-Z]{2,}))$" ;
193
- final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern .compile (validEmailRegExpression ,
194
- Pattern .CASE_INSENSITIVE );
195
- Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher (email );
194
+ String validEmailRegEx = null ;
195
+ validEmailRegEx = RegExProvider .NOTIFICATION_META ;
196
+
197
+ if (validEmailRegEx == null || validEmailRegEx .isEmpty ()) {
198
+ throw new SubscriptionValidationException (
199
+ "A valid regular expression for subscription email validation is not provided" );
200
+ }
201
+ final Pattern validEmailAddressRegex = Pattern .compile (validEmailRegEx , Pattern .CASE_INSENSITIVE );
202
+ Matcher matcher = validEmailAddressRegex .matcher (email );
196
203
if (!(matcher .matches ())) {
197
204
throw new SubscriptionValidationException (
198
205
"Notification type is set to [MAIL] but the given notificatioMeta contains an invalid e-mail ["
0 commit comments