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.BinaryValue;
30 import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
31 import org.apache.directory.api.ldap.model.entry.Value;
32 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
33
34
35
36
37
38
39
40 public class BinaryAnonymizer extends AbstractAnonymizer<byte[]>
41 {
42
43 protected Map<Integer, byte[]> latestBytesMap = new HashMap<>();
44
45
46
47
48 public BinaryAnonymizer()
49 {
50 latestBytesMap = new HashMap<>();
51 }
52
53
54
55
56
57
58
59 public BinaryAnonymizer( Map<Integer, byte[]> latestBytesMap )
60 {
61 if ( latestBytesMap == null )
62 {
63 this.latestBytesMap = new HashMap<>();
64 }
65 else
66 {
67 this.latestBytesMap = latestBytesMap;
68 }
69 }
70
71
72
73
74 @Override
75 public Attribute anonymize( Map<Value<byte[]>, Value<byte[]>> valueMap, Set<Value<byte[]>> valueSet, Attribute attribute )
76 {
77 Attribute result = new DefaultAttribute( attribute.getAttributeType() );
78
79 for ( Value<?> value : attribute )
80 {
81 byte[] bytesValue = ( byte[] ) value.getNormValue();
82 byte[] newValue = computeNewValue( bytesValue );
83
84 try
85 {
86 result.add( newValue );
87 Value<byte[]> anonValue = new BinaryValue( attribute.getAttributeType(), newValue );
88 valueMap.put( ( Value<byte[]> ) value, anonValue );
89 valueSet.add( anonValue );
90 }
91 catch ( LdapInvalidAttributeValueException e )
92 {
93 throw new RuntimeException( "Error while anonymizing the value" + value );
94 }
95 }
96
97 return result;
98 }
99
100
101
102
103
104 @Override
105 public Map<Integer, byte[]> getLatestBytesMap()
106 {
107 return latestBytesMap;
108 }
109
110
111
112
113
114 @Override
115 public void setLatestBytesMap( Map<Integer, byte[]> latestBytesMap )
116 {
117 this.latestBytesMap = latestBytesMap;
118 }
119 }