Example usage for javax.naming Name toString

List of usage examples for javax.naming Name toString

Introduction

In this page you can find the example usage for javax.naming Name toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.springframework.ldap.core.DirContextAdapter.java

/**
 * @see javax.naming.directory.DirContext#getAttributes(Name, String[])
 *//*w  w w. j  a v a 2s  .  c o m*/
public Attributes getAttributes(Name name, String[] attrIds) throws NamingException {
    return getAttributes(name.toString(), attrIds);
}

From source file:org.springframework.ldap.core.DirContextAdapter.java

public final void setDn(Name dn) {
    if (!updateMode) {
        this.dn = new DistinguishedName(dn.toString());
    } else {/*from w  ww  .  j a  v  a 2  s  .  c  o m*/
        throw new IllegalStateException("Not possible to call setDn() on a DirContextAdapter in update mode");
    }

}

From source file:org.springframework.ldap.core.support.DefaultDirObjectFactory.java

/**
 * Construct a DirContextAdapter given the supplied paramters. The
 * <code>name</code> is normally a JNDI <code>CompositeName</code>, which
 * needs to be handled with particuclar care. Specifically the escaping of a
 * <code>CompositeName</code> destroys proper escaping of Distinguished
 * Names. Also, the name might contain referral information, in which case
 * we need to separate the server information from the actual Distinguished
 * Name so that we can create a representing DirContextAdapter.
 * /* w w  w  . j  a  va  2 s  .  c  o  m*/
 * @param attrs the attributes
 * @param name the Name, typically a <code>CompositeName</code>, possibly
 * including referral information.
 * @param nameInNamespace the Name in namespace.
 * @return a {@link DirContextAdapter} representing the specified
 * information.
 */
DirContextAdapter constructAdapterFromName(Attributes attrs, Name name, String nameInNamespace) {
    String nameString = "";
    String referralUrl = "";

    if (name instanceof CompositeName) {
        // Which it most certainly will be, and therein lies the
        // problem. CompositeName.toString() completely screws up the
        // formatting
        // in some cases, particularly when backslashes are involved.
        nameString = LdapUtils.convertCompositeNameToString((CompositeName) name);
    } else {
        log.warn("Expecting a CompositeName as input to getObjectInstance but received a '"
                + name.getClass().toString() + "' - using toString and proceeding with undefined results");
        nameString = name.toString();
    }

    if (nameString.startsWith(LDAP_PROTOCOL_PREFIX) || nameString.startsWith(LDAPS_PROTOCOL_PREFIX)) {
        if (log.isDebugEnabled()) {
            log.debug("Received name '" + nameString + "' contains protocol delimiter; indicating a referral."
                    + "Stripping protocol and address info to enable construction of a proper DistinguishedName");
        }
        try {
            URI url = new URI(nameString);
            String pathString = url.getPath();
            referralUrl = nameString.substring(0, nameString.length() - pathString.length());

            if (StringUtils.hasLength(pathString) && pathString.startsWith("/")) {
                // We don't want any slash in the beginning of the
                // Distinguished Name.
                pathString = pathString.substring(1);
            }

            nameString = pathString;
        } catch (URISyntaxException e) {
            if (JdkVersion.isAtLeastJava15()) {
                throw new IllegalArgumentException(
                        "Supplied name starts with protocol prefix indicating a referral,"
                                + " but is not possible to parse to an URI",
                        e);
            } else {
                throw new IllegalArgumentException(
                        "Supplied name starts with protocol prefix indicating a referral,"
                                + " but is not possible to parse to an URI: " + e.getMessage());
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Resulting name after removal of referral information: '" + nameString + "'");
        }
    }

    DirContextAdapter dirContextAdapter = new DirContextAdapter(attrs, new DistinguishedName(nameString),
            new DistinguishedName(nameInNamespace), referralUrl);
    dirContextAdapter.setUpdateMode(true);

    return dirContextAdapter;
}

From source file:org.springframework.ldap.samples.article.dao.PersonDaoImpl.java

public void create(Person person) {
    Name dn = buildDn(person);
    System.out.println(dn.toString());
    log.info(dn.toString());/*from  www.j a  v a2s.co  m*/
    DirContextAdapter context = new DirContextAdapter(dn);
    mapToContext(person, context);
    ldapTemplate.bind(dn, context, null);
}