use of javax.naming.directory.DirContext in project jdk8u_jdk by JetBrains.
the class DirContextStringPair method getTargetContext.
protected DirContextStringPair getTargetContext(String name) throws NamingException {
if (cpe.getResolvedObj() == null)
throw (NamingException) cpe.fillInStackTrace();
Context ctx = NamingManager.getContext(cpe.getResolvedObj(), cpe.getAltName(), cpe.getAltNameCtx(), env);
if (ctx instanceof DirContext)
return new DirContextStringPair((DirContext) ctx, name);
if (ctx instanceof Resolver) {
Resolver res = (Resolver) ctx;
ResolveResult rr = res.resolveToClass(name, DirContext.class);
// Reached a DirContext; return result.
DirContext dctx = (DirContext) rr.getResolvedObj();
Name tmp = rr.getRemainingName();
String remains = (tmp != null) ? tmp.toString() : "";
return (new DirContextStringPair(dctx, remains));
}
// Resolve all the way using lookup(). This may allow the operation
// to succeed if it doesn't require the penultimate context.
Object ultimate = ctx.lookup(name);
if (ultimate instanceof DirContext) {
return (new DirContextStringPair((DirContext) ultimate, ""));
}
throw (NamingException) cpe.fillInStackTrace();
}
use of javax.naming.directory.DirContext in project jdk8u_jdk by JetBrains.
the class ldapURLContextFactory method getUsingURLIgnoreRootDN.
static ResolveResult getUsingURLIgnoreRootDN(String url, Hashtable<?, ?> env) throws NamingException {
LdapURL ldapUrl = new LdapURL(url);
DirContext ctx = new LdapCtx("", ldapUrl.getHost(), ldapUrl.getPort(), env, ldapUrl.useSsl());
String dn = (ldapUrl.getDN() != null ? ldapUrl.getDN() : "");
// Represent DN as empty or single-component composite name.
CompositeName remaining = new CompositeName();
if (!"".equals(dn)) {
// if nonempty, add component
remaining.add(dn);
}
return new ResolveResult(ctx, remaining);
}
use of javax.naming.directory.DirContext in project uPortal by Jasig.
the class LDAPGroupStore method getConnection.
protected DirContext getConnection() {
//JNDI boilerplate to connect to an initial context
DirContext context = (DirContext) contexts.get("context");
if (context == null) {
Hashtable jndienv = new Hashtable();
jndienv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
jndienv.put(Context.SECURITY_AUTHENTICATION, "simple");
if (url.startsWith("ldaps")) {
// Handle SSL connections
String newurl = url.substring(0, 4) + url.substring(5);
jndienv.put(Context.SECURITY_PROTOCOL, "ssl");
jndienv.put(Context.PROVIDER_URL, newurl);
} else {
jndienv.put(Context.PROVIDER_URL, url);
}
if (logonid != null)
jndienv.put(Context.SECURITY_PRINCIPAL, logonid);
if (logonpassword != null)
jndienv.put(Context.SECURITY_CREDENTIALS, logonpassword);
try {
context = new InitialDirContext(jndienv);
} catch (NamingException nex) {
log.error("LDAPGroupStore: unable to get context", nex);
}
contexts.put("context", context);
}
return context;
}
use of javax.naming.directory.DirContext in project jetcd by coreos.
the class DnsSrvNameResolver method getServers.
@Override
protected List<ResolvedServerInfo> getServers() {
try {
DirContext ctx = new InitialDirContext(ENV);
NamingEnumeration<?> resolved = ctx.getAttributes(name, ATTRIBUTE_IDS).get("srv").getAll();
List<ResolvedServerInfo> servers = new LinkedList<>();
while (resolved.hasMore()) {
servers.add(srvRecordToServerInfo((String) resolved.next()));
}
return servers;
} catch (Exception e) {
LOGGER.warn("", e);
}
return Collections.emptyList();
}
use of javax.naming.directory.DirContext in project tomcat by apache.
the class JNDIRealm method authenticate.
// ---------------------------------------------------------- Realm Methods
/**
* Return the Principal associated with the specified username and
* credentials, if there is one; otherwise return <code>null</code>.
*
* If there are any errors with the JDBC connection, executing
* the query or anything we return null (don't authenticate). This
* event is also logged, and the connection will be closed so that
* a subsequent request will automatically re-open it.
*
* @param username Username of the Principal to look up
* @param credentials Password or other credentials to use in
* authenticating this username
* @return the associated principal, or <code>null</code> if there is none.
*/
@Override
public Principal authenticate(String username, String credentials) {
DirContext context = null;
Principal principal = null;
try {
// Ensure that we have a directory context available
context = open();
// time before giving up.
try {
// Authenticate the specified username if possible
principal = authenticate(context, username, credentials);
} catch (NullPointerException | CommunicationException | ServiceUnavailableException e) {
/* BZ 42449 - Catch NPE - Kludge Sun's LDAP provider
with broken SSL
*/
// log the exception so we know it's there.
containerLog.info(sm.getString("jndiRealm.exception.retry"), e);
// close the connection so we know it will be reopened.
if (context != null)
close(context);
// open a new directory context.
context = open();
// Try the authentication again.
principal = authenticate(context, username, credentials);
}
// Release this context
release(context);
// Return the authenticated Principal (if any)
return principal;
} catch (NamingException e) {
// Log the problem for posterity
containerLog.error(sm.getString("jndiRealm.exception"), e);
// Close the connection so that it gets reopened next time
if (context != null)
close(context);
// Return "not authenticated" for this request
if (containerLog.isDebugEnabled())
containerLog.debug("Returning null principal.");
return null;
}
}
Aggregations