View Javadoc
1   /*
2    * Copyright 2019 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.dccon.repository.ldap;
18  
19  import static org.bremersee.data.ldaptive.LdaptiveEntryMapper.getAttributeValue;
20  
21  import javax.validation.constraints.NotNull;
22  import lombok.AccessLevel;
23  import lombok.Getter;
24  import org.bremersee.dccon.config.DomainControllerProperties;
25  import org.bremersee.dccon.model.CommonAttributes;
26  import org.bremersee.dccon.repository.ldap.transcoder.GeneralizedTimeToOffsetDateTimeValueTranscoder;
27  import org.ldaptive.LdapEntry;
28  
29  /**
30   * The abstract ldap mapper.
31   *
32   * @author Christian Bremer
33   */
34  abstract class AbstractLdapMapper {
35  
36    static final String WHEN_CREATED = "whenCreated";
37  
38    static final String WHEN_CHANGED = "whenChanged";
39  
40    private static final GeneralizedTimeToOffsetDateTimeValueTranscoder WHEN_TIME_VALUE_TRANSCODER
41        = new GeneralizedTimeToOffsetDateTimeValueTranscoder();
42  
43    @Getter(AccessLevel.PROTECTED)
44    private DomainControllerProperties properties;
45  
46    /**
47     * Instantiates a new abstract ldap mapper.
48     *
49     * @param properties the properties
50     */
51    AbstractLdapMapper(DomainControllerProperties properties) {
52      this.properties = properties;
53    }
54  
55    /**
56     * Create distinguished name.
57     *
58     * @param rdn the rdn
59     * @param rdnValue the rdn value
60     * @param baseDn the base dn
61     * @return the string
62     */
63    String createDn(
64        @NotNull final String rdn,
65        @NotNull final String rdnValue,
66        @NotNull final String baseDn) {
67      return rdn + "=" + rdnValue + "," + baseDn;
68    }
69  
70    /**
71     * Map common attributes.
72     *
73     * @param source the source
74     * @param destination the destination
75     */
76    void mapCommonAttributes(final LdapEntry source, final CommonAttributes destination) {
77      if (source != null && destination != null) {
78        destination.setCreated(getAttributeValue(
79            source, WHEN_CREATED, WHEN_TIME_VALUE_TRANSCODER, null));
80        destination.setDistinguishedName(source.getDn());
81        destination.setModified(getAttributeValue(
82            source, WHEN_CHANGED, WHEN_TIME_VALUE_TRANSCODER, null));
83      }
84    }
85  
86  }