Search in sources :

Example 41 with Attribute

use of javax.naming.directory.Attribute in project pentaho-kettle by pentaho.

the class LDAPConnection method update.

public int update(String dn, String[] attributes, String[] values, boolean checkEntry) throws KettleException {
    try {
        int nrAttributes = attributes.length;
        ModificationItem[] mods = new ModificationItem[nrAttributes];
        for (int i = 0; i < nrAttributes; i++) {
            // Define attribute
            Attribute mod = new BasicAttribute(attributes[i], values[i]);
            if (log.isDebug()) {
                log.logDebug(BaseMessages.getString(PKG, "LDAPConnection.Update.Attribute", attributes[i], values[i]));
            }
            // Save update action on attribute
            mods[i] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod);
        }
        // We have all requested attribute
        // let's update now
        getInitialContext().modifyAttributes(dn, mods);
        return STATUS_UPDATED;
    } catch (NameNotFoundException n) {
        // The entry is not found
        if (checkEntry) {
            throw new KettleException(BaseMessages.getString(PKG, "LDAPConnection.Error.Deleting.NameNotFound", dn), n);
        }
        return STATUS_SKIPPED;
    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "LDAPConnection.Error.Update", dn), e);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) KettleException(org.pentaho.di.core.exception.KettleException) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) NameNotFoundException(javax.naming.NameNotFoundException) KettleException(org.pentaho.di.core.exception.KettleException) NameNotFoundException(javax.naming.NameNotFoundException)

Example 42 with Attribute

use of javax.naming.directory.Attribute in project pentaho-kettle by pentaho.

the class LDAPConnection method buildAttributes.

private Attributes buildAttributes(String dn, String[] attributes, String[] values, String multValuedSeparator) {
    Attributes attrs = new javax.naming.directory.BasicAttributes(true);
    int nrAttributes = attributes.length;
    for (int i = 0; i < nrAttributes; i++) {
        if (!Utils.isEmpty(values[i])) {
            // We have a value
            String value = values[i].trim();
            if (multValuedSeparator != null && value.indexOf(multValuedSeparator) > 0) {
                Attribute attr = new javax.naming.directory.BasicAttribute(attributes[i]);
                for (String attribute : value.split(multValuedSeparator)) {
                    attr.add(attribute);
                }
                attrs.put(attr);
            } else {
                attrs.put(attributes[i], value);
            }
        }
    }
    return attrs;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes)

Example 43 with Attribute

use of javax.naming.directory.Attribute in project pentaho-kettle by pentaho.

the class LDAPConnection method getFields.

public RowMeta getFields(String searchBase) throws KettleException {
    RowMeta fields = new RowMeta();
    List<String> fieldsl = new ArrayList<String>();
    try {
        search(searchBase, null, 0, null, SEARCH_SCOPE_SUBTREE_SCOPE);
        Attributes attributes = null;
        fieldsl = new ArrayList<String>();
        while ((attributes = getAttributes()) != null) {
            NamingEnumeration<? extends Attribute> ne = attributes.getAll();
            while (ne.hasMore()) {
                Attribute attr = ne.next();
                String fieldName = attr.getID();
                if (!fieldsl.contains(fieldName)) {
                    fieldsl.add(fieldName);
                    String attributeValue = attr.get().toString();
                    int valueType;
                    // 
                    if (IsDate(attributeValue)) {
                        valueType = ValueMetaInterface.TYPE_DATE;
                    } else if (IsInteger(attributeValue)) {
                        valueType = ValueMetaInterface.TYPE_INTEGER;
                    } else if (IsNumber(attributeValue)) {
                        valueType = ValueMetaInterface.TYPE_NUMBER;
                    } else {
                        valueType = ValueMetaInterface.TYPE_STRING;
                    }
                    ValueMetaInterface value = ValueMetaFactory.createValueMeta(fieldName, valueType);
                    fields.addValueMeta(value);
                }
            }
        }
        return fields;
    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "LDAPConnection.Error.RetrievingFields"));
    } finally {
        fieldsl = null;
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) RowMeta(org.pentaho.di.core.row.RowMeta) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) KettleException(org.pentaho.di.core.exception.KettleException) NameNotFoundException(javax.naming.NameNotFoundException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 44 with Attribute

use of javax.naming.directory.Attribute in project pentaho-kettle by pentaho.

the class LDAPInput method buildRow.

private Object[] buildRow() throws KettleException {
    // Build an empty row based on the meta-data
    Object[] outputRowData = buildEmptyRow();
    if (data.dynamic) {
        // Reserve room for new row
        System.arraycopy(data.readRow, 0, outputRowData, 0, data.readRow.length);
    }
    try {
        // Execute for each Input field...
        for (int i = 0; i < meta.getInputFields().length; i++) {
            LDAPInputField field = meta.getInputFields()[i];
            // Get attribute value
            int index = data.nrIncomingFields + i;
            Attribute attr = data.attributes.get(field.getRealAttribute());
            if (attr != null) {
                // Let's try to get value of this attribute
                outputRowData[index] = getAttributeValue(field, attr, index, outputRowData[index]);
            }
            // Do we need to repeat this field if it is null?
            if (field.isRepeated()) {
                if (data.previousRow != null && outputRowData[index] == null) {
                    outputRowData[index] = data.previousRow[index];
                }
            }
        }
        // End of loop over fields...
        int fIndex = data.nrIncomingFields + data.nrfields;
        // See if we need to add the row number to the row...
        if (meta.includeRowNumber() && !Utils.isEmpty(meta.getRowNumberField())) {
            outputRowData[fIndex] = new Long(data.rownr);
        }
        RowMetaInterface irow = getInputRowMeta();
        // copy it to make
        data.previousRow = irow == null ? outputRowData : irow.cloneRow(outputRowData);
        // surely the next step doesn't change it in between...
        data.rownr++;
        incrementLinesInput();
    } catch (Exception e) {
        throw new KettleException(BaseMessages.getString(PKG, "LDAPInput.Exception.CanNotReadLDAP"), e);
    }
    return outputRowData;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Attribute(javax.naming.directory.Attribute) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) KettleException(org.pentaho.di.core.exception.KettleException)

Example 45 with Attribute

use of javax.naming.directory.Attribute in project iaf by ibissource.

the class LdapSender method performOperationUpdate.

private String performOperationUpdate(String entryName, ParameterResolutionContext prc, Map paramValueMap, Attributes attrs) throws SenderException, ParameterException {
    String entryNameAfter = entryName;
    if (paramValueMap != null) {
        String newEntryName = (String) paramValueMap.get("newEntryName");
        if (newEntryName != null && StringUtils.isNotEmpty(newEntryName)) {
            if (log.isDebugEnabled())
                log.debug("newEntryName=[" + newEntryName + "]");
            DirContext dirContext = null;
            try {
                dirContext = getDirContext(paramValueMap);
                dirContext.rename(entryName, newEntryName);
                entryNameAfter = newEntryName;
            } catch (NamingException e) {
                String msg;
                // [LDAP: error code 32 - No Such Object...
                if (e.getMessage().startsWith("[LDAP: error code 32 - ")) {
                    msg = "Operation [" + getOperation() + "] failed - wrong entryName [" + entryName + "]";
                } else {
                    msg = "Exception in operation [" + getOperation() + "] entryName [" + entryName + "]";
                }
                storeLdapException(e, prc);
                throw new SenderException(msg, e);
            } finally {
                closeDirContext(dirContext);
            }
        }
    }
    if (manipulationSubject.equals(MANIPULATION_ATTRIBUTE)) {
        if (attrs == null && !entryNameAfter.equals(entryName)) {
            // it should be possible to only 'rename' the entry (without attribute change)
            return DEFAULT_RESULT;
        }
        NamingEnumeration na = attrs.getAll();
        while (na.hasMoreElements()) {
            Attribute a = (Attribute) na.nextElement();
            log.debug("Update attribute: " + a.getID());
            NamingEnumeration values;
            try {
                values = a.getAll();
            } catch (NamingException e1) {
                storeLdapException(e1, prc);
                throw new SenderException("cannot obtain values of Attribute [" + a.getID() + "]", e1);
            }
            while (values.hasMoreElements()) {
                Attributes partialAttrs = new BasicAttributes();
                Attribute singleValuedAttribute;
                String id = a.getID();
                Object value = values.nextElement();
                if (log.isDebugEnabled()) {
                    if (id.toLowerCase().contains("password") || id.toLowerCase().contains("pwd")) {
                        log.debug("Update value: ***");
                    } else {
                        log.debug("Update value: " + value);
                    }
                }
                if (unicodePwd && "unicodePwd".equalsIgnoreCase(id)) {
                    singleValuedAttribute = new BasicAttribute(id, encodeUnicodePwd(value));
                } else {
                    singleValuedAttribute = new BasicAttribute(id, value);
                }
                partialAttrs.put(singleValuedAttribute);
                DirContext dirContext = null;
                try {
                    dirContext = getDirContext(paramValueMap);
                    dirContext.modifyAttributes(entryNameAfter, DirContext.REPLACE_ATTRIBUTE, partialAttrs);
                } catch (NamingException e) {
                    String msg;
                    // [LDAP: error code 32 - No Such Object...
                    if (e.getMessage().startsWith("[LDAP: error code 32 - ")) {
                        msg = "Operation [" + getOperation() + "] failed - wrong entryName [" + entryNameAfter + "]";
                    } else {
                        msg = "Exception in operation [" + getOperation() + "] entryName [" + entryNameAfter + "]";
                    }
                    // result = DEFAULT_RESULT_UPDATE_NOK;
                    storeLdapException(e, prc);
                    throw new SenderException(msg, e);
                } finally {
                    closeDirContext(dirContext);
                }
            }
        }
        return DEFAULT_RESULT;
    } else {
        DirContext dirContext = null;
        try {
            dirContext = getDirContext(paramValueMap);
            // dirContext.rename(newEntryName, oldEntryName);
            // result = DEFAULT_RESULT;
            dirContext.rename(entryName, entryName);
            return "<LdapResult>Deze functionaliteit is nog niet beschikbaar - naam niet veranderd.</LdapResult>";
        } catch (NamingException e) {
            // [LDAP: error code 68 - Entry Already Exists]
            if (!e.getMessage().startsWith("[LDAP: error code 68 - ")) {
                storeLdapException(e, prc);
                throw new SenderException(e);
            }
            return DEFAULT_RESULT_CREATE_NOK;
        } finally {
            closeDirContext(dirContext);
        }
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) NamingEnumeration(javax.naming.NamingEnumeration) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) SenderException(nl.nn.adapterframework.core.SenderException)

Aggregations

Attribute (javax.naming.directory.Attribute)288 Attributes (javax.naming.directory.Attributes)162 NamingException (javax.naming.NamingException)133 BasicAttribute (javax.naming.directory.BasicAttribute)97 SearchResult (javax.naming.directory.SearchResult)92 ArrayList (java.util.ArrayList)74 BasicAttributes (javax.naming.directory.BasicAttributes)64 NamingEnumeration (javax.naming.NamingEnumeration)56 SearchControls (javax.naming.directory.SearchControls)55 DirContext (javax.naming.directory.DirContext)46 InitialDirContext (javax.naming.directory.InitialDirContext)40 HashSet (java.util.HashSet)38 HashMap (java.util.HashMap)29 IOException (java.io.IOException)24 LdapName (javax.naming.ldap.LdapName)20 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)18 Hashtable (java.util.Hashtable)17 Map (java.util.Map)17 ModificationItem (javax.naming.directory.ModificationItem)17 List (java.util.List)15