Search in sources :

Example 1 with BlobItem

use of com.microsoft.tooling.msservices.model.storage.BlobItem in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method uploadFile.

private void uploadFile(final String path, final File selectedFile) {
    Job job = new Job("Uploading blob...") {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask("Uploading blob...", IProgressMonitor.UNKNOWN);
            try {
                final BlobDirectory blobDirectory = directoryQueue.peekLast();
                final BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(selectedFile));
                monitor.subTask("0% uploaded");
                try {
                    final CallableSingleArg<Void, Long> callable = new CallableSingleArg<Void, Long>() {

                        @Override
                        public Void call(Long uploadedBytes) throws Exception {
                            double progress = ((double) uploadedBytes) / selectedFile.length();
                            monitor.worked((int) (100 * progress));
                            monitor.subTask(String.format("%s%% uploaded", (int) (progress * 100)));
                            return null;
                        }
                    };
                    try {
                        StorageClientSDKManager.getManager().uploadBlobFileContent(connectionString, blobContainer, path, bufferedInputStream, callable, 1024 * 1024, selectedFile.length());
                    } catch (AzureCmdException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            bufferedInputStream.close();
                        } catch (IOException ignored) {
                        }
                    }
                    if (monitor.isCanceled()) {
                        //                                future.cancel(true);
                        bufferedInputStream.close();
                        for (BlobItem blobItem : StorageClientSDKManager.getManager().getBlobItems(connectionString, blobDirectory)) {
                            if (blobItem instanceof BlobFile && blobItem.getPath().equals(path)) {
                                StorageClientSDKManager.getManager().deleteBlobFile(connectionString, (BlobFile) blobItem);
                            }
                        }
                    }
                    try {
                        directoryQueue.clear();
                        directoryQueue.addLast(StorageClientSDKManager.getManager().getRootDirectory(connectionString, blobContainer));
                        for (String pathDir : path.split("/")) {
                            for (BlobItem blobItem : StorageClientSDKManager.getManager().getBlobItems(connectionString, directoryQueue.getLast())) {
                                if (blobItem instanceof BlobDirectory && blobItem.getName().equals(pathDir)) {
                                    directoryQueue.addLast((BlobDirectory) blobItem);
                                }
                            }
                        }
                    } catch (AzureCmdException e) {
                        DefaultLoader.getUIHelper().showException("Error showing new blob", e, "Error showing new blob", false, true);
                    }
                    DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            fillGrid();
                        }
                    });
                } catch (Exception e) {
                    Throwable connectionFault = e.getCause();
                    Throwable realFault = null;
                    if (connectionFault != null) {
                        realFault = connectionFault.getCause();
                    }
                    monitor.setTaskName("Error uploading Blob");
                    String message = realFault == null ? null : realFault.getMessage();
                    if (connectionFault != null && message == null) {
                        message = "Error type " + connectionFault.getClass().getName();
                    }
                    monitor.subTask((connectionFault instanceof SocketTimeoutException) ? "Connection timed out" : message);
                }
            } catch (Exception e) {
                DefaultLoader.getUIHelper().showException("Error uploading Blob", e, "Error uploading Blob", false, true);
                return Status.CANCEL_STATUS;
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    job.schedule();
}
Also used : BlobDirectory(com.microsoft.tooling.msservices.model.storage.BlobDirectory) CallableSingleArg(com.microsoft.tooling.msservices.helpers.CallableSingleArg) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PartInitException(org.eclipse.ui.PartInitException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) BlobItem(com.microsoft.tooling.msservices.model.storage.BlobItem) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) Job(org.eclipse.core.runtime.jobs.Job)

Example 2 with BlobItem

use of com.microsoft.tooling.msservices.model.storage.BlobItem in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method fillGrid.

public void fillGrid() {
    setUIState(true);
    DefaultLoader.getIdeHelper().runInBackground(null, "Loading blobs...", false, true, "Loading blobs...", new Runnable() {

        @Override
        public void run() {
            try {
                if (directoryQueue.peekLast() == null) {
                    directoryQueue.addLast(StorageClientSDKManager.getManager().getRootDirectory(connectionString, blobContainer));
                }
                blobItems = StorageClientSDKManager.getManager().getBlobItems(connectionString, directoryQueue.peekLast());
                DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        if (!queryTextField.getText().isEmpty()) {
                            for (int i = blobItems.size() - 1; i >= 0; i--) {
                                BlobItem blobItem = blobItems.get(i);
                                if (blobItem instanceof BlobFile && !blobItem.getName().startsWith(queryTextField.getText())) {
                                    blobItems.remove(i);
                                }
                            }
                        }
                        pathLabel.setText(directoryQueue.peekLast().getPath());
                        tableViewer.setInput(blobItems);
                        tableViewer.refresh();
                        setUIState(false);
                    //
                    //                            blobListTable.clearSelection();
                    }
                });
            } catch (AzureCmdException ex) {
                DefaultLoader.getUIHelper().showException("Error querying blob list.", ex, "Error querying blobs", false, true);
            }
        }
    });
}
Also used : BlobItem(com.microsoft.tooling.msservices.model.storage.BlobItem) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile)

Example 3 with BlobItem

use of com.microsoft.tooling.msservices.model.storage.BlobItem in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method fillGrid.

public void fillGrid() {
    setUIState(true);
    ProgressManager.getInstance().run(new Task.Backgroundable(project, "Loading blobs...", false) {

        @Override
        public void run(@NotNull ProgressIndicator progressIndicator) {
            try {
                progressIndicator.setIndeterminate(true);
                if (directoryQueue.peekLast() == null) {
                    directoryQueue.addLast(StorageClientSDKManager.getManager().getRootDirectory(connectionString, blobContainer));
                }
                blobItems = StorageClientSDKManager.getManager().getBlobItems(connectionString, directoryQueue.peekLast());
                if (!queryTextField.getText().isEmpty()) {
                    for (int i = blobItems.size() - 1; i >= 0; i--) {
                        BlobItem blobItem = blobItems.get(i);
                        if (blobItem instanceof BlobFile && !blobItem.getName().startsWith(queryTextField.getText())) {
                            blobItems.remove(i);
                        }
                    }
                }
                ApplicationManager.getApplication().invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        pathLabel.setText(directoryQueue.peekLast().getPath());
                        DefaultTableModel model = (DefaultTableModel) blobListTable.getModel();
                        while (model.getRowCount() > 0) {
                            model.removeRow(0);
                        }
                        for (BlobItem blobItem : blobItems) {
                            if (blobItem instanceof BlobDirectory) {
                                model.addRow(new Object[] { UIHelperImpl.loadIcon("storagefolder.png"), blobItem.getName(), "", "", "", blobItem.getUri() });
                            } else {
                                BlobFile blobFile = (BlobFile) blobItem;
                                model.addRow(new String[] { "", blobFile.getName(), UIHelperImpl.readableFileSize(blobFile.getSize()), new SimpleDateFormat().format(blobFile.getLastModified().getTime()), blobFile.getContentType(), blobFile.getUri() });
                            }
                        }
                        setUIState(false);
                        blobListTable.clearSelection();
                    }
                });
            } catch (AzureCmdException ex) {
                String msg = "An error occurred while attempting to query blob list." + "\n" + String.format(message("webappExpMsg"), ex.getMessage());
                PluginUtil.displayErrorDialogAndLog(message("errTtl"), msg, ex);
            }
        }
    });
}
Also used : BlobItem(com.microsoft.tooling.msservices.model.storage.BlobItem) Task(com.intellij.openapi.progress.Task) BlobDirectory(com.microsoft.tooling.msservices.model.storage.BlobDirectory) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) DefaultTableModel(javax.swing.table.DefaultTableModel) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with BlobItem

use of com.microsoft.tooling.msservices.model.storage.BlobItem in project azure-tools-for-java by Microsoft.

the class StorageClientSDKManager method getBlobItems.

@NotNull
public List<BlobItem> getBlobItems(@NotNull String connectionString, @NotNull BlobDirectory blobDirectory) throws AzureCmdException {
    List<BlobItem> biList = new ArrayList<BlobItem>();
    try {
        CloudBlobClient client = getCloudBlobClient(connectionString);
        String containerName = blobDirectory.getContainerName();
        String delimiter = client.getDirectoryDelimiter();
        CloudBlobContainer container = client.getContainerReference(containerName);
        CloudBlobDirectory directory = container.getDirectoryReference(blobDirectory.getPath());
        for (ListBlobItem item : directory.listBlobs()) {
            String uri = item.getUri() != null ? item.getUri().toString() : "";
            if (item instanceof CloudBlobDirectory) {
                CloudBlobDirectory subDirectory = (CloudBlobDirectory) item;
                String name = extractBlobItemName(subDirectory.getPrefix(), delimiter);
                String path = Strings.nullToEmpty(subDirectory.getPrefix());
                biList.add(new BlobDirectory(name, uri, containerName, path));
            } else if (item instanceof CloudBlob) {
                CloudBlob blob = (CloudBlob) item;
                String name = extractBlobItemName(blob.getName(), delimiter);
                String path = Strings.nullToEmpty(blob.getName());
                String type = "";
                String cacheControlHeader = "";
                String contentEncoding = "";
                String contentLanguage = "";
                String contentType = "";
                String contentMD5Header = "";
                String eTag = "";
                Calendar lastModified = new GregorianCalendar();
                long size = 0;
                BlobProperties properties = blob.getProperties();
                if (properties != null) {
                    if (properties.getBlobType() != null) {
                        type = properties.getBlobType().toString();
                    }
                    cacheControlHeader = Strings.nullToEmpty(properties.getCacheControl());
                    contentEncoding = Strings.nullToEmpty(properties.getContentEncoding());
                    contentLanguage = Strings.nullToEmpty(properties.getContentLanguage());
                    contentType = Strings.nullToEmpty(properties.getContentType());
                    contentMD5Header = Strings.nullToEmpty(properties.getContentMD5());
                    eTag = Strings.nullToEmpty(properties.getEtag());
                    if (properties.getLastModified() != null) {
                        lastModified.setTime(properties.getLastModified());
                    }
                    size = properties.getLength();
                }
                biList.add(new BlobFile(name, uri, containerName, path, type, cacheControlHeader, contentEncoding, contentLanguage, contentType, contentMD5Header, eTag, lastModified, size));
            }
        }
        return biList;
    } catch (Throwable t) {
        throw new AzureCmdException("Error retrieving the Blob Item list", t);
    }
}
Also used : BlobDirectory(com.microsoft.tooling.msservices.model.storage.BlobDirectory) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) BlobItem(com.microsoft.tooling.msservices.model.storage.BlobItem) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) NotNull(com.microsoft.azuretools.azurecommons.helpers.NotNull)

Aggregations

AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)4 BlobFile (com.microsoft.tooling.msservices.model.storage.BlobFile)4 BlobItem (com.microsoft.tooling.msservices.model.storage.BlobItem)4 BlobDirectory (com.microsoft.tooling.msservices.model.storage.BlobDirectory)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 NotNull (com.microsoft.azuretools.azurecommons.helpers.NotNull)1 CallableSingleArg (com.microsoft.tooling.msservices.helpers.CallableSingleArg)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 DefaultTableModel (javax.swing.table.DefaultTableModel)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 PartInitException (org.eclipse.ui.PartInitException)1