/* (non-Javadoc)
  * @see com.aptana.ide.core.ftp.BaseFTPConnectionFileManager#setModificationTime(org.eclipse.core.runtime.IPath, long, org.eclipse.core.runtime.IProgressMonitor)
  */
 @Override
 protected void setModificationTime(IPath path, long modificationTime, IProgressMonitor monitor)
     throws CoreException, FileNotFoundException {
   if (!serverSupportsFeature("MFMT")
       && !serverSupportsFeature("SITE UTIME")) { // $NON-NLS-1$ //$NON-NLS-2$
     return;
   }
   try {
     IPath dirPath = path.removeLastSegments(1);
     changeCurrentDir(dirPath);
     Policy.checkCanceled(monitor);
     if (serverSupportsFeature("MFMT")) { // $NON-NLS-1$
       ftpClient.setModTime(path.lastSegment(), new Date(modificationTime));
     } else if (serverSupportsFeature("SITE UTIME")) { // $NON-NLS-1$
       Calendar cal = Calendar.getInstance();
       long localTimezoneShift = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
       Date date = new Date(modificationTime - localTimezoneShift);
       if (utimeFormat == -1) {
         for (utimeFormat = 0; utimeFormat < UTIME_FORMATS.length; ++utimeFormat) {
           String format = UTIME_FORMATS[utimeFormat].format(date);
           FTPReply reply =
               ftpClient.sendCommand(
                   "SITE " + MessageFormat.format(format, path.lastSegment())); // $NON-NLS-1$
           if (!"500".equals(reply.getReplyCode())
               && !"501".equals(reply.getReplyCode())) { // $NON-NLS-1$ //$NON-NLS-2$
             break;
           }
         }
       } else if (utimeFormat >= 0 && utimeFormat < UTIME_FORMATS.length) {
         String format = UTIME_FORMATS[utimeFormat].format(date);
         ftpClient.site(MessageFormat.format(format, path.lastSegment()));
       }
     }
   } catch (FileNotFoundException e) {
     throw e;
   } catch (OperationCanceledException e) {
     throw e;
   } catch (Exception e) {
     throw new CoreException(
         new Status(
             Status.ERROR,
             FTPPlugin.PLUGIN_ID,
             Messages.FTPConnectionFileManager_set_modification_time_failed,
             e));
   } finally {
     monitor.done();
   }
 }
  private FTPFile[] ftpSTAT(String dirname) throws IOException, FTPException, ParseException {
    if (fileFactory == null) {
      try {
        fileFactory = new FTPFileFactory(ftpClient.system());
      } catch (FTPException ex) {
        fileFactory = new FTPFileFactory(""); // $NON-NLS-1$
      }
      fileFactory.setLocales(FTPClient.DEFAULT_LISTING_LOCALES);
    }

    String[] validCodes = {"211", "212", "213"}; // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    FTPReply reply = ftpClient.sendCommand("STAT " + dirname); // $NON-NLS-1$
    ftpClient.validateReply(reply, validCodes);
    String[] data = reply.getReplyData();
    if (data == null) {
      return null;
    }
    for (int i = 0; i < data.length; ++i) {
      data[i] = data[i].trim();
    }
    return fileFactory.parse(data);
  }
  @SuppressWarnings("deprecation")
  protected void getherServerInfo(ConnectionContext context, IProgressMonitor monitor) {
    Policy.checkCanceled(monitor);
    monitor.subTask(Messages.FTPConnectionFileManager_gethering_server_info);
    serverFeatures = null;
    try {
      String[] features = ftpClient.features();
      if (features != null && features.length > 0) {
        serverFeatures = new ArrayList<String>();
        for (int i = 0; i < features.length; ++i) {
          String feature = features[i].trim();
          if (feature.indexOf(' ') > 0) {
            feature = feature.substring(0, feature.indexOf(' '));
          }
          serverFeatures.add(feature);
        }
      }
    } catch (Exception e) {
    }
    try {
      String[] validCodes = {"214"}; // $NON-NLS-1$
      FTPReply reply = ftpClient.sendCommand("SITE HELP"); // $NON-NLS-1$
      ftpClient.validateReply(reply, validCodes);
      if (serverFeatures == null) {
        serverFeatures = new ArrayList<String>();
      }
      String[] data = reply.getReplyData();
      for (int i = 0; i < data.length; ++i) {
        String cmd = data[i].trim();
        if (cmd.startsWith("214")) { // $NON-NLS-1$
          continue;
        }
        serverFeatures.add(MessageFormat.format("SITE {0}", cmd)); // $NON-NLS-1$
      }
    } catch (Exception e) {
      e.getCause();
    }

    Policy.checkCanceled(monitor);
    FTPFile[] rootFiles = null;
    try {
      rootFiles = listFiles(Path.ROOT, monitor);
    } catch (Exception e) {
    }

    if (context != null && context.getBoolean(ConnectionContext.DETECT_TIMEZONE)) {
      serverTimeZoneShift = Integer.MIN_VALUE;
    } else if (timezone != null) {
      TimeZone tz = TimeZone.getTimeZone(timezone);
      if (tz != null) {
        long time = new Date().getTime();
        serverTimeZoneShift = TimeZone.getDefault().getOffset(time) - tz.getOffset(time);
      }
    }
    if (serverTimeZoneShift == Integer.MIN_VALUE) {
      Policy.checkCanceled(monitor);
      try {
        changeCurrentDir(Path.ROOT);
        FTPFile file = null;
        if (rootFiles != null) {
          for (FTPFile ftpFile : rootFiles) {
            if (ftpFile.isFile()
                && !ftpFile.getName().startsWith(".ht")
                && !(ftpFile.lastModified().getHours() == 0
                    && ftpFile.lastModified().getMinutes() == 0
                    && ftpFile.lastModified().getSeconds() == 0)) {
              file = ftpFile;
              break;
            }
          }
        }
        if (file == null && !Path.ROOT.equals(basePath)) {
          FTPFile[] ftpFiles = listFiles(basePath, monitor);
          for (FTPFile ftpFile : ftpFiles) {
            if (ftpFile.isFile()
                && !ftpFile.getName().startsWith(".ht")
                && !(ftpFile.lastModified().getHours() == 0
                    && ftpFile.lastModified().getMinutes() == 0
                    && ftpFile.lastModified().getSeconds() == 0)) {
              file = ftpFile;
              break;
            }
          }
        }
        Date lastModifiedLocal = null;
        if (file == null) {
          changeCurrentDir(basePath);
          lastModifiedLocal = new Date();
          ftpClient.put(new ByteArrayInputStream(new byte[] {}), TMP_TIMEZONE_CHECK);
          for (FTPFile ftpFile : listFiles(basePath, monitor)) {
            if (TMP_TIMEZONE_CHECK.equals(ftpFile.getName())) {
              file = ftpFile;
              break;
            }
          }
        }
        if (file != null) {
          Date lastModifiedServerInLocalTZ = file.lastModified();
          if (serverSupportsFeature("MDTM")) { // $NON-NLS-1$
            Date lastModifiedLocalTZ = ftpClient.modtime(file.getName());
            if (lastModifiedLocalTZ != null) {
              // align to minutes
              serverTimeZoneShift =
                  (lastModifiedLocalTZ.getTime() - lastModifiedLocalTZ.getTime() % 60000)
                      - (lastModifiedServerInLocalTZ.getTime()
                          - lastModifiedServerInLocalTZ.getTime() % 60000);
            }
          }
          if (serverTimeZoneShift == Integer.MIN_VALUE) {
            serverTimeZoneShift =
                (lastModifiedLocal.getTime() - lastModifiedLocal.getTime() % 60000)
                    - (lastModifiedServerInLocalTZ.getTime()
                        - lastModifiedServerInLocalTZ.getTime() % 60000);
            // align to 1/4 hour
            long rem = serverTimeZoneShift % 900000;
            if (rem < 450000) {
              serverTimeZoneShift -= rem;
            } else {
              serverTimeZoneShift += (900000 - rem);
            }
          }
          if (TMP_TIMEZONE_CHECK.equals(file.getName())) {
            ftpClient.delete(file.getName());
          }
          if (context != null) {
            Calendar cal = Calendar.getInstance();
            int rawOffset =
                (int)
                    (cal.get(Calendar.ZONE_OFFSET)
                        + cal.get(Calendar.DST_OFFSET)
                        - serverTimeZoneShift);
            context.put(ConnectionContext.SERVER_TIMEZONE, TimeZone.getAvailableIDs(rawOffset));
          }
        }
      } catch (OperationCanceledException e) {
        throw e;
      } catch (Exception e) {
        IdeLog.logImportant(
            FTPPlugin.getDefault(), Messages.FTPConnectionFileManager_server_tz_check, e);
      }
      if (serverTimeZoneShift == Integer.MIN_VALUE) {
        Calendar cal = Calendar.getInstance();
        serverTimeZoneShift = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
      }
    }

    hasServerInfo = true;
  }