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) {
		}
	}
}
最近下载更多
阿文sjw  LV1 2023年10月8日
微信网友_6633935955365888  LV1 2023年9月3日
聪明的小叶子  LV1 2023年6月12日
bigboos  LV1 2023年4月9日
rcrcrc  LV1 2023年2月14日
longtaolee  LV11 2022年11月28日
jjbbjb  LV1 2022年11月15日
lonhjk  LV4 2022年11月12日
T  LV5 2022年11月5日
king9ku 2022年10月13日
暂无贡献等级
最近浏览更多
747932094 3月1日
暂无贡献等级
云易科技  LV1 2月7日
上理小z  LV1 1月17日
康先生ee 1月2日
暂无贡献等级
sscssc123456 2023年12月27日
暂无贡献等级
杨火火  LV1 2023年12月16日
逍遥2020  LV11 2023年12月13日
fenjian  LV3 2023年12月12日
白菜白菜菜 2023年11月19日
暂无贡献等级
2652378774  LV7 2023年11月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友