/**
  * Upload a single {@link ErrorContainer}. This can be overwritten in order to test error handling
  * without touching QA.
  *
  * @param errorContainer the error container
  */
 public void uploadFile(ErrorContainer errorContainer) {
   errorContainer.setToken(serverReply);
   try {
     fileUploader = new FileUploader(messenger.getCommunicationLink(config.getUploaderUrl()));
     fileUploader.addObserver(this);
     fileUploader.uploadFiles(config.getUploaderUrl(), 2000, errorContainer);
   } catch (Exception e) {
     log.error("Error during upload", e);
   }
 }
  /**
   * Add detailed error to error container array
   *
   * @param error - error thrown
   * @param file - head file for error
   * @param files - all files in import collection
   * @param readerType - reader type supplied from bio-formats
   */
  protected void addError(Throwable error, File file, String[] files, String readerType) {
    ErrorContainer errorContainer = new ErrorContainer();
    errorContainer.setFiles(files);
    errorContainer.setSelectedFile(file);
    errorContainer.setReaderType(readerType);
    errorContainer.setCommentType("2");

    errorContainer.setJavaVersion(System.getProperty("java.version"));
    errorContainer.setJavaClasspath(System.getProperty("java.class.path"));
    errorContainer.setOSName(System.getProperty("os.name"));
    errorContainer.setOSArch(System.getProperty("os.arch"));
    errorContainer.setOSVersion(System.getProperty("os.version"));
    errorContainer.setAppVersion(config.getVersionNumber());
    errorContainer.setError(error);
    addError(errorContainer);
  }
  /** Send existing errors in ErrorContainer array to server */
  protected void sendErrors() {

    // create an omero client.
    client sc = null;
    client client = null;
    ServiceFactoryPrx session = null;
    try {
      if (sendLogs || sendFiles) {
        sc = new client(config.hostname.get(), config.port.get());
        ServiceFactoryPrx entryEncrypted;
        if (!config.sessionKey.empty()) {
          entryEncrypted = sc.joinSession(config.sessionKey.get());
        } else {
          entryEncrypted = sc.createSession(config.username.get(), config.password.get());
        }
        client = sc.createClient(false);
        session = client.getSession();
      }
      for (int i = 0; i < errors.size(); i++) {

        if (!isSend(i)) {
          onSent(i);
          continue; // Don't send file if not selected
        }

        if (cancelUploads) {
          onCancel();
          break;
        }

        ErrorContainer errorContainer = errors.get(i);
        if (errorContainer.getStatus() != -1) // if file not pending, skip
          // it
          continue;

        Map<String, String> postList = new HashMap<String, String>();

        postList.put("java_version", errorContainer.getJavaVersion());
        postList.put("java_classpath", errorContainer.getJavaClasspath());
        postList.put("app_version", errorContainer.getAppVersion());
        postList.put("comment_type", errorContainer.getCommentType());
        postList.put("os_name", errorContainer.getOSName());
        postList.put("os_arch", errorContainer.getOSArch());
        postList.put("os_version", errorContainer.getOSVersion());
        postList.put("extra", errorContainer.getExtra());
        postList.put("error", getStackTrace(errorContainer.getError()));
        postList.put("comment", errorContainer.getComment());
        postList.put("email", errorContainer.getEmail());
        postList.put("app_name", "2");
        postList.put("import_session", "test");
        postList.put("absolute_path", errorContainer.getAbsolutePath() + "/");

        String sendUrl = config.getTokenUrl();

        if (isSend(i)) {
          if (!sendFiles) {
            errorContainer.clearFiles();
          }

          if (sendLogs || sendFiles) {
            File f = errorContainer.getSelectedFile();
            if (f != null) {
              Long id = logFiles.get(f.getAbsolutePath());
              // load the log
              File logFile = null;
              try {
                logFile = retrieveLogFile(id, session);
              } catch (Throwable e) {
                log.error("Cannot load log file", e);
              }

              if (logFile != null) {
                sendLogs = true;
                errorContainer.addFile(logFile.getAbsolutePath());
              } else sendLogs = false;
            } else sendLogs = false;
          }
        }
        messenger = new HtmlMessenger(sendUrl, postList);
        serverReply = messenger.executePost();
        if (sendFiles || sendLogs) {
          onSending(i);
          log.info("Sending File(s)...");
          errorContainer.setToken(serverReply);
          fileUploader = new FileUploader(messenger.getCommunicationLink(config.getUploaderUrl()));
          fileUploader.addObserver(this);
          fileUploader.uploadFiles(config.getUploaderUrl(), 2000, errorContainer);
          onSent(i);
        } else {
          onNotSending(i, serverReply);
        }
      }
    } catch (Exception e) {
      log.error("Error during upload", e);
    } finally {
      if (client != null) client.__del__();
      if (sc != null) sc.__del__();
    }
    if (cancelUploads) {
      finishCancelled();
    }

    if (fileUploadErrors) {
      finishWithErroredFiles();
      notifyObservers(new ImportEvent.ERRORS_COMPLETE());
    } else {
      finishComplete();
      notifyObservers(new ImportEvent.ERRORS_COMPLETE());
    }
  }