Package io.axoniq.dataprotection.api
Class FieldEncryptingSerializer
- java.lang.Object
-
- io.axoniq.dataprotection.api.FieldEncryptingSerializer
-
- All Implemented Interfaces:
Serializer
public final class FieldEncryptingSerializer extends Object implements Serializer
Implementation ofSerializerwhich wraps a delegateSerializer, but performs encryption before serializing and decryption after deserializing, using aFieldEncrypter. The delegateSerializermay support multiple expected representations, but must supportbyte[]. This serializer will then support any representation supported by the delegate serializer.
-
-
Constructor Summary
Constructors Constructor Description FieldEncryptingSerializer(FieldEncrypter fieldEncrypter, Serializer delegateSerializer)FieldEncryptingSerializer(CryptoEngine cryptoEngine, ReplacementValueProvider replacementValueProvider, Serializer delegateSerializer)Deprecated.in favor of using theFieldEncryptingSerializer(CryptoEngine, Serializer, ReplacementValueProvider, Serializer)to construct an instance using a configuredSerializerinstances.FieldEncryptingSerializer(CryptoEngine cryptoEngine, Serializer delegateSerializer)Deprecated.in favor of using theFieldEncryptingSerializer(CryptoEngine, Serializer, Serializer)to construct an instance using a configuredSerializerinstances.FieldEncryptingSerializer(CryptoEngine cryptoEngine, Serializer personalDataSerializer, ReplacementValueProvider replacementValueProvider, Serializer delegateSerializer)Constructs aFieldEncryptingSerializerusing a givenCryptoEngine,ReplacementValueProviderandSerializer.FieldEncryptingSerializer(CryptoEngine cryptoEngine, Serializer personalDataSerializer, Serializer delegateSerializer)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> booleancanSerializeTo(Class<T> expectedRepresentation)Indicates whether this Serializer is capable of serializing to the givenexpectedRepresentation.ClassclassForType(SerializedType type)Returns the class for the given type identifier.protected Objectclone(Object object)Clones the object.<S,T>
Tdeserialize(SerializedObject<S> serializedObject)Deserializes the first object read from the givenbytes.ConvertergetConverter()Returns theConverterused by this Serializer to convert between serialized representations.<T> SerializedObject<T>serialize(Object object, Class<T> expectedRepresentation)Serialize the givenobjectinto a Serialized Object containing the givenexpectedRepresentation.SerializedTypetypeForClass(Class type)Returns the type identifier for the given class.
-
-
-
Constructor Detail
-
FieldEncryptingSerializer
public FieldEncryptingSerializer(FieldEncrypter fieldEncrypter, Serializer delegateSerializer)
Constructs aFieldEncryptingSerializerfor a givenFieldEncrypterandSerializer. This version of the constructor gives maximum control over theFieldEncrypter.- Parameters:
fieldEncrypter- theFieldEncrypterto be useddelegateSerializer- theSerializerto wrap
-
FieldEncryptingSerializer
@Deprecated public FieldEncryptingSerializer(CryptoEngine cryptoEngine, Serializer delegateSerializer)
Deprecated.in favor of using theFieldEncryptingSerializer(CryptoEngine, Serializer, Serializer)to construct an instance using a configuredSerializerinstances. Using this shorthand still works, but will use anXStreaminstance that allows everything. Although this works, XStream expects the types or wildcards for the types to be defined to ensure the application stays secure. As such, it is highly recommended to follow their recommended approach.Constructs aFieldEncryptingSerializerusing a givenCryptoEngineandSerializer. AFieldEncrypterwill be constructed using theCryptoEngineand using the delegate serializer also as the serializer for theFieldEncrypter. TheFieldEncrypterwill use the defaultReplacementValueProvider.- Parameters:
cryptoEngine- theCryptoEngineto be useddelegateSerializer- theSerializerto wrap
-
FieldEncryptingSerializer
@Deprecated public FieldEncryptingSerializer(CryptoEngine cryptoEngine, ReplacementValueProvider replacementValueProvider, Serializer delegateSerializer)
Deprecated.in favor of using theFieldEncryptingSerializer(CryptoEngine, Serializer, ReplacementValueProvider, Serializer)to construct an instance using a configuredSerializerinstances. Using this shorthand still works, but will use anXStreaminstance that allows everything. Although this works, XStream expects the types or wildcards for the types to be defined to ensure the application stays secure. As such, it is highly recommended to follow their recommended approach.Constructs aFieldEncryptingSerializerusing a givenCryptoEngine,ReplacementValueProviderandSerializer. AFieldEncrypterwill be constructed using theCryptoEngineandReplacementValueProvider, using the delegate serializer also as the serializer for theFieldEncrypter.- Parameters:
cryptoEngine- theCryptoEngineto be usedreplacementValueProvider- theReplacementValueProviderto usedelegateSerializer- theSerializerto wrap
-
FieldEncryptingSerializer
public FieldEncryptingSerializer(CryptoEngine cryptoEngine, Serializer personalDataSerializer, Serializer delegateSerializer)
Constructs aFieldEncryptingSerializerusing a givenCryptoEngineandSerializer. AFieldEncrypterwill be constructed using theCryptoEngineand using the delegate serializer also as the serializer for theFieldEncrypter. TheFieldEncrypterwill use the defaultReplacementValueProvider.- Parameters:
cryptoEngine- theCryptoEngineto be usedpersonalDataSerializer- theSerializerto be used internally to serialize personal data to a string for encryptiondelegateSerializer- theSerializerto wrap
-
FieldEncryptingSerializer
public FieldEncryptingSerializer(CryptoEngine cryptoEngine, Serializer personalDataSerializer, ReplacementValueProvider replacementValueProvider, Serializer delegateSerializer)
Constructs aFieldEncryptingSerializerusing a givenCryptoEngine,ReplacementValueProviderandSerializer. AFieldEncrypterwill be constructed using theCryptoEngineandReplacementValueProvider, using the delegate serializer also as the serializer for theFieldEncrypter.- Parameters:
cryptoEngine- theCryptoEngineto be usedpersonalDataSerializer- theSerializerto be used internally to serialize personal data to a string for encryptionreplacementValueProvider- theReplacementValueProviderto usedelegateSerializer- theSerializerto wrap
-
-
Method Detail
-
clone
protected Object clone(Object object)
Clones the object. Will be invoked prior to encryption ifskipCloningistrue. The default implementation performs cloning by serializing and deserializing, usingbyte[]as the expected intermediate representation. Application may create their own version of this class with a different cloning mechanism.- Parameters:
object- the object to clone- Returns:
- the cloned object
-
serialize
public <T> SerializedObject<T> serialize(Object object, Class<T> expectedRepresentation)
Serialize the givenobjectinto a Serialized Object containing the givenexpectedRepresentation. Performs encryption prior to serialization, and optional cloning before encryption. UsecanSerializeTo(Class)to detect whether theexpectedRepresentationis supported by this serializer.byte[]is always supported.- Specified by:
serializein interfaceSerializer- Type Parameters:
T- The expected data type representing the serialized object- Parameters:
object- The object to serializeexpectedRepresentation- The expected data type representing the serialized object- Returns:
- the instance representing the serialized object.
-
canSerializeTo
public <T> boolean canSerializeTo(Class<T> expectedRepresentation)
Indicates whether this Serializer is capable of serializing to the givenexpectedRepresentation. Will return whatever value is returned by the delegate. Will always returntrueforbyte[].class. Whentrue, this does *not* guarantee that the serialization and (optional) conversion will also succeed when executed. For example, when a serializer produces abyte[]containing JSON, trying to convert to a Dom4J Document will fail, even though this serializer has a converter to convertbyte[]to Dom4J instances.- Specified by:
canSerializeToin interfaceSerializer- Type Parameters:
T- The type of data a Serialized Object should contain- Parameters:
expectedRepresentation- The type of data a Serialized Object should contain- Returns:
trueif theexpectedRepresentationis supported, otherwisefalse.
-
deserialize
public <S,T> T deserialize(SerializedObject<S> serializedObject)
Deserializes the first object read from the givenbytes. Thebytesare not consumed from the array or modified in any way. The resulting object instance is cast to the expected type. After that, decryption takes place.- Specified by:
deserializein interfaceSerializer- Type Parameters:
S- The data type of the serialized objectT- The expected deserialized type- Parameters:
serializedObject- the instance describing the type of object and the bytes providing the serialized data- Returns:
- the serialized object, cast to the expected type
- Throws:
ClassCastException- if the first object in the stream is not an instance of <T>.
-
classForType
public Class classForType(SerializedType type)
Returns the class for the given type identifier. The result of this method must guarantee that the deserialized SerializedObject with the giventypeis an instance of the returned Class. This method will be executed by the delegate. If a class cannot be resolved (i.e. because the class is not available on this JVM's classpath) this method throws UnknownSerializedTypeException (not on Axon 4).- Specified by:
classForTypein interfaceSerializer- Parameters:
type- The type identifier of the object- Returns:
- the Class representing the type of the serialized Object
-
typeForClass
public SerializedType typeForClass(Class type)
Returns the type identifier for the given class. This is the type identifier of the Serialized object as returned byserialize(Object, Class). This method will be executed by the delegate.- Specified by:
typeForClassin interfaceSerializer- Parameters:
type- Class representing the type of the serializable Object.- Returns:
- The type identifier of the object
-
getConverter
public Converter getConverter()
Returns theConverterused by this Serializer to convert between serialized representations. Generally, this Converter depends on the type of data the serializer serializes to. This method will be executed by the delegate.- Specified by:
getConverterin interfaceSerializer- Returns:
- the converter used by this Serializer
-
-