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);
}
}
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;
}
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;
}
}
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;
}
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);
}
}
}
Aggregations