Skip to content

Commit

Permalink
adjusting of code to convert to string only once if payload was encry…
Browse files Browse the repository at this point in the history
…pted or compressed
  • Loading branch information
tannersherman committed Sep 17, 2024
1 parent 8fb21cc commit 84599db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,25 @@ public String transformPayload(CustomLoggerConfiguration loggerConfig,
if (payloadString == null)
return payloadString;
Compressor compressor = loggerConfig.getCompressor();
Result<InputStream, Void> executeCompress = null;
Result<InputStream, Void> executionResult = null;
if (compressor != null) {
executeCompress = compressPayload(loggerConfig, payloadString);
try {
payloadString = Base64.encodeBytes(convertToByteArray(executeCompress.getOutput(), streamingHelper));
} catch (IOException e) {
throw new RuntimeException("Exception while transforming payload", e);
}
executionResult = compressPayload(loggerConfig, payloadString);
}

EncryptionAlgorithm encryptionAlgorithm = loggerConfig.getEncryptionAlgorithm();
if (encryptionAlgorithm != null) {
payloadString = encryptPayload(loggerConfig, streamingHelper, executeCompress, encryptionAlgorithm,
executionResult = encryptPayload(loggerConfig, streamingHelper, executionResult, encryptionAlgorithm,
payloadString);
}

if (executionResult != null) {
try {
payloadString = Base64.encodeBytes(convertToByteArray(executionResult.getOutput(), streamingHelper));
} catch (IOException e) {
throw new RuntimeException("Exception while transforming payload", e);
}
}

return payloadString;
}

Expand All @@ -88,12 +91,11 @@ public Result<InputStream, Void> compressPayload(CustomLoggerConfiguration logge
.addParameter("content", new ByteArrayInputStream(payload.getBytes()))
.addParameter("compressor", compressorStrategy);
try {
executeCompress = loggerConfig.getExtensionsClient().execute("Compression", "compress",
return loggerConfig.getExtensionsClient().execute("Compression", "compress",
parametersBuilder.build());
} catch (Exception e) {
throw new RuntimeException("Compression Exception", e);
}
return executeCompress;
}

/**
Expand All @@ -106,7 +108,7 @@ public Result<InputStream, Void> compressPayload(CustomLoggerConfiguration logge
* @since 2.1.0
* @return encrypted string
*/
public String encryptPayload(CustomLoggerConfiguration loggerConfig,
public Result<InputStream, Void> encryptPayload(CustomLoggerConfiguration loggerConfig,
StreamingHelper streamingHelper, Result<InputStream, Void> executeCompress,
EncryptionAlgorithm encryptionAlgorithm, String payload) {
/*
Expand All @@ -127,11 +129,9 @@ public String encryptPayload(CustomLoggerConfiguration loggerConfig,
.addParameter("algorithm", jceEncryptionPbeAlgorithm)
.addParameter("password", loggerConfig.getEncryptionPassword());
try {
Result<InputStream, Void> executeEncrypt = loggerConfig.getExtensionsClient().execute("Crypto",
return loggerConfig.getExtensionsClient().execute("Crypto",
"jceEncryptPbe",
encryptionParametersBuilder.build());
return Base64
.encodeBytes(convertToByteArray(executeEncrypt.getOutput(), streamingHelper));
} catch (Exception e) {
throw new RuntimeException("Encryption Error", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,12 @@ public void log_encryptedPayloadIsSet_compressionNull_test() throws MuleExceptio

PayloadTransformer payloadTransformer = new PayloadTransformer();
PayloadTransformer spyTransformer = spy(payloadTransformer);

// mock response from convertToByteArray
doReturn(examplePayload.getBytes()).when(spyTransformer)
.convertToByteArray(any(), any());

// call method
String resultPayload = spyTransformer.encryptPayload(
Result<InputStream, Void> executionResult = spyTransformer.encryptPayload(
loggerConfig,
mock(StreamingHelper.class),
executeCompress,
Expand All @@ -133,7 +132,7 @@ public void log_encryptedPayloadIsSet_compressionNull_test() throws MuleExceptio

// assertions
verify(extensionsClient, atMost(1)).execute(any(), any(), any());
Assert.assertEquals("RXhhbXBsZSBQYXlsb2FkIFRleHQ=", resultPayload);
Assert.assertNotNull(executionResult);
}

@Test
Expand Down

0 comments on commit 84599db

Please sign in to comment.