Java Code Examples for javax.naming.directory.InitialDirContext#getAttributes()

The following examples show how to use javax.naming.directory.InitialDirContext#getAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScoreCommand.java    From AntiVPN with MIT License 6 votes vote down vote up
private static Set<String> collectRecords(String dns) {
    if (ConfigUtil.getDebugOrFalse()) {
        logger.info("Collecting A records for " + dns);
    }
    Set<String> retVal = new HashSet<>();
    try {
        InitialDirContext context = new InitialDirContext();
        Attributes attributes = context.getAttributes("dns:/" + dns, new String[] { "A" });
        NamingEnumeration<?> attributeEnum = attributes.get("A").getAll();
        while (attributeEnum.hasMore()) {
            retVal.add(attributeEnum.next().toString());
        }
    } catch (NamingException ex) {
        logger.error(ex.getMessage(), ex);
    }
    if (ConfigUtil.getDebugOrFalse()) {
        logger.info("Got " + retVal.size() + " record(s) for " + dns);
    }
    return retVal;
}
 
Example 2
Source File: ScoreCommand.java    From AntiVPN with MIT License 6 votes vote down vote up
private static Set<String> collectRecords(String dns) {
    if (ConfigUtil.getDebugOrFalse()) {
        logger.info("Collecting A records for " + dns);
    }
    Set<String> retVal = new HashSet<>();
    try {
        InitialDirContext context = new InitialDirContext();
        Attributes attributes = context.getAttributes("dns:/" + dns, new String[] { "A" });
        NamingEnumeration<?> attributeEnum = attributes.get("A").getAll();
        while (attributeEnum.hasMore()) {
            retVal.add(attributeEnum.next().toString());
        }
    } catch (NamingException ex) {
        logger.error(ex.getMessage(), ex);
    }
    if (ConfigUtil.getDebugOrFalse()) {
        logger.info("Got " + retVal.size() + " record(s) for " + dns);
    }
    return retVal;
}
 
Example 3
Source File: ScoreCommand.java    From AntiVPN with MIT License 6 votes vote down vote up
private static Set<String> collectRecords(String dns) {
    if (ConfigUtil.getDebugOrFalse()) {
        logger.info("Collecting A records for " + dns);
    }
    Set<String> retVal = new HashSet<>();
    try {
        InitialDirContext context = new InitialDirContext();
        Attributes attributes = context.getAttributes("dns:/" + dns, new String[] { "A" });
        NamingEnumeration<?> attributeEnum = attributes.get("A").getAll();
        while (attributeEnum.hasMore()) {
            retVal.add(attributeEnum.next().toString());
        }
    } catch (NamingException ex) {
        logger.error(ex.getMessage(), ex);
    }
    if (ConfigUtil.getDebugOrFalse()) {
        logger.info("Got " + retVal.size() + " record(s) for " + dns);
    }
    return retVal;
}
 
Example 4
Source File: DownloadEngineStatus.java    From TorrentEngine with GNU General Public License v3.0 5 votes vote down vote up
public static String getRevName(String oipAddr) throws NamingException {

        String ipAddr = oipAddr;
        try {
            Properties env = new Properties();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
            InitialDirContext idc = new InitialDirContext(env);

            String revName = null;
            String[] quads = ipAddr.split("\\.");

            //StringBuilder would be better, I know.
            ipAddr = "";

            for (int i = quads.length - 1; i >= 0; i--) {
                ipAddr += quads[i] + ".";
            }

            ipAddr += "in-addr.arpa.";
            Attributes attrs = idc.getAttributes(ipAddr, new String[]{"PTR"});
            Attribute attr = attrs.get("PTR");

            if (attr != null) {
                revName = (String) attr.get(0);
            }

            return revName;
        } catch (Exception e) {

            return oipAddr;
        }

    }
 
Example 5
Source File: MailSender.java    From tlaplus with MIT License 5 votes vote down vote up
private static List<MXRecord> getMXForDomain(String aDomain) throws NamingException {
	final InitialDirContext ctx = new InitialDirContext();
	final Attributes attributes = ctx.getAttributes("dns:/" + aDomain,
			new String[] { "MX" });
	final Attribute attr = attributes.get("MX");

	final List<MXRecord> list = new ArrayList<MXRecord>();
	
	// RFC 974
	if (attr == null) {
		list.add(new MXRecord(0, aDomain));
	} else {
		// split pref from hostname
		for (int i = 0; i < attr.size(); i++) {
			Object object = attr.get(i);
			if (object != null && object instanceof String) {
				String[] split = ((String) object).split("\\s+");
				if (split != null && split.length == 2) {
					Integer weight = Integer.parseInt(split[0]);
					list.add(new MXRecord(weight, split[1]));
				}
			}
		}
	}
	
	// sort (according to weight of mxrecord)
	Collections.sort(list);
	
	return list;
}
 
Example 6
Source File: LdapAuthenticateModule.java    From unitime with Apache License 2.0 4 votes vote down vote up
/**
 * Perform actual authentication the user
 */
public boolean doAuthenticate(HashMap userProps) throws Exception {
	if (ApplicationProperties
			.getProperty("tmtbl.authenticate.ldap.provider") == null)
		throw new Exception("Ldap provider is not set.");

	String principal = ApplicationProperties
			.getProperty("tmtbl.authenticate.ldap.principal");
	if (principal == null)
		throw new Exception("Ldap principal is not set.");

	String query = ApplicationProperties
			.getProperty("tmtbl.authenticate.ldap.query");
	if (query == null)
		throw new Exception("Ldap query is not set.");

	String n = (String) userProps.get("username");
	String p = (String) userProps.get("password");

	Hashtable<String, String> env = getEnv();
	env.put(Context.SECURITY_PRINCIPAL, principal.replaceAll("%", n));
	env.put(Context.SECURITY_CREDENTIALS, p);
	InitialDirContext cx = new InitialDirContext(env);

	String idAttributeName = ApplicationProperties.getProperty(
			"tmtbl.authenticate.ldap.externalId", "uid");
	Attributes attributes = cx.getAttributes(query.replaceAll("%", n),
			new String[] { idAttributeName });

	Attribute idAttribute = attributes.get(idAttributeName);
	if (idAttribute != null) {
		sLog.debug("Ldap authentication passed ... ");
		setAuthSucceeded(true);
		iExternalUid = (String) idAttribute.get();
		try {
			if (iExternalUid != null
					&& ApplicationProperties
							.getProperty("tmtbl.authenticate.ldap.externalId.format") != null)
				iExternalUid = new DecimalFormat(
						ApplicationProperties
								.getProperty("tmtbl.authenticate.ldap.externalId.format"))
						.format(Long.parseLong(iExternalUid));
		} catch (NumberFormatException e) {
		}
		setUser(n);
		return true;
	}

	return false;
}