protected List<LdapAttribute> getLdapAttributes(final Map<String, Object> attributes) { final List<LdapAttribute> ldapAttributes = new ArrayList<>(); for (final Map.Entry<String, Object> entry : attributes.entrySet()) { final Object value = entry.getValue(); if (value != null) { final String key = entry.getKey(); final LdapAttribute ldapAttribute; if (value instanceof String) { ldapAttribute = new LdapAttribute(key, (String) value); } else if (value instanceof List) { final List<String> list = (List<String>) value; ldapAttribute = new LdapAttribute(key, list.toArray(new String[list.size()])); } else { ldapAttribute = new LdapAttribute(key, value.toString()); } ldapAttributes.add(ldapAttribute); } } return ldapAttributes; }
/** * Ldap attribute test data. * * @return ldap attributes */ @DataProvider(name = "attributes") public Object[][] createSerializable() { return new Object[][] { new Object[] { new LdapAttribute("givenName", "John"), new LdapAttribute("sn", "Doe"), }, }; }
@Override protected int dispatch(final CommandLine line) throws Exception { if (line.hasOption(OPT_HELP)) { printHelp(); } else { final LdapAttribute la; final String[] attr = line.getOptionValue(OPT_ATTR).split(":", 2); if (attr[1].startsWith(":")) { la = new LdapAttribute(attr[0], LdapUtils.base64Decode(attr[1].substring(1))); } else { la = new LdapAttribute(attr[0], attr[1]); } return compare(initConnectionFactory(line), line.getOptionValue(OPT_DN), la); } return -1; }
@Override protected int dispatch(final CommandLine line) throws Exception { if (line.hasOption(OPT_HELP)) { printHelp(); } else { LdapAttribute la; final String[] attr = line.getOptionValue(OPT_ATTR).split(":", 2); if (attr[1].startsWith(":")) { la = new LdapAttribute(attr[0], LdapUtils.base64Decode(attr[1].substring(1))); } else { la = new LdapAttribute(attr[0], attr[1]); } return compare(initConnectionFactory(line), line.getOptionValue(OPT_DN), la); } return -1; }
@Override protected int dispatch(final CommandLine line) throws Exception { if (line.hasOption(OPT_HELP)) { printHelp(); } else { final LdapAttribute la; final String[] attr = line.getOptionValue(OPT_ATTR).split(":", 2); if (attr[1].startsWith(":")) { la = new LdapAttribute(attr[0], LdapUtils.base64Decode(attr[1].substring(1))); } else { la = new LdapAttribute(attr[0], attr[1]); } return compare(initConnectionFactory(line), line.getOptionValue(OPT_DN), la); } return -1; }
/** {@inheritDoc} */ @Override public boolean update(@Nonnull @NotEmpty final String context, @Nonnull @NotEmpty final String key, @Nonnull @NotEmpty final String value, @Nullable @Positive final 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); } }
/** * Executes a {@link ModifyOperation} on the supplied DN, removing the supplied attribute. * * @param dn to modify * @param attrName to remove * * @return response for the modify operation * * @throws LdapException if the operation fails */ @Nonnull private Response<Void> deleteAttribute(@Nonnull final String dn, @Nonnull final String attrName) throws LdapException { Connection conn = null; try { conn = connectionFactory.getConnection(); final ModifyOperation modify = new ModifyOperation(conn); return modify.execute(new ModifyRequest(dn, new AttributeModification(AttributeModificationType.REMOVE, new LdapAttribute(attrName)))); } finally { conn.close(); } }
/** {@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); } }
/** Creates a new compare validator. */ public CompareValidator() { compareRequest = new CompareRequest(); compareRequest.setDn(""); compareRequest.setAttribute(new LdapAttribute("objectClass", "top")); }
/** Creates a new compare validator. */ public CompareValidator() { compareRequest = new CompareRequest(); compareRequest.setDn(""); compareRequest.setAttribute(new LdapAttribute("objectClass", "top")); }
@Override public SearchResult deserialize(final JsonElement json, final Type type, final JsonDeserializationContext context) throws JsonParseException { final SearchResult result = new SearchResult(sortBehavior); final JsonArray jsonResult = json.getAsJsonArray(); for (JsonElement jsonEntry : jsonResult) { final LdapEntry entry = new LdapEntry(sortBehavior); for (Map.Entry<String, JsonElement> jsonAttr : jsonEntry.getAsJsonObject().entrySet()) { if ("dn".equals(jsonAttr.getKey())) { entry.setDn(jsonAttr.getValue().getAsString()); } else { final LdapAttribute attr = new LdapAttribute(sortBehavior); attr.setName(jsonAttr.getKey()); jsonAttr.getValue().getAsJsonArray().forEach(i -> attr.addStringValue(i.getAsString())); entry.addAttribute(attr); } } result.addEntry(entry); } return result; } }
/** Creates a new compare validator. */ public CompareValidator() { compareRequest = new CompareRequest(); compareRequest.setDn(""); compareRequest.setAttribute(new LdapAttribute("objectClass", "top")); }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { if (entry.getAttribute(dnAttributeName) == null) { entry.addAttribute(new LdapAttribute(dnAttributeName, entry.getDn())); } else if (addIfExists) { entry.getAttribute(dnAttributeName).addStringValue(entry.getDn()); } }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { if (entry.getAttribute(dnAttributeName) == null) { entry.addAttribute(new LdapAttribute(dnAttributeName, entry.getDn())); } else if (addIfExists) { entry.getAttribute(dnAttributeName).addStringValue(entry.getDn()); } }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { if (entry.getAttribute(dnAttributeName) == null) { entry.addAttribute(new LdapAttribute(dnAttributeName, entry.getDn())); } else if (addIfExists) { entry.getAttribute(dnAttributeName).addStringValue(entry.getDn()); } }
/** Tests create with two values. */ @Test(groups = {"bean"}) public void createTwo() { final LdapAttribute la = new LdapAttribute("givenName", "Bill", "William"); AssertJUnit.assertEquals(2, la.getStringValues().size()); AssertJUnit.assertEquals(2, la.size()); AssertJUnit.assertEquals(la, new LdapAttribute("givenName", "William", "Bill")); la.removeStringValue("William"); AssertJUnit.assertEquals(1, la.size()); la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** Tests ordered sort behavior. */ @Test(groups = {"bean"}) public void orderedSortBehavior() { final LdapAttribute la = new LdapAttribute(SortBehavior.ORDERED); AssertJUnit.assertEquals(SortBehavior.ORDERED, la.getSortBehavior()); la.addStringValue("William", "Bill"); final String[] values = la.getStringValues().toArray(new String[2]); AssertJUnit.assertEquals("William", values[0]); AssertJUnit.assertEquals("Bill", values[1]); la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** Tests sorted sort behavior. */ @Test(groups = {"bean"}) public void sortedSortBehavior() { final LdapAttribute la = new LdapAttribute(SortBehavior.SORTED); AssertJUnit.assertEquals(SortBehavior.SORTED, la.getSortBehavior()); la.addStringValue("William", "Bill"); final String[] values = la.getStringValues().toArray(new String[2]); AssertJUnit.assertEquals("Bill", values[0]); AssertJUnit.assertEquals("William", values[1]); la.clear(); AssertJUnit.assertEquals(0, la.size()); }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { boolean newAttribute = false; LdapAttribute mergedAttribute = entry.getAttribute(mergeAttributeName); if (mergedAttribute == null) { mergedAttribute = new LdapAttribute(mergeAttributeName); newAttribute = true; } for (String s : attributeNames) { final LdapAttribute la = entry.getAttribute(s); if (la != null) { if (la.isBinary()) { mergedAttribute.addBinaryValues(la.getBinaryValues()); } else { mergedAttribute.addStringValues(la.getStringValues()); } } } if (mergedAttribute.size() > 0 && newAttribute) { entry.addAttribute(mergedAttribute); } }
/** Tests default sort behavior. */ @Test(groups = {"bean"}) public void defaultSortBehavior() { final LdapAttribute la = new LdapAttribute("givenName"); AssertJUnit.assertEquals(SortBehavior.getDefaultSortBehavior(), la.getSortBehavior()); AssertJUnit.assertEquals(0, la.size()); AssertJUnit.assertNull(la.getStringValue()); AssertJUnit.assertNull(la.getBinaryValue()); AssertJUnit.assertEquals("givenName", la.getName()); la.setName("sn"); AssertJUnit.assertEquals("sn", la.getName()); la.clear(); AssertJUnit.assertEquals(0, la.size()); }