KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > command > impl > ftp > RetrCommand


1 /**
2  * Command RETR.
3  * See FTP spec for details on the command.
4  */

5 package com.coldcore.coloradoftp.command.impl.ftp;
6
7 import com.coldcore.coloradoftp.command.Reply;
8 import com.coldcore.coloradoftp.command.impl.AbstractCommand;
9 import com.coldcore.coloradoftp.connection.DataConnectionMode;
10 import com.coldcore.coloradoftp.factory.ObjectFactory;
11 import com.coldcore.coloradoftp.factory.ObjectName;
12 import com.coldcore.coloradoftp.filesystem.FileSystem;
13 import com.coldcore.coloradoftp.filter.DataFilterApplicator;
14 import com.coldcore.coloradoftp.session.Session;
15 import com.coldcore.coloradoftp.session.SessionAttributeName;
16 import org.apache.log4j.Logger;
17
18 import java.nio.channels.ReadableByteChannel JavaDoc;
19 import java.nio.channels.Channel JavaDoc;
20
21 public class RetrCommand extends AbstractCommand {
22
23   private static Logger log = Logger.getLogger(RetrCommand.class);
24
25
26   public Reply execute() {
27     Reply reply = getReply();
28     if (!testLogin()) return reply;
29
30     String JavaDoc filename = getParameter();
31     if (filename.length() == 0) {
32       reply.setCode("501");
33       reply.setText("Send file name.");
34       return reply;
35     }
36
37     //Test if there is data channel left in the session
38
closeSessionDataChannel();
39
40     Session session = controlConnection.getSession();
41     Long JavaDoc marker = (Long JavaDoc) session.getAttribute(SessionAttributeName.DATA_MARKER);
42     session.removeAttribute(SessionAttributeName.DATA_MARKER);
43     if (marker == null) marker = 0L;
44
45     FileSystem fileSystem = (FileSystem) ObjectFactory.getObject(ObjectName.FILESYSTEM);
46     ReadableByteChannel JavaDoc rbc = fileSystem.readFile(filename, marker, session);
47
48     DataFilterApplicator applicator = (DataFilterApplicator) ObjectFactory.getObject(ObjectName.DATA_FILTER_APPLICATOR);
49     rbc = applicator.applyFilters(rbc, session);
50
51     String JavaDoc type = (String JavaDoc) session.getAttribute(SessionAttributeName.DATA_TYPE);
52     if (type == null) type = "A";
53
54     if (!prepareForDataConnection()) {
55       try {
56         rbc.close();
57       } catch (Throwable JavaDoc e) {
58         log.error("Error closing data channel (ignoring)", e);
59       }
60       return reply;
61     }
62
63     session.setAttribute(SessionAttributeName.DATA_CONNECTION_FILENAME, filename);
64     session.setAttribute(SessionAttributeName.DATA_CONNECTION_MODE, DataConnectionMode.RETR);
65     session.setAttribute(SessionAttributeName.DATA_CONNECTION_CHANNEL, rbc);
66
67     reply.setCode("150");
68     reply.setText("Opening "+type+" mode data connection for "+filename+".");
69     return reply;
70   }
71
72
73   /** Close a data channel if exists in the session */
74   protected void closeSessionDataChannel() {
75     Session session = controlConnection.getSession();
76     Channel JavaDoc odc = (Channel JavaDoc) session.getAttribute(SessionAttributeName.DATA_CONNECTION_CHANNEL);
77     if (odc != null) {
78       log.debug("Attempting to close data channel in session");
79       session.removeAttribute(SessionAttributeName.DATA_CONNECTION_CHANNEL);
80       try {
81         odc.close();
82       } catch (Throwable JavaDoc e) {
83         log.error("Error closing data channel (ignoring)", e);
84       }
85     }
86   }
87 }
88
Popular Tags