package mx.nic.rdap.server.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import mx.nic.rdap.db.AutnumDAO;
import mx.nic.rdap.db.model.AutnumModel;
import mx.nic.rdap.server.RdapResult;
import mx.nic.rdap.server.RdapServlet;
import mx.nic.rdap.server.Util;
import mx.nic.rdap.server.db.DatabaseSession;
import mx.nic.rdap.server.exception.RequestHandleException;
import mx.nic.rdap.server.result.AutnumResult;

@WebServlet(name = "autnum", urlPatterns = { "/autnum/*" })
public class AutnumServlet extends RdapServlet {

	private static final long serialVersionUID = 1L;

	public AutnumServlet() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mx.nic.rdap.server.RdapServlet#doRdapGet(javax.servlet.http.
	 * HttpServletRequest)
	 */
	@Override
	protected RdapResult doRdapGet(HttpServletRequest httpRequest)
			throws RequestHandleException, IOException, SQLException {
		AutnumRequest request = new AutnumRequest(Util.getRequestParams(httpRequest)[0]);
		AutnumDAO autnum = null;
		String username = httpRequest.getRemoteUser();

		try (Connection con = DatabaseSession.getRdapConnection()) {
			autnum = AutnumModel.getByRange(request.getAutnum(), con);
		}
		return new AutnumResult(autnum, username);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mx.nic.rdap.server.RdapServlet#doRdapHead(javax.servlet.http.
	 * HttpServletRequest)
	 */
	@Override
	protected RdapResult doRdapHead(HttpServletRequest request)
			throws RequestHandleException, IOException, SQLException {
		throw new RequestHandleException(501, "Not implemented yet.");
	}

	private class AutnumRequest {

		private Long autnum;

		public AutnumRequest(String autnum) {
			super();
			this.autnum = Long.parseLong(autnum);
		}

		public Long getAutnum() {
			return autnum;
		}
	}

}

@@ -448,24 +448,88 @@
 seSession;%0D%0A
+import mx.nic.rdap.server.exception.MalformedRequestException;%0D%0A
 import mx.ni
@@ -1898,16 +1898,49 @@
  autnum)
+ throws MalformedRequestException
  %7B%0D%0A%09%09%09s
@@ -1948,16 +1948,27 @@
 per();%0D%0A
+%09%09%09try %7B%0D%0A%09
 %09%09%09this.
@@ -1997,24 +1997,144 @@
 g(autnum);%0D%0A
+%09%09%09%7D catch (NumberFormatException e) %7B%0D%0A%09%09%09%09throw new MalformedRequestException(%22Autnum must be an Int number%22);%0D%0A%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%0D%0A%09%09pub

Add validation when parsing long for autnum