ServerResponse.java 1.88 KB
package com.taover.heartbeat.bean;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;

public class ServerResponse {
	public static String RESPONSE_CODE_OK = "ok";
	
	private boolean isOverdue;
	private boolean isCodeOk;
	private String responseContent;
	private Exception exception;
	
	private ServerResponse(boolean isOverdue, boolean isCodeOk, String responseContent, Exception e) {
		super();
		this.isOverdue = isOverdue;
		this.isCodeOk = isCodeOk;
		this.responseContent = responseContent;
		this.exception = e;
	}
	
	public boolean isOverdue() {
		return isOverdue;
	}
	public void setOverdue(boolean isOverdue) {
		this.isOverdue = isOverdue;
	}
	public boolean isCodeOk() {
		return isCodeOk;
	}
	public void setCodeOk(boolean isCodeOk) {
		this.isCodeOk = isCodeOk;
	}
	public String getResponseContent() {
		return responseContent;
	}
	public void setResponseContent(String responseContent) {
		this.responseContent = responseContent;
	}
	public Exception getException() {
		return exception;
	}
	public void setException(Exception exception) {
		this.exception = exception;
	}

	public static ServerResponse createByJSONString(String response) {
		if(StringUtils.isBlank(response)) {
			return new ServerResponse(false, false, response, null);
		}else {
			try {
				JSONObject data = JSONObject.parseObject(response);
				String code = data.getString("code");
				if(RESPONSE_CODE_OK.equals(code.toLowerCase())) {
					return new ServerResponse(false, true, response, null);
				}else {
					return new ServerResponse(false, false, response, null);
				}
			}catch (Exception e) {
				return new ServerResponse(false, false, response, e);
			}
		}
	}

	public static ServerResponse createOverdue() {
		return new ServerResponse(false, false, "", null);
	}

	public static ServerResponse createException(Exception e) {
		return new ServerResponse(false, false, "", e);
	}
}