package mx.nic.rdap.server.result;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.servlet.http.HttpServletRequest;

import mx.nic.rdap.server.RdapResult;
import mx.nic.rdap.server.UserRequestInfo;

/**
 * A result from a exception generated in a request.
 */
public class ExceptionResult extends UserRequestInfo implements RdapResult {

	private final static Logger logger = Logger.getLogger(ExceptionResult.class.getName());

	private String errorCode;
	private String errorTitle;
	private String errorDescription;

	/**
	 * Read the request and fill the error data
	 * 
	 * @param httpRequest
	 */
	public ExceptionResult(HttpServletRequest httpRequest) {
		errorCode = httpRequest.getAttribute("javax.servlet.error.status_code").toString();
		if (errorCode != null) {
			switch (errorCode) {
			case "401":
				errorTitle = "Forbidden request";
				errorDescription = "Must loggin to process the request";
				break;
			case "403":
				errorTitle = "Forbidden request";
				errorDescription = httpRequest.getAttribute("javax.servlet.error.message").toString()+".Verify User role";
				break;
			case "404":
				errorTitle = "Object not found";
				errorDescription = httpRequest.getAttribute("javax.servlet.error.message").toString();
				break;
			case "422":
				errorTitle = "Forbidden request";
				errorDescription = httpRequest.getAttribute("javax.servlet.error.message").toString();
				break;
			case "500":
				errorTitle = "Internal server error";
				errorDescription = httpRequest.getAttribute("javax.servlet.error.message").toString();
				break;
			}
			logger.log(Level.WARNING, errorCode + ":" + httpRequest.getAttribute("javax.servlet.error.message").toString());
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mx.nic.rdap.server.RdapResult#toJson()
	 */
	@Override
	public JsonObject toJson() {
		JsonObjectBuilder object = Json.createObjectBuilder();
		if (errorCode != null) {
			object.add("erroCode", errorCode);
		}
		if (errorTitle != null) {
			object.add("title", errorTitle);
		}
		if (errorCode != null && errorDescription != null) {
			if (errorCode.compareTo("500") != 0)
				object.add("description", errorDescription);
		}
		return object.build();
	}

}

@@ -1399,33 +1399,36 @@
 itle = %22
-Forbidden request
+Unprocessable Entity
 %22;%0A%09%09%09%09e

Fix Unprocessable Entity exception title