001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020
021package org.apache.directory.api.ldap.model.ldif.anonymizer;
022
023
024import java.util.HashMap;
025import java.util.Map;
026import java.util.Set;
027
028import org.apache.directory.api.ldap.model.entry.Attribute;
029import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
030import org.apache.directory.api.ldap.model.entry.StringValue;
031import org.apache.directory.api.ldap.model.entry.Value;
032import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
033import org.apache.directory.api.ldap.model.schema.AttributeType;
034
035
036/**
037 * A default anonymizer for attributes that are HR. It covers DirectoryString, Ia5String, ...
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class CaseSensitiveStringAnonymizer extends AbstractAnonymizer<String>
042{
043    /** The latest anonymized String value map */
044    private Map<Integer, String> latestStringMap;
045
046    /**
047     * Creates a new instance of StringAnonymizer.
048     */
049    public CaseSensitiveStringAnonymizer()
050    {
051        latestStringMap = new HashMap<>();
052        caseSensitive = true;
053    }
054
055    
056    /**
057     * Creates a new instance of StringAnonymizer.
058     * 
059     * @param latestStringMap The map containing the latest value for each length 
060     */
061    public CaseSensitiveStringAnonymizer( Map<Integer, String> latestStringMap )
062    {
063        if ( latestStringMap == null ) 
064        {
065            this.latestStringMap = new HashMap<>();
066        }
067        else
068        {
069            this.latestStringMap = latestStringMap;
070        }
071
072        caseSensitive = true;
073    }
074    
075    
076    /**
077     * Anonymize an attribute using pure random values (either chars of bytes, depending on the Attribute type)
078     */
079    @Override
080    public Attribute anonymize( Map<Value<String>, Value<String>> valueMap, Set<Value<String>> valueSet, Attribute attribute )
081    {
082        AttributeType attributeType = attribute.getAttributeType();
083        Attribute result = new DefaultAttribute( attributeType );
084
085        for ( Value<?> value : attribute )
086        {
087            if ( value instanceof StringValue )
088            {
089                Value<String> anonymized =  valueMap.get( value );
090                
091                if ( anonymized != null )
092                {
093                    try
094                    {
095                        result.add( anonymized );
096                    }
097                    catch ( LdapInvalidAttributeValueException e )
098                    {
099                        // 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}