Package io.axoniq.dataprotection.api

## Class ReplacementValueProvider

- java.lang.Object
- - io.axoniq.dataprotection.api.ReplacementValueProvider

-     public class ReplacementValueProvider
      extends Object

  Class that represents the replacement value mechanism. It takes care of setting a value in the class when the decryption key is no longer there. The default behaviour of this class is very simple: for `String` fields, it will use the value of the `replacement` attribute in the `PersonalData` annotation, which is the empty `String` by default. In all other cases, it will be `null`. Applications may define more fine grained behaviour by creating their own subclass and providing this when constructing a `FieldEncrypter` or `FieldEncryptingSerializer`.

  A method of this class is invoked when a key is missing upon decryption. However, we also see an invocation upon each encryption. This is to provide a partial, clear to be included in the storage. By default, nothing is stored. This allows functionality such as keeping the year of a full date even if the date itself gets deleted, or keeping the last 4 digits of a credit card number.

- - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `ReplacementValueProvider()` |

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `byte[]` | `partialValueForStorage​(Class<?> clazz, Field field, Type fieldType, String groupName, String replacement, Object inputValue)` | Invoked as part of the encryption process.
    `Object` | `replacementValue​(Class<?> clazz, Field field, Type fieldType, String groupName, String replacement, byte[] storedPartialValue)` | Invoked when a field cannot be decrypted because the key is missing.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ReplacementValueProvider

          public ReplacementValueProvider()

  - ### Method Detail

    - #### replacementValue

          public Object replacementValue​(Class<?> clazz,
                                         Field field,
                                         Type fieldType,
                                         String groupName,
                                         String replacement,
                                         byte[] storedPartialValue)

      Invoked when a field cannot be decrypted because the key is missing. The default implementation returns the value of `replacement` if `fieldType` equals `String`, and `null` otherwise (or the default value if `fieldType` represents a primitive).
      Parameters:
        `clazz` - the class of the object on which the field was found. This is not necessarily the same as `field.getDeclaringClass()`, since the field may be declared on a superclass of the class of our current object
        `field` - the field we're decrypting
        `fieldType` - the generic type of value that we need. This is not always the same as `field.getGenericType()`, since the module itself recurses into collections and arrays. So, if the field has type `List<String>`, then the `fieldType` will be `String` rather than `List<String>`.
        `groupName` - the `groupName` from the `PersonalData` or `SerializedPersonalData` annotation
        `replacement` - the `replacement` from the `PersonalData` or `SerializedPersonalData` annotation
        `storedPartialValue` - the clear partial value previously stored with the encrypted value; may be `null`
      Returns:
        the object to be used as field value; may be `null`

    - #### partialValueForStorage

          public byte[] partialValueForStorage​(Class<?> clazz,
                                               Field field,
                                               Type fieldType,
                                               String groupName,
                                               String replacement,
                                               Object inputValue)

      Invoked as part of the encryption process. Provides a partial, clear value in the form of a byte array to be stored with the encrypted value. The default implementation always returns null.
      Parameters:
        `clazz` - the class of the object on which the field was found. This is not necessarily the same as `field.getDeclaringClass()`, since the field may be declared on a superclass of the class of our current object
        `field` - the field we're decrypting
        `fieldType` - the generic type of value that we need. This is not always the same as `field.getGenericType()`, since the module itself recurses into collections and arrays. So, if the field has type `List<String>`, then the `fieldType` will be `String` rather than `List<String>`.
        `groupName` - the `groupName` from the `PersonalData` or `SerializedPersonalData` annotation
        `replacement` - the `replacement` from the `PersonalData` or `SerializedPersonalData` annotation
        `inputValue` - the input value
      Returns:
        the byte array to be stored; may be `null`