160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/*
260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Copyright 2007 the original author or authors.
360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Licensed under the Apache License, Version 2.0 (the "License");
560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * you may not use this file except in compliance with the License.
660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * You may obtain a copy of the License at
760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *      http://www.apache.org/licenses/LICENSE-2.0
960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
1060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Unless required by applicable law or agreed to in writing, software
1160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * See the License for the specific language governing permissions and
1460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * limitations under the License.
1560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
1660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpackage org.mockftpserver.stub.command;
1760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
1860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.Command;
1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.CommandHandler;
2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.InvocationRecord;
2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.ReplyCodes;
2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.session.Session;
2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.util.Assert;
2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport java.util.StringTokenizer;
2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/**
2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * CommandHandler for the ALLO (Allocate) command. Send back a reply code of 200.
2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <p/>
3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Each invocation record stored by this CommandHandler includes the following data element key/values:
3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <ul>
3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>{@link #NUMBER_OF_BYTES_KEY} ("numberOfBytes") - the number of bytes submitted
3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * on the invocation (the first command parameter)
3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * <li>{@link #RECORD_SIZE_KEY} ("recordSize") - the record size optionally submitted
3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * on the invocation (the second command parameter)
3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * </ul>
3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair
3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$
4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpublic class AlloCommandHandler extends AbstractStubCommandHandler implements CommandHandler {
4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    public static final String NUMBER_OF_BYTES_KEY = "numberOfBytes";
4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    public static final String RECORD_SIZE_KEY = "recordSize";
4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    private static final String RECORD_SIZE_DELIMITER = " R ";
4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * Constructor. Initialize the replyCode.
4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    public AlloCommandHandler() {
5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        setReplyCode(ReplyCodes.ALLO_OK);
5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    /**
5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     * @see org.mockftpserver.core.command.CommandHandler#handleCommand(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session)
5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair     */
5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) {
5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        String parametersString = command.getRequiredParameter(0);
5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        if (parametersString.indexOf(RECORD_SIZE_DELIMITER) == -1) {
6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            invocationRecord.set(NUMBER_OF_BYTES_KEY, Integer.valueOf(parametersString));
6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        } else {
6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            // If the recordSize delimiter (" R ") is specified, then it must be followed by the recordSize.
6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            StringTokenizer tokenizer = new StringTokenizer(parametersString, RECORD_SIZE_DELIMITER);
6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            invocationRecord.set(NUMBER_OF_BYTES_KEY, Integer.valueOf(tokenizer.nextToken()));
6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            Assert.isTrue(tokenizer.hasMoreTokens(), "Missing record size: [" + parametersString + "]");
6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair            invocationRecord.set(RECORD_SIZE_KEY, Integer.valueOf(tokenizer.nextToken()));
6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        }
6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        sendReply(session);
7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}
74