AlloCommandHandler.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.mockftpserver.core.command.Command;
19import org.mockftpserver.core.command.CommandHandler;
20import org.mockftpserver.core.command.InvocationRecord;
21import org.mockftpserver.core.command.ReplyCodes;
22import org.mockftpserver.core.session.Session;
23import org.mockftpserver.core.util.Assert;
24
25import java.util.StringTokenizer;
26
27/**
28 * CommandHandler for the ALLO (Allocate) command. Send back a reply code of 200.
29 * <p/>
30 * Each invocation record stored by this CommandHandler includes the following data element key/values:
31 * <ul>
32 * <li>{@link #NUMBER_OF_BYTES_KEY} ("numberOfBytes") - the number of bytes submitted
33 * on the invocation (the first command parameter)
34 * <li>{@link #RECORD_SIZE_KEY} ("recordSize") - the record size optionally submitted
35 * on the invocation (the second command parameter)
36 * </ul>
37 *
38 * @author Chris Mair
39 * @version $Revision$ - $Date$
40 */
41public final class AlloCommandHandler extends AbstractStubCommandHandler implements CommandHandler {
42
43    public static final String NUMBER_OF_BYTES_KEY = "numberOfBytes";
44    public static final String RECORD_SIZE_KEY = "recordSize";
45    private static final String RECORD_SIZE_DELIMITER = " R ";
46
47    /**
48     * Constructor. Initialize the replyCode.
49     */
50    public AlloCommandHandler() {
51        setReplyCode(ReplyCodes.ALLO_OK);
52    }
53
54    /**
55     * @see org.mockftpserver.core.command.CommandHandler#handleCommand(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session)
56     */
57    public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) {
58        String parametersString = command.getRequiredParameter(0);
59
60        if (parametersString.indexOf(RECORD_SIZE_DELIMITER) == -1) {
61            invocationRecord.set(NUMBER_OF_BYTES_KEY, Integer.valueOf(parametersString));
62        } else {
63            // If the recordSize delimiter (" R ") is specified, then it must be followed by the recordSize.
64            StringTokenizer tokenizer = new StringTokenizer(parametersString, RECORD_SIZE_DELIMITER);
65            invocationRecord.set(NUMBER_OF_BYTES_KEY, Integer.valueOf(tokenizer.nextToken()));
66            Assert.isTrue(tokenizer.hasMoreTokens(), "Missing record size: [" + parametersString + "]");
67            invocationRecord.set(RECORD_SIZE_KEY, Integer.valueOf(tokenizer.nextToken()));
68        }
69
70        sendReply(session);
71    }
72
73}
74