我们的身份验证算法会根据其主体计算HTTP请求的签名。该算法将主体作为字节数组。
我已经开始实现SignatureCalculator
,但是发现异步HTTP客户端定义了Request
主体的许多不同形式:
Request#getByteData
,Request#getCompositeByteData
,Request#getStringData
,Request#getFile
,每个都需要不同的序列化为byte[]
的方式。异步HTTP客户端中是否有任何serializeRequestBody(Request) : byte[]
方法涵盖不同的正文类型?
我已经研究了AHC代码库中的现有SignatureCalculator
实现,但是所有这些都只考虑一种请求主体:Request#getFormParams
:
到目前为止,我的实现方式是
private byte[] serializeRequestBody(Request request) {
if (request.getByteData() != null) {
return request.getByteData();
} else if (request.getCompositeByteData() != null) {
List<Byte> 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[]{};
}
}