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.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   * A default anonymizer for attributes that are HR. It covers DirectoryString, Ia5String, ...
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class CaseSensitiveStringAnonymizer extends AbstractAnonymizer<String>
42  {
43      /** The latest anonymized String value map */
44      private Map<Integer, String> latestStringMap;
45  
46      /**
47       * Creates a new instance of StringAnonymizer.
48       */
49      public CaseSensitiveStringAnonymizer()
50      {
51          latestStringMap = new HashMap<>();
52          caseSensitive = true;
53      }
54  
55      
56      /**
57       * Creates a new instance of StringAnonymizer.
58       * 
59       * @param latestStringMap The map containing the latest value for each length 
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       * Anonymize an attribute using pure random values (either chars of bytes, depending on the Attribute type)
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                          // TODO : handle that
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      * {@inheritDoc}
128      */
129     @Override
130     public Map<Integer, String> getLatestStringMap()
131     {
132         return latestStringMap;
133     }
134 
135     
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public void setLatestStringMap( Map<Integer, String> latestStringMap )
141     {
142         this.latestStringMap = latestStringMap;
143     }
144 }