/** * Resolve an attribute for a hostname. * * @param hostname * @param attrName * @return * @throws NamingException * @throws UnknownHostException */ @SuppressWarnings("rawtypes") private List<InetAddress> resolve(String hostname, String attrName) throws NamingException, UnknownHostException { Attributes attrs = context.getAttributes(hostname, new String[] { attrName }); List<InetAddress> inetAddresses = new ArrayList<>(); Attribute attr = attrs.get(attrName); if (attr != null && attr.size() > 0) { NamingEnumeration e = attr.getAll(); while (e.hasMore()) { InetAddress inetAddress = InetAddress.getByName("" + e.next()); inetAddresses.add(InetAddress.getByAddress(hostname, inetAddress.getAddress())); } } return inetAddresses; }
private String[] getSMTPServerByJNDI(String to) throws Exception { String host=getDomainFromAddress(to); Properties jndiEnvironmentProperties = new Properties(); jndiEnvironmentProperties.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); InitialDirContext initialDirContext = new InitialDirContext(jndiEnvironmentProperties); Attributes attributes = initialDirContext.getAttributes(host, new String[] {"MX"}); Attribute attribute = attributes.get("MX"); String[] servers = new String[attribute.size()]; for (int i = 0; i < attribute.size(); i++) { servers[i] = attribute.get(i).toString(); servers[i]=servers[i].substring(servers[i].indexOf(" ") + 1, servers[i].length() -1); } return servers; }
Attributes attrs = context.getAttributes(hostname, new String[] { "CNAME" }); Attribute attr = attrs.get("CNAME");
InitialDirContext dirContext = createDnsDirContext(); try { Attributes attributes = dirContext.getAttributes(host, new String[]{"TXT"}); Attribute attribute = attributes.get("TXT"); if (attribute != null) {
InitialDirContext dirContext = createDnsDirContext(); try { Attributes attributes = dirContext.getAttributes("_mongodb._tcp." + srvHost, new String[]{"SRV"}); Attribute attribute = attributes.get("SRV"); if (attribute == null) {
@Test(groups = "unit") public void should_return_same_address_when_exception_encountered() throws Exception { InitialDirContext mock = mock(InitialDirContext.class); when(mock.getAttributes(anyString(), any(String[].class))) .thenThrow(new NamingException("Problem resolving address (not really).")); EC2MultiRegionAddressTranslator translator = new EC2MultiRegionAddressTranslator(mock); InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); assertThat(translator.translate(address)).isEqualTo(address); }
@Test(groups = "unit") public void should_return_new_address_when_match_found() throws Exception { InetSocketAddress expectedAddress = new InetSocketAddress("54.32.55.66", 9042); InitialDirContext mock = mock(InitialDirContext.class); when(mock.getAttributes("5.2.0.192.in-addr.arpa", new String[] {"PTR"})) .thenReturn(new BasicAttributes("PTR", expectedAddress.getHostName())); EC2MultiRegionAddressTranslator translator = new EC2MultiRegionAddressTranslator(mock); InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); assertThat(translator.translate(address)).isEqualTo(expectedAddress); }
@Test(groups = "unit") public void should_return_same_address_when_no_entry_found() throws Exception { InitialDirContext mock = mock(InitialDirContext.class); when(mock.getAttributes(anyString(), any(String[].class))).thenReturn(new BasicAttributes()); EC2MultiRegionAddressTranslator translator = new EC2MultiRegionAddressTranslator(mock); InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); assertThat(translator.translate(address)).isEqualTo(address); }
public Attributes getMxRecords(final String name) throws NamingException { return m_context.getAttributes(name, new String[]{"MX"}); }
public Attributes getPtrRecords(final String name) throws NamingException { return m_context.getAttributes(name, new String[]{"PTR"}); }
public Attributes getARecords(final String name) throws NamingException { return m_context.getAttributes(name, new String[]{"A"}); }
public Attributes getNsRecords(final String name) throws NamingException { return m_context.getAttributes(name, new String[]{"NS"}); }
public Attributes getCnamRecords(final String name) throws NamingException { return m_context.getAttributes(name, new String[]{"CNAME"}); }
public Attributes getSrvRecords(final String name) throws NamingException { return m_context.getAttributes(name, new String[]{"SRV"}); }
public Attributes getSoaRecords(final String name) throws NamingException { return m_context.getAttributes(name, new String[]{"SOA"}); }
// get the default initial Directory Context InitialDirContext idc = new InitialDirContext(); // get the DNS records for inetAddress Attributes attributes = idc.getAttributes("dns:/" + inetAddress.getHostName()); // get an enumeration of the attributes and print them out NamingEnumeration attributeEnumeration = attributes.getAll(); System.out.println("-- DNS INFORMATION --"); while (attributeEnumeration.hasMore()) { System.out.println("" + attributeEnumeration.next()); } attributeEnumeration.close();
public Stream<SrvRecord> resolveSrvRecord( String url ) throws NamingException { Attributes attrs = _idc.orElseGet( this::setIdc ).getAttributes( url, SRV_RECORDS ); return enumerationAsStream( (NamingEnumeration<String>) attrs.get( SRV_ATTR ).getAll() ).map( SrvRecord::parse ); }
@Test(groups = "unit") public void should_return_same_address_when_exception_encountered() throws Exception { InitialDirContext mock = mock(InitialDirContext.class); when(mock.getAttributes(anyString(), any(String[].class))) .thenThrow(new NamingException("Problem resolving address (not really).")); EC2MultiRegionAddressTranslator translator = new EC2MultiRegionAddressTranslator(mock); InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); assertThat(translator.translate(address)).isEqualTo(address); }
@Test(groups = "unit") public void should_return_same_address_when_no_entry_found() throws Exception { InitialDirContext mock = mock(InitialDirContext.class); when(mock.getAttributes(anyString(), any(String[].class))).thenReturn(new BasicAttributes()); EC2MultiRegionAddressTranslator translator = new EC2MultiRegionAddressTranslator(mock); InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); assertThat(translator.translate(address)).isEqualTo(address); }
@Test(groups = "unit") public void should_return_new_address_when_match_found() throws Exception { InetSocketAddress expectedAddress = new InetSocketAddress("54.32.55.66", 9042); InitialDirContext mock = mock(InitialDirContext.class); when(mock.getAttributes("5.2.0.192.in-addr.arpa", new String[] {"PTR"})) .thenReturn(new BasicAttributes("PTR", expectedAddress.getHostName())); EC2MultiRegionAddressTranslator translator = new EC2MultiRegionAddressTranslator(mock); InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); assertThat(translator.translate(address)).isEqualTo(expectedAddress); }