use of javax.naming.ldap.LdapName in project jdk8u_jdk by JetBrains.
the class ServiceLocator method mapDnToDomainName.
/**
* Maps a distinguished name (RFC 2253) to a fully qualified domain name.
* Processes a sequence of RDNs having a DC attribute.
* The special RDN "DC=." denotes the root of the domain tree.
* Multi-valued RDNs, non-DC attributes, binary-valued attributes and the
* RDN "DC=." all reset the domain name and processing continues.
*
* @param dn A string distinguished name (RFC 2253).
* @return A domain name or null if none can be derived.
* @throw InvalidNameException If the distinugished name is invalid.
*/
static String mapDnToDomainName(String dn) throws InvalidNameException {
if (dn == null) {
return null;
}
StringBuffer domain = new StringBuffer();
LdapName ldapName = new LdapName(dn);
// process RDNs left-to-right
//List<Rdn> rdnList = ldapName.getRdns();
List<Rdn> rdnList = ldapName.getRdns();
for (int i = rdnList.size() - 1; i >= 0; i--) {
//Rdn rdn = rdnList.get(i);
Rdn rdn = rdnList.get(i);
// single-valued RDN with a DC attribute
if ((rdn.size() == 1) && ("dc".equalsIgnoreCase(rdn.getType()))) {
Object attrval = rdn.getValue();
if (attrval instanceof String) {
if (attrval.equals(".") || (domain.length() == 1 && domain.charAt(0) == '.')) {
// reset (when current or previous
domain.setLength(0);
// RDN value is "DC=.")
}
if (domain.length() > 0) {
domain.append('.');
}
domain.append(attrval);
} else {
// reset (when binary-valued attribute)
domain.setLength(0);
}
} else {
// reset (when multi-valued RDN or non-DC)
domain.setLength(0);
}
}
return (domain.length() != 0) ? domain.toString() : null;
}
use of javax.naming.ldap.LdapName in project jdk8u_jdk by JetBrains.
the class LdapSearchEnumeration method createItem.
@Override
protected SearchResult createItem(String dn, Attributes attrs, Vector<Control> respCtls) throws NamingException {
Object obj = null;
// name relative to starting search context
String relStart;
// name relative to homeCtx.currentDN
String relHome;
// whether relative to currentDN
boolean relative = true;
try {
Name parsed = new LdapName(dn);
if (startName != null && parsed.startsWith(startName)) {
relStart = parsed.getSuffix(startName.size()).toString();
relHome = parsed.getSuffix(homeCtx.currentParsedDN.size()).toString();
} else {
relative = false;
relHome = relStart = LdapURL.toUrlString(homeCtx.hostname, homeCtx.port_number, dn, homeCtx.hasLdapsScheme);
}
} catch (NamingException e) {
// could not parse name
relative = false;
relHome = relStart = LdapURL.toUrlString(homeCtx.hostname, homeCtx.port_number, dn, homeCtx.hasLdapsScheme);
}
// Name relative to search context
CompositeName cn = new CompositeName();
if (!relStart.equals("")) {
cn.add(relStart);
}
// Name relative to homeCtx
CompositeName rcn = new CompositeName();
if (!relHome.equals("")) {
rcn.add(relHome);
}
//System.err.println("relStart: " + cn);
//System.err.println("relHome: " + rcn);
// Fix attributes to be able to get schema
homeCtx.setParents(attrs, rcn);
// only generate object when requested
if (searchArgs.cons.getReturningObjFlag()) {
if (attrs.get(Obj.JAVA_ATTRIBUTES[Obj.CLASSNAME]) != null) {
// serialized object or object reference
try {
obj = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws NamingException {
return Obj.decodeObject(attrs);
}
}, acc);
} catch (PrivilegedActionException e) {
throw (NamingException) e.getException();
}
}
if (obj == null) {
obj = new LdapCtx(homeCtx, dn);
}
// Call getObjectInstance before removing unrequested attributes
try {
// rcn is either relative to homeCtx or a fully qualified DN
obj = DirectoryManager.getObjectInstance(obj, rcn, (relative ? homeCtx : null), homeCtx.envprops, attrs);
} catch (NamingException e) {
throw e;
} catch (Exception e) {
NamingException ne = new NamingException("problem generating object using object factory");
ne.setRootCause(e);
throw ne;
}
// remove Java attributes from result, if necessary
// Even if CLASSNAME attr not there, there might be some
// residual attributes
String[] reqAttrs;
if ((reqAttrs = searchArgs.reqAttrs) != null) {
// create an attribute set for those requested
// caseignore
Attributes rattrs = new BasicAttributes(true);
for (int i = 0; i < reqAttrs.length; i++) {
rattrs.put(reqAttrs[i], null);
}
for (int i = 0; i < Obj.JAVA_ATTRIBUTES.length; i++) {
// Remove Java-object attributes if not requested
if (rattrs.get(Obj.JAVA_ATTRIBUTES[i]) == null) {
attrs.remove(Obj.JAVA_ATTRIBUTES[i]);
}
}
}
}
/*
* name in search result is either the stringified composite name
* relative to the search context that can be passed directly to
* methods of the search context, or the fully qualified DN
* which can be used with the initial context.
*/
SearchResult sr;
if (respCtls != null) {
sr = new SearchResultWithControls((relative ? cn.toString() : relStart), obj, attrs, relative, homeCtx.convertControls(respCtls));
} else {
sr = new SearchResult((relative ? cn.toString() : relStart), obj, attrs, relative);
}
sr.setNameInNamespace(dn);
return sr;
}
use of javax.naming.ldap.LdapName in project jdk8u_jdk by JetBrains.
the class NamingEventNotifier method fireObjectRenamed.
/**
* Fires an "object renamed" to registered NamingListeners.
*/
private void fireObjectRenamed(Binding newBd, String oldDN, long changeID) {
if (namingListeners == null || namingListeners.size() == 0)
return;
Binding oldBd = null;
try {
LdapName dn = new LdapName(oldDN);
if (dn.startsWith(context.currentParsedDN)) {
String relDN = dn.getSuffix(context.currentParsedDN.size()).toString();
oldBd = new Binding(relDN, null);
}
} catch (NamingException e) {
}
if (oldBd == null) {
oldBd = new Binding(oldDN, null, false);
}
NamingEvent e = new NamingEvent(eventSrc, NamingEvent.OBJECT_RENAMED, newBd, oldBd, new Long(changeID));
support.queueEvent(e, namingListeners);
}
use of javax.naming.ldap.LdapName in project jdk8u_jdk by JetBrains.
the class LdapCtx method c_rename.
protected void c_rename(Name oldName, Name newName, Continuation cont) throws NamingException {
Name oldParsed, newParsed;
Name oldParent, newParent;
String newRDN = null;
String newSuperior = null;
// assert (oldName instanceOf CompositeName);
cont.setError(this, oldName);
try {
ensureOpen();
// permit oldName to be empty (for processing referral contexts)
if (oldName.isEmpty()) {
oldParent = parser.parse("");
} else {
// extract DN & parse
oldParsed = parser.parse(oldName.get(0));
oldParent = oldParsed.getPrefix(oldParsed.size() - 1);
}
if (newName instanceof CompositeName) {
// extract DN & parse
newParsed = parser.parse(newName.get(0));
} else {
// CompoundName/LdapName is already parsed
newParsed = newName;
}
newParent = newParsed.getPrefix(newParsed.size() - 1);
if (!oldParent.equals(newParent)) {
if (!clnt.isLdapv3) {
throw new InvalidNameException("LDAPv2 doesn't support changing " + "the parent as a result of a rename");
} else {
newSuperior = fullyQualifiedName(newParent.toString());
}
}
newRDN = newParsed.get(newParsed.size() - 1);
LdapResult answer = clnt.moddn(fullyQualifiedName(oldName), newRDN, deleteRDN, newSuperior, reqCtls);
// retrieve response controls
respCtls = answer.resControls;
if (answer.status != LdapClient.LDAP_SUCCESS) {
processReturnCode(answer, oldName);
}
} catch (LdapReferralException e) {
// Record the new RDN (for use after the referral is followed).
e.setNewRdn(newRDN);
// relative to a naming context BEFORE the referral is followed).
if (newSuperior != null) {
PartialResultException pre = new PartialResultException("Cannot continue referral processing when newSuperior is " + "nonempty: " + newSuperior);
pre.setRootCause(cont.fillInException(e));
throw cont.fillInException(pre);
}
if (handleReferrals == LdapClient.LDAP_REF_THROW)
throw cont.fillInException(e);
// process the referrals sequentially
while (true) {
LdapReferralContext refCtx = (LdapReferralContext) e.getReferralContext(envprops, bindCtls);
// repeat the original operation at the new context
try {
refCtx.rename(oldName, newName);
return;
} catch (LdapReferralException re) {
e = re;
continue;
} finally {
// Make sure we close referral context
refCtx.close();
}
}
} catch (IOException e) {
NamingException e2 = new CommunicationException(e.getMessage());
e2.setRootCause(e);
throw cont.fillInException(e2);
} catch (NamingException e) {
throw cont.fillInException(e);
}
}
use of javax.naming.ldap.LdapName in project jdk8u_jdk by JetBrains.
the class LdapCtx method addRdnAttributes.
/**
* Adds attributes from RDN to attrs if not already present.
* Note that if attrs already contains an attribute by the same name,
* or if the distinguished name is empty, then leave attrs unchanged.
*
* @param dn The non-null DN of the entry to add
* @param attrs The non-null attributes of entry to add
* @param directUpdate Whether attrs can be updated directly
* @returns Non-null attributes with attributes from the RDN added
*/
private static Attributes addRdnAttributes(String dn, Attributes attrs, boolean directUpdate) throws NamingException {
// Handle the empty name
if (dn.equals("")) {
return attrs;
}
// Parse string name into list of RDNs
List<Rdn> rdnList = (new LdapName(dn)).getRdns();
// Get leaf RDN
Rdn rdn = rdnList.get(rdnList.size() - 1);
Attributes nameAttrs = rdn.toAttributes();
// Add attributes of RDN to attrs if not already there
NamingEnumeration<? extends Attribute> enum_ = nameAttrs.getAll();
Attribute nameAttr;
while (enum_.hasMore()) {
nameAttr = enum_.next();
// If attrs already has the attribute, don't change or add to it
if (attrs.get(nameAttr.getID()) == null) {
/**
* When attrs.isCaseIgnored() is false, attrs.get() will
* return null when the case mis-matches for otherwise
* equal attrIDs.
* As the attrIDs' case is irrelevant for LDAP, ignore
* the case of attrIDs even when attrs.isCaseIgnored() is
* false. This is done by explicitly comparing the elements in
* the enumeration of IDs with their case ignored.
*/
if (!attrs.isCaseIgnored() && containsIgnoreCase(attrs.getIDs(), nameAttr.getID())) {
continue;
}
if (!directUpdate) {
attrs = (Attributes) attrs.clone();
directUpdate = true;
}
attrs.put(nameAttr);
}
}
return attrs;
}
Aggregations