<commit_before>package mx.nic.rdap.server.servlet;

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

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

import mx.nic.rdap.core.exception.UnprocessableEntityException;
import mx.nic.rdap.db.exception.InvalidValueException;
import mx.nic.rdap.db.model.DomainModel;
import mx.nic.rdap.db.struct.SearchResultStruct;
import mx.nic.rdap.server.RdapConfiguration;
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.MalformedRequestException;
import mx.nic.rdap.server.exception.RequestHandleException;
import mx.nic.rdap.server.result.DomainSearchResult;
import mx.nic.rdap.server.result.OkResult;

@WebServlet(name = "domains", urlPatterns = { "/domains" })
public class DomainSearchServlet extends RdapServlet {

	private static final long serialVersionUID = 1L;

	public final String DOMAIN_NAME = "name";

	public final String NAMESERVER_NAME = "nsLdhName";

	public final String NAMESERVER_IP = "nsIp";

	public DomainSearchServlet() throws IOException {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mx.nic.rdap.server.RdapServlet#doRdapGet(javax.servlet.http.
	 * HttpServletRequest)
	 */
	@Override
	protected RdapResult doRdapGet(HttpServletRequest httpRequest)
			throws RequestHandleException, IOException, SQLException {
		DomainSearchRequest request;
		try {
			request = new DomainSearchRequest(httpRequest);
		} catch (UnprocessableEntityException e) {
			throw new RequestHandleException(e.getHttpResponseStatusCode(), e.getMessage());
		}

		SearchResultStruct result = new SearchResultStruct();
		String username = httpRequest.getRemoteUser();
		boolean useNameserverAsAttribute=RdapConfiguration.useNameserverAsDomainAttribute();
		Integer resultLimit = RdapConfiguration.getMaxNumberOfResultsForUnauthenticatedUser();// Default
		try (Connection connection = DatabaseSession.getRdapConnection()) {
			resultLimit = Util.getMaxNumberOfResultsForUser(username, connection);
			String domain = request.getValue();
			if (IDN.toASCII(domain) != domain) {
				domain = IDN.toASCII(domain);
			}
			switch (request.getParameter()) {
			case DOMAIN_NAME:
				// Gets domain by its name with zone
				if (request.getValue().contains(".")) {
					String[] split = request.getValue().split("\\.", 2);
					domain = split[0];
					if (IDN.toASCII(domain) != domain) {
						domain = IDN.toASCII(domain);
					}
					String zone = split[1];

					try {
						result = DomainModel.searchByName(domain, zone, resultLimit,useNameserverAsAttribute, connection);
					} catch (InvalidValueException e) {
						e.printStackTrace();
					}
				} else {

					// Search domain by it´s name without zone.
					result = DomainModel.searchByName(domain, resultLimit,useNameserverAsAttribute, connection);
				}
				break;
			case NAMESERVER_NAME:
				// Gets´s domain by it´s Nameserver name
				result = DomainModel.searchByNsLdhName(domain, resultLimit,useNameserverAsAttribute, connection);
				break;
			case NAMESERVER_IP:
				// Get´s domain by it´s Nameserver Ip
				result = DomainModel.searchByNsIp(domain, resultLimit,useNameserverAsAttribute, connection);
				break;
			default:

				break;
			}

		}<commit_msg>Add error not implemented if server has no zones configurated<commit_after>package mx.nic.rdap.server.servlet;

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

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

import mx.nic.rdap.core.exception.UnprocessableEntityException;
import mx.nic.rdap.db.exception.InvalidValueException;
import mx.nic.rdap.db.model.DomainModel;
import mx.nic.rdap.db.model.ZoneModel;
import mx.nic.rdap.db.struct.SearchResultStruct;
import mx.nic.rdap.server.RdapConfiguration;
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.MalformedRequestException;
import mx.nic.rdap.server.exception.RequestHandleException;
import mx.nic.rdap.server.result.DomainSearchResult;
import mx.nic.rdap.server.result.OkResult;

@WebServlet(name = "domains", urlPatterns = { "/domains" })
public class DomainSearchServlet extends RdapServlet {

	private static final long serialVersionUID = 1L;

	public final String DOMAIN_NAME = "name";

	public final String NAMESERVER_NAME = "nsLdhName";

	public final String NAMESERVER_IP = "nsIp";

	public DomainSearchServlet() throws IOException {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mx.nic.rdap.server.RdapServlet#doRdapGet(javax.servlet.http.
	 * HttpServletRequest)
	 */
	@Override
	protected RdapResult doRdapGet(HttpServletRequest httpRequest)
			throws RequestHandleException, IOException, SQLException {
		DomainSearchRequest request;
		try {
			request = new DomainSearchRequest(httpRequest);
		} catch (UnprocessableEntityException e) {
			throw new RequestHandleException(e.getHttpResponseStatusCode(), e.getMessage());
		}

		SearchResultStruct result = new SearchResultStruct();
		String username = httpRequest.getRemoteUser();
		boolean useNameserverAsAttribute = RdapConfiguration.useNameserverAsDomainAttribute();
		Integer resultLimit = RdapConfiguration.getMaxNumberOfResultsForUnauthenticatedUser();// Default
		try (Connection connection = DatabaseSession.getRdapConnection()) {
			resultLimit = Util.getMaxNumberOfResultsForUser(username, connection);
			String domain = request.getValue();
			if (IDN.toASCII(domain) != domain) {
				domain = IDN.toASCII(domain);
			}
			switch (request.getParameter()) {
			case DOMAIN_NAME:
				// Gets domain by its name with zone
				if (request.getValue().contains(".")) {
					String[] split = request.getValue().split("\\.", 2);
					domain = split[0];
					if (IDN.toASCII(domain) != domain) {
						domain = IDN.toASCII(domain);
					}
					String zone = split[1];

					if (ZoneModel.getValidZoneIds() == null || ZoneModel.getValidZoneIds().isEmpty()) {
						throw new RequestHandleException(501, "Not implemented yet.");
					}

					try {
						result = DomainModel.searchByName(domain, zone, resultLimit, useNameserverAsAttribute,
								connection);
					} catch (InvalidValueException e) {
						e.printStackTrace();
					}
				} else {

					// Search domain by it´s name without zone.
					result = DomainModel.searchByName(domain, resultLimit, useNameserverAsAttribute, connection);
				}
				break;
			case NAMESERVER_NAME:
				// Gets´s domain by it´s Nameserver name
				result = DomainModel.searchByNsLdhName(domain, resultLimit, useNameserverAsAttribute, connection);
				break;
			case NAMESERVER_IP:
				// Get´s domain by it´s Nameserver Ip
				result = DomainModel.searchByNsIp(domain, resultLimit, useNameserverAsAttribute, connection);
				break;
			default:

				break;
			}

		}
{'fraction_non_alphanumeric': 0.10735979658143806, 'fraction_numerical': 0.0015538917926260771, 'mean_word_length': 4.514018691588785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2560984', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2150, 'n_words': 440}