@Override public JsonElement serialize(final SearchResult result, final Type type, final JsonSerializationContext context) { final JsonArray json = new JsonArray(); for (LdapEntry entry : result.getEntries()) { final JsonObject jsonEntry = new JsonObject(); jsonEntry.add("dn", entry.getDn() != null ? new JsonPrimitive(entry.getDn()) : null); for (LdapAttribute attr : entry.getAttributes()) { final JsonArray jsonAttrValues = new JsonArray(); attr.getStringValues().forEach(jsonAttrValues::add); jsonEntry.add(attr.getName(), jsonAttrValues); } json.add(jsonEntry); } return json; } }
@Override public LdapEntry resolve(final AuthenticationCriteria criteria, final AuthenticationHandlerResponse response) throws LdapException { return new LdapEntry(criteria.getDn()); }
/** * Changes the name of an attribute in this entry. The old attribute is removed from this entry, the name is changed * with {@link LdapAttribute#setName(String)}, and the attribute is added back to this entry. If oldName does not * exist, this method does nothing. * * @param oldName attribute name to change from * @param newName attribute name to change to */ public void renameAttribute(final String oldName, final String newName) { final LdapAttribute la = getAttribute(oldName); if (la != null) { removeAttribute(oldName); la.setName(newName); addAttribute(la); } }
/** * Creates a new ldap entry. * * @param dn dn for this entry * @param attrs collection of attributes to add */ public LdapEntry(final String dn, final Collection<LdapAttribute> attrs) { this(); setDn(dn); addAttributes(attrs); }
/** * Creates a new ldap entry. * * @param dn dn for this entry * @param attr ldap attribute for this entry */ public LdapEntry(final String dn, final LdapAttribute... attr) { this(); setDn(dn); for (LdapAttribute a : attr) { addAttribute(a); } }
final LdapEntry ldapEntry = new LdapEntry(sortBehavior); ldapEntry.setDn(""); ldapEntry.setDn(name); "objectClass", ocElement.getElementsByTagName("dsml:oc-value")); ldapEntry.addAttribute(ldapAttribute); attrName, attrElement.getElementsByTagName("dsml:value")); ldapEntry.addAttribute(ldapAttribute);
/** * Tests create with one entry. * * @param attr1 ldap attribute * @param attr2 ldap attribute */ @Test(groups = {"bean"}, dataProvider = "attributes") public void createOne(final LdapAttribute attr1, final LdapAttribute attr2) { final LdapEntry le = new LdapEntry("uid=1", attr1); AssertJUnit.assertEquals(attr1, le.getAttribute()); AssertJUnit.assertEquals(attr1, le.getAttribute("givenName")); AssertJUnit.assertEquals(attr1, le.getAttribute("givenname")); AssertJUnit.assertEquals("givenName", le.getAttributeNames()[0]); AssertJUnit.assertEquals(1, le.size()); AssertJUnit.assertEquals(le, new LdapEntry("uid=1", attr1)); le.clear(); AssertJUnit.assertEquals(0, le.size()); }
mergedEntry = le; } else { for (LdapAttribute la : le.getAttributes()) { final LdapAttribute oldAttr = mergedEntry.getAttribute(la.getName()); if (oldAttr == null) { mergedEntry.addAttribute(la); } else { if (oldAttr.isBinary()) {
/** * Tests ordered sort behavior. * * @param attr1 ldap attribute * @param attr2 ldap attribute */ @Test(groups = {"bean"}, dataProvider = "attributes") public void orderedSortBehavior(final LdapAttribute attr1, final LdapAttribute attr2) { final LdapEntry le = new LdapEntry(SortBehavior.ORDERED); AssertJUnit.assertEquals(SortBehavior.ORDERED, le.getSortBehavior()); le.addAttribute(attr2, attr1); final LdapAttribute[] attrs = le.getAttributes().toArray(new LdapAttribute[2]); AssertJUnit.assertEquals(attr2, attrs[0]); AssertJUnit.assertEquals(attr1, attrs[1]); le.clear(); AssertJUnit.assertEquals(0, le.size()); }
groupSearchExecutor.getSearchFilter().setParameter(0, userEntry.getDn()); SearchResult searchResult = groupSearchExecutor.search(connectionFactory).getResult(); Collection<LdapEntry> groupEntries = searchResult.getEntries(); String[] groups = groupEntries.stream() .map(groupEntry -> groupEntry.getAttributes() .stream() .map(ldapAttribute -> ldapAttribute.getStringValue()) .flatMap(List::stream) .toArray(size -> new String[size]); userEntry.addAttribute(new LdapAttribute(MEMBEROF_ATTRIBUTE, groups)); } catch (Exception e) { LOGGER.warn("No group found for user {}", userEntry.getDn(), e);
/** Tests default sort behavior. */ @Test(groups = {"bean"}) public void defaultSortBehavior() { final LdapEntry le = new LdapEntry("uid=1"); AssertJUnit.assertEquals(SortBehavior.getDefaultSortBehavior(), le.getSortBehavior()); AssertJUnit.assertEquals(0, le.size()); AssertJUnit.assertNull(le.getAttribute()); AssertJUnit.assertEquals("uid=1", le.getDn()); le.setDn("uid=2"); AssertJUnit.assertEquals("uid=2", le.getDn()); le.clear(); AssertJUnit.assertEquals(0, le.size()); }
/** * Returns the DN for the supplied ldap entry. * * @param entry to retrieve the DN from * * @return dn */ protected String resolveDn(final LdapEntry entry) { return entry.getDn(); }
/** * Converts an ldaptive <code>LdapEntry</code> containing result entry attributes into an attribute map as needed * by Person Directory components. * * @param entry Ldap entry. * @return Attribute map. */ private Map<String, List<Object>> convertLdapEntryToMap(final LdapEntry entry) { final Map<String, List<Object>> attributeMap = new LinkedHashMap<>(entry.size()); for (final LdapAttribute attr : entry.getAttributes()) { attributeMap.put(attr.getName(), new ArrayList<Object>(attr.getStringValues())); } logger.debug("Converted ldap DN entry [{}] to attribute map {}", entry.getDn(), attributeMap.toString()); return attributeMap; }
@Override protected void insert(final Map<String, Object> attributes) { attributes.put("objectClass", "person"); final LdapEntry ldapEntry = new LdapEntry(getEntryId(attributes)); ldapEntry.addAttributes(getLdapAttributes(attributes)); Connection connection = null; try { connection = connectionFactory.getConnection(); connection.open(); final AddOperation add = new AddOperation(connection); add.execute(new AddRequest(ldapEntry.getDn(), ldapEntry.getAttributes())); } catch (final LdapException e) { throw new TechnicalException(e); } finally { if (connection != null) { connection.close(); } } }
@Override public Response<Void> add(final T object) throws LdapException { final LdapEntry entry = new LdapEntry(); getLdapEntryMapper().map(object, entry); final AddRequest request = new AddRequest(entry.getDn(), entry.getAttributes()); try (Connection conn = getConnectionFactory().getConnection()) { conn.open(); final AddOperation add = new AddOperation(conn); return add.execute(request); } }
private Map<String, Object> addClaim(Map<String, Object> claims, LdapEntry ldapEntry, String claimKey, String attributeKey) { if (ldapEntry.getAttribute(attributeKey) != null) { claims.put(claimKey, ldapEntry.getAttribute(attributeKey).getStringValue()); } return claims; }
/** * Iterates over the supplied entry and returns all attributes as a set of ldap roles. * * @param entry to read * * @return ldap roles */ public static Set<LdapRole> toRoles(final LdapEntry entry) { return toRoles(entry.getAttributes()); }
/** * Execute modify operation boolean. * * @param currentDn the current dn * @param connectionFactory the connection factory * @param entry the entry * @return the boolean */ public static boolean executeModifyOperation(final String currentDn, final ConnectionFactory connectionFactory, final LdapEntry entry) { final Map<String, Set<String>> attributes = new HashMap<>(entry.getAttribute().size()); for (final LdapAttribute ldapAttribute : entry.getAttributes()) { attributes.put(ldapAttribute.getName(), ImmutableSet.copyOf(ldapAttribute.getStringValues())); } return executeModifyOperation(currentDn, connectionFactory, attributes); }
/** {@inheritDoc} */ @Override public boolean create(@Nonnull @NotEmpty final String context, @Nonnull @NotEmpty final String key, @Nonnull @NotEmpty final String value, @Nullable @Positive Long expiration) throws IOException { if (expiration != null) { throw new UnsupportedOperationException("Expiration not supported"); } final LdapEntry entry = new LdapEntry(context, defaultAttributes); entry.addAttribute(new LdapAttribute(key, value)); try { merge(entry); return true; } catch (LdapException e) { log.error("LDAP merge operation failed", e); throw new IOException(e); } }
@Override protected void writeResponse(final SearchResult result, final HttpServletResponse response) throws IOException { final LdapEntry e = result.getEntry(); if (e != null && e.size() > 0) { final LdapAttribute a = e.getAttribute(); if (a != null && a.size() > 0) { if (a.isBinary()) { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s.bin\"", a.getName())); } else { response.setContentType("text/plain"); } final OutputStream out = response.getOutputStream(); out.write(a.getBinaryValue()); out.flush(); } } } }