List of usage examples for javax.naming.directory Attribute add
boolean add(Object attrVal);
From source file:org.swordess.ldap.util.ModUtils.java
public static <T> ModificationItem create(int operationMod, String id, Collection<?> values, Evaluator<T> evaluator) {// www. ja va 2 s . c om if (CollectionUtils.isEmpty(values)) { return null; } boolean hasOneNotNullAtLeast = false; Attribute attr = new BasicAttribute(id); if (null == evaluator) { for (Object value : values) { if (null != value) { hasOneNotNullAtLeast = true; attr.add(value); } } } else { for (Object value : values) { if (null == value) { continue; } T evaled = evaluator.eval(value); if (null != evaled) { hasOneNotNullAtLeast = true; attr.add(evaled); } } } return hasOneNotNullAtLeast ? new ModificationItem(operationMod, attr) : null; }
From source file:org.projectforge.business.ldap.LdapUtils.java
public static Attribute putAttribute(final Attributes attributes, final String attrId, final String attrValue) { final Attribute attr = attributes.get(attrId); if (attrValue == null) { return attr; }//from ww w. j a va2 s . c o m if (attr == null) { return attributes.put(attrId, attrValue); } attr.add(attrValue); return attr; }
From source file:org.swordess.ldap.util.AttrUtils.java
public static <T> Attribute create(String id, Collection<?> values, Evaluator<T> evaluator) { if (StringUtils.isEmpty(id) || CollectionUtils.isEmpty(values)) { return null; }/*from w w w . ja va2s . c om*/ boolean hasOneNotNullAtLeast = false; Attribute attr = new BasicAttribute(id); if (null == evaluator) { for (Object value : values) { if (null != value) { hasOneNotNullAtLeast = true; attr.add(value); } } } else { for (Object value : values) { if (null == value) { continue; } T evaled = evaluator.eval(value); if (null != evaled) { hasOneNotNullAtLeast = true; attr.add(evaled); } } } return hasOneNotNullAtLeast ? attr : null; }
From source file:org.swordess.ldap.util.AttrUtils.java
public static <T> Attribute create(String id, Object[] values, Evaluator<T> evaluator) { if (StringUtils.isEmpty(id) || ArrayUtils.isEmpty(values)) { return null; }//from w w w .j ava2 s .c o m boolean hasOneNotNullAtLeast = false; Attribute attr = new BasicAttribute(id); if (null == evaluator) { for (Object value : values) { if (null != value) { hasOneNotNullAtLeast = true; attr.add(value); } } } else { for (Object value : values) { if (null == value) { continue; } T evaled = evaluator.eval(value); if (null != evaled) { hasOneNotNullAtLeast = true; attr.add(evaled); } } } return hasOneNotNullAtLeast ? attr : null; }
From source file:org.apache.directory.studio.ldapbrowser.core.jobs.ImportLdifRunnable.java
/** * Imports the LDIF record.//from ww w . ja v a2s.c o m * * @param browserConnection the browser connection * @param record the LDIF record * @param updateIfEntryExists the update if entry exists flag * @param monitor the progress monitor * * @throws NamingException the naming exception * @throws LdapInvalidDnException */ static void importLdifRecord(IBrowserConnection browserConnection, LdifRecord record, boolean updateIfEntryExists, StudioProgressMonitor monitor) throws NamingException, LdapInvalidDnException { if (!record.isValid()) { throw new NamingException( BrowserCoreMessages.bind(BrowserCoreMessages.model__invalid_record, record.getInvalidString())); } String dn = record.getDnLine().getValueAsString(); if (record instanceof LdifContentRecord || record instanceof LdifChangeAddRecord) { LdifAttrValLine[] attrVals; IEntry dummyEntry; if (record instanceof LdifContentRecord) { LdifContentRecord attrValRecord = (LdifContentRecord) record; attrVals = attrValRecord.getAttrVals(); try { dummyEntry = ModelConverter.ldifContentRecordToEntry(attrValRecord, browserConnection); } catch (LdapInvalidDnException e) { monitor.reportError(e); return; } } else { LdifChangeAddRecord changeAddRecord = (LdifChangeAddRecord) record; attrVals = changeAddRecord.getAttrVals(); try { dummyEntry = ModelConverter.ldifChangeAddRecordToEntry(changeAddRecord, browserConnection); } catch (LdapInvalidDnException e) { monitor.reportError(e); return; } } Attributes jndiAttributes = new BasicAttributes(); for (LdifAttrValLine attrVal : attrVals) { String attributeName = attrVal.getUnfoldedAttributeDescription(); Object realValue = attrVal.getValueAsObject(); if (jndiAttributes.get(attributeName) != null) { jndiAttributes.get(attributeName).add(realValue); } else { jndiAttributes.put(attributeName, realValue); } } browserConnection.getConnection().getConnectionWrapper().createEntry(dn, jndiAttributes, getControls(record), monitor, null); if (monitor.errorsReported() && updateIfEntryExists && monitor.getException() instanceof NameAlreadyBoundException) { // creation failed with Error 68, now try to update the existing entry monitor.reset(); ModificationItem[] mis = ModelConverter.entryToReplaceModificationItems(dummyEntry); browserConnection.getConnection().getConnectionWrapper().modifyEntry(dn, mis, getControls(record), monitor, null); } } else if (record instanceof LdifChangeDeleteRecord) { LdifChangeDeleteRecord changeDeleteRecord = (LdifChangeDeleteRecord) record; browserConnection.getConnection().getConnectionWrapper().deleteEntry(dn, getControls(changeDeleteRecord), monitor, null); } else if (record instanceof LdifChangeModifyRecord) { LdifChangeModifyRecord modifyRecord = (LdifChangeModifyRecord) record; LdifModSpec[] modSpecs = modifyRecord.getModSpecs(); ModificationItem[] mis = new ModificationItem[modSpecs.length]; for (int ii = 0; ii < modSpecs.length; ii++) { LdifModSpecTypeLine modSpecType = modSpecs[ii].getModSpecType(); LdifAttrValLine[] attrVals = modSpecs[ii].getAttrVals(); Attribute attribute = new BasicAttribute(modSpecType.getUnfoldedAttributeDescription()); for (int x = 0; x < attrVals.length; x++) { attribute.add(attrVals[x].getValueAsObject()); } if (modSpecType.isAdd()) { mis[ii] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attribute); } else if (modSpecType.isDelete()) { mis[ii] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attribute); } else if (modSpecType.isReplace()) { mis[ii] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute); } } browserConnection.getConnection().getConnectionWrapper().modifyEntry(dn, mis, getControls(modifyRecord), monitor, null); } else if (record instanceof LdifChangeModDnRecord) { LdifChangeModDnRecord modDnRecord = (LdifChangeModDnRecord) record; if (modDnRecord.getNewrdnLine() != null && modDnRecord.getDeloldrdnLine() != null) { String newRdn = modDnRecord.getNewrdnLine().getValueAsString(); boolean deleteOldRdn = modDnRecord.getDeloldrdnLine().isDeleteOldRdn(); Dn newDn; if (modDnRecord.getNewsuperiorLine() != null) { newDn = new Dn(newRdn, modDnRecord.getNewsuperiorLine().getValueAsString()); } else { Dn dnObject = new Dn(dn); Dn parent = dnObject.getParent(); newDn = new Dn(newRdn, parent.getName()); } browserConnection.getConnection().getConnectionWrapper().renameEntry(dn, newDn.toString(), deleteOldRdn, getControls(modDnRecord), monitor, null); } } }
From source file:it.infn.ct.security.utilities.LDAPUtils.java
public static boolean addOrganisation(LDAPUser lus, Organization org) { boolean registration = false; DirContext ctx = null;//from w ww . j a v a 2 s . c o m try { ctx = getAuthContext(lus.getUsername(), lus.getPassword()); Attributes attrsBag = new BasicAttributes(); Attribute oc = new BasicAttribute("objectClass"); oc.add("organization"); oc.add("top"); attrsBag.put(oc); Attribute o = new BasicAttribute("o", org.getKey()); attrsBag.put(o); Attribute description = new BasicAttribute("description", org.getDescription()); attrsBag.put(description); if (org.getReference() != null && !org.getReference().isEmpty()) { Attribute registeredAddr = new BasicAttribute("registeredAddress", org.getReference()); attrsBag.put(registeredAddr); } ResourceBundle rb = ResourceBundle.getBundle("ldap"); ctx.createSubcontext( "o=" + org.getKey() + ",c=" + org.getCountryCode() + "," + rb.getString("organisationsRoot"), attrsBag); registration = true; } catch (NameNotFoundException ex) { _log.error(ex); } catch (NamingException e) { _log.error(e); } finally { if (ctx != null) { try { ctx.close(); } catch (Exception e) { // Never mind this. } } } return registration; }
From source file:it.infn.ct.security.utilities.LDAPUtils.java
private static boolean toggleUserIDPGroup(String cn, boolean activate) { ResourceBundle rb = ResourceBundle.getBundle("ldap"); String userDN = "cn=" + cn + "," + rb.getString("peopleRoot"); String idpUser = rb.getString("usersGroup"); DirContext ctx = null;/*from w ww. j a va 2 s.c o m*/ try { ctx = getMainAuthContext(); ModificationItem modAttrs[] = new ModificationItem[1]; String attrsList[] = { "uniqueMember" }; Attributes attributes = ctx.getAttributes(idpUser, attrsList); Attribute att = attributes.get("uniqueMember"); if (activate) { att.add(userDN); } else { att.remove(userDN); } modAttrs[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, att); ctx.modifyAttributes(idpUser, modAttrs); return true; } catch (NamingException ex) { _log.error(ex); } return false; }
From source file:it.infn.ct.security.utilities.LDAPUtils.java
public static boolean registerUser(LDAPUser lus, UserRequest userReq, String OrgDN, String OrgUDN) { boolean registration = false; DirContext ctx = null;//from ww w. j a v a 2s .c om try { ctx = getAuthContext(lus.getUsername(), lus.getPassword()); Attributes attrsBag = new BasicAttributes(); Attribute oc = new BasicAttribute("objectClass"); oc.add("inetOrgPerson"); oc.add("organizationalPerson"); oc.add("person"); oc.add("top"); attrsBag.put(oc); Attribute sn = new BasicAttribute("sn", userReq.getSurname()); attrsBag.put(sn); Attribute cn = new BasicAttribute("cn", userReq.getUsername()); attrsBag.put(cn); Attribute dispName = new BasicAttribute("displayName", userReq.getUsername()); attrsBag.put(dispName); Attribute uPass = new BasicAttribute("userPassword", userReq.getPassword()); attrsBag.put(uPass); Attribute regAdd = new BasicAttribute("registeredAddress", userReq.getPreferredMail()); attrsBag.put(regAdd); if (userReq.getTitle() != null && !userReq.getTitle().isEmpty()) { Attribute title = new BasicAttribute("title", userReq.getTitle()); attrsBag.put(title); } Attribute gName = new BasicAttribute("givenName", userReq.getGivenname()); attrsBag.put(gName); Attribute inits = new BasicAttribute("initials", userReq.getGivenname().substring(0, 1).toUpperCase() + userReq.getSurname().substring(0, 1).toUpperCase()); attrsBag.put(inits); Attribute mails = new BasicAttribute("mail"); mails.add(userReq.getPreferredMail()); for (String adMail : userReq.getAdditionalMails().split("[,\\s;]")) if (!adMail.isEmpty()) mails.add(adMail.trim()); attrsBag.put(mails); Attribute org = new BasicAttribute("o", OrgDN); attrsBag.put(org); if (OrgUDN != null && !OrgUDN.isEmpty()) { Attribute orgU = new BasicAttribute("ou", OrgUDN); attrsBag.put(orgU); } ResourceBundle rb = ResourceBundle.getBundle("ldap"); ctx.createSubcontext("cn=" + userReq.getUsername() + "," + rb.getString("peopleRoot"), attrsBag); ModificationItem[] modItems = new ModificationItem[1]; modItems[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("uniqueMember", "cn=" + userReq.getUsername() + "," + rb.getString("peopleRoot"))); ctx.modifyAttributes(rb.getString("usersGroup"), modItems); registration = true; } catch (NameNotFoundException ex) { _log.error(ex); } catch (NamingException e) { _log.error(e); } finally { if (ctx != null) { try { ctx.close(); } catch (Exception e) { // Never mind this. } } } return registration; }
From source file:org.wso2.carbon.connector.ldap.UpdateEntry.java
@Override public void connect(MessageContext messageContext) throws ConnectException { String attributesString = (String) getParameter(messageContext, LDAPConstants.ATTRIBUTES); String dn = (String) getParameter(messageContext, LDAPConstants.DN); String mode = (String) getParameter(messageContext, LDAPConstants.MODE); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE, LDAPConstants.NAMESPACE); OMElement result = factory.createOMElement(LDAPConstants.RESULT, ns); OMElement message = factory.createOMElement(LDAPConstants.MESSAGE, ns); try {//from w ww .ja v a 2 s.c om DirContext context = LDAPUtils.getDirectoryContext(messageContext); Attributes entry = new BasicAttributes(); if (StringUtils.isNotEmpty(attributesString)) { JSONObject object = new JSONObject(attributesString); Iterator keys = object.keys(); while (keys.hasNext()) { String key = (String) keys.next(); String val = object.getString(key); Attribute newAttr = new BasicAttribute(key); newAttr.add(val); entry.put(newAttr); } } try { if (mode.equals(LDAPConstants.REPLACE)) { context.modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, entry); } else if (mode.equals(LDAPConstants.ADD)) { context.modifyAttributes(dn, DirContext.ADD_ATTRIBUTE, entry); } else if (mode.equals(LDAPConstants.REMOVE)) { context.modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, entry); } message.setText(LDAPConstants.SUCCESS); result.addChild(message); LDAPUtils.preparePayload(messageContext, result); } catch (NamingException e) { // LDAP Errors are catched LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.UPDATE_ENTRY_ERROR, e); throw new SynapseException(e); } } catch (NamingException e) { //Authentication failures are catched LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.INVALID_LDAP_CREDENTIALS, e); throw new SynapseException(e); } catch (JSONException e) { handleException("Error while passing the JSON object", e, messageContext); } }
From source file:org.wso2.carbon.connector.ldap.AddEntry.java
@Override public void connect(MessageContext messageContext) throws ConnectException { String objectClass = (String) getParameter(messageContext, LDAPConstants.OBJECT_CLASS); String attributesString = (String) getParameter(messageContext, LDAPConstants.ATTRIBUTES); String dn = (String) getParameter(messageContext, LDAPConstants.DN); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE, LDAPConstants.NAMESPACE); OMElement result = factory.createOMElement(LDAPConstants.RESULT, ns); OMElement message = factory.createOMElement(LDAPConstants.MESSAGE, ns); try {/* w ww . ja va2 s. co m*/ DirContext context = LDAPUtils.getDirectoryContext(messageContext); String classes[] = objectClass.split(","); Attributes entry = new BasicAttributes(); Attribute obClassAttr = new BasicAttribute(LDAPConstants.OBJECT_CLASS); for (int i = 0; i < classes.length; i++) { obClassAttr.add(classes[i]); } entry.put(obClassAttr); if (StringUtils.isNotEmpty(attributesString)) { JSONObject object = new JSONObject(attributesString); Iterator keys = object.keys(); while (keys.hasNext()) { String key = (String) keys.next(); String val = object.getString(key); Attribute newAttr = new BasicAttribute(key); newAttr.add(val); entry.put(newAttr); } } try { context.createSubcontext(dn, entry); message.setText(LDAPConstants.SUCCESS); result.addChild(message); LDAPUtils.preparePayload(messageContext, result); } catch (NamingException e) { log.error("Failed to create ldap entry with dn = " + dn, e); LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.ADD_ENTRY_ERROR, e); throw new SynapseException(e); } } catch (NamingException e) { LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.INVALID_LDAP_CREDENTIALS, e); throw new SynapseException(e); } catch (JSONException e) { handleException("Error while passing the JSON object", e, messageContext); } }