Example usage for javax.naming.ldap LdapContext createSubcontext

List of usage examples for javax.naming.ldap LdapContext createSubcontext

Introduction

In this page you can find the example usage for javax.naming.ldap LdapContext createSubcontext.

Prototype

public DirContext createSubcontext(Name name, Attributes attrs) throws NamingException;

Source Link

Document

Creates and binds a new context, along with associated attributes.

Usage

From source file:edu.vt.middleware.ldap.AbstractLdap.java

/**
 * This will create the supplied dn in the LDAP namespace with the supplied
 * attributes. See {@link javax.naming.DirContext#createSubcontext(String,
 * Attributes)}. Note that the context created by this operation is
 * immediately closed./*from www  . j a va 2s.com*/
 *
 * @param  dn  <code>String</code> named object in the LDAP
 * @param  attrs  <code>Attributes</code> attributes to be added to this entry
 *
 * @throws  NamingException  if the LDAP returns an error
 */
protected void create(final String dn, final Attributes attrs) throws NamingException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Create name with the following parameters:");
        this.logger.debug("  dn = " + dn);
        this.logger.debug("  attrs = " + attrs);
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("  config = " + this.config.getEnvironment());
        }
    }

    LdapContext ctx = null;
    try {
        for (int i = 0; i <= this.config.getOperationRetry() || this.config.getOperationRetry() == -1; i++) {
            try {
                ctx = this.getContext();
                ctx.createSubcontext(dn, attrs).close();
                break;
            } catch (NamingException e) {
                this.operationRetry(ctx, e, i);
            }
        }
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:org.apache.directory.studio.connection.core.io.jndi.JNDIConnectionWrapper.java

/**
 * Creates an entry.//w w w  . j av a2s.co  m
 * 
 * @param dn the entry's Dn
 * @param attributes the entry's attributes
 * @param controls the controls
 * @param monitor the progress monitor
 * @param referralsInfo the referrals info
 */
public void createEntry(final String dn, final Attributes attributes, final Control[] controls,
        final StudioProgressMonitor monitor, final ReferralsInfo referralsInfo) {
    if (connection.isReadOnly()) {
        monitor.reportError(
                new Exception(NLS.bind(Messages.error__connection_is_readonly, connection.getName())));
        return;
    }

    InnerRunnable runnable = new InnerRunnable() {
        public void run() {
            try {
                // create modify context
                LdapContext modCtx = context.newInstance(controls);

                // use "throw" as we handle referrals manually
                modCtx.addToEnvironment(Context.REFERRAL, REFERRAL_THROW);

                // create entry
                modCtx.createSubcontext(getSaveJndiName(dn), attributes);
            } catch (ReferralException re) {
                try {
                    ReferralsInfo newReferralsInfo = handleReferralException(re, referralsInfo);
                    Referral referral = newReferralsInfo.getNextReferral();

                    if (referral != null) {
                        Connection referralConnection = ConnectionWrapperUtils.getReferralConnection(referral,
                                monitor, this);

                        if (referralConnection != null) {
                            List<String> urls = new ArrayList<>(referral.getLdapUrls());

                            String referralDn = new LdapUrl(urls.get(0)).getDn().getName();
                            referralConnection.getConnectionWrapper().createEntry(referralDn, attributes,
                                    controls, monitor, newReferralsInfo);
                        } else {
                            canceled = true;
                        }
                    }
                } catch (NamingException ne) {
                    namingException = ne;
                } catch (LdapURLEncodingException e) {
                    namingException = new NamingException(e.getMessage());
                }
            } catch (NamingException ne) {
                namingException = ne;
            }

            for (IJndiLogger logger : getJndiLoggers()) {
                logger.logChangetypeAdd(connection, dn, attributes, controls, namingException);
            }
        }
    };

    try {
        checkConnectionAndRunAndMonitor(runnable, monitor);
    } catch (NamingException ne) {
        monitor.reportError(ne);
    }

    if (runnable.isCanceled()) {
        monitor.setCanceled(true);
    }
    if (runnable.getException() != null) {
        monitor.reportError(runnable.getException());
    }
}