Search in sources :

Example 1 with AgentFileData

use of org.netxms.client.AgentFileData in project netxms by netxms.

the class AgentFileManager method downloadFile.

/**
 * Downloads file
 * @throws NXCException
 * @throws IOException
 */
private void downloadFile(final AgentFile sf, final String localName, final IProgressMonitor monitor, final boolean subTask, ConsoleJobCallingServerJob job) throws IOException, NXCException {
    if (subTask)
        monitor.subTask(String.format("Downloading file %s", sf.getFullName()));
    final AgentFileData file = session.downloadFileFromAgent(objectId, sf.getFullName(), 0, false, new ProgressListener() {

        @Override
        public void setTotalWorkAmount(long workTotal) {
            if (!subTask)
                monitor.beginTask(String.format("Downloading file %s", sf.getFullName()), (int) workTotal);
        }

        @Override
        public void markProgress(long workDone) {
            monitor.worked((int) workDone);
        }
    }, job);
    if (file.getFile() != null) {
        File outputFile = new File(localName);
        outputFile.createNewFile();
        InputStream in = new FileInputStream(file.getFile());
        OutputStream out = new FileOutputStream(outputFile);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        outputFile.setLastModified(sf.getModifyicationTime().getTime());
    }
}
Also used : ProgressListener(org.netxms.client.ProgressListener) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) AgentFile(org.netxms.client.server.AgentFile) File(java.io.File) AgentFileData(org.netxms.client.AgentFileData) FileInputStream(java.io.FileInputStream)

Example 2 with AgentFileData

use of org.netxms.client.AgentFileData in project netxms by netxms.

the class ObjectToolExecutor method executeFileDownload.

/**
 * @param node
 * @param tool
 * @param inputValues
 * @param inputValues
 */
private static void executeFileDownload(final ObjectContext node, final ObjectTool tool, final Map<String, String> inputValues) {
    final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
    // $NON-NLS-1$
    String[] parameters = tool.getData().split("\u007F");
    final String fileName = parameters[0];
    final int maxFileSize = Integer.parseInt(parameters[1]);
    // $NON-NLS-1$
    final boolean follow = parameters[2].equals("true") ? true : false;
    ConsoleJobCallingServerJob job = new ConsoleJobCallingServerJob(Messages.get().ObjectToolsDynamicMenu_DownloadFromAgent, null, Activator.PLUGIN_ID, null) {

        @Override
        protected String getErrorMessage() {
            return String.format(Messages.get().ObjectToolsDynamicMenu_DownloadError, fileName, node.object.getObjectName());
        }

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            final AgentFileData file = session.downloadFileFromAgent(node.object.getObjectId(), fileName, maxFileSize, follow, inputValues, node.getAlarmId(), new ProgressListener() {

                @Override
                public void setTotalWorkAmount(long workTotal) {
                    monitor.beginTask("Download file " + fileName, (int) workTotal);
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) workDone);
                }
            }, this);
            runInUIThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        String secondaryId = Long.toString(node.object.getObjectId()) + "&" + URLEncoder.encode(fileName, "UTF-8");
                        AgentFileViewer.createView(secondaryId, node.object.getObjectId(), file, follow);
                    } catch (Exception e) {
                        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                        MessageDialogHelper.openError(window.getShell(), Messages.get().ObjectToolsDynamicMenu_Error, String.format(Messages.get().ObjectToolsDynamicMenu_ErrorOpeningView, e.getLocalizedMessage()));
                    }
                }
            });
        }
    };
    job.start();
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) NXCSession(org.netxms.client.NXCSession) AgentFileData(org.netxms.client.AgentFileData) PartInitException(org.eclipse.ui.PartInitException) ConsoleJobCallingServerJob(org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener)

Example 3 with AgentFileData

use of org.netxms.client.AgentFileData in project netxms by netxms.

the class AgentFileManager method tailFile.

/**
 * Starts file tail
 */
private void tailFile(final boolean followChanges, final int offset) {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.isEmpty())
        return;
    final Object[] objects = selection.toArray();
    if (((AgentFile) objects[0]).isDirectory())
        return;
    final AgentFile sf = ((AgentFile) objects[0]);
    ConsoleJobCallingServerJob job = new ConsoleJobCallingServerJob(Messages.get().AgentFileManager_DownloadJobTitle, null, Activator.PLUGIN_ID, null) {

        @Override
        protected String getErrorMessage() {
            return String.format(Messages.get().AgentFileManager_DownloadJobError, sf.getFullName(), objectId);
        }

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            final AgentFileData file = session.downloadFileFromAgent(objectId, sf.getFullName(), offset, followChanges, new ProgressListener() {

                @Override
                public void setTotalWorkAmount(long workTotal) {
                    monitor.beginTask("Download file " + sf.getFullName(), (int) workTotal);
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) workDone);
                }
            }, this);
            runInUIThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        String secondaryId = Long.toString(objectId) + "&" + URLEncoder.encode(sf.getName(), "UTF-8");
                        AgentFileViewer.createView(secondaryId, objectId, file, followChanges);
                    } catch (Exception e) {
                        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                        MessageDialogHelper.openError(window.getShell(), Messages.get().AgentFileManager_Error, String.format(Messages.get().AgentFileManager_OpenViewError, e.getLocalizedMessage()));
                        Activator.logError("Exception in AgentFileManager.tailFile", e);
                    }
                }
            });
        }
    };
    job.start();
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) AgentFile(org.netxms.client.server.AgentFile) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) AgentFileData(org.netxms.client.AgentFileData) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) NXCException(org.netxms.client.NXCException) ConsoleJobCallingServerJob(org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener)

Example 4 with AgentFileData

use of org.netxms.client.AgentFileData 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 5 with AgentFileData

use of org.netxms.client.AgentFileData in project netxms by netxms.

the class AgentFileManager method downloadFile.

/**
 * Downloads file
 * @throws NXCException
 * @throws IOException
 */
private void downloadFile(final String remoteName) {
    ConsoleJobCallingServerJob job = new ConsoleJobCallingServerJob(Messages.get().SelectServerFileDialog_JobTitle, null, Activator.PLUGIN_ID, null) {

        @Override
        protected void runInternal(final IProgressMonitor monitor) throws Exception {
            final AgentFileData file = session.downloadFileFromAgent(objectId, remoteName, 0, false, new ProgressListener() {

                @Override
                public void setTotalWorkAmount(long workTotal) {
                    monitor.beginTask("Downloading file " + remoteName, (int) workTotal);
                }

                @Override
                public void markProgress(long workDone) {
                    monitor.worked((int) workDone);
                }
            }, this);
            if (file != null && file.getFile() != null) {
                DownloadServiceHandler.addDownload(file.getFile().getName(), remoteName, file.getFile(), "application/octet-stream");
                runInUIThread(new Runnable() {

                    @Override
                    public void run() {
                        DownloadServiceHandler.startDownload(file.getFile().getName());
                    }
                });
            }
        }

        @Override
        protected String getErrorMessage() {
            return Messages.get().AgentFileManager_DirectoryReadError;
        }
    };
    job.setUser(false);
    job.start();
}
Also used : ConsoleJobCallingServerJob(org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProgressListener(org.netxms.client.ProgressListener) AgentFileData(org.netxms.client.AgentFileData)

Aggregations

AgentFileData (org.netxms.client.AgentFileData)6 ProgressListener (org.netxms.client.ProgressListener)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 ConsoleJobCallingServerJob (org.netxms.ui.eclipse.jobs.ConsoleJobCallingServerJob)4 AgentFile (org.netxms.client.server.AgentFile)3 FileInputStream (java.io.FileInputStream)2 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)2 PartInitException (org.eclipse.ui.PartInitException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 NXCException (org.netxms.client.NXCException)1 NXCSession (org.netxms.client.NXCSession)1