StorCommandHandler.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;
23
24/**
25 * CommandHandler for the STOR (Store) command. Send back two replies on the control connection: a
26 * reply code of 150 and another of 226.
27 * <p/>
28 * Each invocation record stored by this CommandHandler includes the following data element key/values:
29 * <ul>
30 * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the directory submitted on the invocation (the first command parameter)
31 * <li>{@link #FILE_CONTENTS_KEY} ("fileContents") - the file contents (<code>byte[]</code>) sent on the data connection
32 * </ul>
33 *
34 * @author Chris Mair
35 * @version $Revision$ - $Date$
36 */
37public final class StorCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
38
39    public static final String PATHNAME_KEY = "pathname";
40    public static final String FILE_CONTENTS_KEY = "filecontents";
41
42    private static final Logger LOG = Logger.getLogger(StorCommandHandler.class);
43
44    /**
45     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
46     */
47    protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
48        String filename = command.getRequiredParameter(0);
49        invocationRecord.set(PATHNAME_KEY, filename);
50    }
51
52    /**
53     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
54     */
55    protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
56        byte[] data = session.readData();
57        LOG.info("Received " + data.length + " bytes");
58        LOG.trace("Received data [" + new String(data) + "]");
59        invocationRecord.set(FILE_CONTENTS_KEY, data);
60    }
61
62}
63