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 023import java.util.Arrays; 024import java.util.HashMap; 025import java.util.Map; 026 027import org.apache.directory.api.i18n.I18n; 028import org.apache.directory.api.ldap.model.schema.SchemaManager; 029 030/** 031 * An abstract class implementing the default behavior of an Aninymizer instance 032 * 033 * @param <K> The type of object being anonymized 034 * 035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 036 */ 037public abstract class AbstractAnonymizer<K> implements Anonymizer<K> 038{ 039 /** The SchemaManager instance */ 040 protected SchemaManager schemaManager; 041 042 /** The map of AttributeType'sOID we want to anonymize. They are all associated with anonymizers */ 043 protected Map<String, Anonymizer<K>> attributeAnonymizers = new HashMap<>(); 044 045 /** A flag set to <tt>true</tt> if the AttributeType is case sensitive */ 046 protected boolean caseSensitive = false; 047 048 /** Map of chars to use in the anonymized values 0 5 10 15 20 25 30 35 40*/ 049 private static final char[] NOT_SENSITIVE_MAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'()-./".toCharArray(); 050 private static final char[] SENSITIVE_MAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'()-./abcdefghijklmnopqrstuvwxyz".toCharArray(); 051 052 /** A table containing booleans when the corresponding char is printable */ 053 private static final int[] CHAR_MAP = 054 { 055 // ---, ---, ---, ---, ---, ---, ---, --- 056 0, 0, 0, 0, 0, 0, 0, 0, 057 // ---, ---, ---, ---, ---, ---, ---, --- 058 0, 0, 0, 0, 0, 0, 0, 0, 059 // ---, ---, ---, ---, ---, ---, ---, --- 060 0, 0, 0, 0, 0, 0, 0, 0, 061 // ---, ---, ---, ---, ---, ---, ---, --- 062 0, 0, 0, 0, 0, 0, 0, 0, 063 // ---, ---, ---, ---, ---, ---, ---, "'" 064 0, 0, 0, 0, 0, 0, 0, 36, 065 // '(', ')', ---, '+', ',', '-', '.', '/' 066 37, 38, 0, 0, 0, 39, 40, 41, 067 // '0', '1', '2', '3', '4', '5', '6', '7', 068 26, 27, 28, 29, 30, 31, 32, 33, 069 // '8', '9', ':', ---, ---, '=', ---, '?' 070 34, 35, 0, 0, 0, 0, 0, 26, 071 // ---, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 072 0, 0, 1, 2, 3, 4, 5, 6, 073 // 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O' 074 7, 8, 9, 10, 11, 12, 13, 14, 075 // 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W' 076 15, 16, 17, 18, 19, 20, 21, 22, 077 // 'X', 'Y', 'Z', ---, ---, ---, ---, --- 078 23, 24, 25, 0, 0, 0, 0, 0, 079 // ---, 'a', 'b', 'c', 'd', 'e', 'f', 'g' 080 0, 42, 43, 44, 45, 46, 47, 48, 081 // 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o' 082 49, 50, 51, 52, 53, 54, 55, 56, 083 // 'p', 'q', 'r', 's', 't', 'u', 'v', 'w' 084 57, 58, 59, 60, 61, 62, 63, 64, 085 // 'x', 'y', 'z', ---, ---, ---, ---, --- 086 65, 66, 67, 0, 0, 0, 0, 0, 087 }; 088 089 /** 090 * {@inheritDoc} 091 */ 092 @Override 093 public void setSchemaManager( SchemaManager schemaManager ) 094 { 095 this.schemaManager = schemaManager; 096 } 097 098 099 /** 100 * Set the list of existing anonymizers 101 * 102 * @param attributeAnonymizers The list of existing anonymizers 103 */ 104 @Override 105 public void setAnonymizers( Map<String, Anonymizer<K>> attributeAnonymizers ) 106 { 107 this.attributeAnonymizers = attributeAnonymizers; 108 } 109 110 111 /** 112 * {@inheritDoc} 113 */ 114 @Override 115 public Map<Integer, String> getLatestStringMap() 116 { 117 return null; 118 } 119 120 121 /** 122 * @param latestStringMap The latest String anonymized value map 123 */ 124 @Override 125 public void setLatestStringMap( Map<Integer, String> latestStringMap ) 126 { 127 // Do nothing 128 } 129 130 131 /** 132 * {@inheritDoc} 133 */ 134 @Override 135 public Map<Integer, byte[]> getLatestBytesMap() 136 { 137 return null; 138 } 139 140 141 /** 142 * @param latestBytesMap The latest byte[] anonymized value map 143 */ 144 @Override 145 public void setLatestBytesMap( Map<Integer, byte[]> latestBytesMap ) 146 { 147 // Do nothing 148 } 149 150 151 /** 152 * Compute the next String value 153 * 154 * @param valStr The original value 155 * @return The anonymized value 156 */ 157 protected String computeNewValue( String valStr ) 158 { 159 int length = valStr.length(); 160 String latestString = getLatestStringMap().get( length ); 161 char[] charMap; 162 163 if ( caseSensitive ) 164 { 165 charMap = SENSITIVE_MAP; 166 } 167 else 168 { 169 charMap = NOT_SENSITIVE_MAP; 170 } 171 172 int lastMapChar = charMap.length - 1; 173 174 if ( latestString == null ) 175 { 176 // No previous value : create a new one 177 char[] newValue = new char[length]; 178 179 Arrays.fill( newValue, charMap[0] ); 180 181 String anonymizedValue = new String( newValue ); 182 getLatestStringMap().put( length, anonymizedValue ); 183 184 return anonymizedValue; 185 } 186 else 187 { 188 // Compute a new value 189 char[] latest = latestString.toCharArray(); 190 boolean overflow = true; 191 192 for ( int i = length - 1; i >= 0; i-- ) 193 { 194 if ( latest[i] == charMap[lastMapChar] ) 195 { 196 latest[i] = charMap[0]; 197 } 198 else 199 { 200 latest[i] = charMap[CHAR_MAP[latest[i]] + 1]; 201 overflow = false; 202 break; 203 } 204 } 205 206 String anonymizedValue = new String( latest ); 207 208 if ( overflow ) 209 { 210 // We have exhausted all the possible values... 211 String msg = I18n.err( I18n.ERR_13435_CANNOT_COMPUTE_NEW_VALUE, anonymizedValue ); 212 213 throw new RuntimeException( msg ); 214 } 215 216 getLatestStringMap().put( length, anonymizedValue ); 217 218 return anonymizedValue; 219 } 220 } 221 222 223 /** 224 * Compute the next byte[] value 225 * 226 * @param valBytes The original value 227 * @return The anonymized value 228 */ 229 protected byte[] computeNewValue( byte[] valBytes ) 230 { 231 int length = valBytes.length; 232 byte[] latestBytes = getLatestBytesMap().get( length ); 233 234 if ( latestBytes == null ) 235 { 236 // No previous value : create a new one 237 byte[] newValue = new byte[length]; 238 239 Arrays.fill( newValue, ( byte ) 'A' ); 240 241 getLatestBytesMap().put( length, newValue ); 242 243 return newValue; 244 } 245 else 246 { 247 // Compute a new value 248 boolean overflow = true; 249 250 for ( int i = length - 1; i >= 0; i-- ) 251 { 252 if ( latestBytes[i] == ( byte ) 'Z' ) 253 { 254 latestBytes[i] = ( byte ) 'A'; 255 } 256 else 257 { 258 latestBytes[i]++; 259 overflow = false; 260 break; 261 } 262 } 263 264 if ( overflow ) 265 { 266 // We have exhausted all the possible values... 267 String msg = I18n.err( I18n.ERR_13435_CANNOT_COMPUTE_NEW_VALUE, latestBytes ); 268 269 throw new RuntimeException( msg ); 270 } 271 272 return latestBytes; 273 } 274 } 275}