/** * 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()); }
/** * 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()); }
/** * 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()); }
/** * 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; }
/** * 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; }
protected Map<String, Object> getAttributesFromEntry(final LdapEntry entry) { final Map<String, Object> attributes = new HashMap<>(); for (final LdapAttribute attribute : entry.getAttributes()) { final String name = attribute.getName(); if (attribute.size() > 1) { attributes.put(name, attribute.getStringValues()); } else { attributes.put(name, attribute.getStringValue()); } } return attributes; }
@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 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; } }
/** * 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); }
/** * Returns an apache ldap entry that represents the supplied ldap entry. * * @param le ldap entry * * @return apache ldap entry * * @throws LdapException if the apache object cannot be created */ public Entry fromLdapEntry(final LdapEntry le) throws LdapException { final DefaultEntry entry = new DefaultEntry(le.getDn()); entry.add(fromLdapAttributes(le.getAttributes())); return entry; }
/** * Executes an {@link AddOperation} for the supplied entry. * * @param request merge request * @param entry to add to the LDAP * * @return response of the add operation * * @throws LdapException if an error occurs executing the add operation */ protected Response<Void> add(final MergeRequest request, final LdapEntry entry) throws LdapException { final Response<Void> response; final AddOperation add = new AddOperation(getConnection()); response = add.execute(new AddRequest(entry.getDn(), entry.getAttributes())); logger.info("added entry {} for request {}", entry, request); return response; }
/** * Executes an {@link AddOperation} for the supplied entry. * * @param request merge request * @param entry to add to the LDAP * * @return response of the add operation * * @throws LdapException if an error occurs executing the add operation */ protected Response<Void> add(final MergeRequest request, final LdapEntry entry) throws LdapException { final Response<Void> response; final AddOperation add = new AddOperation(getConnection()); response = add.execute(new AddRequest(entry.getDn(), entry.getAttributes())); logger.info("added entry {} for request {}", entry, request); return response; }
/** * Executes an {@link AddOperation} for the supplied entry. * * @param request merge request * @param entry to add to the LDAP * * @return response of the add operation * * @throws LdapException if an error occurs executing the add operation */ protected Response<Void> add(final MergeRequest request, final LdapEntry entry) throws LdapException { Response<Void> response; final AddOperation add = new AddOperation(getConnection()); response = add.execute(new AddRequest(entry.getDn(), entry.getAttributes())); logger.info("added entry {} for request {}", entry, request); return response; }
/** {@inheritDoc} */ @Override @Nonnull @NotEmpty public String serialize(@Nonnull final Principal principal) throws IOException { final StringWriter sink = new StringWriter(32); final JsonGenerator gen = getJsonGenerator(sink); gen.writeStartObject() .write(PRINCIPAL_NAME_FIELD, principal.getName()); final LdapEntry entry = ((LdapPrincipal) principal).getLdapEntry(); if (entry != null) { final JsonObjectBuilder objectBuilder = getJsonObjectBuilder(); objectBuilder.add("dn", entry.getDn()); for (final LdapAttribute attr : entry.getAttributes()) { final JsonArrayBuilder arrayBuilder = getJsonArrayBuilder(); for (final String value : attr.getStringValues()) { arrayBuilder.add(value); } objectBuilder.add(attr.getName(), arrayBuilder.build()); } gen.write(PRINCIPAL_ENTRY_FIELD, objectBuilder.build()); } gen.writeEnd(); gen.close(); return sink.toString(); }
/** * Execute add operation boolean. * * @param connectionFactory the connection factory * @param entry the entry * @return the boolean * @throws LdapException the ldap exception */ public static boolean executeAddOperation(final ConnectionFactory connectionFactory, final LdapEntry entry) throws LdapException { try (Connection connection = createConnection(connectionFactory)) { final AddOperation operation = new AddOperation(connection); operation.execute(new AddRequest(entry.getDn(), entry.getAttributes())); return true; } catch (final LdapException e) { LOGGER.error(e.getMessage(), e); } return false; }
/** * Executes the ldap add operation. * * @param cf connection factory * @param file to read ldif from * * @return status code * * @throws Exception on any LDAP search error */ protected int add(final ConnectionFactory cf, final String file) throws Exception { final Connection conn = cf.getConnection(); conn.open(); final LdifReader reader = new LdifReader(new FileReader(file)); final SearchResult sr = reader.read(); for (LdapEntry le : sr.getEntries()) { final AddOperation op = new AddOperation(conn); op.execute(new AddRequest(le.getDn(), le.getAttributes())); System.out.println(String.format("Added entry: %s", le)); } conn.close(); return 0; }
@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); } }
/** * 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()); }
/** * Tests sorted sort behavior. * * @param attr1 ldap attribute * @param attr2 ldap attribute */ @Test(groups = {"bean"}, dataProvider = "attributes") public void sortedSortBehavior(final LdapAttribute attr1, final LdapAttribute attr2) { final LdapEntry le = new LdapEntry(SortBehavior.SORTED); AssertJUnit.assertEquals(SortBehavior.SORTED, le.getSortBehavior()); le.addAttribute(attr2, attr1); final LdapAttribute[] attrs = le.getAttributes().toArray(new LdapAttribute[2]); AssertJUnit.assertEquals(attr1, attrs[0]); AssertJUnit.assertEquals(attr2, attrs[1]); le.clear(); AssertJUnit.assertEquals(0, le.size()); }