/*
   * Returns a session id for keeping a session on a HTTP site.
   *
   * @param url is the url of the HTTP site.
   *
   * @return a session id for keeping a session on a HTTP site or null if no session is available.
   */
  public static String getSessionId(String url) {
    HttpURLConnection conn = null;

    try {
      // Open connection
      conn = FileTransfer.connectToHttpInputConnection(new URL(url));
      if (conn == null) {
        throw new IOException("Could not open connection to '" + url + "'");
      }

      // Get a session id if available
      final GetSessionIdThread sessionIdThread = new GetSessionIdThread(conn);

      sessionIdThread.start();

      // Wait for the session id
      synchronized (sessionIdThread.monitor) {
        while (!sessionIdThread.isFinished) {
          try {
            sessionIdThread.monitor.wait(sessionTimeout);
            sessionIdThread.interrupt();
          } catch (InterruptedException e) {
            // Immediately reasserts the exception by interrupting the caller thread itself
            Thread.currentThread().interrupt();

            return null;
          }
        }
      }

      // Return the session id
      return sessionIdThread.sessionId;

    } catch (final IOException e) {
      return null;
    } finally {
      // Make sure the connection is disconnected.
      // This will cause threads using the connection to throw an exception
      // and thereby terminate if they were hanging.
      if (conn != null) {
        conn.disconnect();
      }
    }
  }
  /**
   * Handles buttons action events.
   *
   * @param evt the <tt>ActionEvent</tt> that notified us
   */
  public void actionPerformed(ActionEvent evt) {
    JButton sourceButton = (JButton) evt.getSource();

    if (sourceButton.equals(openFileButton)) {
      this.openFile(downloadFile);
    } else if (sourceButton.equals(openFolderButton)) {
      try {
        File downloadDir = GuiActivator.getFileAccessService().getDefaultDownloadDirectory();

        GuiActivator.getDesktopService().open(downloadDir);
      } catch (IllegalArgumentException e) {
        if (logger.isDebugEnabled()) logger.debug("Unable to open folder.", e);

        this.showErrorMessage(resources.getI18NString("service.gui.FOLDER_DOES_NOT_EXIST"));
      } catch (NullPointerException e) {
        if (logger.isDebugEnabled()) logger.debug("Unable to open folder.", e);

        this.showErrorMessage(resources.getI18NString("service.gui.FOLDER_DOES_NOT_EXIST"));
      } catch (UnsupportedOperationException e) {
        if (logger.isDebugEnabled()) logger.debug("Unable to open folder.", e);

        this.showErrorMessage(resources.getI18NString("service.gui.FILE_OPEN_NOT_SUPPORTED"));
      } catch (SecurityException e) {
        if (logger.isDebugEnabled()) logger.debug("Unable to open folder.", e);

        this.showErrorMessage(resources.getI18NString("service.gui.FOLDER_OPEN_NO_PERMISSION"));
      } catch (IOException e) {
        if (logger.isDebugEnabled()) logger.debug("Unable to open folder.", e);

        this.showErrorMessage(resources.getI18NString("service.gui.FOLDER_OPEN_NO_APPLICATION"));
      } catch (Exception e) {
        if (logger.isDebugEnabled()) logger.debug("Unable to open file.", e);

        this.showErrorMessage(resources.getI18NString("service.gui.FOLDER_OPEN_FAILED"));
      }
    } else if (sourceButton.equals(cancelButton)) {
      if (fileTransfer != null) fileTransfer.cancel();
    }
  }
  /**
   * Sets the file transfer.
   *
   * @param fileTransfer the file transfer
   * @param transferredFileSize the size of the transferred file
   */
  protected void setFileTransfer(FileTransfer fileTransfer, long transferredFileSize) {
    this.fileTransfer = fileTransfer;
    this.transferredFileSize = transferredFileSize;

    fileTransfer.addProgressListener(this);
  }