if (useBinary) {
if (responseMessage.getStatus().getCode().isSuccess())
byteBuf.writeBytes(serializer.serializeResponseAsBinary(responseMessage, channelHandlerContext.alloc()));
else {
byteBuf.writeBytes(serializer.serializeResponseAsBinary(responseMessage, channelHandlerContext.alloc()));
final ResponseMessage terminator = ResponseMessage.build(responseMessage.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
byteBuf.writeBytes(serializer.serializeResponseAsBinary(terminator, channelHandlerContext.alloc()));
errorMeter.mark();
}
} else {
// the expectation is that the GremlinTextRequestDecoder will have placed a MessageTextSerializer
// instance on the channel.
final MessageTextSerializer textSerializer = (MessageTextSerializer) serializer;
if (responseMessage.getStatus().getCode().isSuccess())
byteBuf.writeBytes(textSerializer.serializeResponseAsString(responseMessage).getBytes(UTF8));
else {
byteBuf.writeBytes(textSerializer.serializeResponseAsString(responseMessage).getBytes(UTF8));
final ResponseMessage terminator = ResponseMessage.build(responseMessage.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
byteBuf.writeBytes(textSerializer.serializeResponseAsString(terminator).getBytes(UTF8));
errorMeter.mark();
}
}
} catch (Exception ex) {
errorMeter.mark();
logger.warn("The result [{}] in the request {} could not be serialized and returned.", responseMessage.getResult(), responseMessage.getRequestId(), ex);
final String errorMessage = String.format("Error during serialization: %s",
ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage());
final ResponseMessage error = ResponseMessage.build(responseMessage.getRequestId())
.statusMessage(errorMessage)
.code(ResponseStatusCode.SERVER_ERROR_SERIALIZATION).create();
if (useBinary) {
channelHandlerContext.write(serializer.serializeResponseAsBinary(error, channelHandlerContext.alloc()));
final ResponseMessage terminator = ResponseMessage.build(responseMessage.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
channelHandlerContext.writeAndFlush(serializer.serializeResponseAsBinary(terminator, channelHandlerContext.alloc()));
} else {
final MessageTextSerializer textSerializer = (MessageTextSerializer) serializer;
channelHandlerContext.write(textSerializer.serializeResponseAsString(error));
final ResponseMessage terminator = ResponseMessage.build(responseMessage.getRequestId()).code(ResponseStatusCode.SUCCESS_TERMINATOR).create();
channelHandlerContext.writeAndFlush(textSerializer.serializeResponseAsString(terminator));
}
}
}