forked from aayushgoyal1/rest-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationValidator.java
146 lines (129 loc) · 5.17 KB
/
ApplicationValidator.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.naming.directory.SearchControls;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
@Component(value="applicationValidator")
public class ApplicationValidator implements Validator{
protected final Logger logger = Logger.getLogger(getClass());
public ApplicationValidator(){
}
@Autowired
LdapConfiguration ldapConfig = new LdapConfiguration();
List<String> validatedUsers = new ArrayList<String>();
/**
* This Validator validates *just* Applications
*/
public boolean supports(Class<?> clazz) {
return ApplicationExternal.class.equals(clazz);
}
@Override
public void validate(Object arg0, Errors arg1) {
logger.debug("** IN VALIDATE - " + arg0 + ", ERRORS: " + arg1);
ApplicationExternal application = (ApplicationExternal) arg0;
Errors errors = (Errors) arg1;
String techOwner = application.getTechnicalOwner();
String businessOwner = application.getBusinessOwner();
String otherContact = application.getOtherContact();
String supportContact = application.getSupportContact();
if(!StringUtils.hasText(techOwner)){
logger.debug("Technical owner required." );
errors.rejectValue("technicalOwner", "Technical owner required.");
return;
}
if(!StringUtils.hasText(businessOwner)){
logger.debug("Business owner required." );
errors.rejectValue("businessOwner", "Business owner required.");
return;
}
if(!StringUtils.hasText(application.getName() )){
logger.debug("Application name required." );
errors.rejectValue("name", "Application name required.");
return;
}
if(!StringUtils.hasText(application.getDescription() )){
logger.debug("Application name required." );
errors.rejectValue("description", "Application description required.");
return;
}
if(application.getPlannedDeploymentDate()!=null && !application.getPlannedDeploymentDate().equals("")){ // validate, this field is not mandatory
logger.debug("Validating planned production deployment date" );
boolean isValid = ApplicationUtils.isDateValid(application.getPlannedDeploymentDate());
logger.debug("Validating planned production deployment date: isvalid "+ isValid );
if(!isValid){
logger.debug("Planned Production Deployment Date is invalid." );
errors.rejectValue("plannedDeploymentDate", "Planned Production Deployment Date is invalid.");
}
if(!ApplicationUtils.isFutureDate(application.getPlannedDeploymentDate())){
logger.debug("Past date is not allowed for Planned Production Deployment." );
errors.rejectValue("plannedDeploymentDate", "Past date is not allowed for Planned Production Deployment");
return;
}
}
if(application.getMaxLoginsPerHour() == null){
logger.debug("Max number logins per hour required." );
errors.rejectValue("maxLoginsPerHour", "Max number logins per hour required.");
return;
}
if(application.getMaxLoginsPerHour() != null && application.getMaxLoginsPerHour().intValue() <= 0){
logger.debug("Max number logins must be > 0" );
errors.rejectValue("maxLoginsPerHour", "Max number logins must be > 0");
return;
}
if(application.getNumberUsers() == null){
logger.debug("Number users required." );
errors.rejectValue("numberUsers", "Number users required.");
return;
}
if(application.getNumberUsers() != null && application.getNumberUsers() .intValue() <= 0){
logger.debug("Number of users must be > 0" );
errors.rejectValue("numberUsers", "Number users must be > 0");
return;
}
}
public Boolean validUser(String internet){
try{
/* If already validated. */
if (validatedUsers.contains(internet)) return Boolean.TRUE;
logger.debug("Validating user = "+internet);
LdapAttributesMapper attributeMapper = new LdapAttributesMapper();
String searchBase=Constants.getString("ldap.client.base");
String searchFilter=Constants.getString("ldap.client.filter");
searchFilter = searchFilter.replace((CharSequence)"{0}", (CharSequence)internet);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setReturningAttributes(new String[] {"preferredIdentity", "uid"});
List<Object> userList = (List<Object>) ldapConfig.ldapTemplate().search(searchBase, searchFilter, controls, attributeMapper);
if (userList != null && userList.size() > 0) {
logger.info("Found valid user = "+internet);
validatedUsers.add(internet);
return true;
}
else{
logger.info("Invalid user = "+internet);
return false;
}
}catch(Exception ex){
ex.printStackTrace();
logger.error(ex.getClass().getName());
return false;
}
}
public Boolean validOtherContacts(String otherContact){
try{
StringTokenizer lineTok = new StringTokenizer(otherContact,",");
while(lineTok.hasMoreTokens()){
String email = lineTok.nextToken().trim();
if(!validUser(email))return false;
}
return true;
}catch(Exception ex){
return false;
}
}
}