177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair/*
277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Copyright 2007 the original author or authors.
377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Licensed under the Apache License, Version 2.0 (the "License");
577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * you may not use this file except in compliance with the License.
677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * You may obtain a copy of the License at
777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *      http://www.apache.org/licenses/LICENSE-2.0
977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
1077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Unless required by applicable law or agreed to in writing, software
1177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * See the License for the specific language governing permissions and
1477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * limitations under the License.
1577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */
1677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairpackage org.mockftpserver.stub.command;
1777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
1877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.Command;
1977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.CommandHandler;
2077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.command.InvocationRecord;
2177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.session.Session;
2277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairimport org.mockftpserver.core.util.Assert;
2377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
2477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair/**
2577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * CommandHandler for the RETR (Retrieve) command. Return the configured file contents on the data
2677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * connection, along with two replies on the control connection: a reply code of 150 and
2777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * another of 226. By default, return an empty file (i.e., a zero-length byte[]). You can
2877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * customize the returned file contents by setting the <code>fileContents</code> property,
2977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * specified either as a String or as a byte array.
3077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <p/>
3177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * Each invocation record stored by this CommandHandler includes the following data element key/values:
3277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <ul>
3377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the file submitted on the invocation (the first command parameter)
3477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * </ul>
3577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair *
3677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @author Chris Mair
3777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair * @version $Revision$ - $Date$
3877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair */
3977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismairpublic class RetrCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
4077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
4177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public static final String PATHNAME_KEY = "pathname";
4277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
4377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    private byte[] fileContents = new byte[0];
4477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
4577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
4677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Create new uninitialized instance
4777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
4877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public RetrCommandHandler() {
4977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
5077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
5177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
5277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Create new instance using the specified fileContents
5377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *
5477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param fileContents - the file contents
5577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @throws org.mockftpserver.core.util.AssertFailedException
5677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *          - if the fileContents is null
5777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
5877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public RetrCommandHandler(String fileContents) {
5977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        setFileContents(fileContents);
6077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
6177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
6277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
6377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Create new instance using the specified fileContents
6477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *
6577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param fileContents - the file contents
6677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @throws org.mockftpserver.core.util.AssertFailedException
6777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *          - if the fileContents is null
6877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
6977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public RetrCommandHandler(byte[] fileContents) {
7077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        setFileContents(fileContents);
7177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
7277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
7377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
7477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
7577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
7677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
7777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        String filename = command.getRequiredParameter(0);
7877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        invocationRecord.set(PATHNAME_KEY, filename);
7977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
8077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
8177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
8277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
8377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
8477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
8577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        LOG.info("Sending " + fileContents.length + " bytes");
8677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        session.sendData(fileContents, fileContents.length);
8777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
8877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
8977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
9077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Set the file contents to return from subsequent command invocations
9177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *
9277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param fileContents - the fileContents to set
9377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @throws org.mockftpserver.core.util.AssertFailedException
9477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *          - if the fileContents is null
9577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
9677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void setFileContents(String fileContents) {
9777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        Assert.notNull(fileContents, "fileContents");
9877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        setFileContents(fileContents.getBytes());
9977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
10077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
10177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    /**
10277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * Set the file contents to return from subsequent command invocations
10377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *
10477b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @param fileContents - the file contents
10577b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     * @throws org.mockftpserver.core.util.AssertFailedException
10677b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     *          - if the fileContents is null
10777b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair     */
10877b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    public void setFileContents(byte[] fileContents) {
10977b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        Assert.notNull(fileContents, "fileContents");
11077b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair        this.fileContents = fileContents;
11177b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair    }
11277b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair
11377b8661f08d1379c0bdf2af93d8004fced9f1ab0chrismair}
114