org.apache.commons.net.ftp.FTPCommand Java Examples

The following examples show how to use org.apache.commons.net.ftp.FTPCommand. 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());
  }

}
 
Example #5
Source File: FTPsClient.java    From iaf with Apache License 2.0 4 votes vote down vote up
protected Socket _openDataConnection_(int cmdNr, String param) throws IOException {
		// if explicit FTPS, the socket connection is establisch unsecure
		if (session.getFtpType() == FtpSession.FTPS_EXPLICIT_SSL || session.getFtpType() == FtpSession.FTPS_EXPLICIT_TLS) {
			if (session.isProtp()) {
				// With Prot P the result is returned over a different port
				// .. send protp commands  
 
 				sendCommand("PBSZ", "0");
				checkReply("PBSZ 0");
				sendCommand("PROT", "P");
				checkReply("PROT P");
				sendCommand("PASV");
				checkReply("PASV");
		
				// Parse the host and port name to which the result is send
				String reply = getReplyString(); 
				String line = reply.substring(reply.indexOf('(')+1, reply.lastIndexOf(')'));
				String[] hostinfo = line.split(",");
				String host=hostinfo[0] + "." + hostinfo[1] + "." + hostinfo[2] + "." + hostinfo[3];
				int port=(Integer.parseInt(hostinfo[4]) << 8) + Integer.parseInt(hostinfo[5]);
				log.debug("channel from pasv reply="+host+":"+port);
				InetSocketAddress address = new InetSocketAddress(host,port);

				// connect to the result address
				Socket socket = new Socket();
				socket.connect(address);
				socket.setSoTimeout(1000);
				host=socket.getInetAddress().getHostAddress();
				port=socket.getPort();
				log.debug("channel from socket="+host+":"+port);
				socket = socketFactory.createSocket(socket, host, port, true);

				String cmdLine=FTPCommand.getCommand(cmdNr);
				if (param!=null) {
					cmdLine+=' ' + param;
				}
				// send the requested command (over the original socket)  <-- toch maar niet! GvB
//				_sendCommand(cmdLine, _socket_.getOutputStream(), null);			
				sendCommand(cmdNr, param);
				
				// return the new socket for the reply 
				return socket;
				
			}
		}
		return super._openDataConnection_(cmdNr, param);
	}