Search in sources :

Example 6 with UnsupportedFileOperationException

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();
                    }
                }
            }
        };
    }
}
Also used : ImageTransfer(org.ovirt.engine.sdk4.types.ImageTransfer) IOException(java.io.IOException) Disk(org.ovirt.engine.sdk4.types.Disk) BufferedOutputStream(java.io.BufferedOutputStream) URL(java.net.URL) FileURL(com.mucommander.commons.file.FileURL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnsupportedFileOperationException(com.mucommander.commons.file.UnsupportedFileOperationException)

Aggregations

UnsupportedFileOperationException (com.mucommander.commons.file.UnsupportedFileOperationException)6 IOException (java.io.IOException)6 FileURL (com.mucommander.commons.file.FileURL)4 AbstractFile (com.mucommander.commons.file.AbstractFile)2 FileOperation (com.mucommander.commons.file.FileOperation)2 BufferedInputStream (java.io.BufferedInputStream)2 BufferedOutputStream (java.io.BufferedOutputStream)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2 ImageTransfer (org.ovirt.engine.sdk4.types.ImageTransfer)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 DbxException (com.dropbox.core.DbxException)1 CommitInfo (com.dropbox.core.v2.files.CommitInfo)1 CreateFolderResult (com.dropbox.core.v2.files.CreateFolderResult)1 DbxUserFilesRequests (com.dropbox.core.v2.files.DbxUserFilesRequests)1 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)1 FileMetadata (com.dropbox.core.v2.files.FileMetadata)1 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)1