Search in sources :

Example 16 with AgentFile

use of org.netxms.client.server.AgentFile in project netxms by netxms.

the class AgentFileManager method downloadDir.

/**
 * Recursively download directory from agent to local pc
 *
 * @param sf
 * @param localFileName
 * @throws IOException
 * @throws NXCException
 */
private void downloadDir(final AgentFile sf, String localFileName, ZipOutputStream zos, final IProgressMonitor monitor, ConsoleJobCallingServerJob job) throws NXCException, IOException {
    List<AgentFile> files = sf.getChildren();
    if (files == null) {
        files = session.listAgentFiles(sf, sf.getFullName(), sf.getNodeId());
        sf.setChildren(files);
    }
    for (AgentFile f : files) {
        if (job.isCanceled())
            break;
        if (f.isDirectory()) {
            downloadDir(f, localFileName + "/" + f.getName(), zos, monitor, job);
        } else {
            monitor.subTask(String.format("Compressing file %s", f.getFullName()));
            final AgentFileData file = session.downloadFileFromAgent(objectId, f.getFullName(), 0, false, new ProgressListener() {

                @Override
                public void setTotalWorkAmount(long workTotal) {
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) workDone);
                }
            }, job);
            if (file != null && file.getFile() != null) {
                FileInputStream fis = new FileInputStream(file.getFile());
                ZipEntry zipEntry = new ZipEntry(localFileName + "/" + f.getName());
                zos.putNextEntry(zipEntry);
                byte[] bytes = new byte[1024];
                int length;
                while ((length = fis.read(bytes)) >= 0) {
                    zos.write(bytes, 0, length);
                }
                zos.closeEntry();
                fis.close();
            }
        }
    }
}
Also used : ProgressListener(org.netxms.client.ProgressListener) AgentFile(org.netxms.client.server.AgentFile) ZipEntry(java.util.zip.ZipEntry) AgentFileData(org.netxms.client.AgentFileData) FileInputStream(java.io.FileInputStream)

Example 17 with AgentFile

use of org.netxms.client.server.AgentFile in project netxms by netxms.

the class AgentFileManager method uploadFile.

/**
 * Upload local file to agent
 */
private void uploadFile(final boolean overvrite) {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.isEmpty())
        return;
    final Object[] objects = selection.toArray();
    final AgentFile upladFolder = ((AgentFile) objects[0]).isDirectory() ? ((AgentFile) objects[0]) : ((AgentFile) objects[0]).getParent();
    final StartClientToServerFileUploadDialog dlg = new StartClientToServerFileUploadDialog(getSite().getShell());
    if (dlg.open() == Window.OK) {
        final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
        new ConsoleJob(Messages.get().AgentFileManager_UploadFileJobTitle, null, Activator.PLUGIN_ID, null) {

            @Override
            protected void runInternal(final IProgressMonitor monitor) throws Exception {
                List<File> fileList = dlg.getLocalFiles();
                for (int i = 0; i < fileList.size(); i++) {
                    final File localFile = fileList.get(i);
                    String remoteFile = fileList.get(i).getName();
                    if (fileList.size() == 1)
                        remoteFile = dlg.getRemoteFileName();
                    final String rFileName = remoteFile;
                    new NestedVerifyOverwrite(localFile.isDirectory() ? AgentFile.DIRECTORY : AgentFile.FILE, localFile.getName(), true, true, false) {

                        @Override
                        public void executeAction() throws NXCException, IOException {
                            session.uploadLocalFileToAgent(objectId, localFile, upladFolder.getFullName() + "/" + rFileName, overvrite, new // $NON-NLS-1$
                            ProgressListener() {

                                private long prevWorkDone = 0;

                                @Override
                                public void setTotalWorkAmount(long workTotal) {
                                    monitor.beginTask(Messages.get(getDisplay()).UploadFileToServer_TaskNamePrefix + localFile.getAbsolutePath(), (int) workTotal);
                                }

                                @Override
                                public void markProgress(long workDone) {
                                    monitor.worked((int) (workDone - prevWorkDone));
                                    prevWorkDone = workDone;
                                }
                            });
                            monitor.done();
                        }

                        @Override
                        public void executeSameFunctionWithOverwrite() throws IOException, NXCException {
                            session.uploadLocalFileToAgent(objectId, localFile, upladFolder.getFullName() + "/" + rFileName, true, new // $NON-NLS-1$
                            ProgressListener() {

                                private long prevWorkDone = 0;

                                @Override
                                public void setTotalWorkAmount(long workTotal) {
                                    monitor.beginTask(Messages.get(getDisplay()).UploadFileToServer_TaskNamePrefix + localFile.getAbsolutePath(), (int) workTotal);
                                }

                                @Override
                                public void markProgress(long workDone) {
                                    monitor.worked((int) (workDone - prevWorkDone));
                                    prevWorkDone = workDone;
                                }
                            });
                            monitor.done();
                        }
                    }.run(viewer.getControl().getDisplay());
                }
                upladFolder.setChildren(session.listAgentFiles(upladFolder, upladFolder.getFullName(), objectId));
                runInUIThread(new Runnable() {

                    @Override
                    public void run() {
                        viewer.refresh(upladFolder, true);
                    }
                });
            }

            @Override
            protected String getErrorMessage() {
                return "Cannot upload file to remote agent";
            }
        }.start();
    }
}
Also used : NXCSession(org.netxms.client.NXCSession) AgentFile(org.netxms.client.server.AgentFile) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) StartClientToServerFileUploadDialog(org.netxms.ui.eclipse.filemanager.dialogs.StartClientToServerFileUploadDialog) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) List(java.util.List) ArrayList(java.util.ArrayList) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) AgentFile(org.netxms.client.server.AgentFile) File(java.io.File)

Example 18 with AgentFile

use of org.netxms.client.server.AgentFile in project netxms by netxms.

the class AgentFileManager method copyFilePath.

/**
 * Copy full path to file to clipboard
 */
private void copyFilePath() {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.size() != 1)
        return;
    String filePath = ((AgentFile) selection.getFirstElement()).getFilePath();
    JavaScriptExecutor executor = RWT.getClient().getService(JavaScriptExecutor.class);
    if (executor != null) {
        StringBuilder js = new StringBuilder();
        filePath = filePath.replace("\\", "\\\\");
        // Substring is made to remove first "/"
        js.append("copyTextToClipboard(\'" + filePath + "\');");
        executor.execute(js.toString());
    }
}
Also used : AgentFile(org.netxms.client.server.AgentFile) JavaScriptExecutor(org.eclipse.rap.rwt.client.service.JavaScriptExecutor) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Aggregations

AgentFile (org.netxms.client.server.AgentFile)18 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)10 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)8 IOException (java.io.IOException)7 PartInitException (org.eclipse.ui.PartInitException)7 NXCException (org.netxms.client.NXCException)7 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)7 File (java.io.File)4 ArrayList (java.util.ArrayList)3 AgentFileData (org.netxms.client.AgentFileData)3 ProgressListener (org.netxms.client.ProgressListener)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 ConsoleJobCallingServerJob (org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 GroupMarker (org.eclipse.jface.action.GroupMarker)1