@Test public void mapsEntryToSimpleClass() throws LdapInvalidAttributeValueException, LdapInvalidDnException { final LdapMapper<Person> mapper = new LdapMapper<>(); final Entry entry = new DefaultEntry("uid=hendy,ou=users,dc=aksimata,dc=com"); entry.put("objectClass", "organizationalPerson", "extensibleObject"); entry.put("uid", "hendy"); entry.put("uniqueIdentifier", "hendy.irawan"); entry.put("gn", "Hendy"); entry.put("sn", "Irawan"); entry.put("cn", "Hendy Irawan"); entry.put("mail", "hendy@soluvas.com"); log.info("Input Entry: {}", entry); final Person person = mapper.fromEntry(entry, Person.class); log.info("Output Person: {}", person); assertEquals("hendy", person.getId()); assertEquals("hendy.irawan", person.getSlug()); assertEquals("Hendy", person.getFirstName()); assertEquals("Irawan", person.getLastName()); assertEquals("Hendy Irawan", person.getName()); assertEquals(ImmutableSet.of("hendy@soluvas.com"), person.getEmails()); }
public T fromEntry(Entry entry, Class<? extends T> entityClass) { if (entry.getDn() == null) throw new LdapMappingException("LDAP Entry DN cannot be null"); try { final T bean = entityClass.newInstance(); Class<?> currentClass = entityClass; while (currentClass != null) { mapToProperties(entry, currentClass, bean); currentClass = currentClass.getSuperclass(); if (currentClass == Object.class) break; } return bean; } catch (InstantiationException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (IllegalAccessException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (InvocationTargetException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (LdapInvalidAttributeValueException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (NoSuchMethodException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } }
LdapMapper { public T fromEntry(Entry entry, Class<? extends T> entityClass) { if (entry.getDn() == null) throw new LdapMappingException("LDAP Entry DN cannot be null"); try { final T bean = entityClass.newInstance(); Class<?> currentClass = entityClass; while (currentClass != null) { mapToProperties(entry, currentClass, bean); currentClass = currentClass.getSuperclass(); if (currentClass == Object.class) break; } return bean; } catch (InstantiationException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (IllegalAccessException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (InvocationTargetException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (LdapInvalidAttributeValueException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (NoSuchMethodException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } } }
LdapMapper { public T fromEntry(Entry entry, Class<? extends T> entityClass) { if (entry.getDn() == null) throw new LdapMappingException("LDAP Entry DN cannot be null"); try { final T bean = entityClass.newInstance(); Class<?> currentClass = entityClass; while (currentClass != null) { mapToProperties(entry, currentClass, bean); currentClass = currentClass.getSuperclass(); if (currentClass == Object.class) break; } return bean; } catch (InstantiationException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (IllegalAccessException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (InvocationTargetException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (LdapInvalidAttributeValueException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (NoSuchMethodException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } } LdapMapper(); }
LdapMapper { public T fromEntry(Entry entry, Class<? extends T> entityClass) { if (entry.getDn() == null) throw new LdapMappingException("LDAP Entry DN cannot be null"); try { final T bean = entityClass.newInstance(); Class<?> currentClass = entityClass; while (currentClass != null) { mapToProperties(entry, currentClass, bean); currentClass = currentClass.getSuperclass(); if (currentClass == Object.class) break; } return bean; } catch (InstantiationException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (IllegalAccessException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (InvocationTargetException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (LdapInvalidAttributeValueException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (NoSuchMethodException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } } LdapMapper(); LdapMapping getMapping(final Class<?> clazz); Entry toEntry(Object obj, String baseDn); Set<String> getAttributeIds(Class<?> clazz); T fromEntry(Entry entry, Class<? extends T> entityClass); String getDn(final String rdnValue, final Class<?> clazz, final String baseDn); @Nonnull String getDn(final Object obj, final String baseDn); @Nullable String getRdnValue(final Object obj); ModifyRequest createModifyRequest(final Entry existingEntry, final T existing, final T up); }
LdapMapper { public T fromEntry(Entry entry, Class<? extends T> entityClass) { if (entry.getDn() == null) throw new LdapMappingException("LDAP Entry DN cannot be null"); try { final T bean = entityClass.newInstance(); Class<?> currentClass = entityClass; while (currentClass != null) { mapToProperties(entry, currentClass, bean); currentClass = currentClass.getSuperclass(); if (currentClass == Object.class) break; } return bean; } catch (InstantiationException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (IllegalAccessException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (InvocationTargetException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (LdapInvalidAttributeValueException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } catch (NoSuchMethodException e) { log.error("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); throw new LdapMappingException("Error mapping Entry " + entry.getDn() + " to " + entityClass.getName(), e); } } LdapMapper(); LdapMapping getMapping(final Class<?> clazz); Entry toEntry(Object obj, String baseDn); Set<String> getAttributeIds(Class<?> clazz); T fromEntry(Entry entry, Class<? extends T> entityClass); String getDn(final String rdnValue, final Class<?> clazz, final String baseDn); @Nonnull String getDn(final Object obj, final String baseDn); @Nullable String getRdnValue(final Object obj); ModifyRequest createModifyRequest(final Entry existingEntry, final T existing, final T up);  }