1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.api.ldap.model.ldif.anonymizer;
22
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.directory.api.ldap.model.entry.Attribute;
29 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
30 import org.apache.directory.api.ldap.model.entry.StringValue;
31 import org.apache.directory.api.ldap.model.entry.Value;
32 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
33 import org.apache.directory.api.ldap.model.schema.AttributeType;
34
35
36
37
38
39
40
41 public class CaseSensitiveStringAnonymizer extends AbstractAnonymizer<String>
42 {
43
44 private Map<Integer, String> latestStringMap;
45
46
47
48
49 public CaseSensitiveStringAnonymizer()
50 {
51 latestStringMap = new HashMap<>();
52 caseSensitive = true;
53 }
54
55
56
57
58
59
60
61 public CaseSensitiveStringAnonymizer( Map<Integer, String> latestStringMap )
62 {
63 if ( latestStringMap == null )
64 {
65 this.latestStringMap = new HashMap<>();
66 }
67 else
68 {
69 this.latestStringMap = latestStringMap;
70 }
71
72 caseSensitive = true;
73 }
74
75
76
77
78
79 @Override
80 public Attribute anonymize( Map<Value<String>, Value<String>> valueMap, Set<Value<String>> valueSet, Attribute attribute )
81 {
82 AttributeType attributeType = attribute.getAttributeType();
83 Attribute result = new DefaultAttribute( attributeType );
84
85 for ( Value<?> value : attribute )
86 {
87 if ( value instanceof StringValue )
88 {
89 Value<String> anonymized = valueMap.get( value );
90
91 if ( anonymized != null )
92 {
93 try
94 {
95 result.add( anonymized );
96 }
97 catch ( LdapInvalidAttributeValueException e )
98 {
99
100 }
101 }
102 else
103 {
104 String strValue = value.getNormValue().toString();
105 String newValue = computeNewValue( strValue );
106
107 try
108 {
109 result.add( newValue );
110 Value<String> anonValue = new StringValue( attribute.getAttributeType(), newValue );
111 valueMap.put( ( Value<String> ) value, anonValue );
112 valueSet.add( anonValue );
113 }
114 catch ( LdapInvalidAttributeValueException e )
115 {
116 throw new RuntimeException( "Error while anonymizing the value" + strValue );
117 }
118 }
119 }
120 }
121
122 return result;
123 }
124
125
126
127
128
129 @Override
130 public Map<Integer, String> getLatestStringMap()
131 {
132 return latestStringMap;
133 }
134
135
136
137
138
139 @Override
140 public void setLatestStringMap( Map<Integer, String> latestStringMap )
141 {
142 this.latestStringMap = latestStringMap;
143 }
144 }