Java 8 DirContext - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 DirContext class.

This code example shows how to use the following methods:close, getAttributes
    // 
    String[] parts = hostIp.getHostAddress().split("\\.");
    String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "."
      + parts[0] + ".in-addr.arpa";
 
    DirContext ictx = new InitialDirContext();
    Attributes attribute;
    try { 
      attribute = ictx.getAttributes("dns://"               // Use "dns:///" if the default
                         + ((ns == null) ? "" : ns) +
                         // nameserver is to be used 
                         "/" + reverseIP, new String[] { "PTR" });
    } finally { 
      ictx.close();
    } 
 
    String hostname = attribute.get("PTR").get().toString();
    int hostnameLength = hostname.length();
    if (hostname.charAt(hostnameLength - 1) == '.') {
      hostname = hostname.substring(0, hostnameLength - 1);
6
CodeRank
This code example shows how to use the following methods:close, getAttributes
    // 
    String[] parts = hostIp.getHostAddress().split("\\.");
    String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "."
      + parts[0] + ".in-addr.arpa";
 
    DirContext ictx = new InitialDirContext();
    Attributes attribute =
      ictx.getAttributes("dns://"               // Use "dns:///" if the default
                         + ((ns == null) ? "" : ns) + 
                         // nameserver is to be used 
                         "/" + reverseIP, new String[] { "PTR" });
    ictx.close();
     
    return attribute.get("PTR").get().toString();
  } 
 
  /** 
   * Returns all the IPs associated with the provided interface, if any, in 
   * textual form. 
   *  
6
CodeRank
This code example shows how to use the following methods:getAttributes
 
    @Test
    public void testResolveServiceIpAddress() throws Exception {
        BasicAttributes srvRecords = createSrvRecords();
        BasicAttributes aRecords = new BasicAttributes("A", "63.246.7.80");
        when(context.getAttributes("_ldap._tcp.springsource.com", new String[] {"SRV"})).thenReturn(srvRecords);
        when(context.getAttributes("kdc.springsource.com", new String[] {"A"})).thenReturn(aRecords);
 
        String ipAddress = dnsResolver.resolveServiceIpAddress("ldap", "springsource.com");
        assertEquals("63.246.7.80", ipAddress);
    } 
 
    @Test(expected=DnsLookupException.class)
    public void testUnknowError() throws Exception {
        when(context.getAttributes(any(String.class), any(String[].class))).thenThrow(new NamingException("error"));
        dnsResolver.resolveIpAddress("");
    } 
 
 
 
6
CodeRank
This code example shows how to use the following methods:addToEnvironment, removeFromEnvironment, getAttributes
6
CodeRank
This code example shows how to use the following methods:addToEnvironment, removeFromEnvironment, getAttributes
        return buf.toString();
    } 
 
    protected void bindUser(DirContext context, String dn) throws NamingException, FailedLoginException {
 
        context.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
        try { 
            context.getAttributes("", null);
        } catch (AuthenticationException e) {
            log.debug("Authentication failed for dn=" + dn);
            throw new FailedLoginException();
        } finally { 
 
            if (connectionUsername != null) {
                context.addToEnvironment(Context.SECURITY_PRINCIPAL, connectionUsername);
            } else { 
                context.removeFromEnvironment(Context.SECURITY_PRINCIPAL);
            } 
 
            if (connectionPassword != null) {

6
CodeRank
This code example shows how to use the following methods:getAttributes
   { 
      log.info("IN CHECK DNS!!!!"); 
      Hashtable<String, String> env = new Hashtable<String, String>();
      env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
      env.put("java.naming.provider.url", "dns://localhost:6363");
      DirContext dnsContext = new InitialDirContext(env);
 
      log.info("Check domain: " + domain);
      Attributes attrs1 = dnsContext.getAttributes(domain, new String[]{"TXT"});
      log.info("Attributes size: " + attrs1.size());
      Assert.assertTrue(attrs1 != null);
      Assert.assertTrue(attrs1.size() > 0);
      javax.naming.directory.Attribute txtrecord = attrs1.get("txt");
      log.info("TEXT RECORD: " + txtrecord.get());
   } 
} 
6
CodeRank
This code example shows how to use the following methods:close, getAttributes
        // 
        String[] parts = hostIp.getHostAddress().split("\\.");
        String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "."
                + parts[0] + ".in-addr.arpa";
 
        DirContext ictx = new InitialDirContext();
        Attributes attribute;
        try { 
            attribute = ictx.getAttributes("dns://"               // Use "dns:///" if the default
                    + ((ns == null) ? "" : ns) +
                    // nameserver is to be used 
                    "/" + reverseIP, new String[]{"PTR"});
        } finally { 
            ictx.close();
        } 
 
        return attribute.get("PTR").get().toString();
    } 
 
    /** 
6
CodeRank
This code example shows how to use the following methods:getAttributes
 
		public Boolean call() {
			Hashtable env = new Hashtable();
			env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
			try { 
				DirContext ictx = new InitialDirContext(env);
				Attributes attrs = ictx.getAttributes(_hostName, new String[] { "MX" });
				Attribute attr = attrs.get("MX");
				return attr != null ? Boolean.TRUE : Boolean.FALSE;
			} 
			catch (NameNotFoundException e) {
				return Boolean.FALSE;
			} 
			catch (NamingException e) {
				throw NSForwardException._runtimeExceptionForThrowable(e);
			} 
		} 
 
	} 
 
5
CodeRank
This code example shows how to use the following methods:lookup, createSubcontext
        attribute.add( "top" );
        attribute.add( "organizationalUnit" );
        attributes.put( attribute );
        attributes.put( "ou", "testing00" );
 
        DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
        assertNotNull( ctx );
 
        ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
        assertNotNull( ctx );
        attributes = ctx.getAttributes( "" );
        assertNotNull( attributes );
        assertEquals( "testing00", attributes.get( "ou" ).get() );
        attribute = attributes.get( "objectClass" );
        assertNotNull( attribute );
        assertTrue( attribute.contains( "top" ) );
        assertTrue( attribute.contains( "organizationalUnit" ) );
 
        /* 
         * create ou=testing01,ou=system 

5
CodeRank
This code example shows how to use the following methods:lookup, createSubcontext
        Attribute attribute = new BasicAttribute( "objectClass" );
        attribute.add( "top" );
        attribute.add( "organizationalUnit" );
        attributes.put( attribute );
        attributes.put( "ou", "testing00" );
        DirContext ctx = sysRoot.createSubcontext( "ou=testing00", attributes );
        assertNotNull( ctx );
 
        ctx = ( DirContext ) sysRoot.lookup( "ou=testing00" );
        assertNotNull( ctx );
 
        attributes = ctx.getAttributes( "" );
        assertNotNull( attributes );
        assertEquals( "testing00", attributes.get( "ou" ).get() );
        attribute = attributes.get( "objectClass" );
        assertNotNull( attribute );
        assertTrue( attribute.contains( "top" ) );
        assertTrue( attribute.contains( "organizationalUnit" ) );
 
        /* 

5
CodeRank
See Code Examples for Java 8 DirContext Methods: