View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
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   * A default anonymizer for attributes that are not HR
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class BinaryAnonymizer extends AbstractAnonymizer<byte[]>
41  {
42      /** The latest anonymized byte[] value map */
43      protected Map<Integer, byte[]> latestBytesMap = new HashMap<>();
44  
45      /**
46       * Creates a new instance of BinaryAnonymizer.
47       */
48      public BinaryAnonymizer()
49      {
50          latestBytesMap = new HashMap<>();
51      }
52  
53      
54      /**
55       * Creates a new instance of BinaryAnonymizer.
56       * 
57       * @param latestBytesMap The map containing the latest value for each length 
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       * Anonymize an attribute using pure random values (either chars of bytes, depending on the Attribute type)
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      * {@inheritDoc}
103      */
104     @Override
105     public Map<Integer, byte[]> getLatestBytesMap()
106     {
107         return latestBytesMap;
108     }
109 
110     
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public void setLatestBytesMap( Map<Integer, byte[]> latestBytesMap )
116     {
117         this.latestBytesMap = latestBytesMap;
118     }
119 }