Java Code Examples for org.apache.commons.net.ftp.FTPConnectionClosedException#getMessage()

The following examples show how to use org.apache.commons.net.ftp.FTPConnectionClosedException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Client.java    From anthelion with Apache License 2.0 4 votes vote down vote up
public void retrieveList(String path, List entries, int limit,
  FTPFileEntryParser parser)
  throws IOException,
    FtpExceptionCanNotHaveDataConnection,
    FtpExceptionUnknownForcedDataClose,
    FtpExceptionControlClosedByForcedDataClose {
  Socket socket = __openPassiveDataConnection(FTPCommand.LIST, path);

  if (socket == null)
    throw new FtpExceptionCanNotHaveDataConnection("LIST "
      + ((path == null) ? "" : path));

  BufferedReader reader =
      new BufferedReader(new InputStreamReader(socket.getInputStream()));

  // force-close data channel socket, when download limit is reached
  boolean mandatory_close = false;

  //List entries = new LinkedList();
  int count = 0;
  String line = parser.readNextEntry(reader);
  while (line != null) {
    FTPFile ftpFile = parser.parseFTPEntry(line);
    // skip non-formatted lines
    if (ftpFile == null) {
      line = parser.readNextEntry(reader);
      continue;
    }
    entries.add(ftpFile);
    count += line.length();
    // impose download limit if limit >= 0, otherwise no limit
    // here, cut off is up to the line when total bytes is just over limit
    if (limit >= 0 && count > limit) {
      mandatory_close = true;
      break;
    }
    line = parser.readNextEntry(reader);
  }

  //if (mandatory_close)
  // you always close here, no matter mandatory_close or not.
  // however different ftp servers respond differently, see below.
  socket.close();

  // scenarios:
  // (1) mandatory_close is false, download limit not reached
  //     no special care here
  // (2) mandatory_close is true, download limit is reached
  //     different servers have different reply codes:

  try {
    int reply = getReply();
    if (!_notBadReply(reply))
      throw new FtpExceptionUnknownForcedDataClose(getReplyString());
  } catch (FTPConnectionClosedException e) {
    // some ftp servers will close control channel if data channel socket
    // is closed by our end before all data has been read out. Check:
    // tux414.q-tam.hp.com FTP server (hp.com version whp02)
    // so must catch FTPConnectionClosedException thrown by getReply() above
    //disconnect();
    throw new FtpExceptionControlClosedByForcedDataClose(e.getMessage());
  }

}
 
Example 2
Source File: Client.java    From anthelion with Apache License 2.0 4 votes vote down vote up
public void retrieveFile(String path, OutputStream os, int limit)
  throws IOException,
    FtpExceptionCanNotHaveDataConnection,
    FtpExceptionUnknownForcedDataClose,
    FtpExceptionControlClosedByForcedDataClose {

  Socket socket = __openPassiveDataConnection(FTPCommand.RETR, path);

  if (socket == null)
    throw new FtpExceptionCanNotHaveDataConnection("RETR "
      + ((path == null) ? "" : path));

  InputStream input = socket.getInputStream();

  // 20040318, xing, treat everything as BINARY_FILE_TYPE for now
  // do we ever need ASCII_FILE_TYPE?
  //if (__fileType == ASCII_FILE_TYPE)
  // input = new FromNetASCIIInputStream(input);

  // fixme, should we instruct server here for binary file type?

  // force-close data channel socket
  boolean mandatory_close = false;

  int len; int count = 0;
  byte[] buf =
    new byte[org.apache.commons.net.io.Util.DEFAULT_COPY_BUFFER_SIZE];
  while((len=input.read(buf,0,buf.length)) != -1){
    count += len;
    // impose download limit if limit >= 0, otherwise no limit
    // here, cut off is exactly of limit bytes
    if (limit >= 0 && count > limit) {
      os.write(buf,0,len-(count-limit));
      mandatory_close = true;
      break;
    }
    os.write(buf,0,len);
    os.flush();
  }

  //if (mandatory_close)
  // you always close here, no matter mandatory_close or not.
  // however different ftp servers respond differently, see below.
  socket.close();

  // scenarios:
  // (1) mandatory_close is false, download limit not reached
  //     no special care here
  // (2) mandatory_close is true, download limit is reached
  //     different servers have different reply codes:

  // do not need this
  //sendCommand("ABOR");

  try {
    int reply = getReply();
    if (!_notBadReply(reply))
      throw new FtpExceptionUnknownForcedDataClose(getReplyString());
  } catch (FTPConnectionClosedException e) {
    // some ftp servers will close control channel if data channel socket
    // is closed by our end before all data has been read out. Check:
    // tux414.q-tam.hp.com FTP server (hp.com version whp02)
    // so must catch FTPConnectionClosedException thrown by getReply() above
    //disconnect();
    throw new FtpExceptionControlClosedByForcedDataClose(e.getMessage());
  }

}
 
Example 3
Source File: Client.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
     * retrieve list reply for path
     * @param path
     * @param entries
     * @param limit
     * @param parser
     * @throws IOException
     * @throws FtpExceptionCanNotHaveDataConnection
     * @throws FtpExceptionUnknownForcedDataClose
     * @throws FtpExceptionControlClosedByForcedDataClose
     */
    public void retrieveList(String path, List<FTPFile> entries, int limit,
      FTPFileEntryParser parser)
      throws IOException,
        FtpExceptionCanNotHaveDataConnection,
        FtpExceptionUnknownForcedDataClose,
        FtpExceptionControlClosedByForcedDataClose {
      Socket socket = __openPassiveDataConnection(FTPCommand.LIST, path);

      if (socket == null)
        throw new FtpExceptionCanNotHaveDataConnection("LIST "
          + ((path == null) ? "" : path));

      BufferedReader reader =
          new BufferedReader(new InputStreamReader(socket.getInputStream()));

      // force-close data channel socket, when download limit is reached
//      boolean mandatory_close = false;

      //List entries = new LinkedList();
      int count = 0;
      String line = parser.readNextEntry(reader);
      while (line != null) {
        FTPFile ftpFile = parser.parseFTPEntry(line);
        // skip non-formatted lines
        if (ftpFile == null) {
          line = parser.readNextEntry(reader);
          continue;
        }
        entries.add(ftpFile);
        count += line.length();
        // impose download limit if limit >= 0, otherwise no limit
        // here, cut off is up to the line when total bytes is just over limit
        if (limit >= 0 && count > limit) {
//          mandatory_close = true;
          break;
        }
        line = parser.readNextEntry(reader);
      }

      //if (mandatory_close)
      // you always close here, no matter mandatory_close or not.
      // however different ftp servers respond differently, see below.
      socket.close();

      // scenarios:
      // (1) mandatory_close is false, download limit not reached
      //     no special care here
      // (2) mandatory_close is true, download limit is reached
      //     different servers have different reply codes:

      try {
        int reply = getReply();
        if (!_notBadReply(reply))
          throw new FtpExceptionUnknownForcedDataClose(getReplyString());
      } catch (FTPConnectionClosedException e) {
        // some ftp servers will close control channel if data channel socket
        // is closed by our end before all data has been read out. Check:
        // tux414.q-tam.hp.com FTP server (hp.com version whp02)
        // so must catch FTPConnectionClosedException thrown by getReply() above
        //disconnect();
        throw new FtpExceptionControlClosedByForcedDataClose(e.getMessage());
      }

    }
 
Example 4
Source File: Client.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * retrieve file for path
 * @param path
 * @param os
 * @param limit
 * @throws IOException
 * @throws FtpExceptionCanNotHaveDataConnection
 * @throws FtpExceptionUnknownForcedDataClose
 * @throws FtpExceptionControlClosedByForcedDataClose
 */
public void retrieveFile(String path, OutputStream os, int limit)
  throws IOException,
    FtpExceptionCanNotHaveDataConnection,
    FtpExceptionUnknownForcedDataClose,
    FtpExceptionControlClosedByForcedDataClose {

  Socket socket = __openPassiveDataConnection(FTPCommand.RETR, path);

  if (socket == null)
    throw new FtpExceptionCanNotHaveDataConnection("RETR "
      + ((path == null) ? "" : path));

  InputStream input = socket.getInputStream();

  // 20040318, xing, treat everything as BINARY_FILE_TYPE for now
  // do we ever need ASCII_FILE_TYPE?
  //if (__fileType == ASCII_FILE_TYPE)
  // input = new FromNetASCIIInputStream(input);

  // fixme, should we instruct server here for binary file type?

  // force-close data channel socket
  // boolean mandatory_close = false;

  int len; int count = 0;
  byte[] buf =
    new byte[org.apache.commons.net.io.Util.DEFAULT_COPY_BUFFER_SIZE];
  while((len=input.read(buf,0,buf.length)) != -1){
    count += len;
    // impose download limit if limit >= 0, otherwise no limit
    // here, cut off is exactly of limit bytes
    if (limit >= 0 && count > limit) {
      os.write(buf,0,len-(count-limit));
   //   mandatory_close = true;
      break;
    }
    os.write(buf,0,len);
    os.flush();
  }

  //if (mandatory_close)
  // you always close here, no matter mandatory_close or not.
  // however different ftp servers respond differently, see below.
  socket.close();

  // scenarios:
  // (1) mandatory_close is false, download limit not reached
  //     no special care here
  // (2) mandatory_close is true, download limit is reached
  //     different servers have different reply codes:

  // do not need this
  //sendCommand("ABOR");

  try {
    int reply = getReply();
    if (!_notBadReply(reply))
      throw new FtpExceptionUnknownForcedDataClose(getReplyString());
  } catch (FTPConnectionClosedException e) {
    // some ftp servers will close control channel if data channel socket
    // is closed by our end before all data has been read out. Check:
    // tux414.q-tam.hp.com FTP server (hp.com version whp02)
    // so must catch FTPConnectionClosedException thrown by getReply() above
    //disconnect();
    throw new FtpExceptionControlClosedByForcedDataClose(e.getMessage());
  }

}