public ExtendedResponse extendedOperation(ExtendedRequest request) throws NamingException { ExtendedOp op = new ExtendedOp(request); try { doBasicOperation(op); } catch (ReferralException e) { if (isFollowReferral(e)) { LdapContext referralContext = (LdapContext) getReferralContext(e); return referralContext.extendedOperation(request); } throw e; } ExtendedResponse response = op.getExtendedResponse(); // set existing underlying socket to startTls extended response if (response instanceof StartTlsResponseImpl) { ((StartTlsResponseImpl) response).setSocket(client.getSocket()); } return response; }
private boolean isFollowReferral(ReferralException e) throws ReferralException, PartialResultException { // ignore referral String action = (String) env.get(Context.REFERRAL); if (action == null) { action = "ignore"; } if ("follow".equals(action)) { return true; } else if ("throw".equals(action)) { return false; } else if ("ignore".equals(action)) { // ldap.1A=[LDAP: error code 10 - Referral] throw new PartialResultException(Messages.getString("ldap.1A")); } else { throw new IllegalArgumentException(Messages.getString( "ldap.30", new Object[] { //$NON-NLS-1$ env.get(Context.REFERRAL), Context.REFERRAL })); } }
@Override public DirContext createSubcontext(Name name, Attributes attributes) throws NamingException { int size = name.size(); Hashtable<String, Object> subSchemaTree = doLookup(name .getPrefix(size - 1), size - 1); if (null == attributes || attributes.size() == 0) { // jndi.8D=Must supply attributes describing schema throw new SchemaViolationException(Messages.getString("jndi.8D")); //$NON-NLS-1$ } if (level - size == 2) { // jndi.8E=Cannot create new entry under schema root throw new SchemaViolationException(Messages.getString("jndi.8E")); //$NON-NLS-1$ } String subSchemaType = name.getSuffix(size - 1).toString(); if (subSchemaTree.get(subSchemaType.toLowerCase()) != null) { throw new NameAlreadyBoundException(subSchemaType); } String schemaLine = SchemaParser.format(attributes); ModifyOp op = new ModifyOp(ldapContext.subschemasubentry); Name modifySchemaName = name.getPrefix(size - 1).addAll(rdn); BasicAttribute schemaEntry = new LdapAttribute(new BasicAttribute( jndi2ldap(modifySchemaName.toString()), schemaLine), ldapContext); op.addModification(OperationJndi2Ldap[DirContext.ADD_ATTRIBUTE], new LdapAttribute(schemaEntry, ldapContext)); try { doBasicOperation(op); subSchemaTree.put(subSchemaType.toLowerCase(), schemaLine); } catch (ReferralException e) { // TODO } return (DirContext) lookup(name); }
private DirContext getReferralContext(ReferralException e) throws LimitExceededException, NamingException { int limit = 0; if (env.get("java.naming.ldap.referral.limit") != null) { limit = Integer.valueOf( (String) env.get("java.naming.ldap.referral.limit")) .intValue(); } if (limit == -1) { throw new LimitExceededException(Messages.getString("ldap.25")); //$NON-NLS-1$ } if (limit == 1) { limit = -1; } else if (limit != 0) { limit -= 1; } Hashtable<Object, Object> newEnv = (Hashtable<Object, Object>) env .clone(); newEnv.put("java.naming.ldap.referral.limit", String.valueOf(limit)); DirContext referralContext = null; while (true) { try { referralContext = (DirContext) e.getReferralContext(newEnv); break; } catch (NamingException ex) { if (e.skipReferral()) { continue; } throw ex; } } return referralContext; }
/** * Follow referrals in SearchResultReference. Referrals in * SearchResultReference is different with LDAPResult, which may contians * filter parts. Filter and dn part of url will overwrite filter and * baseObject of last search operation. * * @param op * last search operation * @param ex */ private void processSearchRef(SearchOp op, ReferralException ex) { LdapSearchResult result = op.getSearchResult(); List<String> urls = result.getRefURLs(); // clean referrals result.setRefURLs(null); try { for (String url : urls) { LdapUrlParser urlParser = LdapUtils.parserURL(url, true); // if url has dn part overwrite baseObject of last search // operation if (!urlParser.getBaseObject().equals("")) { op.setBaseObject(urlParser.getBaseObject()); } // if url has filter part overwrite filter of last search // operation if (urlParser.hasFilter()) { op.setFilter(urlParser.getFilter()); } LdapContextImpl ctx = (LdapContextImpl) getReferralContext(ex); result.setAddress("ldap://" + urlParser.getHost() + ":" + urlParser.getPort() + "/"); ctx.doSearch(op); result.setAddress(null); } } catch (NamingException e) { /* * occrus exception, set to search result and return, not continue * to follow referral * * TODO test the behavior of ri * */ result.setException(e); return; } }
public void rename(Name nOld, Name nNew) throws NamingException { checkName(nOld); checkName(nNew); if (!isInSameNamespace(nOld, nNew)) { throw new InvalidNameException(Messages.getString("ldap.2A")); //$NON-NLS-1$ } if (hasMultiNamingSpace(nOld) && hasMultiNamingSpace(nNew)) { Context context = findNnsContext(nOld); context.rename(nOld.getSuffix(1), nNew.getSuffix(1)); return; } // get absolute dn name String oldTargetDN = getTargetDN(nOld, contextDn); String newTargetDN = getTargetDN(nNew, contextDn); LdapName name = new LdapName(newTargetDN); Rdn rdn = name.getRdn(name.size() - 1); String value = (String) env.get(LDAP_DELETE_RDN); // true is default value boolean isDeleteRdn = true; if (value != null) { isDeleteRdn = Boolean.getBoolean(value); } ModifyDNOp op = new ModifyDNOp(oldTargetDN, rdn.toString(), isDeleteRdn, name.getPrefix(name.size() - 1).toString()); try { doBasicOperation(op); } catch (ReferralException e) { if (isFollowReferral(e)) { DirContext referralContext = getReferralContext(e); referralContext.rename(nOld, nNew); return; } throw e; } }
public void testAllCoveragePurpose() throws NamingException { log.setMethod("testAllCoveragePurpose()"); ReferralException ex = new MockReferralException(); ex = new MockReferralException("message"); ex.getReferralContext(); ex.getReferralContext(null); ex.getReferralInfo(); ex.skipReferral(); ex.retryReferral(); }
@Override public void destroySubcontext(Name name) throws NamingException { int size = name.size(); Hashtable<String, Object> subSchemaTree = doLookup(name .getPrefix(size - 1), size - 1); String subSchemaType = name.getSuffix(size - 1).toString() .toLowerCase(); Object schema = subSchemaTree.get(jndi2ldap(subSchemaType)); if (schema == null) { // Return silently. return; } if (level - size == 2) { // ldap.37=Can't delete schema root throw new SchemaViolationException(Messages.getString("ldap.37")); //$NON-NLS-1$ } if (level == size) { // Return silently. return; } String schemaLine = schema.toString(); if (schema instanceof Hashtable) { Hashtable<String, Object> table = (Hashtable<String, Object>) schema; schemaLine = table.get(SchemaParser.ORIG).toString(); } ModifyOp op = new ModifyOp(ldapContext.subschemasubentry); Name modifySchemaName = name.getPrefix(size - 1).addAll(rdn); BasicAttribute schemaEntry = new LdapAttribute(new BasicAttribute( jndi2ldap(modifySchemaName.toString()), schemaLine), ldapContext); op.addModification(OperationJndi2Ldap[DirContext.REMOVE_ATTRIBUTE], new LdapAttribute(schemaEntry, ldapContext)); try { doBasicOperation(op); subSchemaTree.remove(subSchemaType); } catch (ReferralException e) { // TODO } }
public void modifyAttributes(Name name, ModificationItem[] modificationItems) throws NamingException { checkName(name); if (modificationItems == null) { // FIXME: spec say ModificationItem may not be null, but ri // silence in this case throw new NullPointerException(Messages.getString("ldap.27")); //$NON-NLS-1$ } if (hasMultiNamingSpace(name)) { /* * multi ns, find next ns context, delegate operation to the next * context */ DirContext nns = (DirContext) findNnsContext(name); Name remainingName = name.getSuffix(1); nns.modifyAttributes(remainingName, modificationItems); return; } if (modificationItems.length == 0) { return; } /* * there is only one ldap ns */ // get absolute dn name String targetDN = getTargetDN(name, contextDn); ModifyOp op = new ModifyOp(targetDN); for (ModificationItem item : modificationItems) { switch (item.getModificationOp()) { case DirContext.ADD_ATTRIBUTE: op.addModification(0, new LdapAttribute(item.getAttribute(), this)); break; case DirContext.REMOVE_ATTRIBUTE: op.addModification(1, new LdapAttribute(item.getAttribute(), this)); break; case DirContext.REPLACE_ATTRIBUTE: op.addModification(2, new LdapAttribute(item.getAttribute(), this)); break; default: throw new IllegalArgumentException(Messages.getString( "jndi.14", item.getModificationOp())); //$NON-NLS-1$ } } try { doBasicOperation(op); } catch (ReferralException e) { if (isFollowReferral(e)) { DirContext referralContext = getReferralContext(e); referralContext.modifyAttributes(name, modificationItems); return; } throw e; } }
public DirContext createSubcontext(Name name, Attributes attributes) throws NamingException { checkName(name); if (hasMultiNamingSpace(name)) { /* * multi ns, find next ns context, delegate operation to the next * context */ DirContext nns = (DirContext) findNnsContext(name); Name remainingName = name.getSuffix(1); return nns.createSubcontext(remainingName, attributes); } /* * there is only one ldap ns */ if (attributes == null) { attributes = new BasicAttributes(true); Attribute attr = new LdapAttribute("objectClass", this); attr.add("top"); attr.add("javaContainer"); attributes.put(attr); } // get absolute dn name String targetDN = getTargetDN(name, contextDn); // merge attributes from dn and args Attributes attrs = mergeAttributes(attributes, getAttributesFromDN(name)); // convert to LdapAttribute List<LdapAttribute> la = new ArrayList<LdapAttribute>(attrs.size()); NamingEnumeration<? extends Attribute> enu = attrs.getAll(); while (enu.hasMore()) { Attribute att = enu.next(); if (att.size() > 0) { la.add(new LdapAttribute(att, this)); } } // do add operation AddOp op = new AddOp(targetDN, la); try { doBasicOperation(op); } catch (ReferralException e) { if (isFollowReferral(e)) { DirContext referralContext = getReferralContext(e); return referralContext.createSubcontext(name, attributes); } throw e; } LdapResult result = op.getResult(); return new LdapContextImpl(this, env, result.getMachedDN()); }