123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /*
- * Copyright (c) 2006-2017 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
- package com.dmdirc.parser.common;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
-
- import javax.naming.NamingEnumeration;
- import javax.naming.NamingException;
- import javax.naming.directory.Attribute;
-
- /**
- * Class to represent an SRV Record.
- */
- public class SRVRecord implements Comparable<SRVRecord> {
- /** Priority of this record. */
- private final int priority;
-
- /** Weight of this record. */
- private final int weight;
-
- /** Port of this record. */
- private final int port;
-
- /** Host of this record. */
- private final String host;
-
- /**
- * Create a new SRV Record from a String.
- *
- * @param record Record as a string.
- * @throws NamingException if the record is invalid
- */
- public SRVRecord(final String record) throws NamingException {
- final String[] bits = record.split(" ", 4);
- if (bits.length < 4) {
- throw new NamingException("Invalid SRV Record.");
- }
- try {
- priority = Integer.parseInt(bits[0]);
- weight = Integer.parseInt(bits[1]);
- port = Integer.parseInt(bits[2]);
- host = bits[3].charAt(bits[3].length() - 1) == '.' ? bits[3].substring(0, bits[3].length() - 1) : bits[3];
- } catch (final NumberFormatException nfe) {
- throw new NamingException("Unable to parse SRV Record parameters."); // NOPMD
- }
- }
-
- /**
- * Get the priority for this SRVRecord.
- *
- * @return The priority for this SRVRecord.
- */
- public int getPriority() {
- return priority;
- }
-
- /**
- * Get the weight for this SRVRecord.
- *
- * @return The weight for this SRVRecord.
- */
- public int getWeight() {
- return weight;
- }
-
- /**
- * Get the port for this SRVRecord.
- *
- * @return The port for this SRVRecord.
- */
- public int getPort() {
- return port;
- }
-
- /**
- * Get the host for this SRVRecord.
- *
- * @return The host for this SRVRecord.
- */
- public String getHost() {
- return host;
- }
-
- @Override
- public String toString() {
- return priority + " " + weight + ' ' + port + ' ' + host + '.';
- }
-
- @Override
- public int compareTo(final SRVRecord o) {
- if (priority < o.getPriority()) { return -1; }
- if (priority > o.getPriority()) { return 1; }
- return 0;
- }
-
- public static List<SRVRecord> getRecords(final String host) {
- final List<SRVRecord> result = new ArrayList<>();
- final HashMap<String, String> env = new HashMap<>();
- env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
- env.put("java.naming.provider.url", "dns:");
- try (AutoCloseableInitialDirContext dirContext = new AutoCloseableInitialDirContext(env)) {
- final Attribute attr = dirContext.getAttributes(host, new String [] { "SRV" }).get("SRV");
-
- if (attr != null) {
- final NamingEnumeration<?> ne = attr.getAll();
- while (ne.hasMore()) {
- try {
- final SRVRecord record = new SRVRecord((String)ne.next());
- result.add(record);
- } catch (final NamingException nex) { /* Ignore if invalid. */ }
- }
- }
- } catch (final NamingException nex) { /* Ignore errors. */ }
-
- // Ideally we should be sorting this list so that it contains all
- // the servers with the lowest priority randomly sorted first, then the
- // next priority etc.
- // However, in general, the DNS resolver will take care of the ordering
- // for us, so we should already get it in a suitable order.
- return result;
- }
- }
|