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 java.util.Set;
22  import java.util.stream.Collectors;
23  import org.bremersee.ldaptive.LdaptiveAttribute;
24  import org.bremersee.ldaptive.LdaptiveEntryMapper;
25  import org.ldaptive.AttributeModification;
26  import org.ldaptive.LdapEntry;
27  import org.ldaptive.dn.Dn;
28  import org.ldaptive.dn.NameValue;
29  import org.ldaptive.dn.RDn;
30  import org.springframework.beans.factory.annotation.Value;
31  import org.springframework.stereotype.Component;
32  import org.springframework.util.StringUtils;
33  
34  /**
35   * The test group mapper.
36   *
37   * @author Christian Bremer
38   */
39  @Component
40  public class GroupMapper implements LdaptiveEntryMapper<Group> {
41  
42    @Value("${spring.ldap.embedded.base-dn}")
43    private String baseDn;
44  
45    private String getBaseDn() {
46      return "ou=groups," + baseDn;
47    }
48  
49    @Override
50    public String[] getObjectClasses() {
51      return new String[]{"top", "groupOfUniqueNames"};
52    }
53  
54    @Override
55    public String mapDn(Group group) {
56      if (group == null || !StringUtils.hasText(group.getCn())) {
57        return null;
58      }
59      return Dn.builder()
60          .add(new RDn(new NameValue("cn", group.getCn())))
61          .add(new Dn(getBaseDn()))
62          .build()
63          .format();
64    }
65  
66    @Override
67    public Group map(LdapEntry ldapEntry) {
68      if (ldapEntry == null) {
69        return null;
70      }
71      Group group = new Group();
72      map(ldapEntry, group);
73      return group;
74    }
75  
76    @Override
77    public void map(LdapEntry ldapEntry, Group group) {
78      LdaptiveAttribute.define("cn").getValue(ldapEntry)
79          .ifPresent(group::setCn);
80      LdaptiveAttribute.define("ou").getValue(ldapEntry)
81          .ifPresent(group::setOu);
82      Set<String> members = LdaptiveAttribute.define("uniqueMember")
83          .getValues(ldapEntry)
84          .collect(Collectors.toSet());
85      group.setMembers(members);
86    }
87  
88    @Override
89    public AttributeModification[] mapAndComputeModifications(Group source, LdapEntry destination) {
90      List<AttributeModification> modifications = new ArrayList<>();
91      LdaptiveAttribute.define("cn").setValue(destination, source.getCn())
92          .ifPresent(modifications::add);
93      LdaptiveAttribute.define("ou").setValue(destination, source.getOu())
94          .ifPresent(modifications::add);
95      LdaptiveAttribute.define("uniqueMember").setValues(destination, source.getMembers())
96          .ifPresent(modifications::add);
97      return modifications.toArray(new AttributeModification[0]);
98    }
99  
100 }