Fixed thrown errot on file download job cancelation throught server job view + Change...
authorzev <zev@netxms.org>
Wed, 8 Nov 2017 11:33:55 +0000 (13:33 +0200)
committerzev <zev@netxms.org>
Wed, 8 Nov 2017 11:33:55 +0000 (13:33 +0200)
ChangeLog
include/nms_cscp.h
src/client/java/netxms-client/src/main/java/org/netxms/client/NXCReceivedFile.java
src/client/java/netxms-client/src/main/java/org/netxms/client/NXCSession.java
src/client/java/netxms-client/src/main/java/org/netxms/client/RecievedFile.java [new file with mode: 0644]
src/java/netxms-eclipse/Core/src/org/netxms/ui/eclipse/jobs/ConsoleJobCallingServerJob.java
src/java/netxms-eclipse/FileManager/src/org/netxms/ui/eclipse/filemanager/views/AgentFileManager.java
src/libnxjava/java/base/netxms-base/src/main/java/org/netxms/base/NXCPCodes.java
src/server/core/download_job.cpp
webui/webapp/Core/src/org/netxms/ui/eclipse/jobs/ConsoleJobCallingServerJob.java

index 35649e8..5073c36 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -13,6 +13,7 @@
 
 - Option to read log files using VSS snapshots on Windows
 - Per stage confirmation in database manager during database check
+- Fixed file download cancelation
 - Fixed issues: NX-703, NX-1045, NX-1268, NX-1339, NX-1341, NX-1342, NX-1343, NX-1344
 
 
index 7c34e95..67c3259 100644 (file)
@@ -1216,6 +1216,7 @@ typedef struct
 #define VID_ACTION_LIST             ((UINT32)606)
 #define VID_ZONE_SNMP_PORT_COUNT    ((UINT32)607)
 #define VID_INCLUDE_RAW_VALUES      ((UINT32)608)
+#define VID_JOB_CANCELED            ((UINT32)609)
 
 // Base variabe for single threshold in message
 #define VID_THRESHOLD_BASE          ((UINT32)0x00800000)
index 4496814..3ef7a98 100644 (file)
@@ -35,6 +35,7 @@ public class NXCReceivedFile
        public static final int OPEN = 0;
        public static final int RECEIVED = 1;
        public static final int FAILED = 2;
+   public static final int CANCELED = 3;
        
        private long id;
        private File file;
@@ -154,7 +155,7 @@ public class NXCReceivedFile
        /**
         * Abort file transfer
         */
-       protected void abortTransfer()
+       protected void abortTransfer(boolean isCanceled)
        {
                if (status == OPEN)
                {
@@ -168,7 +169,10 @@ public class NXCReceivedFile
                        }
                }
                timestamp = System.currentTimeMillis();
-               status = FAILED;
+               if(isCanceled)
+                  status = CANCELED;
+               else
+                  status = FAILED;
                exception = new IOException();
        }
 
index 3ebbbcc..e7f6718 100644 (file)
@@ -765,6 +765,7 @@ public class NXCSession
       private void processFileTransferError(final NXCPMessage msg)
       {
          long id = msg.getMessageId();
+         
          NXCReceivedFile file;
          synchronized(receivedFiles)
          {
@@ -774,7 +775,7 @@ public class NXCSession
                file = new NXCReceivedFile(id);
                receivedFiles.put(id, file);
             }
-            file.abortTransfer();
+            file.abortTransfer(msg.getFieldAsBoolean(NXCPCodes.VID_JOB_CANCELED));
             receivedFiles.notifyAll();
          }
       }
@@ -1657,10 +1658,11 @@ public class NXCSession
     * @param timeout Wait timeout in milliseconds
     * @return Received file or null in case of failure
     */
-   public File waitForFile(final long id, final int timeout)
+   public RecievedFile waitForFile(final long id, final int timeout)
    {
       int timeRemaining = timeout;
       File file = null;
+      int status = RecievedFile.FAILED;
 
       while(timeRemaining > 0)
       {
@@ -1672,7 +1674,10 @@ public class NXCSession
                if (rf.getStatus() != NXCReceivedFile.OPEN)
                {
                   if (rf.getStatus() == NXCReceivedFile.RECEIVED) 
+                  {
                      file = rf.getFile();
+                     status = RecievedFile.SUCCESS;
+                  }
                   break;
                }
             }
@@ -1688,7 +1693,7 @@ public class NXCSession
             timeRemaining -= System.currentTimeMillis() - startTime;
          }
       }
-      return file;
+      return new RecievedFile(file, timeRemaining <= 0 ? RecievedFile.TIMEOUT : status);
    }
    
    /**
@@ -7835,9 +7840,9 @@ public class NXCSession
       msg.setField(NXCPCodes.VID_GUID, guid);
       sendMessage(msg);
       final NXCPMessage response = waitForRCC(msg.getMessageId());
-      final File imageFile = waitForFile(msg.getMessageId(), 600000);
-      if (imageFile == null) throw new NXCException(RCC.IO_ERROR);
-      return new LibraryImage(response, imageFile);
+      final RecievedFile imageFile = waitForFile(msg.getMessageId(), 600000);
+      if (imageFile.isErrorRecieved()) throw new NXCException(RCC.IO_ERROR);
+      return new LibraryImage(response, imageFile.getFile());
    }
 
    /**
@@ -8314,10 +8319,10 @@ public class NXCSession
          }
       }
       
-      File remoteFile = waitForFile(msg.getMessageId(), 36000000);
+      RecievedFile remoteFile = waitForFile(msg.getMessageId(), 36000000);
       if (remoteFile == null)
          throw new NXCException(RCC.AGENT_FILE_DOWNLOAD_ERROR);
-      AgentFileData file =  new AgentFileData(id, remoteFileName, remoteFile);
+      AgentFileData file =  new AgentFileData(id, remoteFileName, remoteFile.getFile());
 
       try
       {
@@ -8344,7 +8349,7 @@ public class NXCSession
       msg.setField(NXCPCodes.VID_FILE_NAME, remoteFileName);
       sendMessage(msg);
       waitForRCC(msg.getMessageId());
-      return waitForFile(msg.getMessageId(), 3600000);
+      return waitForFile(msg.getMessageId(), 3600000).getFile();
    }
    
    /**
@@ -9392,12 +9397,12 @@ public class NXCSession
       msg.setFieldInt32(NXCPCodes.VID_RENDER_FORMAT, format.getCode());
       sendMessage(msg);
       waitForRCC(msg.getMessageId());
-      final File file = waitForFile(msg.getMessageId(), 600000);
-      if (file == null)
+      final RecievedFile file = waitForFile(msg.getMessageId(), 600000);
+      if (file.isErrorRecieved())
       {
          throw new NXCException(RCC.IO_ERROR);
       }
-      return file;
+      return file.getFile();
    }
 
        /**
diff --git a/src/client/java/netxms-client/src/main/java/org/netxms/client/RecievedFile.java b/src/client/java/netxms-client/src/main/java/org/netxms/client/RecievedFile.java
new file mode 100644 (file)
index 0000000..e286c0e
--- /dev/null
@@ -0,0 +1,52 @@
+package org.netxms.client;
+
+import java.io.File;
+
+/**
+ * Class that returns recieved file and 
+ *
+ */
+public class RecievedFile
+{
+   public static final int SUCCESS = 0;
+   public static final int CANCELED = 1;
+   public static final int FAILED = 2;
+   public static final int TIMEOUT = 3;
+   
+   
+   private File file;
+   private int status;
+   
+   /*
+    * Constructor
+    */
+   public RecievedFile(File f, int status)
+   {
+      file = f;
+      this.status = status;
+   }
+
+   /**
+    * @return the status
+    */
+   public int getStatus()
+   {
+      return status;
+   }
+
+   /**
+    * @return the file
+    */
+   public File getFile()
+   {
+      return file;
+   }
+   
+   /**
+    * Method that checks if error should be generated
+    */
+   public boolean isErrorRecieved()
+   {
+      return status == FAILED || status == TIMEOUT;
+   }
+}
index 2b4b59e..8610364 100644 (file)
@@ -18,9 +18,6 @@
  */
 package org.netxms.ui.eclipse.jobs;
 
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
 import org.eclipse.ui.IWorkbenchPart;
 import org.netxms.client.NXCSession;
 import org.netxms.client.server.ServerJobIdUpdater;
@@ -42,40 +39,6 @@ public abstract class ConsoleJobCallingServerJob extends ConsoleJob implements S
       session = (NXCSession)ConsoleSharedData.getSession();
    }
 
-   /*
-    * (non-Javadoc)
-    * 
-    * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
-    */
-   @Override
-   protected IStatus run(IProgressMonitor monitor)
-   {
-      IStatus status;
-      try
-      {
-         runInternal(monitor);
-         status = Status.OK_STATUS;
-      }
-      catch(Exception e)
-      {
-         if(jobCanceled)
-         {
-            status = Status.CANCEL_STATUS;
-         }
-         else
-         {
-            Activator.logError("Exception in ConsoleJob", e); //$NON-NLS-1$
-            jobFailureHandler();
-            status = createFailureStatus(e);
-         }
-      }
-      finally
-      {
-         jobFinalize();
-      }
-      return status;
-   }
-
    /* (non-Javadoc)
     * @see org.eclipse.core.runtime.jobs.Job#canceling()
     */
@@ -98,4 +61,9 @@ public abstract class ConsoleJobCallingServerJob extends ConsoleJob implements S
    {
       serverJobId = id;
    }
+
+   public boolean isCanceled()
+   {
+      return jobCanceled;
+   }
 }
index f8bea6b..79c242a 100644 (file)
@@ -1298,6 +1298,8 @@ public class AgentFileManager extends ViewPart
       }
       for(AgentFile f : files)
       {
+         if(job.isCanceled())
+            break;
          if (f.isDirectory())
          {
             downloadDir(f, localFileName + "/" + f.getName(), monitor, job); //$NON-NLS-1$
@@ -1333,19 +1335,22 @@ public class AgentFileManager extends ViewPart
             monitor.worked((int)workDone);
          }
       }, job);
-      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)
+      if(file.getFile() != null)
       {
-         out.write(buf, 0, len);
+         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());
       }
-      in.close();
-      out.close();
-      outputFile.setLastModified(sf.getModifyicationTime().getTime());
    }
    
    /**
index 5103d87..805ed7d 100644 (file)
@@ -996,6 +996,7 @@ public class NXCPCodes
    public static final long VID_ACTION_LIST = 606;
    public static final long VID_ZONE_SNMP_PORT_COUNT = 607;
    public static final long VID_INCLUDE_RAW_VALUES = 608;
+   public static final long VID_JOB_CANCELED = 609;
 
        public static final long VID_ACL_USER_BASE = 0x00001000L;
        public static final long VID_ACL_USER_LAST = 0x00001FFFL;
index 0cc3a05..8456d6c 100644 (file)
@@ -231,24 +231,32 @@ ServerJobResult FileDownloadJob::run()
           NXCPMessage abortCmd;
           abortCmd.setCode(CMD_ABORT_FILE_TRANSFER);
           abortCmd.setId(m_requestId);
+          abortCmd.setField(VID_JOB_CANCELED, getStatus() != JOB_CANCELLED || getStatus() != JOB_CANCEL_PENDING);
           m_session->sendMessage(&abortCmd);
 
-      switch(rcc)
-      {
-         case ERR_ACCESS_DENIED:
-            response.setField(VID_RCC, RCC_ACCESS_DENIED);
-            break;
-         case ERR_IO_FAILURE:
-            response.setField(VID_RCC, RCC_IO_ERROR);
-            break;
-                       case ERR_FILE_OPEN_ERROR:
-                       case ERR_FILE_STAT_FAILED:
-            response.setField(VID_RCC, RCC_FILE_IO_ERROR);
-            break;
-         default:
-            response.setField(VID_RCC, RCC_COMM_FAILURE);
-            break;
-      }
+          if(getStatus() != JOB_CANCELLED || getStatus() != JOB_CANCEL_PENDING)
+          {
+             response.setField(VID_RCC, RCC_SUCCESS);
+          }
+          else
+          {
+         switch(rcc)
+         {
+            case ERR_ACCESS_DENIED:
+               response.setField(VID_RCC, RCC_ACCESS_DENIED);
+               break;
+            case ERR_IO_FAILURE:
+               response.setField(VID_RCC, RCC_IO_ERROR);
+               break;
+            case ERR_FILE_OPEN_ERROR:
+            case ERR_FILE_STAT_FAILED:
+               response.setField(VID_RCC, RCC_FILE_IO_ERROR);
+               break;
+            default:
+               response.setField(VID_RCC, RCC_COMM_FAILURE);
+               break;
+         }
+          }
                m_session->sendMessage(&response);
        }
 
index 2b4b59e..8610364 100644 (file)
@@ -18,9 +18,6 @@
  */
 package org.netxms.ui.eclipse.jobs;
 
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
 import org.eclipse.ui.IWorkbenchPart;
 import org.netxms.client.NXCSession;
 import org.netxms.client.server.ServerJobIdUpdater;
@@ -42,40 +39,6 @@ public abstract class ConsoleJobCallingServerJob extends ConsoleJob implements S
       session = (NXCSession)ConsoleSharedData.getSession();
    }
 
-   /*
-    * (non-Javadoc)
-    * 
-    * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
-    */
-   @Override
-   protected IStatus run(IProgressMonitor monitor)
-   {
-      IStatus status;
-      try
-      {
-         runInternal(monitor);
-         status = Status.OK_STATUS;
-      }
-      catch(Exception e)
-      {
-         if(jobCanceled)
-         {
-            status = Status.CANCEL_STATUS;
-         }
-         else
-         {
-            Activator.logError("Exception in ConsoleJob", e); //$NON-NLS-1$
-            jobFailureHandler();
-            status = createFailureStatus(e);
-         }
-      }
-      finally
-      {
-         jobFinalize();
-      }
-      return status;
-   }
-
    /* (non-Javadoc)
     * @see org.eclipse.core.runtime.jobs.Job#canceling()
     */
@@ -98,4 +61,9 @@ public abstract class ConsoleJobCallingServerJob extends ConsoleJob implements S
    {
       serverJobId = id;
    }
+
+   public boolean isCanceled()
+   {
+      return jobCanceled;
+   }
 }