public void messageReceived(BlobRecoveryStartTransferRequest request, TransportChannel channel) throws Exception {
BlobRecoveryStatus status = onGoingRecoveries.get(request.recoveryId());
logger.debug("received BlobRecoveryStartTransferRequest for file {} with size {}",
request.path(), request.size());
if (status == null) {
throw new IllegalBlobRecoveryStateException("Could not retrieve onGoingRecoveryStatus");
}
if (status.canceled()) {
throw new IndexShardClosedException(status.shardId());
}
BlobShard shard = status.blobShard;
String tmpPath = request.path() + "." + request.transferId();
FileOutputStream outputStream = new FileOutputStream(
new File(shard.blobContainer().getBaseDirectory(), tmpPath)
);
BytesReference content = request.content();
if (!content.hasArray()) {
content = content.toBytesArray();
}
outputStream.write(content.array(), content.arrayOffset(), content.length());
if (request.size() == request.content().length()) { // start request contains the whole file.
outputStream.close();
File source = new File(shard.blobContainer().getBaseDirectory(), tmpPath);
File target = new File(shard.blobContainer().getBaseDirectory(), request.path());
if (!target.exists()) {
if (!source.renameTo(target)) {
throw new IllegalBlobRecoveryStateException(
"couldn't rename file to " + request.path()
);
}
}
} else {