Example usage for javax.naming.directory Attributes toString

List of usage examples for javax.naming.directory Attributes toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.nuxeo.ecm.directory.ldap.LDAPReference.java

/**
 * Store new links using the LDAP staticAttributeId strategy.
 *
 * @see org.nuxeo.ecm.directory.Reference#addLinks(List, String)
 *//*from w w w .j a v a  2  s .  c  o  m*/
@Override
public void addLinks(List<String> sourceIds, String targetId) throws DirectoryException {
    String attributeId = getStaticAttributeId();
    if (attributeId == null && !sourceIds.isEmpty()) {
        log.warn("trying to edit a non-static reference: ignoring");
        return;
    }
    LDAPDirectory ldapTargetDirectory = (LDAPDirectory) getTargetDirectory();
    LDAPDirectory ldapSourceDirectory = (LDAPDirectory) getSourceDirectory();

    String emptyRefMarker = ldapSourceDirectory.getDescriptor().getEmptyRefMarker();
    try (LDAPSession targetSession = (LDAPSession) ldapTargetDirectory.getSession();
            LDAPSession sourceSession = (LDAPSession) ldapSourceDirectory.getSession()) {
        if (!sourceSession.isReadOnly()) {
            // compute the target dn to add to all the matching source
            // entries
            SearchResult ldapEntry = targetSession.getLdapEntry(targetId);
            if (ldapEntry == null) {
                throw new DirectoryException(
                        String.format("could not add links to unexisting %s in directory %s", targetId,
                                ldapTargetDirectory.getName()));
            }
            String targetAttributeValue;
            if (staticAttributeIdIsDn) {
                targetAttributeValue = ldapEntry.getNameInNamespace();
            } else {
                targetAttributeValue = targetId;
            }

            for (String sourceId : sourceIds) {
                // fetch the entry to be able to run the security policy
                // implemented in an entry adaptor
                DocumentModel sourceEntry = sourceSession.getEntry(sourceId, false);
                if (sourceEntry == null) {
                    log.warn(String.format(
                            "entry %s in directory %s not found: could not add link to %s in directory %s",
                            sourceId, ldapSourceDirectory.getName(), targetId, ldapTargetDirectory.getName()));
                    continue;
                }
                if (BaseSession.isReadOnlyEntry(sourceEntry)) {
                    // skip this entry since it cannot be edited to add the
                    // reference to targetId
                    log.warn(String.format(
                            "entry %s in directory %s is readonly: could not add link to %s in directory %s",
                            sourceId, ldapSourceDirectory.getName(), targetId, ldapTargetDirectory.getName()));
                    continue;
                }
                ldapEntry = sourceSession.getLdapEntry(sourceId);
                String sourceDn = ldapEntry.getNameInNamespace();
                Attribute storedAttr = ldapEntry.getAttributes().get(attributeId);
                if (storedAttr.contains(targetAttributeValue)) {
                    // no need to readd
                    continue;
                }
                try {
                    // add the new dn
                    Attributes attrs = new BasicAttributes(attributeId, targetAttributeValue);

                    if (log.isDebugEnabled()) {
                        log.debug(String.format(
                                "LDAPReference.addLinks([%s], %s): LDAP modifyAttributes dn='%s'"
                                        + " mod_op='ADD_ATTRIBUTE' attrs='%s' [%s]",
                                StringUtils.join(sourceIds, ", "), targetId, sourceDn, attrs, this));
                    }
                    sourceSession.dirContext.modifyAttributes(sourceDn, DirContext.ADD_ATTRIBUTE, attrs);

                    // robustly clean any existing empty marker now that we
                    // are sure that the list in not empty
                    if (storedAttr.contains(emptyRefMarker)) {
                        Attributes cleanAttrs = new BasicAttributes(attributeId, emptyRefMarker);
                        if (log.isDebugEnabled()) {
                            log.debug(String.format(
                                    "LDAPReference.addLinks(%s, %s): LDAP modifyAttributes dn='%s'"
                                            + " mod_op='REMOVE_ATTRIBUTE' attrs='%s' [%s]",
                                    StringUtils.join(sourceIds, ", "), targetId, sourceDn,
                                    cleanAttrs.toString(), this));
                        }
                        sourceSession.dirContext.modifyAttributes(sourceDn, DirContext.REMOVE_ATTRIBUTE,
                                cleanAttrs);
                    }
                } catch (SchemaViolationException e) {
                    if (isDynamic()) {
                        // we are editing an entry that has no static part
                        log.warn(String.format("cannot add dynamic reference in field %s for target %s",
                                getFieldName(), targetId));
                    } else {
                        // this is a real schema configuration problem,
                        // wrap the exception
                        throw new DirectoryException(e);
                    }
                }
            }
        }
    } catch (NamingException e) {
        throw new DirectoryException("addLinks failed: " + e.getMessage(), e);
    }
}

From source file:org.sipfoundry.sipxconfig.bulk.ldap.LdapRowInserter.java

@Override
protected void insertRow(SearchResult searchResult) {
    Attributes attrs = searchResult.getAttributes();
    LOG.info("Inserting:" + attrs.toString());
    insertRow(searchResult, attrs);//from w w w .ja va2  s .c o  m
}