Java tutorial
/* Copyright 2010 - iSencia Belgium NV Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.isencia.passerelle.actor.ftp; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPConnectionClosedException; import org.apache.commons.net.ftp.FTPReply; import com.isencia.message.ChannelException; import com.isencia.message.ISenderChannel; import com.isencia.message.ReaderReceiverChannel; import com.isencia.message.extractor.IMessageExtractor; /** * @author Bram Bogaert */ public class FtpReceiverChannel extends ReaderReceiverChannel { private final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(FtpReceiverChannel.class); private String server; private String username; private String password; private boolean binaryTransfer = false; //the transfermode (default ascii) private boolean passiveMode = true; private File remote; //Remote file to read/write private FTPClient ftp; /** * * @param destFile * @param generator Flushes messages through the channel * (channel close => destFile closed, the generator can only write while channel open) */ public FtpReceiverChannel(File destFile, String server, String username, String password, boolean isBinaryTransfer, boolean isPassiveMode, IMessageExtractor extractor) { super(extractor); this.remote = destFile; //Remote file to read/write this.server = server; this.username = username; this.password = password; this.binaryTransfer = isBinaryTransfer; this.passiveMode = isPassiveMode; ftp = new FTPClient(); } /** * @see ISenderChannel#open() */ public void open() throws ChannelException { if (logger.isTraceEnabled()) logger.trace(""); if (server == null) throw new ChannelException("Server is not specified"); if (username == null) throw new ChannelException("Username is not specified"); if (password == null) throw new ChannelException("Password is not specified"); if (remote == null) throw new ChannelException("File is not specified"); // CONNECT TO SERVER try { int reply; ftp.connect(server); logger.debug("Connected to " + server + "."); // After connection attempt, you should check the reply code to verify // success. reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); logger.error("FTP server refused connection"); throw new ChannelException("FTP server refused connection"); } } catch (IOException e) { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } } logger.error("Could not connect to server"); throw new ChannelException("Could not connect to server"); } try { // LOGIN TO THE SERVER if (!ftp.login(username, password)) { ftp.logout(); throw new ChannelException( "Can't login with username " + this.username + " and password " + this.password); } logger.debug("Remote system is " + ftp.getSystemName()); // ADJUST SETTINGS if (binaryTransfer) { ftp.setFileType(FTP.BINARY_FILE_TYPE); } if (passiveMode) { // Use passive mode as default because most of us are // behind firewalls these days. ftp.enterLocalPassiveMode(); //Remark: after the login! } } catch (ChannelException e1) { throw new ChannelException(e1.getMessage()); } catch (IOException e1) { //TODO: need to think about this... throw new ChannelException(e1.getMessage() + " (IOException)"); } try { setReader(new InputStreamReader(ftp.retrieveFileStream(remote.getPath()), "UTF-8")); } catch (FileNotFoundException e) { throw new ChannelException("Error opening source file " + remote.getAbsolutePath() + " (file not found): " + e.getMessage()); } catch (IOException e) { throw new ChannelException( "Error opening source file " + remote.getAbsolutePath() + " : " + e.getMessage()); } super.open(); if (logger.isTraceEnabled()) logger.trace("exit"); } /** * @see ISenderChannel#close() */ public void close() throws ChannelException { if (logger.isTraceEnabled()) logger.trace(""); if (isOpen()) { super.close(); } //First logout, then disconnect (disconnect also closes the outputstream) try { ftp.logout(); } catch (FTPConnectionClosedException e) { throw new ChannelException("Server closed connection."); } catch (IOException e) { throw new ChannelException(e.getMessage()); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } } } if (logger.isTraceEnabled()) logger.trace("exit"); } }