src/jdk.dns.client/share/classes/jdk/dns/client/internal/ResourceClassType.java
branchaefimov-dns-client-branch
changeset 58971 465a15dd6bed
parent 58870 35c438a6d45c
equal deleted inserted replaced
58970:027e4cb87353 58971:465a15dd6bed
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package jdk.dns.client.internal;
    26 package jdk.dns.client.internal;
    27 
    27 
    28 import jdk.dns.client.AddressFamily;
    28 import jdk.dns.client.ex.DnsResolverException;
    29 import jdk.dns.client.ex.DnsInvalidAttributeIdentifierException;
       
    30 
    29 
    31 public class ResourceClassType {
    30 public class ResourceClassType {
    32     int rrclass;
    31     int rrclass;
    33     int rrtype;
    32     int rrtype;
    34 
    33 
    47 
    46 
    48     public static ResourceClassType fromAddressFamily(AddressFamily addressFamily) {
    47     public static ResourceClassType fromAddressFamily(AddressFamily addressFamily) {
    49         ResourceClassType[] cts;
    48         ResourceClassType[] cts;
    50         try {
    49         try {
    51             cts = ResourceClassType.attrIdsToClassesAndTypes(typeFromAddressFamily(addressFamily));
    50             cts = ResourceClassType.attrIdsToClassesAndTypes(typeFromAddressFamily(addressFamily));
    52         } catch (DnsInvalidAttributeIdentifierException e) {
    51         } catch (DnsResolverException e) {
    53             return new ResourceClassType(ResourceRecord.QTYPE_STAR);
    52             return new ResourceClassType(ResourceRecord.TYPE_ANY);
    54         }
    53         }
    55         return ResourceClassType.getClassAndTypeToQuery(cts);
    54         return ResourceClassType.getClassAndTypeToQuery(cts);
    56     }
    55     }
    57 
    56 
    58     private static String[] typeFromAddressFamily(AddressFamily addressFamily) {
    57     private static String[] typeFromAddressFamily(AddressFamily addressFamily) {
    66         }
    65         }
    67     }
    66     }
    68 
    67 
    69 
    68 
    70     public static ResourceClassType[] attrIdsToClassesAndTypes(String[] attrIds)
    69     public static ResourceClassType[] attrIdsToClassesAndTypes(String[] attrIds)
    71             throws DnsInvalidAttributeIdentifierException {
    70             throws DnsResolverException {
    72         if (attrIds == null) {
    71         if (attrIds == null) {
    73             return null;
    72             return null;
    74         }
    73         }
    75         ResourceClassType[] cts = new ResourceClassType[attrIds.length];
    74         ResourceClassType[] cts = new ResourceClassType[attrIds.length];
    76 
    75 
    79         }
    78         }
    80         return cts;
    79         return cts;
    81     }
    80     }
    82 
    81 
    83     private static ResourceClassType fromAttrId(String attrId)
    82     private static ResourceClassType fromAttrId(String attrId)
    84             throws DnsInvalidAttributeIdentifierException {
    83             throws DnsResolverException {
    85 
    84 
    86         if (attrId.isEmpty()) {
    85         if (attrId.isEmpty()) {
    87             throw new DnsInvalidAttributeIdentifierException(
    86             throw new DnsResolverException(
    88                     "Attribute ID cannot be empty");
    87                     "Attribute ID cannot be empty");
    89         }
    88         }
    90         int rrclass;
    89         int rrclass;
    91         int rrtype;
    90         int rrtype;
    92         int space = attrId.indexOf(' ');
    91         int space = attrId.indexOf(' ');
    96             rrclass = ResourceRecord.CLASS_INTERNET;
    95             rrclass = ResourceRecord.CLASS_INTERNET;
    97         } else {
    96         } else {
    98             String className = attrId.substring(0, space);
    97             String className = attrId.substring(0, space);
    99             rrclass = ResourceRecord.getRrclass(className);
    98             rrclass = ResourceRecord.getRrclass(className);
   100             if (rrclass < 0) {
    99             if (rrclass < 0) {
   101                 throw new DnsInvalidAttributeIdentifierException(
   100                 throw new DnsResolverException(
   102                         "Unknown resource record class '" + className + '\'');
   101                         "Unknown resource record class '" + className + '\'');
   103             }
   102             }
   104         }
   103         }
   105 
   104 
   106         // type
   105         // type
   107         String typeName = attrId.substring(space + 1);
   106         String typeName = attrId.substring(space + 1);
   108         rrtype = ResourceRecord.getType(typeName);
   107         rrtype = ResourceRecord.getType(typeName);
   109         if (rrtype < 0) {
   108         if (rrtype < 0) {
   110             throw new DnsInvalidAttributeIdentifierException(
   109             throw new DnsResolverException(
   111                     "Unknown resource record type '" + typeName + '\'');
   110                     "Unknown resource record type '" + typeName + '\'');
   112         }
   111         }
   113 
   112 
   114         return new ResourceClassType(rrtype);
   113         return new ResourceClassType(rrtype);
   115     }
   114     }
   126         if (cts == null) {
   125         if (cts == null) {
   127             // Query all records.
   126             // Query all records.
   128             throw new RuntimeException("Internal DNS resolver error");
   127             throw new RuntimeException("Internal DNS resolver error");
   129         } else if (cts.length == 0) {
   128         } else if (cts.length == 0) {
   130             // No records are requested, but we need to ask for something.
   129             // No records are requested, but we need to ask for something.
   131             rrtype = ResourceRecord.QTYPE_STAR;
   130             rrtype = ResourceRecord.TYPE_ANY;
   132         } else {
   131         } else {
   133             rrclass = ResourceRecord.CLASS_INTERNET;
   132             rrclass = ResourceRecord.CLASS_INTERNET;
   134             rrtype = cts[0].rrtype;
   133             rrtype = cts[0].rrtype;
   135             for (int i = 1; i < cts.length; i++) {
   134             for (int i = 1; i < cts.length; i++) {
   136                 if (rrclass != cts[i].rrclass) {
   135                 if (rrclass != cts[i].rrclass) {
   137                     throw new RuntimeException("Internal error: Only CLASS_INTERNET is supported");
   136                     throw new RuntimeException("Internal error: Only CLASS_INTERNET is supported");
   138                 }
   137                 }
   139                 if (rrtype != cts[i].rrtype) {
   138                 if (rrtype != cts[i].rrtype) {
   140                     rrtype = ResourceRecord.QTYPE_STAR;
   139                     rrtype = ResourceRecord.TYPE_ANY;
   141                 }
   140                 }
   142             }
   141             }
   143         }
   142         }
   144         return new ResourceClassType(rrtype);
   143         return new ResourceClassType(rrtype);
   145     }
   144     }