View Javadoc
1   /*
2    * Copyright 2024 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.serializable;
18  
19  import static java.util.Objects.nonNull;
20  
21  import com.fasterxml.jackson.annotation.JsonCreator;
22  import com.fasterxml.jackson.annotation.JsonProperty;
23  import java.io.Serial;
24  import java.io.Serializable;
25  import java.util.Collection;
26  import java.util.List;
27  import java.util.Objects;
28  import java.util.Optional;
29  import java.util.stream.Stream;
30  import lombok.EqualsAndHashCode;
31  import lombok.Getter;
32  import lombok.ToString;
33  import org.ldaptive.LdapEntry;
34  
35  /**
36   * The serializable ldap entry.
37   *
38   * @author Christian Bremer
39   */
40  @Getter
41  @ToString
42  @EqualsAndHashCode
43  public class SerLdapEntry implements Serializable {
44  
45    @Serial
46    private static final long serialVersionUID = 1L;
47  
48    /**
49     * The distinguished name.
50     */
51    @JsonProperty(value = "dn")
52    private final String dn;
53  
54    /**
55     * The attributes.
56     */
57    @JsonProperty(value = "attributes")
58    private final Collection<SerLdapAttr> attributes;
59  
60    /**
61     * Instantiates a new serializable ldap entry.
62     *
63     * @param ldapEntry the ldap entry
64     */
65    public SerLdapEntry(LdapEntry ldapEntry) {
66      this.dn = Optional.ofNullable(ldapEntry)
67          .map(LdapEntry::getDn)
68          .orElse(null);
69      this.attributes = Stream.ofNullable(ldapEntry)
70          .map(LdapEntry::getAttributes)
71          .flatMap(Collection::stream)
72          .filter(Objects::nonNull)
73          .map(SerLdapAttr::new)
74          .toList();
75    }
76  
77    /**
78     * Instantiates a new Ser ldap entry.
79     *
80     * @param dn the dn
81     * @param attributes the attributes
82     */
83    @JsonCreator
84    public SerLdapEntry(
85        @JsonProperty(value = "dn") String dn,
86        @JsonProperty(value = "attributes") List<SerLdapAttr> attributes) {
87      this.dn = dn;
88      this.attributes = attributes;
89    }
90  
91    /**
92     * To ldap entry.
93     *
94     * @return the ldap entry
95     */
96    public LdapEntry toLdapEntry() {
97      LdapEntry ldapEntry = new LdapEntry();
98      if (nonNull(dn)) {
99        ldapEntry.setDn(dn);
100     }
101     if (nonNull(attributes) && !attributes.isEmpty()) {
102       ldapEntry.addAttributes(attributes
103           .stream()
104           .map(SerLdapAttr::toLdapAttribute)
105           .toList());
106     }
107     return ldapEntry;
108   }
109 
110   /**
111    * Find attribute.
112    *
113    * @param attributeName the attribute name
114    * @return the optional attribute
115    */
116   public Optional<SerLdapAttr> findAttribute(String attributeName) {
117     return Optional.ofNullable(attributeName)
118         .flatMap(name -> Stream.ofNullable(attributes)
119             .flatMap(Collection::stream)
120             .filter(Objects::nonNull)
121             .filter(attr -> name.equalsIgnoreCase(attr.getAttributeName()))
122             .findFirst());
123   }
124 
125 }