RetrCommandHandler.java revision 31d38a11c73497bc273bfc0081b0d3b723482239
1/*
2 * Copyright 2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.stub.command;
17
18import org.apache.log4j.Logger;
19import org.mockftpserver.core.command.Command;
20import org.mockftpserver.core.command.CommandHandler;
21import org.mockftpserver.core.command.InvocationRecord;
22import org.mockftpserver.core.session.Session;
23import org.mockftpserver.core.util.Assert;
24
25/**
26 * CommandHandler for the RETR (Retrieve) command. Return the configured file contents on the data
27 * connection, along with two replies on the control connection: a reply code of 150 and
28 * another of 226. By default, return an empty file (i.e., a zero-length byte[]). You can
29 * customize the returned file contents by setting the <code>fileContents</code> property,
30 * specified either as a String or as a byte array.
31 * <p/>
32 * Each invocation record stored by this CommandHandler includes the following data element key/values:
33 * <ul>
34 * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the file submitted on the invocation (the first command parameter)
35 * </ul>
36 *
37 * @author Chris Mair
38 * @version $Revision$ - $Date$
39 */
40public final class RetrCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
41
42    private static final Logger LOG = Logger.getLogger(RetrCommandHandler.class);
43    public static final String PATHNAME_KEY = "pathname";
44
45    private byte[] fileContents = new byte[0];
46
47    /**
48     * Create new uninitialized instance
49     */
50    public RetrCommandHandler() {
51    }
52
53    /**
54     * Create new instance using the specified fileContents
55     *
56     * @param fileContents - the file contents
57     * @throws org.mockftpserver.core.util.AssertFailedException
58     *          - if the fileContents is null
59     */
60    public RetrCommandHandler(String fileContents) {
61        setFileContents(fileContents);
62    }
63
64    /**
65     * Create new instance using the specified fileContents
66     *
67     * @param fileContents - the file contents
68     * @throws org.mockftpserver.core.util.AssertFailedException
69     *          - if the fileContents is null
70     */
71    public RetrCommandHandler(byte[] fileContents) {
72        setFileContents(fileContents);
73    }
74
75    /**
76     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
77     */
78    protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
79        String filename = command.getRequiredParameter(0);
80        invocationRecord.set(PATHNAME_KEY, filename);
81    }
82
83    /**
84     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
85     */
86    protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
87        LOG.info("Sending " + fileContents.length + " bytes");
88        session.sendData(fileContents, fileContents.length);
89    }
90
91    /**
92     * Set the file contents to return from subsequent command invocations
93     *
94     * @param fileContents - the fileContents to set
95     * @throws org.mockftpserver.core.util.AssertFailedException
96     *          - if the fileContents is null
97     */
98    public void setFileContents(String fileContents) {
99        Assert.notNull(fileContents, "fileContents");
100        setFileContents(fileContents.getBytes());
101    }
102
103    /**
104     * Set the file contents to return from subsequent command invocations
105     *
106     * @param fileContents - the file contents
107     * @throws org.mockftpserver.core.util.AssertFailedException
108     *          - if the fileContents is null
109     */
110    public void setFileContents(byte[] fileContents) {
111        Assert.notNull(fileContents, "fileContents");
112        this.fileContents = fileContents;
113    }
114
115}
116