Class ExampleUtil
This class centralises common functionality used by the
ExampleScenario* programs, including:
- Loading CLI configuration from files or classpath resources
- Authenticating via encrypted credentials or a pre-issued JWT token
- JSON serialisation with ISO-8601 date/time support
- Deriving and decrypting cryptographic keys (PBKDF2, AES-GCM)
- Ensuring a temporary working directory for example artefacts
- Uniform error handling that classifies SDK exceptions (timeout, HTTP, I/O, etc.)
- Pretty-printing operation metadata (encrypt, decrypt, sign, verify, re-encrypt, re-sign, combined operations)
- Comparing input streams for byte-level equality
All methods are static. This class cannot be instantiated.
Warning: Several methods in this class call
fatal(String, Throwable), which terminates the JVM via
System.exit(int). This is acceptable for standalone example
programs but must not be adopted in production SDK
integrations. Prefer throwing exceptions and letting the caller
decide how to handle failures.
- Since:
- 3.0.0
- Author:
- ANKATech Solutions Inc.
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic AuthenticatedSdkauthenticate(Properties props) Authenticates using encrypted credentials stored incli.propertiesand returns anAuthenticatedSdk.static voidEnsures that theTEMP_DIRdirectory exists, creating it (and any parent directories) if necessary.static voidensureTempDir(Path dir) Ensures that the given directory exists, creating it (and any parent directories) if necessary.static voidLogs a fatal error tostderrand terminates the JVM with exit code 1.static PropertiesLoads CLI properties using the following look-up order: Acli.propertiesfile in the current working directory. The file pointed to by thecli.configsystem property. A/cli.propertiesresource on the classpath.static StringReturns the input string if it is non-nulland non-blank; otherwise returns the literal"(none)".static voidPrints decryption metadata extracted from a fullDecryptResult, preceded by a section header.static voidPrints decryption operation metadata tostdout.static voidPrints metadata for a decrypt-then-verify combined operation, showing both the decryption layer and the verification layer.static voidPrints encryption operation metadata tostdout.static voidPrints re-encryption metadata showing both the old and new key and algorithm details.static voidPrints re-signature metadata showing both the old and new key and algorithm details.static voidPrints metadata for a sign-then-encrypt combined operation, showing both the signature layer and the encryption layer.static voidPrints signature operation metadata tostdout.static voidprintSignMeta(String heading, SignResult m) Prints signature metadata preceded by a contextual heading.static voidPrints signature verification metadata tostdout.static voidprintVerifyMeta(String heading, VerifySignatureResult m) Prints signature verification metadata preceded by a contextual heading and including the validity flag.static voidprintWarnings(List<CryptoWarning> warnings) Prints a list of warning messages tostdout.static booleanCompares two input streams byte-by-byte to determine whether they contain identical content.static StringSerialises an object to its JSON string representation using the SDK's internal JSON mapper (ISO-8601 dates, pretty-print).static StringReturns the material version as a string if it is non-null; otherwise returns the literal"n/a"(e.g. utility mode, or streaming flows where the version rides the JOSE artefact).
-
Field Details
-
TEMP_DIR
Working directory for the temporary artefacts produced by examples.
-
-
Method Details
-
loadProperties
Loads CLI properties using the following look-up order:- A
cli.propertiesfile in the current working directory. - The file pointed to by the
cli.configsystem property. - A
/cli.propertiesresource on the classpath.
If none of these sources exist, the method calls
fatal(String, Throwable)and the JVM terminates.- Returns:
- a
Propertiesobject with the loaded key-value pairs; nevernull - Throws:
UncheckedIOException- if an I/O error occurs while reading the configuration file
- A
-
authenticate
Authenticates using encrypted credentials stored incli.propertiesand returns anAuthenticatedSdk.The method reads four mandatory properties, derives an AES key with PBKDF2, decrypts the client credentials, and performs application authentication via
AnkaSecureSdk.authenticateApplication(String, co.ankatech.ankasecure.sdk.security.SecretChars).Required properties:
client.uuid— unique identifier for the clientclient.salt— hex-encoded salt for key derivationclientIdEnc— AES-GCM-encrypted client ID (Base64)clientSecretEnc— AES-GCM-encrypted client secret (Base64)
If any property is missing the method calls
fatal(String, Throwable)and the JVM terminates.- Parameters:
props- CLI properties loaded vialoadProperties(); must not benull- Returns:
- an
AuthenticatedSdkinstance bound to a valid JWT token; nevernullunder normal operation (the JVM exits on failure) - See Also:
-
toJson
Serialises an object to its JSON string representation using the SDK's internal JSON mapper (ISO-8601 dates, pretty-print).- Parameters:
o- the object to serialise; must not benull- Returns:
- a JSON string; never
null - Throws:
UncheckedIOException- if serialisation fails
-
fatal
Logs a fatal error tostderrand terminates the JVM with exit code 1.If the throwable is an
AnkaSecureSdkExceptionthe message is enriched with theSdkErrorCodecategory (timeout, HTTP status, I/O). The full stack trace is printed only when the system propertyankasecure.debugStackis set totrue.Warning — JVM termination: this method calls
System.exit(int)and never returns. It is designed exclusively for standalone example programs. Production code should throw an appropriate exception instead.- Parameters:
msg- the error message to display; must not benullt- the causal exception, ornullif none
-
ensureTempDir
public static void ensureTempDir()Ensures that theTEMP_DIRdirectory exists, creating it (and any parent directories) if necessary.On failure the method calls
fatal(String, Throwable)and the JVM terminates.- See Also:
-
ensureTempDir
Ensures that the given directory exists, creating it (and any parent directories) if necessary.On failure the method calls
fatal(String, Throwable)and the JVM terminates.- Parameters:
dir- the directory to create; must not benull- See Also:
-
streamsAreEqual
Compares two input streams byte-by-byte to determine whether they contain identical content.Both streams are read in 8 KiB chunks until one (or both) reach EOF. The method returns
trueonly if every byte matches and both streams reach EOF at the same position.- Parameters:
a- the first stream; must not benullb- the second stream; must not benull- Returns:
trueif the streams are byte-identical;falseotherwise- Throws:
IOException- if an I/O error occurs while reading either stream
-
nullSafe
Returns the input string if it is non-nulland non-blank; otherwise returns the literal"(none)".- Parameters:
s- the input string; may benull- Returns:
sor"(none)"
-
versionSafe
Returns the material version as a string if it is non-null; otherwise returns the literal"n/a"(e.g. utility mode, or streaming flows where the version rides the JOSE artefact).- Parameters:
v- the material version; may benull- Returns:
vas a string or"n/a"
-
printEncryptMeta
Prints encryption operation metadata tostdout.Displays the key requested, the key-material version, the algorithm, and any non-fatal warnings returned by the server.
- Parameters:
r- the encryption result; must not benull- See Also:
-
printDecryptMeta
Prints decryption operation metadata tostdout.Displays the key requested, the key-material version, the algorithm, and any non-fatal warnings.
- Parameters:
m- the decryption metadata; must not benull- See Also:
-
printDecryptMeta
Prints decryption metadata extracted from a fullDecryptResult, preceded by a section header.- Parameters:
r- the decryption result containing embedded metadata; must not benull- See Also:
-
printSignMeta
Prints signature operation metadata tostdout.- Parameters:
r- the signature result; must not benull- See Also:
-
printSignMeta
Prints signature metadata preceded by a contextual heading.- Parameters:
heading- a section label displayed before the metadatam- the signature result; must not benull- See Also:
-
printVerifyMeta
Prints signature verification metadata tostdout.- Parameters:
r- the verification result; must not benull- See Also:
-
printVerifyMeta
Prints signature verification metadata preceded by a contextual heading and including the validity flag.- Parameters:
heading- a section label displayed before the metadatam- the verification result; must not benull- See Also:
-
printReencryptMeta
Prints re-encryption metadata showing both the old and new key and algorithm details.- Parameters:
m- the re-encryption result; must not benull- See Also:
-
printResignMeta
Prints re-signature metadata showing both the old and new key and algorithm details.- Parameters:
m- the re-signature result; must not benull- See Also:
-
printSignEncryptMeta
Prints metadata for a sign-then-encrypt combined operation, showing both the signature layer and the encryption layer.- Parameters:
r- the combined result; must not benull- See Also:
-
printDecryptVerifyMeta
Prints metadata for a decrypt-then-verify combined operation, showing both the decryption layer and the verification layer.- Parameters:
r- the combined result; must not benull- See Also:
-
printWarnings
Prints a list of warning messages tostdout. Each warning is indented and prefixed with a bullet. Does nothing if the list isnullor empty.- Parameters:
warnings- the warning messages; may benull
-