Home | History | Annotate | Download | only in command
      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  */
     16 package org.mockftpserver.stub.command;
     17 
     18 import java.util.StringTokenizer;
     19 
     20 import org.mockftpserver.core.command.Command;
     21 import org.mockftpserver.core.command.CommandHandler;
     22 import org.mockftpserver.core.command.InvocationRecord;
     23 import org.mockftpserver.core.command.ReplyCodes;
     24 import org.mockftpserver.core.session.Session;
     25 import org.mockftpserver.core.util.Assert;
     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  * @version $Revision$ - $Date$
     39  *
     40  * @author Chris Mair
     41  */
     42 public final class AlloCommandHandler extends AbstractStubCommandHandler implements CommandHandler {
     43 
     44     public static final String NUMBER_OF_BYTES_KEY = "numberOfBytes";
     45     public static final String RECORD_SIZE_KEY = "recordSize";
     46     private static final String RECORD_SIZE_DELIMITER = " R ";
     47 
     48     /**
     49      * Constructor. Initialize the replyCode.
     50      */
     51     public AlloCommandHandler() {
     52         setReplyCode(ReplyCodes.ALLO_OK);
     53     }
     54 
     55     /**
     56      * @see org.mockftpserver.core.command.CommandHandler#handleCommand(Command, Session, InvocationRecord)
     57      */
     58     public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) {
     59         String parametersString = command.getRequiredString(0);
     60 
     61         if (parametersString.indexOf(RECORD_SIZE_DELIMITER) == -1) {
     62             invocationRecord.set(NUMBER_OF_BYTES_KEY, Integer.valueOf(parametersString));
     63         }
     64         else {
     65             // If the recordSize delimiter (" R ") is specified, then it must be followed by the recordSize.
     66             StringTokenizer tokenizer = new StringTokenizer(parametersString, RECORD_SIZE_DELIMITER);
     67             invocationRecord.set(NUMBER_OF_BYTES_KEY, Integer.valueOf(tokenizer.nextToken()));
     68             Assert.isTrue(tokenizer.hasMoreTokens(), "Missing record size: [" + parametersString + "]");
     69             invocationRecord.set(RECORD_SIZE_KEY, Integer.valueOf(tokenizer.nextToken()));
     70         }
     71 
     72         sendReply(session);
     73     }
     74 
     75 }
     76