View Javadoc
1   /*
2   * Copyright 2019-2026 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.ldaptive.app;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import org.bremersee.ldaptive.LdaptiveAttribute;
22  import org.bremersee.ldaptive.LdaptiveEntryMapper;
23  import org.ldaptive.AttributeModification;
24  import org.ldaptive.LdapEntry;
25  import org.ldaptive.dn.Dn;
26  import org.ldaptive.dn.NameValue;
27  import org.ldaptive.dn.RDn;
28  import org.springframework.beans.factory.annotation.Value;
29  import org.springframework.stereotype.Component;
30  import org.springframework.util.StringUtils;
31  
32  /**
33   * The test person mapper.
34   *
35   * @author Christian Bremer
36   */
37  @Component
38  public class PersonMapper implements LdaptiveEntryMapper<Person> {
39  
40    @Value("${spring.ldap.embedded.base-dn}")
41    private String baseDn;
42  
43    private String getBaseDn() {
44      return "ou=people," + baseDn;
45    }
46  
47    @Override
48    public String[] getObjectClasses() {
49      return new String[]{"top", "person", "organizationalPerson", "inetOrgPerson"};
50    }
51  
52    @Override
53    public String mapDn(Person person) {
54      if (person == null || !StringUtils.hasText(person.getUid())) {
55        return null;
56      }
57      return Dn.builder()
58          .add(new RDn(new NameValue("uid", person.getUid())))
59          .add(new Dn(getBaseDn()))
60          .build()
61          .format();
62    }
63  
64    @Override
65    public Person map(LdapEntry ldapEntry) {
66      if (ldapEntry == null) {
67        return null;
68      }
69      Person person = new Person();
70      map(ldapEntry, person);
71      return person;
72    }
73  
74    @Override
75    public void map(LdapEntry ldapEntry, Person person) {
76      LdaptiveAttribute.define("cn").getValue(ldapEntry)
77          .ifPresent(person::setCn);
78      LdaptiveAttribute.define("uid").getValue(ldapEntry)
79          .ifPresent(person::setUid);
80      LdaptiveAttribute.define("sn").getValue(ldapEntry)
81          .ifPresent(person::setSn);
82    }
83  
84    @Override
85    public AttributeModification[] mapAndComputeModifications(
86        Person source,
87        LdapEntry destination) {
88      List<AttributeModification> modifications = new ArrayList<>();
89      LdaptiveAttribute.define("cn").setValue(destination, source.getCn())
90          .ifPresent(modifications::add);
91      LdaptiveAttribute.define("uid").setValue(destination, source.getUid())
92          .ifPresent(modifications::add);
93      LdaptiveAttribute.define("sn").setValue(destination, source.getSn())
94          .ifPresent(modifications::add);
95      return modifications.toArray(new AttributeModification[0]);
96    }
97  
98  }