14531116f8e675a208710e987bfe3b58faeb12db2chrismair/*
24531116f8e675a208710e987bfe3b58faeb12db2chrismair * Copyright 2007 the original author or authors.
34531116f8e675a208710e987bfe3b58faeb12db2chrismair *
44531116f8e675a208710e987bfe3b58faeb12db2chrismair * Licensed under the Apache License, Version 2.0 (the "License");
54531116f8e675a208710e987bfe3b58faeb12db2chrismair * you may not use this file except in compliance with the License.
64531116f8e675a208710e987bfe3b58faeb12db2chrismair * You may obtain a copy of the License at
74531116f8e675a208710e987bfe3b58faeb12db2chrismair *
84531116f8e675a208710e987bfe3b58faeb12db2chrismair *      http://www.apache.org/licenses/LICENSE-2.0
94531116f8e675a208710e987bfe3b58faeb12db2chrismair *
104531116f8e675a208710e987bfe3b58faeb12db2chrismair * Unless required by applicable law or agreed to in writing, software
114531116f8e675a208710e987bfe3b58faeb12db2chrismair * distributed under the License is distributed on an "AS IS" BASIS,
124531116f8e675a208710e987bfe3b58faeb12db2chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134531116f8e675a208710e987bfe3b58faeb12db2chrismair * See the License for the specific language governing permissions and
144531116f8e675a208710e987bfe3b58faeb12db2chrismair * limitations under the License.
154531116f8e675a208710e987bfe3b58faeb12db2chrismair */
164531116f8e675a208710e987bfe3b58faeb12db2chrismairpackage org.mockftpserver.stub.command;
174531116f8e675a208710e987bfe3b58faeb12db2chrismair
184531116f8e675a208710e987bfe3b58faeb12db2chrismairimport org.apache.log4j.Logger;
194531116f8e675a208710e987bfe3b58faeb12db2chrismairimport org.mockftpserver.core.command.Command;
204531116f8e675a208710e987bfe3b58faeb12db2chrismairimport org.mockftpserver.core.command.CommandHandler;
214531116f8e675a208710e987bfe3b58faeb12db2chrismairimport org.mockftpserver.core.command.InvocationRecord;
224531116f8e675a208710e987bfe3b58faeb12db2chrismairimport org.mockftpserver.core.session.Session;
234531116f8e675a208710e987bfe3b58faeb12db2chrismair
244531116f8e675a208710e987bfe3b58faeb12db2chrismair/**
254531116f8e675a208710e987bfe3b58faeb12db2chrismair * CommandHandler for the STOR (Store) command. Send back two replies on the control connection: a
264531116f8e675a208710e987bfe3b58faeb12db2chrismair * reply code of 150 and another of 226.
274531116f8e675a208710e987bfe3b58faeb12db2chrismair * <p>
284531116f8e675a208710e987bfe3b58faeb12db2chrismair * Each invocation record stored by this CommandHandler includes the following data element key/values:
294531116f8e675a208710e987bfe3b58faeb12db2chrismair * <ul>
304531116f8e675a208710e987bfe3b58faeb12db2chrismair *    <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the directory submitted on the invocation (the first command parameter)
314531116f8e675a208710e987bfe3b58faeb12db2chrismair *    <li>{@link #FILE_CONTENTS_KEY} ("fileContents") - the file contents (<code>byte[]</code>) sent on the data connection
324531116f8e675a208710e987bfe3b58faeb12db2chrismair * </ul>
334531116f8e675a208710e987bfe3b58faeb12db2chrismair *
344531116f8e675a208710e987bfe3b58faeb12db2chrismair * @version $Revision$ - $Date$
354531116f8e675a208710e987bfe3b58faeb12db2chrismair *
364531116f8e675a208710e987bfe3b58faeb12db2chrismair * @author Chris Mair
374531116f8e675a208710e987bfe3b58faeb12db2chrismair */
384531116f8e675a208710e987bfe3b58faeb12db2chrismairpublic final class StorCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
394531116f8e675a208710e987bfe3b58faeb12db2chrismair
404531116f8e675a208710e987bfe3b58faeb12db2chrismair    public static final String PATHNAME_KEY = "pathname";
414531116f8e675a208710e987bfe3b58faeb12db2chrismair    public static final String FILE_CONTENTS_KEY = "filecontents";
424531116f8e675a208710e987bfe3b58faeb12db2chrismair
434531116f8e675a208710e987bfe3b58faeb12db2chrismair    private static final Logger LOG = Logger.getLogger(StorCommandHandler.class);
444531116f8e675a208710e987bfe3b58faeb12db2chrismair
454531116f8e675a208710e987bfe3b58faeb12db2chrismair    /**
464531116f8e675a208710e987bfe3b58faeb12db2chrismair     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
474531116f8e675a208710e987bfe3b58faeb12db2chrismair     */
484531116f8e675a208710e987bfe3b58faeb12db2chrismair    protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
494531116f8e675a208710e987bfe3b58faeb12db2chrismair        String filename = command.getRequiredString(0);
504531116f8e675a208710e987bfe3b58faeb12db2chrismair        invocationRecord.set(PATHNAME_KEY, filename);
514531116f8e675a208710e987bfe3b58faeb12db2chrismair    }
524531116f8e675a208710e987bfe3b58faeb12db2chrismair
534531116f8e675a208710e987bfe3b58faeb12db2chrismair    /**
544531116f8e675a208710e987bfe3b58faeb12db2chrismair     * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
554531116f8e675a208710e987bfe3b58faeb12db2chrismair     */
564531116f8e675a208710e987bfe3b58faeb12db2chrismair    protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
574531116f8e675a208710e987bfe3b58faeb12db2chrismair        byte[] data = session.readData();
584531116f8e675a208710e987bfe3b58faeb12db2chrismair        LOG.info("Received " + data.length + " bytes");
594531116f8e675a208710e987bfe3b58faeb12db2chrismair        LOG.trace("Received data [" + new String(data) + "]");
604531116f8e675a208710e987bfe3b58faeb12db2chrismair        invocationRecord.set(FILE_CONTENTS_KEY, data);
614531116f8e675a208710e987bfe3b58faeb12db2chrismair    }
624531116f8e675a208710e987bfe3b58faeb12db2chrismair
634531116f8e675a208710e987bfe3b58faeb12db2chrismair}
64