StouCommandHandler.java revision 4531116f8e675a208710e987bfe3b58faeb12db2
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.command.ReplyCodes;
23import org.mockftpserver.core.session.Session;
24
25/**
26 * CommandHandler for the STOU (Store Unique) command. Send back two replies on the control connection: a
27 * reply code of 150 and another of 226. The text accompanying the final reply (226) is the
28 * unique filename, which is "" by default. You can customize the returned filename by setting
29 * the <code>filename</code> property.
30 * <p>
31 * Each invocation record stored by this CommandHandler includes the following data element key/values:
32 * <ul>
33 *    <li>{@link #FILE_CONTENTS_KEY} ("fileContents") - the file contents (<code>byte[]</code>) sent on the data connection
34 * </ul>
35 *
36 * @version $Revision$ - $Date$
37 *
38 * @author Chris Mair
39 */
40public final class StouCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
41
42    public static final String FILE_CONTENTS_KEY = "filecontents";
43    private static final String FINAL_REPLY_TEXT_KEY = "226.WithFilename";
44
45    private static final Logger LOG = Logger.getLogger(StouCommandHandler.class);
46
47    private String filename = "";
48
49    /**
50     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
51     */
52    protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
53        byte[] data = session.readData();
54        LOG.info("Received " + data.length + " bytes");
55        LOG.trace("Received data [" + new String(data) + "]");
56        invocationRecord.set(FILE_CONTENTS_KEY, data);
57    }
58
59    /**
60     * Override the default implementation to send a custom reply text that includes the STOU response filename
61     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#sendFinalReply(org.mockftpserver.core.session.Session)
62     */
63    protected void sendFinalReply(Session session) {
64        final String[] ARGS = { filename };
65        sendReply(session, ReplyCodes.SEND_DATA_FINAL_OK, FINAL_REPLY_TEXT_KEY, null, ARGS);
66    }
67
68    /**
69     * Set the filename returned with the final reply of the STOU command
70     * @param filename - the filename
71     */
72    public void setFilename(String filename) {
73        this.filename = filename;
74    }
75
76}
77