下面列出了怎么用javax.naming.CompositeName的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testUnbindRecursive() throws Exception {
expectGetReadWriteContext();
when(namingEnumerationMock.hasMore()).thenReturn(true, false, false);
Binding binding = new Binding("cn=Some name", null);
when(namingEnumerationMock.next()).thenReturn(binding);
LdapName listDn = LdapUtils.newLdapName(DEFAULT_BASE_STRING);
when(dirContextMock.listBindings(listDn)).thenReturn(namingEnumerationMock);
LdapName subListDn = LdapUtils.newLdapName("cn=Some name, o=example.com");
when(dirContextMock.listBindings(subListDn)).thenReturn(namingEnumerationMock);
tested.unbind(new CompositeName(DEFAULT_BASE_STRING), true);
verify(dirContextMock).unbind(subListDn);
verify(dirContextMock).unbind(listDn);
verify(namingEnumerationMock, times(2)).close();
verify(dirContextMock).close();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain;UTF-8");
PrintWriter out = resp.getWriter();
try {
Context initCtx = new InitialContext();
Boolean b1 = (Boolean) initCtx.lookup(JNDI_NAME);
Boolean b2 = (Boolean) initCtx.lookup(
new CompositeName(JNDI_NAME));
out.print(b1);
out.print(b2);
} catch (NamingException ne) {
throw new ServletException(ne);
}
}
protected DirContextNamePair getTargetContext(Name name)
throws NamingException {
if (cpe.getResolvedObj() == null)
throw (NamingException)cpe.fillInStackTrace();
Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
cpe.getAltName(),
cpe.getAltNameCtx(),
env);
if (ctx == null)
throw (NamingException)cpe.fillInStackTrace();
if (ctx instanceof DirContext)
return new DirContextNamePair((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();
return (new DirContextNamePair(dctx, rr.getRemainingName()));
}
// 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 DirContextNamePair((DirContext)ultimate,
new CompositeName()));
}
throw (NamingException)cpe.fillInStackTrace();
}
/**
* Constructs a new instance of ResolveResult consisting of
* the resolved object and the remaining unresolved component.
*
* @param robj The non-null object resolved to.
* @param rcomp The single remaining name component that has yet to be
* resolved. Cannot be null (but can be empty).
*/
public ResolveResult(Object robj, String rcomp) {
resolvedObj = robj;
try {
remainingName = new CompositeName(rcomp);
// remainingName.appendComponent(rcomp);
} catch (InvalidNameException e) {
// ignore; shouldn't happen
}
}
/**
* Construct a new <code>DistinguishedName</code> from the supplied
* {@link Name}. The parts of the supplied {@link Name} must be
* syntactically correct {@link LdapRdn}s.
*
* @param name the {@link Name} to construct a new
* <code>DistinguishedName</code> from.
*/
public DistinguishedName(Name name) {
Assert.notNull(name, "name cannot be null");
if (name instanceof CompositeName) {
parse(LdapUtils.convertCompositeNameToString((CompositeName) name));
return;
}
names = new LinkedList();
for (int i = 0; i < name.size(); i++) {
names.add(new LdapRdn(name.get(i)));
}
}
@Test
public void testConstructAdapterFromName() throws InvalidNameException {
CompositeName name = new CompositeName();
name.add("ldap://localhost:389/ou=People,o=JNDITutorial");
DefaultDirObjectFactory tested = new DefaultDirObjectFactory();
DirContextAdapter result = tested.constructAdapterFromName(new BasicAttributes(), name, "");
assertThat(result.getDn().toString()).isEqualTo("ou=People,o=JNDITutorial");
assertThat(result.getReferralUrl().toString()).isEqualTo("ldap://localhost:389");
}
/**
* Constructs a new instance of ResolveResult consisting of
* the resolved object and the remaining unresolved component.
*
* @param robj The non-null object resolved to.
* @param rcomp The single remaining name component that has yet to be
* resolved. Cannot be null (but can be empty).
*/
public ResolveResult(Object robj, String rcomp) {
resolvedObj = robj;
try {
remainingName = new CompositeName(rcomp);
// remainingName.appendComponent(rcomp);
} catch (InvalidNameException e) {
// ignore; shouldn't happen
}
}
protected DirContextNamePair getTargetContext(Name name)
throws NamingException {
if (cpe.getResolvedObj() == null)
throw (NamingException)cpe.fillInStackTrace();
Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
cpe.getAltName(),
cpe.getAltNameCtx(),
env);
if (ctx == null)
throw (NamingException)cpe.fillInStackTrace();
if (ctx instanceof DirContext)
return new DirContextNamePair((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();
return (new DirContextNamePair(dctx, rr.getRemainingName()));
}
// 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 DirContextNamePair((DirContext)ultimate,
new CompositeName()));
}
throw (NamingException)cpe.fillInStackTrace();
}
protected DirContextNamePair getTargetContext(Name name)
throws NamingException {
if (cpe.getResolvedObj() == null)
throw (NamingException)cpe.fillInStackTrace();
Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
cpe.getAltName(),
cpe.getAltNameCtx(),
env);
if (ctx == null)
throw (NamingException)cpe.fillInStackTrace();
if (ctx instanceof DirContext)
return new DirContextNamePair((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();
return (new DirContextNamePair(dctx, rr.getRemainingName()));
}
// 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 DirContextNamePair((DirContext)ultimate,
new CompositeName()));
}
throw (NamingException)cpe.fillInStackTrace();
}
public Object lookup(String name) throws NamingException {
if (TraceCarol.isDebugJndiCarol()) {
TraceCarol.debugJndiCarol("LmiInitialContext.lookup(\"" + name + "\")");
}
if ((name == null) || (name.equals(""))) {
return (new LmiInitialContext(lmiEnv));
}
Object o = bindings.get(name);
if (o != null) {
return resolveObject(o, new CompositeName(name));
} else {
throw new NameNotFoundException(name + " not found");
}
}
/**
* Adds a single component to the end of remaining name.
*
* @param name The component to add. Can be null.
* @see #getRemainingName
* @see #appendRemainingName
*/
public void appendRemainingComponent(String name) {
if (name != null) {
CompositeName rname = new CompositeName();
try {
rname.add(name);
} catch (InvalidNameException e) {
// ignore; shouldn't happen for empty composite name
}
appendRemainingName(rname);
}
}
private String checkName(String name) throws CertStoreException {
if (name == null) {
throw new CertStoreException("Name absent");
}
try {
if (new CompositeName(name).size() > 1) {
throw new CertStoreException("Invalid name: " + name);
}
} catch (InvalidNameException ine) {
throw new CertStoreException("Invalid name: " + name, ine);
}
return name;
}
/**
* Adds a single component to the end of remaining name.
*
* @param name The component to add. Can be null.
* @see #getRemainingName
* @see #appendRemainingName
*/
public void appendRemainingComponent(String name) {
if (name != null) {
CompositeName rname = new CompositeName();
try {
rname.add(name);
} catch (InvalidNameException e) {
// ignore; shouldn't happen for empty composite name
}
appendRemainingName(rname);
}
}
/**
* Constructs a new instance of ResolveResult consisting of
* the resolved object and the remaining unresolved component.
*
* @param robj The non-null object resolved to.
* @param rcomp The single remaining name component that has yet to be
* resolved. Cannot be null (but can be empty).
*/
public ResolveResult(Object robj, String rcomp) {
resolvedObj = robj;
try {
remainingName = new CompositeName(rcomp);
// remainingName.appendComponent(rcomp);
} catch (InvalidNameException e) {
// ignore; shouldn't happen
}
}
/**
* Adds a single component to the end of remaining name.
*
* @param name The component to add. Can be null.
* @see #getRemainingName
* @see #appendRemainingName
*/
public void appendRemainingComponent(String name) {
if (name != null) {
CompositeName rname = new CompositeName();
try {
rname.add(name);
} catch (InvalidNameException e) {
// ignore; shouldn't happen for empty composite name
}
appendRemainingName(rname);
}
}
/**
* Converts a CompositeName to a String in a way that avoids escaping
* problems, such as the dreaded "triple backslash" problem.
*
* @param compositeName The CompositeName to convert
* @return String containing the String representation of <code>name</code>
*/
public static String convertCompositeNameToString(
CompositeName compositeName) {
if (compositeName.size() > 0) {
// A lookup with an empty String seems to produce an empty
// compositeName here; need to take this into account.
return compositeName.get(0);
}
else {
return "";
}
}
/**
* Constructs a new instance of ResolveResult consisting of
* the resolved object and the remaining unresolved component.
*
* @param robj The non-null object resolved to.
* @param rcomp The single remaining name component that has yet to be
* resolved. Cannot be null (but can be empty).
*/
public ResolveResult(Object robj, String rcomp) {
resolvedObj = robj;
try {
remainingName = new CompositeName(rcomp);
// remainingName.appendComponent(rcomp);
} catch (InvalidNameException e) {
// ignore; shouldn't happen
}
}
/**
* Adds a single component to the end of remaining name.
*
* @param name The component to add. Can be null.
* @see #getRemainingName
* @see #appendRemainingName
*/
public void appendRemainingComponent(String name) {
if (name != null) {
CompositeName rname = new CompositeName();
try {
rname.add(name);
} catch (InvalidNameException e) {
// ignore; shouldn't happen for empty composite name
}
appendRemainingName(rname);
}
}
protected DirContextNamePair getTargetContext(Name name)
throws NamingException {
if (cpe.getResolvedObj() == null)
throw (NamingException)cpe.fillInStackTrace();
Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
cpe.getAltName(),
cpe.getAltNameCtx(),
env);
if (ctx == null)
throw (NamingException)cpe.fillInStackTrace();
if (ctx instanceof DirContext)
return new DirContextNamePair((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();
return (new DirContextNamePair(dctx, rr.getRemainingName()));
}
// 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 DirContextNamePair((DirContext)ultimate,
new CompositeName()));
}
throw (NamingException)cpe.fillInStackTrace();
}
/**
* Constructs a new instance of ResolveResult consisting of
* the resolved object and the remaining unresolved component.
*
* @param robj The non-null object resolved to.
* @param rcomp The single remaining name component that has yet to be
* resolved. Cannot be null (but can be empty).
*/
public ResolveResult(Object robj, String rcomp) {
resolvedObj = robj;
try {
remainingName = new CompositeName(rcomp);
// remainingName.appendComponent(rcomp);
} catch (InvalidNameException e) {
// ignore; shouldn't happen
}
}
/**
* Adds a single component to the end of remaining name.
*
* @param name The component to add. Can be null.
* @see #getRemainingName
* @see #appendRemainingName
*/
public void appendRemainingComponent(String name) {
if (name != null) {
CompositeName rname = new CompositeName();
try {
rname.add(name);
} catch (InvalidNameException e) {
// ignore; shouldn't happen for empty composite name
}
appendRemainingName(rname);
}
}
protected DirContextNamePair getTargetContext(Name name)
throws NamingException {
if (cpe.getResolvedObj() == null)
throw (NamingException)cpe.fillInStackTrace();
Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
cpe.getAltName(),
cpe.getAltNameCtx(),
env);
if (ctx == null)
throw (NamingException)cpe.fillInStackTrace();
if (ctx instanceof DirContext)
return new DirContextNamePair((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();
return (new DirContextNamePair(dctx, rr.getRemainingName()));
}
// 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 DirContextNamePair((DirContext)ultimate,
new CompositeName()));
}
throw (NamingException)cpe.fillInStackTrace();
}
/**
* Constructs a new instance of ResolveResult consisting of
* the resolved object and the remaining unresolved component.
*
* @param robj The non-null object resolved to.
* @param rcomp The single remaining name component that has yet to be
* resolved. Cannot be null (but can be empty).
*/
public ResolveResult(Object robj, String rcomp) {
resolvedObj = robj;
try {
remainingName = new CompositeName(rcomp);
// remainingName.appendComponent(rcomp);
} catch (InvalidNameException e) {
// ignore; shouldn't happen
}
}
private String checkName(String name) throws CertStoreException {
if (name == null) {
throw new CertStoreException("Name absent");
}
try {
if (new CompositeName(name).size() > 1) {
throw new CertStoreException("Invalid name: " + name);
}
} catch (InvalidNameException ine) {
throw new CertStoreException("Invalid name: " + name, ine);
}
return name;
}
protected DirContextNamePair getTargetContext(Name name)
throws NamingException {
if (cpe.getResolvedObj() == null)
throw (NamingException)cpe.fillInStackTrace();
Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
cpe.getAltName(),
cpe.getAltNameCtx(),
env);
if (ctx == null)
throw (NamingException)cpe.fillInStackTrace();
if (ctx instanceof DirContext)
return new DirContextNamePair((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();
return (new DirContextNamePair(dctx, rr.getRemainingName()));
}
// 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 DirContextNamePair((DirContext)ultimate,
new CompositeName()));
}
throw (NamingException)cpe.fillInStackTrace();
}
protected DirContextNamePair getTargetContext(Name name)
throws NamingException {
if (cpe.getResolvedObj() == null)
throw (NamingException)cpe.fillInStackTrace();
Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
cpe.getAltName(),
cpe.getAltNameCtx(),
env);
if (ctx == null)
throw (NamingException)cpe.fillInStackTrace();
if (ctx instanceof DirContext)
return new DirContextNamePair((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();
return (new DirContextNamePair(dctx, rr.getRemainingName()));
}
// 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 DirContextNamePair((DirContext)ultimate,
new CompositeName()));
}
throw (NamingException)cpe.fillInStackTrace();
}
/**
* Adds a single component to the end of remaining name.
*
* @param name The component to add. Can be null.
* @see #getRemainingName
* @see #appendRemainingName
*/
public void appendRemainingComponent(String name) {
if (name != null) {
CompositeName rname = new CompositeName();
try {
rname.add(name);
} catch (InvalidNameException e) {
// ignore; shouldn't happen for empty composite name
}
appendRemainingName(rname);
}
}
protected DirContextNamePair getTargetContext(Name name)
throws NamingException {
if (cpe.getResolvedObj() == null)
throw (NamingException)cpe.fillInStackTrace();
Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
cpe.getAltName(),
cpe.getAltNameCtx(),
env);
if (ctx == null)
throw (NamingException)cpe.fillInStackTrace();
if (ctx instanceof DirContext)
return new DirContextNamePair((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();
return (new DirContextNamePair(dctx, rr.getRemainingName()));
}
// 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 DirContextNamePair((DirContext)ultimate,
new CompositeName()));
}
throw (NamingException)cpe.fillInStackTrace();
}
/**
* Adds a single component to the end of remaining name.
*
* @param name The component to add. Can be null.
* @see #getRemainingName
* @see #appendRemainingName
*/
public void appendRemainingComponent(String name) {
if (name != null) {
CompositeName rname = new CompositeName();
try {
rname.add(name);
} catch (InvalidNameException e) {
// ignore; shouldn't happen for empty composite name
}
appendRemainingName(rname);
}
}
/**
* Test destroySubcontext method.
*
* @throws Exception when an error occurs.
*/
@Test
public void testDestroySubcontext2() throws Exception {
DefaultInitialContext context = new DefaultInitialContext();
context.createSubcontext(new CompositeName("context"));
context.bind("context/name", 12);
assertThrows(ContextNotEmptyException.class, () -> context.destroySubcontext("context"));
}