forked from posabsolute/jQuery-Validation-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAjaxTestServer.java
More file actions
202 lines (163 loc) · 5.65 KB
/
Copy pathAjaxTestServer.java
File metadata and controls
202 lines (163 loc) · 5.65 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
/**
* This java class implements a basic HTTP server aimed at testing the
* jQuery.validate AJAX capabilities. Note that this file shouldn't be taken as
* best practice for java back end development. There are much better frameworks
* to do server side processing in Java, for instance, the Play Framework.
*
* @author Olivier Refalo
*/
public class AjaxTestServer extends NanoHTTPD {
private static final int PORT = 9173;
public static final String MIME_JSON = "application/json";
public AjaxTestServer() throws IOException {
super(PORT);
}
public class AjaxValidationFieldResponse {
// the html field id
private String id;
// true - field is valid
private Boolean status;
public AjaxValidationFieldResponse(String fieldId, Boolean s) {
id = fieldId;
status = s;
}
public String toString() {
StringBuffer json = new StringBuffer();
json.append("[\"").append(id).append("\",").append(status.toString()).append(']');
return json.toString();
}
}
public class AjaxValidationFormResponse {
// the html field id
private String id;
// true, the field is valid : the client logic displays a green prompt
// false, the field is invalid : the client logic displays a red prompt
private Boolean status;
// either the string to display in the prompt or an error
// selector to pick the error message from the translation.js
private String error;
public AjaxValidationFormResponse(String fieldId, Boolean s, String err) {
id = fieldId;
status = s;
error = err;
}
public String toString() {
StringBuffer json = new StringBuffer();
json.append("[\"").append(id).append("\",").append(status).append(",\"").append(error.toString()).append("\"]");
return json.toString();
}
}
public Response serve(String uri, String method, Properties header, Properties parms) {
// field validation
if ("/ajaxValidateFieldUser".equals(uri)) {
System.out.println("-> " + method + " '" + uri + "'");
// purposely sleep to let the UI display the AJAX loading prompts
sleep(3000);
String fieldId = parms.getProperty("fieldId");
String fieldValue = parms.getProperty("fieldValue");
AjaxValidationFieldResponse result = new AjaxValidationFieldResponse(fieldId, new Boolean(
"karnius".equals(fieldValue)));
// return ["fieldid", true or false]
return new NanoHTTPD.Response(HTTP_OK, MIME_JSON, result.toString());
}
// field validation
else if ("/ajaxValidateFieldName".equals(uri)) {
System.out.println("-> " + method + " '" + uri + "'");
// purposely sleep to let the UI display the AJAX loading prompts
sleep(3000);
String fieldId = parms.getProperty("fieldId");
String fieldValue = parms.getProperty("fieldValue");
AjaxValidationFieldResponse result = new AjaxValidationFieldResponse(fieldId, new Boolean(
"duncan".equals(fieldValue)));
// return ["fieldid", true or false]
return new NanoHTTPD.Response(HTTP_OK, MIME_JSON, result.toString());
}
// form validation, we get the form data (read: all the form fields), we
// return ALL the errors
else if ("/ajaxSubmitForm".equals(uri)) {
System.out.println("-> " + method + " '" + uri + "'");
// purposely sleep to let the UI display the AJAX loading prompts
sleep(1000);
ArrayList<AjaxValidationFormResponse> errors = new ArrayList<AjaxValidationFormResponse>();
String user = parms.getProperty("user");
String firstname = parms.getProperty("firstname");
String email = parms.getProperty("email");
if (!"karnius".equals(user)) {
// error selector: indirection to the error message -> done in
// javascript
errors.add(new AjaxValidationFormResponse("user", false, "ajaxUserCall"));
}
if (!"duncan".equals(firstname)) {
errors.add(new AjaxValidationFormResponse("firstname", false, "Please enter DUNCAN"));
} else {
errors.add(new AjaxValidationFormResponse("firstname", true, "You got it!"));
}
if (!"someone@here.com".equals(email)) {
errors.add(new AjaxValidationFormResponse("email", false, "The email doesn't match someone@here.com"));
}
String json = "true";
if (errors.size() != 0) {
json = genJSON(errors);
}
return new NanoHTTPD.Response(HTTP_OK, MIME_JSON, json);
}
return super.serve(uri, method, header, parms);
}
/**
* Form validation error generation Generates a list of errors
*
* @param errors
* @return [["field1", "this field is required<br/>
* it doesn't match<br/>
* "],["field2","another error"]]
*/
private String genJSON(ArrayList<AjaxValidationFormResponse> errors) {
StringBuffer json = new StringBuffer();
json.append('[');
for (int i = 0; i < errors.size(); i++) {
AjaxValidationFormResponse err = errors.get(i);
json.append(err.toString());
if (i < errors.size() - 1) {
json.append(',');
}
}
json.append(']');
return json.toString();
}
/**
* Sleeps the current thread for the given delay
*
* @param duration
* in milliseconds
* */
private void sleep(long duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* Application start point, starts the httpd server
*
* @param args
* command line arguments
*/
public static void main(String[] args) {
try {
new AjaxTestServer();
} catch (IOException ioe) {
System.err.println("Couldn't start server:\n" + ioe);
System.exit(-1);
}
System.out.println("Listening on port " + PORT + ". Hit Enter to stop.\nPlease open your browsers to http://localhost:"
+ PORT);
try {
System.in.read();
} catch (Throwable t) {
}
}
}