Package dev.blaauwendraad.masker.json
Interface ValueMaskerContext
public interface ValueMaskerContext
Represents the original value that is currently being masked. The context is passed to
ValueMasker.maskValue(ValueMaskerContext)
to allow the implementation to replace the JSON value
with a custom mask.
The JSON value is represented by a byte array to provide a way to access the content of the
original value, based on index. The index includes the JSON value in its entirety which means
that for example for strings the opening and closing quotes are included. The reason for this is
that the API becomes more flexible as it enables strings to be replaced by null
, for
example.
-
Method Summary
Modifier and TypeMethodDescriptionasString
(int fromIndex, int length) Returns a string representation of the original JSON value.int
Returns the length of the original value that is being masked in bytes.int
countNonVisibleCharacters
(int fromIndex, int length) Returns the number of non-visible, human-readable characters in the original value.byte
getByte
(int index) Retrieve the byte at the given index in the original value that is being masked.invalidJson
(String message, int index) Create anInvalidJsonException
with the given message and index relative to the value (i.e.void
replaceBytes
(int fromIndex, int length, byte[] mask, int maskRepeat) Indicates that the bytes of the original value (or part of the value) needs to be replaced with a mask.
-
Method Details
-
getByte
byte getByte(int index) Retrieve the byte at the given index in the original value that is being masked.- Parameters:
index
- the index in the original value between0
(inclusive) andbyteLength()
(exclusive)
-
byteLength
int byteLength()Returns the length of the original value that is being masked in bytes. -
replaceBytes
void replaceBytes(int fromIndex, int length, byte[] mask, int maskRepeat) Indicates that the bytes of the original value (or part of the value) needs to be replaced with a mask.Note: the replacement might result in an invalid JSON, make sure to include opening and closing quotes when the replacement is a string value.
- Parameters:
fromIndex
- index from which the replacement should start. For string values, the opening quote is includedlength
- length of the value to mask. relative to the fromIndex. For string values the closing quote is includedmask
- the mask to replace the original value withmaskRepeat
- number of times to repeat the mask, useful for masking digits or characters, for static masks the value should be 1.- See Also:
-
countNonVisibleCharacters
int countNonVisibleCharacters(int fromIndex, int length) Returns the number of non-visible, human-readable characters in the original value.- Parameters:
fromIndex
- index from which the counting should startlength
- length of the value to count to, relative to the fromIndex- Returns:
- number of non-visible characters in the value
- See Also:
-
asString
Returns a string representation of the original JSON value.Note: this INCLUDES the opening and closing quotes for string values
-
invalidJson
Create anInvalidJsonException
with the given message and index relative to the value (i.e. an index between0
andbyteLength()
).- Parameters:
message
- error messageindex
- relative index where the JSON contains invalid sequence- Returns:
- the exception to be thrown
-