View Javadoc
1   package org.bremersee.ldaptive;
2   
3   import java.io.Serial;
4   import java.util.regex.Pattern;
5   import org.bremersee.ldaptive.converter.JacksonDnDeserializer;
6   import org.bremersee.ldaptive.converter.JacksonDnSerializer;
7   import org.bremersee.ldaptive.converter.JacksonLdapEntryDeserializer;
8   import org.bremersee.ldaptive.converter.JacksonLdapEntrySerializer;
9   import org.ldaptive.LdapEntry;
10  import org.ldaptive.dn.Dn;
11  import tools.jackson.core.Version;
12  import tools.jackson.databind.module.SimpleModule;
13  
14  /**
15   * The ldaptive object mapper module.
16   */
17  public class LdaptiveObjectMapperModule extends SimpleModule {
18  
19    @Serial
20    private static final long serialVersionUID = 1L;
21  
22    /**
23     * The constant TYPE_ID.
24     */
25    public static final String TYPE_ID = LdaptiveObjectMapperModule.class.getName();
26  
27    /**
28     * Instantiates a new ldaptive object mapper module.
29     */
30    public LdaptiveObjectMapperModule() {
31      super(TYPE_ID, getVersion());
32      addDeserializer(Dn.class, new JacksonDnDeserializer());
33      addDeserializer(LdapEntry.class, new JacksonLdapEntryDeserializer());
34      addSerializer(Dn.class, new JacksonDnSerializer());
35      addSerializer(LdapEntry.class, new JacksonLdapEntrySerializer());
36    }
37  
38    private static Version getVersion() {
39  
40      int defaultMajor = 6;
41      int defaultMinor = 0;
42      int defaultPatchLevel = 0;
43      String defaultSnapshotInfo = "SNAPSHOT";
44  
45      int major = defaultMajor;
46      int minor = defaultMinor;
47      int patchLevel = defaultPatchLevel;
48      String snapshotInfo = defaultSnapshotInfo;
49  
50      String version = LdaptiveObjectMapperModule.class.getPackage().getImplementationVersion();
51      if (version != null) {
52        try {
53          int i = version.indexOf('-');
54          if (i < 0) {
55            snapshotInfo = null;
56          } else {
57            snapshotInfo = version.substring(i + 1);
58            String[] a = version.substring(0, i).split(Pattern.quote("."));
59            major = Integer.parseInt(a[0]);
60            minor = Integer.parseInt(a[1]);
61            patchLevel = Integer.parseInt(a[2]);
62          }
63  
64        } catch (RuntimeException e) {
65          major = defaultMajor;
66          minor = defaultMinor;
67          snapshotInfo = defaultSnapshotInfo;
68        }
69      }
70  
71      return new Version(major, minor, patchLevel, snapshotInfo, "org.bremersee",
72          "spring-integration-ldaptive");
73    }
74  
75  }