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; 033 034 035/** 036 * A default anonymizer for attributes that are not HR 037 * 038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 039 */ 040public class BinaryAnonymizer extends AbstractAnonymizer<byte[]> 041{ 042 /** The latest anonymized byte[] value map */ 043 protected Map<Integer, byte[]> latestBytesMap = new HashMap<>(); 044 045 /** 046 * Creates a new instance of BinaryAnonymizer. 047 */ 048 public BinaryAnonymizer() 049 { 050 latestBytesMap = new HashMap<>(); 051 } 052 053 054 /** 055 * Creates a new instance of BinaryAnonymizer. 056 * 057 * @param latestBytesMap The map containing the latest value for each length 058 */ 059 public BinaryAnonymizer( Map<Integer, byte[]> latestBytesMap ) 060 { 061 if ( latestBytesMap == null ) 062 { 063 this.latestBytesMap = new HashMap<>(); 064 } 065 else 066 { 067 this.latestBytesMap = latestBytesMap; 068 } 069 } 070 071 /** 072 * Anonymize an attribute using pure random values (either chars of bytes, depending on the Attribute type) 073 */ 074 @Override 075 public Attribute anonymize( Map<Value, Value> valueMap, Set<Value> valueSet, Attribute attribute ) 076 { 077 Attribute result = new DefaultAttribute( attribute.getAttributeType() ); 078 079 for ( Value value : attribute ) 080 { 081 byte[] bytesValue = value.getBytes(); 082 byte[] newValue = computeNewValue( bytesValue ); 083 084 try 085 { 086 result.add( newValue ); 087 Value anonValue = new Value( attribute.getAttributeType(), newValue ); 088 valueMap.put( ( Value ) value, anonValue ); 089 valueSet.add( anonValue ); 090 } 091 catch ( LdapInvalidAttributeValueException e ) 092 { 093 throw new RuntimeException( I18n.err( I18n.ERR_13436_ERROR_ANONYMIZING_VALUE, value ) ); 094 } 095 } 096 097 return result; 098 } 099 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}