1 7 package com.coldcore.coloradoftp.command.impl.ftp; 8 9 import com.coldcore.coloradoftp.command.Reply; 10 import com.coldcore.coloradoftp.command.impl.AbstractCommand; 11 import com.coldcore.coloradoftp.connection.DataConnectionMode; 12 import com.coldcore.coloradoftp.factory.ObjectFactory; 13 import com.coldcore.coloradoftp.factory.ObjectName; 14 import com.coldcore.coloradoftp.filesystem.FileSystem; 15 import com.coldcore.coloradoftp.filter.DataFilterApplicator; 16 import com.coldcore.coloradoftp.session.Session; 17 import com.coldcore.coloradoftp.session.SessionAttributeName; 18 import org.apache.log4j.Logger; 19 20 import java.nio.channels.WritableByteChannel ; 21 import java.nio.channels.Channel ; 22 23 public class StorCommand extends AbstractCommand { 24 25 private static Logger log = Logger.getLogger(StorCommand.class); 26 27 28 public Reply execute() { 29 Reply reply = getReply(); 30 if (!testLogin()) return reply; 31 32 String filename = getFilename(); 33 if (filename.length() == 0) { 34 reply.setCode("501"); 35 reply.setText("Send file name."); 36 return reply; 37 } 38 39 closeSessionDataChannel(); 41 42 Session session = controlConnection.getSession(); 43 FileSystem fileSystem = (FileSystem) ObjectFactory.getObject(ObjectName.FILESYSTEM); 44 WritableByteChannel wbc = fileSystem.saveFile(filename, isAppend(), session); 45 46 DataFilterApplicator applicator = (DataFilterApplicator) ObjectFactory.getObject(ObjectName.DATA_FILTER_APPLICATOR); 47 wbc = applicator.applyFilters(wbc, session); 48 49 String type = (String ) session.getAttribute(SessionAttributeName.DATA_TYPE); 50 if (type == null) type = "A"; 51 52 if (!prepareForDataConnection()) { 53 try { 54 wbc.close(); 55 } catch (Throwable e) { 56 log.error("Error closing data channel (ignoring)", e); 57 } 58 return reply; 59 } 60 61 session.setAttribute(SessionAttributeName.DATA_CONNECTION_FILENAME, filename); 62 session.setAttribute(SessionAttributeName.DATA_CONNECTION_MODE, getDataConnectionMode()); 63 session.setAttribute(SessionAttributeName.DATA_CONNECTION_CHANNEL, wbc); 64 65 reply.setCode("150"); 66 reply.setText("Opening "+type+" mode data connection for "+filename+"."); 67 return reply; 68 } 69 70 71 72 protected void closeSessionDataChannel() { 73 Session session = controlConnection.getSession(); 74 Channel odc = (Channel ) session.getAttribute(SessionAttributeName.DATA_CONNECTION_CHANNEL); 75 if (odc != null) { 76 log.debug("Attempting to close data channel in session"); 77 session.removeAttribute(SessionAttributeName.DATA_CONNECTION_CHANNEL); 78 try { 79 odc.close(); 80 } catch (Throwable e) { 81 log.error("Error closing data channel (ignoring)", e); 82 } 83 } 84 } 85 86 87 90 protected String getFilename() { 91 return getParameter(); 92 } 93 94 95 98 protected DataConnectionMode getDataConnectionMode() { 99 return DataConnectionMode.STOR; 100 } 101 102 103 106 protected boolean isAppend() { 107 return false; 108 } 109 } 110 | Popular Tags |