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());
}
}
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();
}
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();
}
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();
}
}
}
}
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();
}
Aggregations