001/*
002 * Copyright 2015-2020 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns.dnsname;
012
013import org.minidns.dnslabel.DnsLabel;
014
015public abstract class InvalidDnsNameException extends IllegalStateException {
016
017    private static final long serialVersionUID = 1L;
018
019    protected final String ace;
020
021    protected InvalidDnsNameException(String ace) {
022        this.ace = ace;
023    }
024
025    public static class LabelTooLongException extends InvalidDnsNameException {
026        /**
027         * 
028         */
029        private static final long serialVersionUID = 1L;
030
031        private final String label;
032
033        public LabelTooLongException(String ace, String label) {
034            super(ace);
035            this.label = label;
036        }
037
038        @Override
039        public String getMessage() {
040            return "The DNS name '" + ace + "' contains the label '" + label
041                    + "' which exceeds the maximum label length of " + DnsLabel.MAX_LABEL_LENGTH_IN_OCTETS + " octets by "
042                    + (label.length() - DnsLabel.MAX_LABEL_LENGTH_IN_OCTETS) + " octets.";
043        }
044    }
045
046    public static class DNSNameTooLongException extends InvalidDnsNameException {
047        /**
048         * 
049         */
050        private static final long serialVersionUID = 1L;
051
052        private final byte[] bytes;
053
054        public DNSNameTooLongException(String ace, byte[] bytes) {
055            super(ace);
056            this.bytes = bytes;
057        }
058
059        @Override
060        public String getMessage() {
061            return "The DNS name '" + ace + "' exceeds the maximum name length of "
062                    + DnsName.MAX_DNSNAME_LENGTH_IN_OCTETS + " octets by "
063                    + (bytes.length - DnsName.MAX_DNSNAME_LENGTH_IN_OCTETS) + " octets.";
064        }
065    }
066}