All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aoindustries.aoserv.client.DNSZoneTable Maven / Gradle / Ivy

There is a newer version: 1.92.0
Show newest version
/*
 * aoserv-client - Java client for the AOServ platform.
 * Copyright (C) 2001-2013, 2016  AO Industries, Inc.
 *     [email protected]
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aoserv-client.
 *
 * aoserv-client is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aoserv-client is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aoserv-client.  If not, see .
 */
package com.aoindustries.aoserv.client;

import com.aoindustries.aoserv.client.validator.DomainName;
import com.aoindustries.aoserv.client.validator.InetAddress;
import com.aoindustries.io.TerminalWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @see  DNSZone
 *
 * @author  AO Industries, Inc.
 */
final public class DNSZoneTable extends CachedTableStringKey {

	DNSZoneTable(AOServConnector connector) {
		super(connector, DNSZone.class);
	}

	private static final OrderBy[] defaultOrderBy = {
		new OrderBy(DNSZone.COLUMN_ZONE_name, ASCENDING)
	};
	@Override
	OrderBy[] getDefaultOrderBy() {
		return defaultOrderBy;
	}

	@Override
	public DNSZone get(String zone) throws IOException, SQLException {
		return getUniqueRow(DNSZone.COLUMN_ZONE, zone);
	}

	private List getDNSTLDs() throws IOException, SQLException {
		List tlds=connector.getDnsTLDs().getRows();
		List names=new ArrayList<>(tlds.size());
		for(DNSTLD tld : tlds) names.add(tld.getDomain());
		return names;
	}

	void addDNSZone(Package packageObj, String zone, InetAddress ip, int ttl) throws IOException, SQLException {
		connector.requestUpdateIL(true, AOServProtocol.CommandID.ADD, SchemaTable.TableID.DNS_ZONES, packageObj.name, zone, ip.toString(), ttl);
	}

	/**
	 * Checks the formatting for a DNS zone.  The format of a DNS zone must be name.tld.
	 */
	public boolean checkDNSZone(String zone) throws IOException, SQLException {
		return checkDNSZone(zone, getDNSTLDs());
	}

	/**
	 * Checks the formatting for a DNS zone.  The format of a DNS zone must be name.tld.
	 */
	public static boolean checkDNSZone(String zone, List tlds) {
		int zoneLen=zone.length();

		String shortestName=null;
		int len=tlds.size();
		for(int c=0;cDNSZone.
	 *
	 * @return  the zone in the format name.tld.
	 */
	public static String getDNSZoneForHostname(String hostname, List tlds) throws IllegalArgumentException, IOException, SQLException {
		int hlen = hostname.length();
		if (hlen>0 && hostname.charAt(hlen-1)=='.') {
			hostname = hostname.substring(0, --hlen);
		}
		String longestTld = null;
		int tldlen = tlds.size();
		for (int i = 0; i < tldlen; i++) {
			DomainName o = tlds.get(i);
			String tld='.'+o.toString(); // No dot at end

			int len = tld.length();
			if (hlen>=len && hostname.substring(hlen-len).equals(tld)) {
				if(longestTld==null || tld.length()>longestTld.length()) {
					longestTld=tld;
				}
			}
		}
		if (longestTld==null) throw new IllegalArgumentException("Unable to determine top level domain for hostname: "+hostname);

		String zone = hostname.substring(0, hlen-longestTld.length());
		int startpos = zone.lastIndexOf('.');
		if (startpos>=0) zone = zone.substring(startpos+1);
		return zone+longestTld+".";
	}

	List getDNSZones(Package packageObj) throws IOException, SQLException {
		return getIndexedRows(DNSZone.COLUMN_PACKAGE, packageObj.name);
	}

	/**
	 * Gets the hostname for a fully qualified hostname.  Gets a hostname in name.tld. format.
	 *
	 * @exception  IllegalArgumentException  if hostname cannot be resolved to a top level domain
	 */
	public static String getHostTLD(String hostname, List tlds) throws IllegalArgumentException {
		int hostnameLen=hostname.length();
		if (hostnameLen>0 && hostname.charAt(hostnameLen-1)!='.') {
			hostname = hostname+".";
			hostnameLen++;
		}

		int len=tlds.size();
		for(int c=0;c 'z') && (ch < '0' || ch > '9') && ch != '-') return false;
			if(ch>='0' && ch<='9') numCount++;
		}
		if(numCount==len) return false;

		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy