You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SRVRecord.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.common;
  23. import java.util.ArrayList;
  24. import java.util.Hashtable;
  25. import java.util.List;
  26. import javax.naming.NamingEnumeration;
  27. import javax.naming.NamingException;
  28. import javax.naming.directory.Attribute;
  29. import javax.naming.directory.InitialDirContext;
  30. /**
  31. * Class to represent an SRV Record.
  32. */
  33. public class SRVRecord implements Comparable<SRVRecord> {
  34. /** Priority of this record. */
  35. private final int priority;
  36. /** Weight of this record. */
  37. private final int weight;
  38. /** Port of this record. */
  39. private final int port;
  40. /** Host of this record. */
  41. private final String host;
  42. /**
  43. * Create a new SRV Record from a String.
  44. *
  45. * @param record Record as a string.
  46. * @throws NamingException if the record is invalid
  47. */
  48. public SRVRecord(final String record) throws NamingException {
  49. final String[] bits = record.split(" ", 4);
  50. if (bits.length < 4) {
  51. throw new NamingException("Invalid SRV Record.");
  52. }
  53. try {
  54. priority = Integer.parseInt(bits[0]);
  55. weight = Integer.parseInt(bits[1]);
  56. port = Integer.parseInt(bits[2]);
  57. host = (bits[3].charAt(bits[3].length() - 1) == '.') ? bits[3].substring(0, bits[3].length() - 1) : bits[3];
  58. } catch (final NumberFormatException nfe) {
  59. throw new NamingException("Unable to parse SRV Record parameters."); // NOPMD
  60. }
  61. }
  62. /**
  63. * Get the priority for this SRVRecord.
  64. *
  65. * @return The priority for this SRVRecord.
  66. */
  67. public int getPriority() {
  68. return priority;
  69. }
  70. /**
  71. * Get the weight for this SRVRecord.
  72. *
  73. * @return The weight for this SRVRecord.
  74. */
  75. public int getWeight() {
  76. return weight;
  77. }
  78. /**
  79. * Get the port for this SRVRecord.
  80. *
  81. * @return The port for this SRVRecord.
  82. */
  83. public int getPort() {
  84. return port;
  85. }
  86. /**
  87. * Get the host for this SRVRecord.
  88. *
  89. * @return The host for this SRVRecord.
  90. */
  91. public String getHost() {
  92. return host;
  93. }
  94. @Override
  95. public String toString() {
  96. return priority + " " + weight + " " + port + " " + host + ".";
  97. }
  98. @Override
  99. public int compareTo(final SRVRecord o) {
  100. if (this.priority < o.priority) { return -1; }
  101. if (this.priority > o.priority) { return 1; }
  102. return 0;
  103. }
  104. public static List<SRVRecord> getRecords(final String host) {
  105. final List<SRVRecord> result = new ArrayList<>();
  106. try {
  107. // Obsolete Collection. yeah yeah...
  108. final Hashtable<String, String> env = new Hashtable<>(); // NOPMD - Required by InitialDirContext
  109. env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
  110. env.put("java.naming.provider.url", "dns:");
  111. final Attribute attr = new InitialDirContext(env).getAttributes(host, new String [] { "SRV" }).get("SRV");
  112. if (attr != null) {
  113. final NamingEnumeration<?> ne = attr.getAll();
  114. while (ne.hasMore()) {
  115. try {
  116. final SRVRecord record = new SRVRecord((String)ne.next());
  117. result.add(record);
  118. } catch (final NamingException nex) { /* Ignore if invalid. */ }
  119. }
  120. }
  121. } catch (final NamingException nex) { /* Ignore errors. */ }
  122. // Ideally we should be sorting this list so that it contains all
  123. // the servers with the lowest priority randomly sorted first, then the
  124. // next priority etc.
  125. // However, in general, the DNS resolver will take care of the ordering
  126. // for us, so we should already get it in a suitable order.
  127. return result;
  128. }
  129. }