Skip to content

Commit 865d487

Browse files
saif-ericssonerik-edling
authored andcommitted
Regex from common to validate subscription in EI back-end (#277)
* Change regex validation to use regex class from eiffel-commons.
1 parent 0e33047 commit 865d487

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<dependency>
7171
<groupId>com.github.eiffel-community</groupId>
7272
<artifactId>eiffel-commons</artifactId>
73-
<version>0.0.4</version>
73+
<version>0.0.10</version>
7474
</dependency>
7575

7676
<dependency>

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

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.ericsson.ei.controller.model.NotificationMessageKeyValue;
2929
import com.ericsson.ei.controller.model.Subscription;
3030
import com.ericsson.ei.exception.SubscriptionValidationException;
31+
import com.ericsson.eiffelcommons.utils.RegExProvider;
3132
import com.fasterxml.jackson.databind.JsonNode;
3233
import com.fasterxml.jackson.databind.ObjectMapper;
3334
import com.github.fge.jackson.JsonLoader;
@@ -44,14 +45,16 @@ public class SubscriptionValidator {
4445
private static final String SCHEMA_FILE_PATH = "/schemas/subscription_schema.json";
4546

4647
/**
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.
4850
*/
4951
private SubscriptionValidator() {
5052
}
5153

5254
/**
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.
5558
*
5659
* @param subscription
5760
*/
@@ -70,29 +73,30 @@ public static void validateSubscription(Subscription subscription) throws Subscr
7073
}
7174

7275
/**
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.
7579
*
7680
* @param subscriptionName
7781
*/
7882
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+
8586
if (subscriptionName == null) {
8687
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.");
8791
} else if (Pattern.matches(invalidSubscriptionNameRegex, subscriptionName)) {
8892
throw new SubscriptionValidationException("Wrong format of SubscriptionName: " + subscriptionName);
8993
}
9094
}
9195

9296
/**
9397
* 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.
96100
*
97101
* @param notificationMessage
98102
* @param restPostBodyMediaType
@@ -129,8 +133,9 @@ private static void validateNotificationMessageKeyValues(List<NotificationMessag
129133
}
130134

131135
/**
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.
134139
*
135140
* @param notificationMeta
136141
* @param notificationType
@@ -151,8 +156,9 @@ private static void validateNotificationMeta(String notificationMeta, String not
151156
}
152157

153158
/**
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.
156162
*
157163
* @param notificationType
158164
*/
@@ -179,20 +185,21 @@ private static void RestPostMediaType(String restPostMediaType) throws Subscript
179185
}
180186

181187
/**
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.
184190
*
185191
* @param email
186192
*/
187193
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);
196203
if (!(matcher.matches())) {
197204
throw new SubscriptionValidationException(
198205
"Notification type is set to [MAIL] but the given notificatioMeta contains an invalid e-mail ["

0 commit comments

Comments
 (0)