use of com.mucommander.commons.file.UnsupportedFileOperationException in project mucommander by mucommander.
the class OvirtDisk method getOutputStream.
@Override
public OutputStream getOutputStream() throws IOException, UnsupportedFileOperationException {
try (OvirtConnHandler connHandler = getConnHandler()) {
Disk disk = Utils.addDisk(connHandler, properties);
Utils.sleep(500);
do {
disk = connHandler.getConnection().systemService().disksService().diskService(disk.id()).get().send().disk();
} while (!isDiskReady(disk));
log.debug("disk has been created");
ImageTransfer transfer = Utils.addImageTransfer(connHandler, disk.id(), ImageTransferDirection.UPLOAD);
final String transferId = transfer.id();
Utils.sleep(500);
do {
transfer = Utils.getImageTransfer(connHandler, transferId);
} while (isTransferInitializing(transfer));
log.debug("Transfer session has been created!");
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
URL url = getDestinationUrl(transfer);
HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
https.setSSLSocketFactory(Utils.setTrustStore(connHandler.getCertificate()));
https.setRequestProperty("PUT", url.getPath());
https.setDoOutput(true);
https.setRequestMethod("PUT");
https.setFixedLengthStreamingMode(properties.actualSizeAsLong());
return new BufferedOutputStream(https.getOutputStream()) {
public void close() throws IOException {
try {
int responseCode = https.getResponseCode();
log.info("Finished uploading disk " + (responseCode == 200 ? "successfully" : "with failure") + " (response code = " + responseCode + ")");
if (responseCode != 200)
throw new IOException("Failed to upload disk");
} catch (Exception e) {
log.error("Failed to upload file", e);
} finally {
try (OvirtConnHandler connHandler = getConnHandler()) {
super.close();
Utils.finalizeImageTransfer(connHandler, transferId);
https.disconnect();
}
}
}
};
}
}
Aggregations