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   *     https://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.i18n.I18n;
29  import org.apache.directory.api.ldap.model.entry.Attribute;
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  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, Value> valueMap, Set<Value> 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.isHumanReadable() )
88              {
89                  Value 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                 else
102                 {
103                     String strValue = value.getString();
104                     String newValue = computeNewValue( strValue );
105                     
106                     try
107                     {
108                         result.add( newValue );
109                         Value anonValue = new Value( attribute.getAttributeType(), newValue );
110                         valueMap.put( ( Value ) value, anonValue );
111                         valueSet.add( anonValue );
112                     }
113                     catch ( LdapInvalidAttributeValueException e )
114                     {
115                         throw new RuntimeException( I18n.err( I18n.ERR_13436_ERROR_ANONYMIZING_VALUE, strValue ) );
116                     }
117                 }
118             }
119         }
120 
121         return result;
122     }
123     
124     
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public Map<Integer, String> getLatestStringMap()
130     {
131         return latestStringMap;
132     }
133 
134     
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public void setLatestStringMap( Map<Integer, String> latestStringMap )
140     {
141         this.latestStringMap = latestStringMap;
142     }
143 }