Package org.apache.commons.net.ftp

Examples of org.apache.commons.net.ftp.FTPClient


            throw new RuntimeException("Failed to validate client: " + e, e);
        }
    }

    public void activateObject(Object object) throws Exception {
        FTPClient client = (FTPClient) object;
        client.setReaderThread(true);
    }
View Full Code Here


        FTPClient client = (FTPClient) object;
        client.setReaderThread(true);
    }

    public void passivateObject(Object object) throws Exception {
        FTPClient client = (FTPClient) object;
        client.setReaderThread(false);
    }
View Full Code Here

    }

    // Implementation methods
    //-------------------------------------------------------------------------
    protected void connect(SocketClient client) throws Exception {
        FTPClient ftp = (FTPClient) client;

        if (config != null) {
            ftp.configure(config);
        }
        super.connect(client);

        int code = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(code)) {
            ftp.disconnect();
            throw new ConnectionRefusedException(code);
        }
        if (!ftp.login(getUsername(), getPassword())) {
            ftp.disconnect();
            throw new ConnectionRefusedException(ftp.getReplyCode());
        }
        if (isBinaryMode()) {
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
        }
    }
View Full Code Here

            ftp.setFileType(FTP.BINARY_FILE_TYPE);
        }
    }

    protected void disconnect(SocketClient client) throws Exception {
        FTPClient ftp = (FTPClient) client;
        if (ftp.isConnected()) {
            ftp.logout();
        }
        super.disconnect(client);
    }
View Full Code Here

        }
        super.disconnect(client);
    }

    protected SocketClient createSocketClient() {
        return new FTPClient();
    }
View Full Code Here

        }
        super.init();
    }

    protected void process(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
        FTPClient client = null;
        OutputStream out = null;
        try {
            client = (FTPClient) getClientPool().borrowClient();

            String name = marshaler.getOutputName(exchange, message);
            if (name == null) {
                if (uniqueFileName != null) {
                    out = client.storeUniqueFileStream(uniqueFileName);
                }
                else {
                    out = client.storeUniqueFileStream();
                }
            }
            else {
                out = client.storeFileStream(name);
                if (out == null) {
                    // lets try overwrite the previous file?
                    if (overwrite) {
                        client.deleteFile(name);
                    }
                    out = client.storeFileStream(name);
                }
            }
            if (out == null) {
                throw new MessagingException("No output stream available for output name: " + name + ". Maybe the file already exists?");
            }
View Full Code Here

    private String getWorkingPath() {
      return path == null ? "." : path;
    }

    public void poll() throws Exception {
        FTPClient ftp = (FTPClient) borrowClient();
        try {
            FTPFile[] files = ftp.listFiles(getWorkingPath());
            for (int i = 0; i < files.length; i++) {
                final FTPFile file = files[i];
                workingSet.add(file);
                getWorkManager().scheduleWork(new Work() {
                    public void run() {
View Full Code Here

    protected void processFile(FTPFile file) {
        if (file.getName().equals(".") || file.getName().equals("..")) { // TODO: what about other directories?
          return;
        }
        FTPClient client = null;
        try {
            client = (FTPClient) borrowClient();
            processFile(client, file);
            if (!client.deleteFile(getWorkingPath() + file.getName())) {
                throw new IOException("Could not delete file " + file);
            }
        }
        catch (Exception e) {
            log.error("Failed to process file: " + file + ". Reason: " + e, e);
View Full Code Here

     * @see smilehouse.opensyncro.user.pipes.Destination#take(java.lang.String,
     *      smilehouse.opensyncro.user.pipes.DestinationInfo, smilehouse.opensyncro.user.pipes.log.MessageLogger)
     */
    public void take(String data, DestinationInfo info, MessageLogger logger) throws FailTransferException, AbortTransferException {

        FTPClient ftp = new FTPClient();

        try {
            // -----------------
            // Try to connect...
            // -----------------
            String host = this.data.getAttribute(HOST_ATTR);
            int port = -1;
            String portStr = this.data.getAttribute(PORT_ATTR);
            if(portStr != null && portStr.length() > 0) {
                try {
                    port = Integer.parseInt(portStr);
                } catch(NumberFormatException nfe) {
                    logger.logMessage(
                        "Invalid value '" + portStr + "' for port.",
                        this,
                        MessageLogger.ERROR);
                    PipeComponentUtils.failTransfer();
                }
            }
            int reply;
           
            try {
                if(port != -1) {
                    ftp.connect(host, port);
                } else {
                    ftp.connect(host);
                }
            } catch(SocketException e) {
                logger.logMessage("SocketException while connecting to host " + host + ", aborting", this, MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            } catch(IOException e) {
                logger.logMessage("IOException while connecting to host " + host + ", aborting", this, MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }

            // After connection attempt, you should check the reply code to verify
            // success.
            reply = ftp.getReplyCode();

            if(!FTPReply.isPositiveCompletion(reply)) {
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                    /**
                     * ftp.disconnect() is called only as additional clean-up here, so we choose to
                     * ignore possible exceptions
                     */
                }
                logger.logMessage(
                    "Couldn't connect to the FTP server: " + ftp.getReplyString(),
                    this,
                    MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }

            // -----------
            // Then log in
            // -----------
           
            try {
                if(!ftp.login(this.data.getAttribute(USER_ATTR), this.data.getAttribute(PASSWORD_ATTR))) {
                    logger.logMessage(
                        "Could not log in, check your username and password settings.",
                        this,
                        MessageLogger.ERROR);
                    ftp.logout();

                    PipeComponentUtils.failTransfer();
                }
            } catch(IOException e) {
                logger.logMessage("IOException while logging in to FTP server", this, MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }

            // Use passive mode
            ftp.enterLocalPassiveMode();

            // -----------------
            // ASCII or binary ?
            // -----------------
            boolean fileTypeSetOk = false;

            try {
                switch(getFileType()) {
                case FILE_TYPE_ASCII:
                    fileTypeSetOk = ftp.setFileType(FTP.ASCII_FILE_TYPE);
                    break;
                case FILE_TYPE_BINARY:
                    fileTypeSetOk = ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                if(!fileTypeSetOk) {
                    logger.logMessage(
                        "Could not set file type: " + ftp.getReplyString(),
                        this,
                        MessageLogger.WARNING);
                }
            } catch(IOException e) {
                logger.logMessage("IOException while setting file transfer type parameter", this, MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }

            // -------------
            // Send the data
            // -------------
            String fileName = getFileName();
            logger.logMessage("Storing file: " + fileName, this, MessageLogger.DEBUG);
            String charSet=this.data.getAttribute(CHARSET_ATTR);
            if(charSet == null || charSet.length() == 0)
                charSet = DEFAULT_CHARSET;
            InputStream dataStream=null;
      try {
        dataStream = new ByteArrayInputStream(data.getBytes(charSet));
      } catch (UnsupportedEncodingException e1) {
       
        logger.logMessage(charSet+" charset not supported", this, MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
      }
            try {
            if(!ftp.storeFile(fileName, dataStream)) {
                logger.logMessage("Could not store file '" + fileName + "': "
                        + ftp.getReplyString(), this, MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }
            } catch (IOException e) {
                logger.logMessage("IOException while uploading the file to FTP server", this, MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();               
            }
        } finally {
            if(ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch(IOException f) {
                    // do nothing
                }
            }
        }
View Full Code Here

        else
            this.allDataOutput = true;

       
        String[] dataArray = new String[1];
        FTPClient ftp = new FTPClient();
       
        try {
            // -----------------
            // Try to connect...
            // -----------------
            String host = this.data.getAttribute(HOST_ATTR);
            int port = -1;
            String portStr = this.data.getAttribute(PORT_ATTR);
            if(portStr != null && portStr.length() > 0) {
                try {
                    port = Integer.parseInt(portStr);
                } catch(NumberFormatException nfe) {
                    logger.logMessage(
                        "Invalid value '" + portStr + "' for port.",
                        this,
                        MessageLogger.ERROR);
                    PipeComponentUtils.failTransfer();
                }
            }
            int reply;

            try {

                if(port != -1) {
                    ftp.connect(host, port);
                } else {
                    ftp.connect(host);
                }

            } catch(SocketException e) {
                logger.logMessage(
                    "SocketException while connecting to host " + host + ", aborting",
                    this,
                    MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            } catch(IOException e) {
                logger.logMessage(
                    "IOException while connecting to host " + host + ", aborting",
                    this,
                    MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }

            // After connection attempt, you should check the reply code to verify
            // success.
            reply = ftp.getReplyCode();

            if(!FTPReply.isPositiveCompletion(reply)) {
                try {
                    ftp.disconnect();
                } catch(IOException e) {
                    /**
                     * ftp.disconnect() is called only as additional clean-up here, so we choose to
                     * ignore possible exceptions
                     */
                }
                logger.logMessage(
                    "Couldn't connect to the FTP server: " + ftp.getReplyString(),
                    this,
                    MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }

            // -----------
            // Then log in
            // -----------
            try {
                if(!ftp.login(this.data.getAttribute(USER_ATTR), this.data
                    .getAttribute(PASSWORD_ATTR))) {
                    logger.logMessage(
                        "Could not log in, check your username and password settings.",
                        this,
                        MessageLogger.ERROR);
                    ftp.logout();

                    PipeComponentUtils.failTransfer();
                }

            } catch(IOException e) {
                logger.logMessage(
                    "IOException while logging in to FTP server",
                    this,
                    MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }

            // Use passive mode
            ftp.enterLocalPassiveMode();

            // -----------------
            // ASCII or binary ?
            // -----------------
            boolean fileTypeSetOk = false;
            try {
                switch(getFileType()) {
                case FILE_TYPE_ASCII:
                    fileTypeSetOk = ftp.setFileType(FTP.ASCII_FILE_TYPE);
                    break;
                case FILE_TYPE_BINARY:
                    fileTypeSetOk = ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                if(!fileTypeSetOk) {
                    logger.logMessage(
                        "Could not set file type: " + ftp.getReplyString(),
                        this,
                        MessageLogger.WARNING);
                }
            } catch(IOException e) {
                logger.logMessage(
                    "IOException while setting file transfer type parameter",
                    this,
                    MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }

            // -----------------
            // Retrieve the data
            // -----------------
            String fileName = getFileName(logger);
            logger.logMessage("Retrieving file: " + fileName, this, MessageLogger.DEBUG);
            OutputStream dataStream = new ByteArrayOutputStream();
            try {
                if(!ftp.retrieveFile(fileName, dataStream)) {
                    logger.logMessage("Could not retrieve file '" + fileName + "': "
                            + ftp.getReplyString(), this, MessageLogger.WARNING);
                    PipeComponentUtils.abortTransfer();
                }
                String charSet=this.data.getAttribute(CHARSET_ATTR);
                if(charSet == null || charSet.length() == 0)
                    charSet = DEFAULT_CHARSET;
                dataArray[0] = ((ByteArrayOutputStream)dataStream).toString(charSet);
            } catch(IOException e) {
                logger.logMessage(
                    "IOException while downloading file from FTP server",
                    this,
                    MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }
        } finally {
            if(ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch(IOException f) {
                    // do nothing
                }
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.commons.net.ftp.FTPClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.