193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair/*
293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * Copyright 2007 the original author or authors.
393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *
493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * Licensed under the Apache License, Version 2.0 (the "License");
593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * you may not use this file except in compliance with the License.
693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * You may obtain a copy of the License at
793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *
893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *      http://www.apache.org/licenses/LICENSE-2.0
993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair *
1093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * Unless required by applicable law or agreed to in writing, software
1193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * See the License for the specific language governing permissions and
1493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * limitations under the License.
1593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair */
1693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairpackage org.mockftpserver.stub.command;
1793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
1893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairimport org.mockftpserver.core.command.Command;
1993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairimport org.mockftpserver.core.command.CommandHandler;
2093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairimport org.mockftpserver.core.command.InvocationRecord;
2193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairimport org.mockftpserver.core.session.Session;
2293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismairimport org.mockftpserver.core.util.Assert;
2393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
2493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair/**
2531d38a11c73497bc273bfc0081b0d3b723482239chrismair * CommandHandler for the RETR (Retrieve) command. Return the configured file contents on the data
2693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * connection, along with two replies on the control connection: a reply code of 150 and
2731d38a11c73497bc273bfc0081b0d3b723482239chrismair * another of 226. By default, return an empty file (i.e., a zero-length byte[]). You can
2831d38a11c73497bc273bfc0081b0d3b723482239chrismair * customize the returned file contents by setting the <code>fileContents</code> property,
2931d38a11c73497bc273bfc0081b0d3b723482239chrismair * specified either as a String or as a byte array.
3031d38a11c73497bc273bfc0081b0d3b723482239chrismair * <p/>
3193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * Each invocation record stored by this CommandHandler includes the following data element key/values:
3293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * <ul>
3331d38a11c73497bc273bfc0081b0d3b723482239chrismair * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the file submitted on the invocation (the first command parameter)
3493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * </ul>
3531d38a11c73497bc273bfc0081b0d3b723482239chrismair *
3693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair * @author Chris Mair
3731d38a11c73497bc273bfc0081b0d3b723482239chrismair * @version $Revision$ - $Date$
3893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair */
393b7668ee59bf567db6bce593a9090f65db171fdbchrismairpublic class RetrCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
4093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
4193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public static final String PATHNAME_KEY = "pathname";
4293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
4393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    private byte[] fileContents = new byte[0];
4493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
4593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    /**
4693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * Create new uninitialized instance
4793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     */
4893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public RetrCommandHandler() {
4993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    }
5031d38a11c73497bc273bfc0081b0d3b723482239chrismair
5193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    /**
5293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * Create new instance using the specified fileContents
5331d38a11c73497bc273bfc0081b0d3b723482239chrismair     *
5493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param fileContents - the file contents
5531d38a11c73497bc273bfc0081b0d3b723482239chrismair     * @throws org.mockftpserver.core.util.AssertFailedException
5631d38a11c73497bc273bfc0081b0d3b723482239chrismair     *          - if the fileContents is null
5793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     */
5893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public RetrCommandHandler(String fileContents) {
5993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        setFileContents(fileContents);
6093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    }
6131d38a11c73497bc273bfc0081b0d3b723482239chrismair
6293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    /**
6393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * Create new instance using the specified fileContents
6431d38a11c73497bc273bfc0081b0d3b723482239chrismair     *
6593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param fileContents - the file contents
6631d38a11c73497bc273bfc0081b0d3b723482239chrismair     * @throws org.mockftpserver.core.util.AssertFailedException
6731d38a11c73497bc273bfc0081b0d3b723482239chrismair     *          - if the fileContents is null
6893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     */
6993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public RetrCommandHandler(byte[] fileContents) {
7093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        setFileContents(fileContents);
7193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    }
7231d38a11c73497bc273bfc0081b0d3b723482239chrismair
7393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    /**
7493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
7593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     */
7693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
7731d38a11c73497bc273bfc0081b0d3b723482239chrismair        String filename = command.getRequiredParameter(0);
7893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        invocationRecord.set(PATHNAME_KEY, filename);
7993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    }
8031d38a11c73497bc273bfc0081b0d3b723482239chrismair
8193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    /**
8293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
8393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     */
8493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
8593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        LOG.info("Sending " + fileContents.length + " bytes");
8693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        session.sendData(fileContents, fileContents.length);
8793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    }
8893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair
8993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    /**
9093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * Set the file contents to return from subsequent command invocations
9131d38a11c73497bc273bfc0081b0d3b723482239chrismair     *
9293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param fileContents - the fileContents to set
9331d38a11c73497bc273bfc0081b0d3b723482239chrismair     * @throws org.mockftpserver.core.util.AssertFailedException
9431d38a11c73497bc273bfc0081b0d3b723482239chrismair     *          - if the fileContents is null
9593102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     */
9693102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public void setFileContents(String fileContents) {
9793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        Assert.notNull(fileContents, "fileContents");
9893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        setFileContents(fileContents.getBytes());
9993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    }
10031d38a11c73497bc273bfc0081b0d3b723482239chrismair
10193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    /**
10293102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * Set the file contents to return from subsequent command invocations
10331d38a11c73497bc273bfc0081b0d3b723482239chrismair     *
10493102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     * @param fileContents - the file contents
10531d38a11c73497bc273bfc0081b0d3b723482239chrismair     * @throws org.mockftpserver.core.util.AssertFailedException
10631d38a11c73497bc273bfc0081b0d3b723482239chrismair     *          - if the fileContents is null
10793102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair     */
10893102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    public void setFileContents(byte[] fileContents) {
10993102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        Assert.notNull(fileContents, "fileContents");
11093102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair        this.fileContents = fileContents;
11193102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair    }
11231d38a11c73497bc273bfc0081b0d3b723482239chrismair
11393102446a7b7c3d17888064b4e2e4e5cb534e6d0chrismair}
114