package mx.nic.rdap.server.servlet;

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

import mx.nic.rdap.core.db.IpNetwork;
import mx.nic.rdap.core.ip.AddressBlock;
import mx.nic.rdap.core.ip.IpAddressFormatException;
import mx.nic.rdap.db.exception.RdapDataAccessException;
import mx.nic.rdap.db.exception.http.BadRequestException;
import mx.nic.rdap.db.exception.http.HttpException;
import mx.nic.rdap.db.service.DataAccessService;
import mx.nic.rdap.db.spi.IpNetworkDAO;
import mx.nic.rdap.server.result.IpResult;
import mx.nic.rdap.server.result.RdapResult;
import mx.nic.rdap.server.util.Util;

@WebServlet(name = "ip", urlPatterns = { "/ip/*" })
public class IpNetworkServlet extends DataAccessServlet<IpNetworkDAO> {

	private static final long serialVersionUID = 1L;

	/**
	 * Constant value to set the maximum params expected in the URI, this servlet expects: ip, cidr
	 */
	private static final int MAX_PARAMS_EXPECTED = 2;

	@Override
	protected IpNetworkDAO initAccessDAO() throws RdapDataAccessException {
		return DataAccessService.getIpNetworkDAO();
	}

	@Override
	protected String getServedObjectName() {
		return "IP network";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mx.nic.rdap.server.RdapServlet#doRdapGet(javax.servlet.http.
	 * HttpServletRequest)
	 */
	@Override
	protected RdapResult doRdapDaGet(HttpServletRequest httpRequest, IpNetworkDAO dao)
			throws HttpException, RdapDataAccessException {
		IpRequest request = new IpRequest(Util.getRequestParams(httpRequest, MAX_PARAMS_EXPECTED));
		AddressBlock block;
		IpNetwork network;

		try {
			block = new AddressBlock(request.getIp(), request.hasCidr() ? request.getCidr() : null);
		} catch (IpAddressFormatException e) {
			throw new BadRequestException(e);
		}

		network = dao.getByAddressBlock(block);
		if (network == null) {
			return null;
		}

		return new IpResult(httpRequest.getHeader("Host"), httpRequest.getContextPath(), network,
				Util.getUsername(httpRequest));
	}

	private class IpRequest {

		private String ip;
		private Integer cidr;

		public IpRequest(String[] params) {
			super();
			this.ip = params[0];

			if (params.length > 1) {
				cidr = Integer.parseInt(params[1]);
			}
		}

		public String getIp() {
			return ip;
		}

		public Integer getCidr() {
			return cidr;
		}

		public boolean hasCidr() {
			return cidr != null;
		}

	}

}

@@ -1597,16 +1597,245 @@
 TED));%0D%0A
+%09%09// Validate cidr parsed (if present), extra validations are made at constructor of AddressBlock%0D%0A%09%09if (request.hasCidr()) %7B%0D%0A%09%09%09if (request.getCidr() %3C 0) %7B%0D%0A%09%09%09%09throw new BadRequestException(%22Invalid cidr value%22);%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A
 %09%09Addres
@@ -2474,24 +2474,36 @@
 gth %3E 1) %7B%0D%0A
+%09%09%09%09try %7B%0D%0A%09
 %09%09%09%09cidr = I
@@ -2527,24 +2527,89 @@
 arams%5B1%5D);%0D%0A
+%09%09%09%09%7D catch (NumberFormatException e) %7B%0D%0A%09%09%09%09%09cidr = -1;%0D%0A%09%09%09%09%7D%0D%0A
 %09%09%09%7D%0D%0A%09%09%7D%0D%0A%0D

Throw BadRequestException when an invalid cidr is used, fixes #61