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 * https://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.i18n.I18n; 029import org.apache.directory.api.ldap.model.entry.Attribute; 030import org.apache.directory.api.ldap.model.entry.DefaultAttribute; 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, Value> valueMap, Set<Value> 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.isHumanReadable() ) 088 { 089 Value anonymized = valueMap.get( value ); 090 091 if ( anonymized != null ) 092 { 093 try 094 { 095 result.add( anonymized ); 096 } 097 catch ( LdapInvalidAttributeValueException e ) 098 { 099 } 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}