本文整理汇总了Java中javax.naming.directory.DirContext.getAttributes方法的典型用法代码示例。如果您正苦于以下问题:Java DirContext.getAttributes方法的具体用法?Java DirContext.getAttributes怎么用?Java DirContext.getAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.naming.directory.DirContext
的用法示例。
在下文中一共展示了DirContext.getAttributes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getServerAddress
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Returns a server's address and port for the specified hostname, looking up the SRV record if possible
*/
private static String[] getServerAddress(String p_78863_0_)
{
try
{
String s = "com.sun.jndi.dns.DnsContextFactory";
Class.forName("com.sun.jndi.dns.DnsContextFactory");
Hashtable<String, String> hashtable = new Hashtable();
hashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
hashtable.put("java.naming.provider.url", "dns:");
hashtable.put("com.sun.jndi.dns.timeout.retries", "1");
DirContext dircontext = new InitialDirContext(hashtable);
Attributes attributes = dircontext.getAttributes("_minecraft._tcp." + p_78863_0_, new String[] {"SRV"});
String[] astring = attributes.get("srv").get().toString().split(" ", 4);
return new String[] {astring[3], astring[2]};
}
catch (Throwable var6)
{
return new String[] {p_78863_0_, Integer.toString(25565)};
}
}
示例2: getServerAddress
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Returns a server's address and port for the specified hostname, looking up the SRV record if possible
*/
private static String[] getServerAddress(String p_78863_0_)
{
try
{
String s = "com.sun.jndi.dns.DnsContextFactory";
Class.forName("com.sun.jndi.dns.DnsContextFactory");
Hashtable hashtable = new Hashtable();
hashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
hashtable.put("java.naming.provider.url", "dns:");
hashtable.put("com.sun.jndi.dns.timeout.retries", "1");
DirContext dircontext = new InitialDirContext(hashtable);
Attributes attributes = dircontext.getAttributes("_minecraft._tcp." + p_78863_0_, new String[] {"SRV"});
String[] astring = attributes.get("srv").get().toString().split(" ", 4);
return new String[] {astring[3], astring[2]};
}
catch (Throwable var6)
{
return new String[] {p_78863_0_, Integer.toString(25565)};
}
}
示例3: uid2ext
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
public String uid2ext(String uid) {
try {
DirContext ctx = null;
try {
ctx = getDirContext();
Attributes attributes = ctx.getAttributes(
ApplicationProperties.getProperty("tmtbl.authenticate.ldap.uid2ext").replaceAll("%", uid),
new String[] {
ApplicationProperties.getProperty("tmtbl.authenticate.ldap.externalId", "puid")
});
if (attributes!=null) {
Attribute puid = attributes.get(ApplicationProperties.getProperty("tmtbl.authenticate.ldap.externalId", "puid"));
if (puid!=null) return (String)puid.get();
}
} finally {
if (ctx!=null) ctx.close();
}
} catch (Exception e) {
Debug.error("Unable to translate uid to ext, "+e.getMessage());
}
return null;
}
示例4: ext2uid
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
public String ext2uid(String puid) {
try {
DirContext ctx = null;
try {
ctx = getDirContext();
Attributes attributes = ctx.getAttributes(
ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ext2uid").replaceAll("%", puid),
new String[] {
ApplicationProperties.getProperty("tmtbl.authenticate.ldap.login", "uid")
});
if (attributes!=null) {
Attribute uid = attributes.get(ApplicationProperties.getProperty("tmtbl.authenticate.ldap.login", "uid"));
if (uid!=null) return (String)uid.get();
}
} finally {
if (ctx!=null) ctx.close();
}
} catch (Exception e) {
Debug.error("Unable to translate ext to uid, "+e.getMessage());
}
return null;
}
示例5: getUserByPattern
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Use the distinguished name to locate the directory
* entry for the user with the specified username and
* return a User object; otherwise return <code>null</code>.
*
* @param context The directory context
* @param username The username
* @param attrIds String[]containing names of attributes to
* @param dn Distinguished name of the user
* retrieve.
*
* @exception NamingException if a directory server error occurs
*/
protected User getUserByPattern(DirContext context,
String username,
String[] attrIds,
String dn)
throws NamingException {
// If no attributes are requested, no need to look for them
if (attrIds == null || attrIds.length == 0) {
return new User(username, dn, null, null,null);
}
// Get required attributes from user entry
Attributes attrs = null;
try {
attrs = context.getAttributes(dn, attrIds);
} catch (NameNotFoundException e) {
return null;
}
if (attrs == null)
return null;
// Retrieve value of userPassword
String password = null;
if (userPassword != null)
password = getAttributeValue(userPassword, attrs);
String userRoleAttrValue = null;
if (userRoleAttribute != null) {
userRoleAttrValue = getAttributeValue(userRoleAttribute, attrs);
}
// Retrieve values of userRoleName attribute
ArrayList<String> roles = null;
if (userRoleName != null)
roles = addAttributeValues(userRoleName, attrs, roles);
return new User(username, dn, password, roles, userRoleAttrValue);
}
示例6: bindAsUser
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Check credentials by binding to the directory as the user
*
* @param context The directory context
* @param user The User to be authenticated
* @param credentials Authentication credentials
*
* @exception NamingException if a directory server error occurs
*/
protected boolean bindAsUser(DirContext context,
User user,
String credentials)
throws NamingException {
if (credentials == null || user == null)
return (false);
String dn = user.getDN();
if (dn == null)
return (false);
// Validate the credentials specified by the user
if (containerLog.isTraceEnabled()) {
containerLog.trace(" validating credentials by binding as the user");
}
userCredentialsAdd(context, dn, credentials);
// Elicit an LDAP bind operation
boolean validated = false;
try {
if (containerLog.isTraceEnabled()) {
containerLog.trace(" binding as " + dn);
}
context.getAttributes("", null);
validated = true;
}
catch (AuthenticationException e) {
if (containerLog.isTraceEnabled()) {
containerLog.trace(" bind attempt failed");
}
}
userCredentialsRemove(context);
return validated;
}
示例7: reverseDns
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Returns the hostname associated with the specified IP address by the
* provided nameserver.
*
* Loopback addresses
* @param hostIp The address to reverse lookup
* @param ns The host name of a reachable DNS server
* @return The host name associated with the provided IP
* @throws NamingException If a NamingException is encountered
*/
public static String reverseDns(InetAddress hostIp, String ns)
throws NamingException {
//
// Builds the reverse IP lookup form
// This is formed by reversing the IP numbers and appending in-addr.arpa
//
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);
}
return hostname;
}
示例8: bindAsUser
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Check credentials by binding to the directory as the user
*
* @param context
* The directory context
* @param user
* The User to be authenticated
* @param credentials
* Authentication credentials
*
* @exception NamingException
* if a directory server error occurs
*/
protected boolean bindAsUser(DirContext context, User user, String credentials) throws NamingException {
if (credentials == null || user == null)
return (false);
String dn = user.getDN();
if (dn == null)
return (false);
// Validate the credentials specified by the user
if (containerLog.isTraceEnabled()) {
containerLog.trace(" validating credentials by binding as the user");
}
userCredentialsAdd(context, dn, credentials);
// Elicit an LDAP bind operation
boolean validated = false;
try {
if (containerLog.isTraceEnabled()) {
containerLog.trace(" binding as " + dn);
}
context.getAttributes("", null);
validated = true;
} catch (AuthenticationException e) {
if (containerLog.isTraceEnabled()) {
containerLog.trace(" bind attempt failed");
}
}
userCredentialsRemove(context);
return validated;
}
示例9: doLookup
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
private static Attribute doLookup(String hostName) throws NamingException {
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
DirContext ictx = new InitialDirContext(env);
Attributes attrs =
ictx.getAttributes(hostName, new String[]{"MX"});
Attribute attr = attrs.get("MX");
if (attr == null) return (null);
return (attr);
}
示例10: getAttributes
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
public Attributes getAttributes(DirContext ctx, Name name, String[] attributes)
{
try
{
return ctx.getAttributes(name, attributes);
}
catch( NamingException ne )
{
throw new RuntimeException(ne);
}
}
示例11: getAttributes
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Retrieves selected attributes associated with a named object.
*
* @return the requested attributes; never null
* @param name the name of the object from which to retrieve attributes
* @param attrIds the identifiers of the attributes to retrieve. null
* indicates that all attributes should be retrieved; an empty array
* indicates that none should be retrieved
* @exception NamingException if a naming exception is encountered
*/
@Override
public final Attributes getAttributes(String name, String[] attrIds)
throws NamingException {
// First check for aliases
if (!aliases.isEmpty()) {
AliasResult result = findAlias(name);
if (result.dirContext != null) {
return result.dirContext.getAttributes(
result.aliasName, attrIds);
}
}
// Next do a standard lookup
Attributes attrs = doGetAttributes(name, attrIds);
if (attrs != null)
return attrs;
String resourceName = "/META-INF/resources" + name;
// Check the alternate locations
for (DirContext altDirContext : altDirContexts) {
if (altDirContext instanceof BaseDirContext)
attrs = ((BaseDirContext) altDirContext).doGetAttributes(resourceName, attrIds);
else {
try {
attrs = altDirContext.getAttributes(name, attrIds);
} catch (NamingException ne) {
// Ignore
}
}
if (attrs != null)
return attrs;
}
// Really not found
throw new NameNotFoundException(
sm.getString("resources.notFound", name));
}
示例12: reverseDNS
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* This method uses JNDI to look up an address in DNS and return its name
*
* @param addr
*
* @return the host name associated with the address or null if lookup isn't possible or there is
* no host name for this address
*/
public static String reverseDNS(InetAddress addr) {
byte[] addrBytes = addr.getAddress();
// reverse the address suitable for reverse lookup
String lookup = "";
for (int index = addrBytes.length - 1; index >= 0; index--) {
lookup = lookup + (addrBytes[index] & 0xff) + '.';
}
lookup += "in-addr.arpa";
// System.out.println("Looking up: " + lookup);
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
DirContext ctx = new InitialDirContext(env);
Attributes attrs = ctx.getAttributes(lookup, new String[] {"PTR"});
for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();) {
Attribute attr = (Attribute) ae.next();
for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
Object elem = vals.nextElement();
if ("PTR".equals(attr.getID()) && elem != null) {
return elem.toString();
}
}
}
ctx.close();
} catch (Exception e) {
// ignored
}
return null;
}
示例13: getUserByPattern
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Use the distinguished name to locate the directory entry for the user
* with the specified username and return a User object; otherwise return
* <code>null</code>.
*
* @param context
* The directory context
* @param username
* The username
* @param attrIds
* String[]containing names of attributes to
* @param dn
* Distinguished name of the user retrieve.
*
* @exception NamingException
* if a directory server error occurs
*/
protected User getUserByPattern(DirContext context, String username, String[] attrIds, String dn)
throws NamingException {
// If no attributes are requested, no need to look for them
if (attrIds == null || attrIds.length == 0) {
return new User(username, dn, null, null, null);
}
// Get required attributes from user entry
Attributes attrs = null;
try {
attrs = context.getAttributes(dn, attrIds);
} catch (NameNotFoundException e) {
return null;
}
if (attrs == null)
return null;
// Retrieve value of userPassword
String password = null;
if (userPassword != null)
password = getAttributeValue(userPassword, attrs);
String userRoleAttrValue = null;
if (userRoleAttribute != null) {
userRoleAttrValue = getAttributeValue(userRoleAttribute, attrs);
}
// Retrieve values of userRoleName attribute
ArrayList<String> roles = null;
if (userRoleName != null)
roles = addAttributeValues(userRoleName, attrs, roles);
return new User(username, dn, password, roles, userRoleAttrValue);
}
示例14: getAttributesDn
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Low level method: Retrieves attributes of a single entry,
* using the specified dn/pswd for authorization.
*
* @param authDn the authorized dn (distinguished name) of the caller
* @param password the password associated with authDn
* @param subjectDn the dn about which info is requested
* @param attrNames an array of attribute names to be returned.
* If null, all available attributes are returned.
*
* @return An LdapEntry representing one returned entry.
* Throws {@link LdapNotFoundException LdapNotFoundException}
* if entry not found.
* If attrNames was specified, the LdapEntry has the same
* attributes in the specified order.
* <p>
* If attrNames is null,
* the LdapEntry contains all available attributes for the entry,
* sorted by attribute name.
*/
public LdapEntry getAttributesDn(
String authDn,
String password,
String subjectDn,
String[] attrNames)
throws LdapException
{
chkstg( "subject dn", subjectDn);
chkstgs( "attr names", attrNames, true); // may be null
LdapEntry entry = null;
DirContext dirctx = getDirContext( authDn, password);
Attributes attrraw = null;
// Whew, what LDAP trickery. Cannot simply specify
// attrraw = dirctx.getAttributes( subjectDn);
// since that won't get the operational attributes:
// createTimestamp, modifyTimestamp, etc.
// The "*" gets all ordinary attributes;
// the "+" gets the operational attributes.
try {
attrraw = dirctx.getAttributes( subjectDn, new String[] {"*", "+"} );
}
catch( NameNotFoundException nfexc) {
if (bugs >= 1) {
prtln("getAttributesDn: not found: nfexc: " + nfexc);
}
throw new LdapNotFoundException("entry not found. dn: \""
+ subjectDn + "\"", nfexc);
}
catch( NamingException nexc) {
if (bugs >= 1) {
prtln("getAttributesDn: nexc: " + nexc);
nexc.printStackTrace();
prtln();
prtln("authDn: \"" + authDn + "\"");
prtln("password: \"" + password + "\"");
prtln("subjectDn: \"" + subjectDn + "\"");
prtln();
}
throw new LdapException("getAttributesDn: exception", nexc);
}
if (attrraw != null)
entry = decodeAttributes( subjectDn, attrNames, attrraw);
return entry;
}
示例15: bindAsUser
import javax.naming.directory.DirContext; //导入方法依赖的package包/类
/**
* Check credentials by binding to the directory as the user
*
* @param context The directory context
* @param user The User to be authenticated
* @param credentials Authentication credentials
* @throws NamingException if a directory server error occurs
*/
private boolean bindAsUser(final DirContext context, final JNDIUser user, final String credentials)
throws NamingException {
if (credentials == null || user == null) {
return false;
}
// Validate the credentials specified by the user
final String dn = user.getDn();
if (log.isDebugEnabled()) {
log.debug("validating credentials by binding as: " + dn);
}
if (dn == null) {
return false;
}
// Set up security environment to bind as the user
context.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
context.addToEnvironment(Context.SECURITY_CREDENTIALS, credentials);
// Elicit an LDAP bind operation
boolean validated = false;
try {
//noinspection UNUSED_SYMBOL,UnusedDeclaration
context.getAttributes("", null);
validated = true;
} catch (AuthenticationException e) {
if (log.isDebugEnabled()) {
log.debug("bind attempt failed: " + e, e);
}
}
// Restore the original security environment
if (connectionPrincipal != null) {
context.addToEnvironment(Context.SECURITY_PRINCIPAL, connectionPrincipal);
} else {
context.removeFromEnvironment(Context.SECURITY_PRINCIPAL);
}
if (connectionCredentials != null) {
context.addToEnvironment(Context.SECURITY_CREDENTIALS, connectionCredentials);
} else {
context.removeFromEnvironment(Context.SECURITY_CREDENTIALS);
}
return validated;
}