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 org.mockftpserver.core.command.Command;
     19 import org.mockftpserver.core.command.CommandHandler;
     20 import org.mockftpserver.core.command.InvocationRecord;
     21 import org.mockftpserver.core.session.Session;
     22 import org.mockftpserver.core.util.Assert;
     23 
     24 /**
     25  * CommandHandler for the RETR (Retrieve) command. Return the configured file contents on the data
     26  * connection, along with two replies on the control connection: a reply code of 150 and
     27  * another of 226. By default, return an empty file (i.e., a zero-length byte[]). You can
     28  * customize the returned file contents by setting the <code>fileContents</code> property,
     29  * specified either as a String or as a byte array.
     30  * <p/>
     31  * Each invocation record stored by this CommandHandler includes the following data element key/values:
     32  * <ul>
     33  * <li>{@link #PATHNAME_KEY} ("pathname") - the pathname of the file submitted on the invocation (the first command parameter)
     34  * </ul>
     35  *
     36  * @author Chris Mair
     37  * @version $Revision$ - $Date$
     38  */
     39 public class RetrCommandHandler extends AbstractStubDataCommandHandler implements CommandHandler {
     40 
     41     public static final String PATHNAME_KEY = "pathname";
     42 
     43     private byte[] fileContents = new byte[0];
     44 
     45     /**
     46      * Create new uninitialized instance
     47      */
     48     public RetrCommandHandler() {
     49     }
     50 
     51     /**
     52      * Create new instance using the specified fileContents
     53      *
     54      * @param fileContents - the file contents
     55      * @throws org.mockftpserver.core.util.AssertFailedException
     56      *          - if the fileContents is null
     57      */
     58     public RetrCommandHandler(String fileContents) {
     59         setFileContents(fileContents);
     60     }
     61 
     62     /**
     63      * Create new instance using the specified fileContents
     64      *
     65      * @param fileContents - the file contents
     66      * @throws org.mockftpserver.core.util.AssertFailedException
     67      *          - if the fileContents is null
     68      */
     69     public RetrCommandHandler(byte[] fileContents) {
     70         setFileContents(fileContents);
     71     }
     72 
     73     /**
     74      * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#beforeProcessData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
     75      */
     76     protected void beforeProcessData(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
     77         String filename = command.getRequiredParameter(0);
     78         invocationRecord.set(PATHNAME_KEY, filename);
     79     }
     80 
     81     /**
     82      * @see org.mockftpserver.stub.command.AbstractStubDataCommandHandler#processData(org.mockftpserver.core.command.Command, org.mockftpserver.core.session.Session, org.mockftpserver.core.command.InvocationRecord)
     83      */
     84     protected void processData(Command command, Session session, InvocationRecord invocationRecord) {
     85         LOG.info("Sending " + fileContents.length + " bytes");
     86         session.sendData(fileContents, fileContents.length);
     87     }
     88 
     89     /**
     90      * Set the file contents to return from subsequent command invocations
     91      *
     92      * @param fileContents - the fileContents to set
     93      * @throws org.mockftpserver.core.util.AssertFailedException
     94      *          - if the fileContents is null
     95      */
     96     public void setFileContents(String fileContents) {
     97         Assert.notNull(fileContents, "fileContents");
     98         setFileContents(fileContents.getBytes());
     99     }
    100 
    101     /**
    102      * Set the file contents to return from subsequent command invocations
    103      *
    104      * @param fileContents - the file contents
    105      * @throws org.mockftpserver.core.util.AssertFailedException
    106      *          - if the fileContents is null
    107      */
    108     public void setFileContents(byte[] fileContents) {
    109         Assert.notNull(fileContents, "fileContents");
    110         this.fileContents = fileContents;
    111     }
    112 
    113 }
    114