-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBaseValidations.java
More file actions
86 lines (80 loc) · 4.29 KB
/
BaseValidations.java
File metadata and controls
86 lines (80 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.skyflow.utils.validations;
import com.skyflow.config.Credentials;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.ErrorMessage;
import com.skyflow.errors.SkyflowException;
import com.skyflow.logs.ErrorLogs;
import com.skyflow.utils.BaseConstants;
import com.skyflow.utils.BaseUtils;
import com.skyflow.utils.logger.LogUtil;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BaseValidations {
BaseValidations() {
}
public static void validateCredentials(Credentials credentials) throws SkyflowException {
int nonNullMembers = 0;
String path = credentials.getPath();
String credentialsString = credentials.getCredentialsString();
String token = credentials.getToken();
String apiKey = credentials.getApiKey();
String context = credentials.getContext();
ArrayList<String> roles = credentials.getRoles();
if (path != null) nonNullMembers++;
if (credentialsString != null) nonNullMembers++;
if (token != null) nonNullMembers++;
if (apiKey != null) nonNullMembers++;
if (nonNullMembers > 1) {
LogUtil.printErrorLog(ErrorLogs.MULTIPLE_TOKEN_GENERATION_MEANS_PASSED.getLog());
throw new SkyflowException(
ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.MultipleTokenGenerationMeansPassed.getMessage()
);
} else if (nonNullMembers < 1) {
LogUtil.printErrorLog(ErrorLogs.NO_TOKEN_GENERATION_MEANS_PASSED.getLog());
throw new SkyflowException(
ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.NoTokenGenerationMeansPassed.getMessage()
);
} else if (path != null && path.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.EMPTY_CREDENTIALS_PATH.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyCredentialFilePath.getMessage());
} else if (credentialsString != null && credentialsString.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.EMPTY_CREDENTIALS_STRING.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyCredentialsString.getMessage());
} else if (token != null && token.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.EMPTY_TOKEN_VALUE.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyToken.getMessage());
} else if (apiKey != null) {
if (apiKey.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.EMPTY_API_KEY_VALUE.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyApikey.getMessage());
} else {
Pattern pattern = Pattern.compile(BaseConstants.API_KEY_REGEX);
Matcher matcher = pattern.matcher(apiKey);
if (!matcher.matches()) {
LogUtil.printErrorLog(ErrorLogs.INVALID_API_KEY.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.InvalidApikey.getMessage());
}
}
} else if (roles != null) {
if (roles.isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.EMPTY_ROLES.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyRoles.getMessage());
} else {
for (int index = 0; index < roles.size(); index++) {
String role = roles.get(index);
if (role == null || role.trim().isEmpty()) {
LogUtil.printErrorLog(BaseUtils.parameterizedString(
ErrorLogs.EMPTY_OR_NULL_ROLE_IN_ROLES.getLog(), Integer.toString(index)
));
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyRoleInRoles.getMessage());
}
}
}
}
if (context != null && context.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.EMPTY_OR_NULL_CONTEXT.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyContext.getMessage());
}
}
}