Signing request body in Async HTTP Client

Hi,

Our authentication algorithm calculates a signature of an HTTP request based on its body. The algorithm takes body as an array of bytes.

I have started to implement SignatureCalculator, but found that Asynchronous HTTP Client (AHC) defines many different forms of body of Request:

  • Request#getByteData,
  • Request#getCompositeByteData,
  • Request#getStringData,
  • Request#getFile,
  • etc.

each requiring different way of serializing to byte[]. Is there any serializeRequestBody(Request) : byte[] method in Gatling or Asynchronous HTTP Client that covers different body types?

I have looked into existing SignatureCalculator implementations in AHC codebase, but all of them consider only one type of request body: Request#getFormParams:

My implementation so far:

private byte[] serializeRequestBody(Request request) {

if (request.getByteData() != null) {
return request.getByteData();
} else if (request.getCompositeByteData() != null) {
List buff = new ArrayList<>();
for (byte[] bytes : request.getCompositeByteData()) {
buff.addAll(Bytes.asList(bytes));
}
return Bytes.toArray(buff);
} else if (request.getStringData() != null) {
throw new UnsupportedOperationException(“Serializing StringData in request body is not supported”);
} else if (request.getByteBufferData() != null) {
throw new UnsupportedOperationException(“Serializing ByteBufferData in request body is not supported”);
} else if (request.getStreamData() != null) {
throw new UnsupportedOperationException(“Serializing StreamData in request body is not supported”);
} else if (isNonEmpty(request.getFormParams())) {
throw new UnsupportedOperationException(“Serializing FormParams in request body is not supported”);
} else if (isNonEmpty(request.getBodyParts())) {
throw new UnsupportedOperationException(“Serializing BodyParts in request body is not supported”);
} else if (request.getFile() != null) {
throw new UnsupportedOperationException(“Serializing File in request body is not supported”);
} else if (request.getBodyGenerator() instanceof FileBodyGenerator) {
throw new UnsupportedOperationException(“Serializing FileBodyGenerator in request body is not supported”);
} else if (request.getBodyGenerator() instanceof InputStreamBodyGenerator) {
throw new UnsupportedOperationException(“Serializing InputStreamBodyGenerator in request body is not supported”);
} else if (request.getBodyGenerator() instanceof ReactiveStreamsBodyGenerator) {
throw new UnsupportedOperationException(“Serializing ReactiveStreamsBodyGenerator in request body is not supported”);
} else if (request.getBodyGenerator() != null) {
throw new UnsupportedOperationException(“Serializing generic BodyGenerator in request body is not supported”);
} else {
return new byte[]{};
}
}