Example usage for org.apache.commons.net.ftp FTPFile getName

List of usage examples for org.apache.commons.net.ftp FTPFile getName

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPFile getName.

Prototype

public String getName() 

Source Link

Document

Return the name of the file.

Usage

From source file:org.mule.transport.ftp.FtpMessageReceiver.java

protected void postProcess(FTPClient client, FTPFile file, MuleMessage message) throws Exception {
    if (!client.deleteFile(file.getName())) {
        throw new IOException(MessageFormat.format("Failed to delete file {0}. Ftp error: {1}", file.getName(),
                client.getReplyCode()));
    }/*from www. ja  va 2s .  com*/
    if (logger.isDebugEnabled()) {
        logger.debug("Deleted processed file " + file.getName());
    }

    if (connector.isStreaming()) {
        if (!client.completePendingCommand()) {
            throw new IOException(MessageFormat.format(
                    "Failed to complete a pending command. Retrieveing file {0}. Ftp error: {1}",
                    file.getName(), client.getReplyCode()));
        }
    }
}

From source file:org.mule.transport.ftp.FtpMessageRequester.java

protected void postProcess(FTPClient client, FTPFile file, MuleMessage message) throws Exception {
    if (!connector.isStreaming()) {
        if (!client.deleteFile(file.getName())) {
            throw new IOException(MessageFormat.format("Failed to delete file {0}. Ftp error: {1}",
                    file.getName(), client.getReplyCode()));
        }//from w ww. ja  v a  2 s.c  o  m
        if (logger.isDebugEnabled()) {
            logger.debug("Deleted file " + file.getName());
        }
    }
}

From source file:org.mule.transport.ftp.FtpMessageRequester.java

protected FTPFile findFileToProcess(FTPClient client) throws Exception {
    FTPListParseEngine engine = client.initiateListParsing();
    FTPFile[] files = null;//from   w  w  w.  ja va  2  s  . c  o m
    while (engine.hasNext()) {
        files = engine.getNext(FTP_LIST_PAGE_SIZE);
        if (files == null) {
            break;
        }
        FilenameFilter filenameFilter = getFilenameFilter();
        for (int i = 0; i < files.length; i++) {
            FTPFile file = files[i];
            if (file.isFile()) {
                if (filenameFilter.accept(null, file.getName())) {
                    if (connector.validateFile(file)) {
                        // only read the first one
                        return file;
                    }
                }
            }
        }
    }
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        throw new IOException("Ftp error: " + client.getReplyCode());
    }

    return null;
}

From source file:org.mule.transport.ftp.FtpMuleMessageFactory.java

@Override
protected Object extractPayload(Object transportMessage, String encoding) throws Exception {
    FTPFile file = (FTPFile) transportMessage;
    if (streaming) {
        InputStream stream = ftpClient.retrieveFileStream(file.getName());
        if (stream == null) {
            throw new IOException(MessageFormat.format("Failed to retrieve file {0}. Ftp error: {1}",
                    file.getName(), ftpClient.getReplyCode()));
        }/*from   www. j a v  a 2  s  . co  m*/
        return stream;
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (!ftpClient.retrieveFile(file.getName(), baos)) {
            throw new IOException(MessageFormat.format("Failed to retrieve file {0}. Ftp error: {1}",
                    file.getName(), ftpClient.getReplyCode()));
        }
        byte[] bytes = baos.toByteArray();
        if (bytes.length > 0) {
            return bytes;
        } else {
            throw new IOException("File " + file.getName() + " is empty (zero bytes)");
        }
    }
}

From source file:org.mule.transport.ftp.FtpMuleMessageFactory.java

@Override
protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception {
    super.addProperties(message, transportMessage);

    FTPFile file = (FTPFile) transportMessage;
    message.setInboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, file.getName());
    message.setInboundProperty(FileConnector.PROPERTY_FILE_SIZE, file.getSize());
    message.setInboundProperty(FileConnector.PROPERTY_FILE_TIMESTAMP, file.getTimestamp());
}

From source file:org.mule.transport.ftps.FtpsMessageReceiver.java

protected FTPFile[] listFiles() throws Exception {
    FTPSClient client = null;//from  w  w w .ja va2 s . c  o  m
    try {
        client = connector.createFTPSClient(endpoint);
        FTPListParseEngine engine = client.initiateListParsing();
        FTPFile[] files = null;
        List<FTPFile> v = new ArrayList<FTPFile>();
        while (engine.hasNext()) {
            if (getLifecycleState().isStopping()) {
                break;
            }
            files = engine.getNext(FTP_LIST_PAGE_SIZE);
            if (files == null || files.length == 0) {
                return files;
            }
            for (FTPFile file : files) {
                if (file.isFile()) {
                    if (filenameFilter == null || filenameFilter.accept(null, file.getName())) {
                        v.add(file);
                    }
                }
            }
        }

        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            throw new IOException("Failed to list files. Ftp error: " + client.getReplyCode());
        }

        return v.toArray(new FTPFile[v.size()]);
    } finally {
        if (client != null) {
            connector.releaseFtp(endpoint.getEndpointURI(), client);
        }
    }
}

From source file:org.mule.transport.ftps.FtpsMessageReceiver.java

protected void postProcess(FTPSClient client, FTPFile file, MuleMessage message) throws Exception {
    if (!client.deleteFile(file.getName())) {
        throw new IOException(MessageFormat.format("Failed to delete file {0}. Ftp error: {1}", file.getName(),
                client.getReplyCode()));
    }/*from   w w w  .j a  v a2 s.c  o m*/
    if (logger.isDebugEnabled()) {
        logger.debug("Deleted processed file " + file.getName());
    }

    if (connector.isStreaming()) {
        if (!client.completePendingCommand()) {
            throw new IOException(MessageFormat.format(
                    "Failed to complete a pending command. Retrieveing file {0}. Ftp error: {1}",
                    file.getName(), client.getReplyCode()));
        }
    }
}

From source file:org.mule.transport.ftps.FtpsMessageRequester.java

protected void postProcess(FTPSClient client, FTPFile file, MuleMessage message) throws Exception {
    if (!connector.isStreaming()) {
        if (!client.deleteFile(file.getName())) {
            throw new IOException(MessageFormat.format("Failed to delete file {0}. Ftp error: {1}",
                    file.getName(), client.getReplyCode()));
        }/*from   ww  w  .j  a v  a 2 s.c  o  m*/
        if (logger.isDebugEnabled()) {
            logger.debug("Deleted file " + file.getName());
        }
    }
}

From source file:org.mule.transport.ftps.FtpsMessageRequester.java

protected boolean isValid(FTPFile file, FilenameFilter filenameFilter) {
    return filenameFilter.accept(null, file.getName()) && connector.validateFile(file);
}

From source file:org.mule.transport.ftps.FtpsMuleMessageFactory.java

@Override
protected Object extractPayload(Object transportMessage, String encoding) throws Exception {
    FTPFile file = (FTPFile) transportMessage;
    if (streaming) {
        InputStream stream = FTPSClient.retrieveFileStream(file.getName());
        if (stream == null) {
            throw new IOException(MessageFormat.format("Failed to retrieve file {0}. Ftp error: {1}",
                    file.getName(), FTPSClient.getReplyCode()));
        }//from  ww  w  .  ja va 2 s. c  o m
        return stream;
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {

            if (!FTPSClient.retrieveFile(file.getName(), baos)) {
                throw new IOException(String.format("Failed to retrieve file %s. Ftp error: %s", file.getName(),
                        FTPSClient.getReplyCode()));
            }

            return baos.toByteArray();
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new IOException(
                    String.format("Failed to retrieve file %s due to unexpected exception", file.getName()), e);
        } catch (OutOfMemoryError e) {
            throw new IOException(String
                    .format("Failed to retrieve file %s because it's larger than the current memory heap. "
                            + "Consider enabling streaming on the FTP connector", file.getName()),
                    e);
        }
    }
}